Changeset 26080 for lang/perl/Helper-Simple
- Timestamp:
- 12/08/08 04:13:18 (4 years ago)
- Location:
- lang/perl/Helper-Simple/trunk/lib/Helper
- Files:
-
- 2 modified
-
Simple.pm (modified) (21 diffs)
-
Simple/Const.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Helper-Simple/trunk/lib/Helper/Simple.pm
r25994 r26080 10 10 use Archive::Zip qw/:ERROR_CODES/; 11 11 use Data::Dumper; 12 use IO::File ();13 12 use Template (); 14 13 use Tie::Hash::Indexed (); … … 284 283 my @path_exchange = @{$class->path_exchange}; 285 284 my $path_exchanger = @path_exchange ? sub { 286 my ($dir , $omit) = @_;285 my ($dir) = @_; 287 286 $$dir =~ s/^\Q$root_dir\E//; 288 287 foreach my $ex (@path_exchange) { … … 309 308 } 310 309 311 sub setup_ helper{310 sub setup_script { 312 311 my ($class, $helper_place, $generator) = @_; 313 312 my $helper; … … 363 362 _ok(exists => $dir . $file); 364 363 } elsif (ref $c and my $compress = $c->{extract}) { 365 my $dir,$pkg->_extract($compress, $file, decode_base64($c->{content}), $path_exchanger, $c);364 $pkg->_extract($compress, $file, decode_base64($c->{content}), $path_exchanger, $c); 366 365 if (defined $c->{render_file} and $c->{render_file}) { 367 366 $pkg->_render_file_recursive($c->{extract_to}, $path_exchanger); … … 551 550 my $res = $pkg->_get_from_url($target, $option); 552 551 if ($res->is_success and $content = $res->content) { 553 _ info('download success', $target);552 _ok('download', $target); 554 553 } else { 555 _ng('download', $target); 554 if ($res->status_line eq '200 OK') { 555 _ng('download', $target . "(empty content)"); 556 } else { 557 _ng('download', $target . "(" . $res->status_line . ")"); 558 } 556 559 $success = 0; 557 560 } 558 561 if ($success) { 559 562 if (my $compress = $option->{extract}) { 560 $pkg->_extract($compress, $target, $content, undef, $option);563 $pkg->_extract($compress, $target, $content, $path_exchanger, $option); 561 564 } else { 562 565 my $name = $option->{filename}; … … 571 574 sub _extract { 572 575 my ($pkg, $compress, $target, $content, $path_exchanger, $option) = @_; 576 if ($compress eq 1) { 577 if ($target =~/\.(tar\.gz|tar\.bz2|zip|t[bg]z)$/) { 578 $compress = $1; 579 } else { 580 Carp::croak "cannot detect compress type from extention: $target"; 581 } 582 } 573 583 $compress =~ s/\./_/g; 574 584 Carp::croak "cannot use $compress as compress option." unless $pkg->can("_extract_" . $compress); 575 585 576 586 my $extract = '_extract_' . $compress; 577 if (my $dir = $pkg->$extract($target, $content, $path_exchanger, $option)) { 578 _ok('extracted to', $dir); 587 my $fh; 588 if ($content) { 589 $fh = IO::String->new(\$content); 590 } else { 591 $fh = IO::String->new(\do{slurp($target)}); 592 } 593 if ($pkg->$extract($target, $fh, $path_exchanger, $option)) { 594 my $dir = $pkg->dir; 595 _ok('extracted to', $dir . ($option->{extract_to} || '')); 579 596 if (defined $path_exchanger) { 580 597 my $_dir = $dir; 581 598 $path_exchanger->(\$_dir); 582 _info("path is exchanged", "$dir => $_dir") ;583 } 584 return $dir;599 _info("path is exchanged", "$dir => $_dir") if $dir ne $_dir; 600 } 601 return 1; 585 602 } else { 586 603 _ng('extract', $target); … … 616 633 } 617 634 635 sub _modify_filename { 636 my ($self, $filename, $option, $path_exchanger) = @_; 637 if (defined $option->{omit}) { 638 unless ($$filename =~s/^\Q$option->{omit}\E//) { 639 return 0; 640 } 641 } 642 $$filename = $self->dir 643 . ($option->{extract_to} ? $option->{extract_to} . '/': '') . $$filename; 644 if (defined $path_exchanger) { 645 $path_exchanger->($filename); 646 } 647 return 1; 648 } 649 618 650 sub _extract_zip { 619 my ($pkg, $target, $content, $path_exchanger, $option) = @_; 620 my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 621 $dir .= '/' unless $dir =~ m{/$}; 622 my $fh; 623 if ($content) { 624 $fh = IO::String->new($content); 625 } else { 626 $fh = IO::File->new($target) or die "cannot open $target"; 627 } 651 my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 628 652 my $zip = Archive::Zip->new(); 629 653 if ($zip->readFromFileHandle($fh) == AZ_OK) { 630 _info('extract', $ target);654 _info('extract', $pkg->dir . $target); 631 655 foreach my $member ($zip->members) { 632 656 my $filename = $member->fileName; 633 if (defined $option->{omit}) { 634 $filename =~s/^\Q$option->{omit}\E//; 635 } 636 $filename = $dir . $filename; 637 if (defined $path_exchanger) { 638 $path_exchanger->(\$filename); 639 } 640 $zip->extractMember($member, $filename); 657 if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 658 $zip->extractMember($member, $filename); 659 } 641 660 chmod $member->unixFileAttributes & ~ umask, $filename; 642 661 } 643 return $dir;662 return 1; 644 663 } else { 645 664 return 0; … … 651 670 652 671 sub _extract_tar_gz { 653 my ($pkg, $target, $content, $path_exchanger, $option) = @_; 654 my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 655 $dir .= '/' unless $dir =~ m{/$}; 656 my $fh; 657 if ($content) { 658 $fh = IO::String->new($content); 659 } else { 660 $fh = IO::File->new($target) or die "cannot open $target"; 661 } 672 my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 662 673 $fh = IO::Uncompress::Gunzip->new($fh); 663 674 my $tar = Archive::Tar->new(); … … 666 677 foreach my $file ($tar->list_files) { 667 678 my $filename = $file; 668 if (defined $option->{omit}) { 669 $filename =~s/^\Q$option->{omit}\E//; 670 } 671 $filename = $dir . $filename; 672 if (defined $path_exchanger) { 673 $path_exchanger->(\$filename); 674 } 675 $tar->extract_file($file, $filename); 676 } 677 return $dir; 679 if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 680 $tar->extract_file($file, $filename); 681 } 682 } 683 return 1; 678 684 } else { 679 685 return 0; … … 682 688 683 689 sub _extract_tar_bz2 { 684 my ($pkg, $target, $content, $path_exchanger, $option) = @_; 685 my $dir = $pkg->dir . ($option->{extract_to} ||= ''); 686 $dir .= '/' unless $dir =~ m{/$}; 687 my $fh; 688 if ($content) { 689 $fh = IO::String->new($content); 690 } else { 691 $fh = IO::File->new($target) or die "cannot open $target"; 692 } 690 my ($pkg, $target, $fh, $path_exchanger, $option) = @_; 693 691 $fh = IO::Uncompress::Bunzip2->new($fh); 694 692 if (my $tar = Archive::Tar->new($fh)) { … … 696 694 foreach my $file ($tar->list_files) { 697 695 my $filename = $file; 698 if (defined $option->{omit}) { 699 $filename =~s/^\Q$option->{omit}\E//; 700 } 701 $filename = $dir . $filename; 702 if (defined $path_exchanger) { 703 $path_exchanger->(\$filename); 704 } 705 $tar->extract_file($file, $filename); 706 } 707 return $dir; 696 if ($pkg->_modify_filename(\$filename, $option, $path_exchanger)) { 697 $tar->extract_file($file, $filename); 698 } 699 } 700 return 1; 708 701 } else { 709 702 return 0; … … 787 780 =head1 NAME 788 781 789 Helper::Simple - simply create your own helper782 Helper::Simple - simply create your own setupper/helper 790 783 791 784 =head1 VERSION … … 815 808 use Helper::Simple; 816 809 817 YourApp::Helper->setup_ helper('/path/to/yourhelper.pl');810 YourApp::Helper->setup_script('/path/to/yourhelper.pl'); 818 811 819 812 1; … … 872 865 This create files under ./path. 873 866 874 =head2 setup_ helper875 876 YourHelper->setup_ helper('./path/to/helper.pl');867 =head2 setup_script 868 869 YourHelper->setup_script('./path/to/helper.pl'); 877 870 878 871 This create helper as ./path/to/helper.pl 879 872 If you don't like default helepr script. you can set your own generator. 880 873 881 YourHelper->setup_ helper('./path/to/helper.pl', sub {874 YourHelper->setup_script('./path/to/helper.pl', sub { 882 875 my ($class, $data) = @_; 883 876 # .... … … 941 934 942 935 This copy file/directory from local. 943 For $option, you can use C<extract>, c<omit> and C<render_file> of C<download>. 936 937 For $option, see the follwoing. 938 939 =head3 render_file => 0/1 940 941 If you want to render extracted files, set this value true. 942 default values is 0. 943 944 =head3 extract => 'zip'/'tar.gz'/'tar.bz2' ... 945 946 If $path_to_file is compressed file and you want to extract it, 947 give the extention. 948 949 1 ... judge from extention 950 zip ... zipped file 951 tar.gz/tgz ... tar gz 952 tar.bz2/tbz ... tar bzip2 944 953 945 954 =head2 download / dl … … 950 959 For $option, see the following. 951 960 952 =head3 render => 0/1953 954 If you want to render extracted files, set this value true.955 default values is 0.956 957 961 =head3 extract => zip 958 962 … … 960 964 give the extention. 961 965 962 zip ... zipped file 963 964 In future, some formats will be added, gz, tar.gz, lzh etc. 966 1 ... judge from extention 967 zip ... zipped file 968 tar.gz/tgz ... tar gz 969 tar.bz2/tbz ... tar bzip2 965 970 966 971 =head3 omit => '/path/for/omit' … … 970 975 give the directory name to omit. 971 976 977 If you specify omit and path is not matched, 978 such files are skipped to extract. 979 972 980 =head3 filename => 'your_favolite_filename' 973 981 … … 990 998 =head2 var 991 999 992 Set variables for Template module used in C<file> ..993 This is inheritable vars. Namespaces under the namespace which this function is used,994 these 1000 Set variables for Template module used in C<file> or C<copy> with render_file option is true. 1001 This is inheritable vars. In the namespaces under the namespace which this function is used, 1002 defined vars can be used. 995 1003 996 1004 var { … … 1001 1009 =head2 my_var 1002 1010 1003 Set variables for Template module used in C<file> .1011 Set variables for Template module used in C<file> or C<copy> with render_file option is true. 1004 1012 This is like C<var>, but B<not> inheritable. 1005 1013 … … 1023 1031 =head2 here 1024 1032 1033 It is not recommended to use. 1034 1025 1035 From this function, you can use all method of Helper::Simple. 1026 1036 It returns the called namespace object. 1027 1037 1028 It is not recommended to use.1029 1030 1038 package YourHelper::Llib::Hoge; 1031 1039 1032 1040 here->dir; # returns "lib/Hoge/" 1033 1041 1034 =head1 TODO 1035 1036 =over 4 1037 1038 =item need helper program to create template. 1039 1040 like the following; 1041 1042 helper-template -template catalyst YourApp 1043 helper-setup -template catalyst YourApp ./yourapp.pl 1044 1045 =back 1042 =head1 EXAMPLE 1043 1044 It is very simple catalyst setupper. 1045 1046 At first, create catalyst template. 1047 1048 % catalyst.pl CatalystTemplate 1049 % cd ./CatalystTemplate 1050 % ./script/catalysttempalte_create.pl View TT 1051 % find -type f | xargs perl -p -i -e 's{CatalystTemplate}{\[% appclass %\]}g' 1052 % find -type f | xargs perl -p -i -e 's{catalysttemplate}{\[% appname %\]}g' 1053 1054 Next, create your catalyst setupper. 1055 1056 #!/usr/bin/perl 1057 1058 my $appclass = $ARGV[0]; 1059 my $appname = lc $ARGV[0]; 1060 1061 die "class name & helper name" unless $ARGV[1]; 1062 1063 package MyHelper; 1064 1065 use strict; 1066 use lib qw(../lib); 1067 use Helper::Simple; 1068 1069 var { 1070 appclass => $appclass, 1071 appname => $appname, 1072 }; 1073 1074 copy ['CatalystTemplate', {render_file => 1}]; 1075 1076 package MyHelper::CatalystTemplate::root::static::js; 1077 1078 use Helper::Simple::Const; 1079 1080 copy ['jquery.ui-1.5.2.tar.bz2', {omit => 'jquery.ui-1.5.2', extract => 'tar.bz2'}]; 1081 dl JQUERY_SUGGEST; 1082 1083 package main; 1084 1085 MyHelper->path_exchange('CatalystTemplate/lib/CatalystTemplate', 'CatalystTemplate/lib/' . $appclass); 1086 MyHelper->path_exchange('CatalystTemplate/script/catalysttemplate', 'CatalystTemplate/script/' . $appname ); 1087 MyHelper->path_exchange('CatalystTemplate/root/static/js/jquery.ui-1.5.2', 'CatalystTemplate/root/static/js/jquery.ui'); 1088 MyHelper->path_exchange('CatalystTemplate', $appclass); 1089 MyHelper->setup_script($ARGV[1]); 1090 1046 1091 1047 1092 =head1 AUTHOR -
lang/perl/Helper-Simple/trunk/lib/Helper/Simple/Const.pm
r25992 r26080 2 2 3 3 use constant { 4 JQUERY_UI => ['http://jquery-ui.googlecode.com/files/jquery.ui-1.5.2.zip', {extract => ' zip'} ],4 JQUERY_UI => ['http://jquery-ui.googlecode.com/files/jquery.ui-1.5.2.zip', {extract => '1'} ], 5 5 JQUERY_SUGGEST => ['http://www.vulgarisoip.com/files/jquery.suggest.js'], 6 6 JQUERY_MULTI_FILE_UPLOAD => ['http://www.fyneworks.com/jquery/multiple-file-upload/multiple-file-upload.zip' ],
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)