blib/lib/auto/share/dist/App-Followme/bundle.pl untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 155 CC 13 SCAR 29.7
CC
13
Cov
0%
get_visitor
56.0
bundle_file
12.0
copy_script
12.0
CRAP 19.5
1 #!/usr/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 use FindBin qw($Bin);
7  
8 use IO::Dir;
9 use IO::File;
10 use MIME::Base64 qw(encode_base64);
11 use File::Spec::Functions qw(catfile no_upwards rel2abs splitdir updir);
12  
13 #----------------------------------------------------------------------
14 # Configuration
15  
16 # Prefix that preceds every command in data section
17 # Must agree with Initialize.pm
18 use constant CMD_PREFIX => '#>>>';
19  
20 # The name of the index file in the template
21 my $index_file = 'index.html';
22  
23 # The location of the initialization module relative to this file
24 my $output = '../lib/App/Followme/Initialize.pm';
25  
26 #----------------------------------------------------------------------
27 # Main routine
28  
29 my $dir = shift(@ARGV) or die "Must supply site directory\n";
30  
31 chdir ($Bin);
32 $dir = rel2abs($dir);
33 my $out = copy_script($output);
34  
35 chdir($dir);
36  
37 my $visitor = get_visitor();
38 while (my $file = &$visitor) {
39 bundle_file($out, $file);
40 }
41  
42 close($out);
43  
44 chdir($Bin);
45 rename("$output.TMP", $output);
46  
47 #----------------------------------------------------------------------
48 # Append a binary file to the bundle
49  
50 sub append_binary_file {
51 my ($out, $file) = @_;
52  
53  
54 my $in = IO::File->new($file, 'r');
55 die "Couldn't read $file: $!\n" unless $in;
56  
57 binmode $in;
58 my $buf;
59  
60 while (read($in, $buf, 60*57)) {
61 print $out encode_base64($buf);
62 }
63  
64 close($in);
65 return;
66 }
67  
68 #----------------------------------------------------------------------
69 # Append a text file to the bundle
70  
71 sub append_text_file {
72 my ($out, $file) = @_;
73  
74 my $in = IO::File->new($file, 'r');
75 die "Couldn't read $file: $!\n" unless $in;
76  
77 while (defined (my $line = <$in>)) {
78 chomp($line);
79 print $out $line, "\n";
80 }
81  
82 close($in);
83 return;
84 }
85  
86 #----------------------------------------------------------------------
87 # Append a file to the bundle
88  
89 sub bundle_file {
90 my ($out, $file) = @_;
91  
92 my $type = -B $file ? 'binary' : 'text';
93 my $cmd = join(' ', CMD_PREFIX, 'copy', $type, $file);
94  
95 print $out $cmd, "\n";
96 if ($type eq 'binary') {
97 append_binary_file($out, $file);
98 } else {
99 append_text_file($out, $file);
100 }
101  
102 return;
103 }
104  
105 #----------------------------------------------------------------------
106 # Copy the script to start
107  
108 sub copy_script {
109 my ($output) = @_;
110  
111 my @path = split(/\//, $output);
112 $output = catfile(@path);
113  
114 my $last = "__DATA__\n";
115 my $in = IO::File->new($output, 'r') or
116 die "Couldn't read $output: $!\n";
117  
118 $output .= '.TMP';
119 my $out = IO::File->new($output, 'w');
120 die "Couldn't write to script: $output\n" unless $out;
121  
122 while (<$in>) {
123 print $out $_;
124 last if $_ eq $last;
125 }
126  
127 close($in);
128 return $out;
129 }
130  
131 #----------------------------------------------------------------------
132 # Return a closure that visits files in a directory
133  
134 sub get_visitor {
135 my () = @_;
136  
137 my @dirlist;
138 my @filelist;
139 push(@dirlist, '.');
140  
141 return sub {
142 for (;;) {
143 my $file = shift @filelist;
144 return $file if defined $file;
145  
146 my $dir = shift @dirlist;
147 return unless defined $dir;
148  
149 my $dd = IO::Dir->new($dir) or die "Couldn't open $dir: $!\n";
150  
151 while (defined ($file = $dd->read())) {
152 next if $file =~ /^\./;
153 my $path = $dir ne '.' ? catfile($dir, $file) : $file;
154  
155 if (-d $path) {
156 push(@dirlist, $path) if no_upwards($file);
157  
158 } else {
159 push(@filelist, $path);
160 }
161 }
162  
163 @filelist = sort(@filelist);
164 @dirlist = sort(@dirlist);
165 $dd->close;
166 }
167 };
168 }
169  
170 #----------------------------------------------------------------------
171 # Split topmost directory off from file name
172  
173 sub split_dir {
174 my ($file) = @_;
175  
176 my@path = splitdir($file);
177 my $dir = shift(@path);
178 my $rest = catfile(@path);
179  
180 return ($dir, $rest);
181 }
182  
183 __END__