blib/lib/VIM/Packager/Installer.pm untested

filter: Statementstmt 0.0% 0 / 228 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 347 CC 26 SCAR 35.3
CC
26
Cov
0%
install_deps_remote
90.0
install
72.0
diff_base_install
30.0
CRAP 34.0
1 package VIM::Packager::Installer;
2 use warnings;
3 use strict;
4 use File::Spec;
5 use File::Path;
6 use File::Copy;
7 use Exporter::Lite;
8 use YAML;
9 use VIM::Packager::Utils qw(vim_rtp_home vim_inst_record_dir findbin);
10 use LWP::UserAgent;
11 use VIM::Packager::Record;
12 use VIM::Packager::MetaReader;
13 use Carp;
14 use FileHandle;
15  
16 our @EXPORT = ();
17 our @EXPORT_OK = qw(
18 install_deps
19 install
20 install_deps_remote
21 install_deps_from_git
22 uninstall
23 bump_version
24 );
25  
26  
27 # FIXME: install deps from vim script archive network.
28  
29  
30 =head2 install_deps_from_git
31  
32 install dependencies from repository. e.g. repositories on github.com
33  
34 =cut
35  
36 sub install_deps_from_git {
37 my $clone_path = shift @ARGV;
38 my $version_required = shift @ARGV;
39  
40 require VIM::Packager::MakeMaker;
41 require VIM::Packager::Git;
42 my $g = VIM::Packager::Git->new;
43 $g->clone( $clone_path );
44  
45 # convert meta to makefile
46 my $maker = VIM::Packager::MakeMaker->new;
47  
48 my $system = "make";
49 my $stderr = $^O eq "MSWin32" ? "" : " 2>&1 ";
50  
51 my $pipe = FileHandle->new("$system install $stderr |")
52 || Carp::croak("Can't execute $system: $!");
53  
54 my $makeout = "";
55 while (<$pipe>) {
56 print $_; # intentionally NOT use Frontend->myprint because it
57 # looks irritating when we markup in color what we
58 # just pass through from an external program
59 $makeout .= $_;
60 }
61 $pipe->close;
62  
63 $g->cleanup();
64 }
65  
66 =head2 install_deps
67  
68 install dependencies from VSAN.
69  
70 XXX: implement me.
71  
72 =cut
73  
74 sub install_deps {
75 my $deps = shift @ARGV;
76 my @pkgs = split /,/,$deps;
77 # use Data::Dumper;warn Dumper( \@pkgs );
78 die 'please implement me!!!';
79  
80 # * foreach dependency
81  
82 # * retreive vimscript tarball
83  
84 # * untar to build directory
85  
86 # * change directory to build directory
87  
88 # * read package meta file
89  
90 # * check dependency
91  
92 # * install dependencies
93  
94 # * call VIM::Pacakger::Installer to install files
95  
96 }
97  
98 our $VERBOSE = $ENV{VERBOSE} ? 1 : 0;
99  
100 =head2 install_deps_remote
101  
102  
103 =cut
104  
105 # XXX: give all dependency pkgnames in one time
106 sub install_deps_remote {
107 my $pkgname = shift @ARGV;
108 my %install = @ARGV;
109  
110 print sprintf( "Installing dependencies: %s\n", $pkgname);
111  
112 $|++;
113 while( my ($target,$from) = each %install ) {
114  
115 # XXX: we might need to expand Makefile macro to support such things like:
116 # $(VIM_BASEDIR)/path/to/
117 # see VIM::Packager::MakeMaker
118 # XXX: we should compare the installed file and the downloaded file.
119 $target = File::Spec->join( vim_rtp_home() , $target );
120  
121 print "Downloading $from " ;
122 print " to " . $target if $VERBOSE;
123 print "...";
124  
125 {
126 my ($v,$dir,$file) = File::Spec->splitpath( $target );
127 File::Path::mkpath [ $dir ] unless -e $dir;
128 }
129  
130 my $ua = LWP::UserAgent->new;
131 $ua->timeout( 10 );
132 $ua->env_proxy();
133  
134 my $content;
135 my $response = $ua->get( $from );
136 if( $response->is_success ) {
137 $content = $response->decoded_content;
138  
139  
140 print "[ OK ]\n";
141 }
142 else {
143 print "[ FAIL ]\n";
144 print $response->status_line;
145 }
146  
147 # XXX: try to get the last modified time
148  
149 # if target exists , then we should do a diff
150 if ( $content and -e $target ) {
151 my @src = split /\n/,$content;
152  
153 open FH_T , "<", $target;
154 my @target = <FH_T>;
155 close FH_T;
156  
157 chomp @target;
158 chomp @src;
159  
160 my $diff = diff_base_install( \@src , \@target );
161 if ( $diff ) {
162 my $ans = prompt_for_different( $target );
163 while( $ans =~ /d/i ) {
164 print "Diff:\n";
165 print $diff;
166 $ans = prompt_for_different( $target );
167 }
168 if( $ans =~ /r/i ) {
169 # do replace
170 open RH,">",$target;
171 print RH join("\n",@src);
172 close RH;
173 print "$target replaced\n";
174 }
175 elsif ( $ans =~ /s/i ) {
176 # do nothing
177 print "Skipped\n";
178 }
179 }
180 }
181 elsif ( $content and ! -e $target ) {
182 open RH,">",$target;
183 print RH $content;
184 close RH;
185 print "$target installed\n";
186 }
187  
188  
189 }
190 }
191  
192  
193 =head2 prompt_for_different
194  
195 =cut
196  
197 sub prompt_for_different {
198 my $target = shift;
199 print "Installed script version not found. instead , we found the installed script file.\n";
200 print "The installed vim script file is different from which you just downloaded.\n";
201 print "Which is: $target.\n";
202 print "(Replace(r) / Diff(d) / Merge(m) / Skip(s) ) it with the remote one ? ";
203 my $ans = <STDIN>;
204 chomp $ans;
205 return $ans;
206 }
207  
208  
209 =head2 diff_base_install ArrayRef:From , ArrayRef:To
210  
211 diff text
212  
213 =cut
214  
215 sub diff_base_install {
216 my ($src_lines,$to_lines) = @_;
217 require Algorithm::Diff;
218  
219 my $diff = Algorithm::Diff->new( $src_lines , $to_lines );
220 $diff->Base(1);
221
222 my $result = "";
223 while( $diff->Next() ) {
224 next if $diff->Same();
225  
226 my $sep = '';
227  
228 if( ! $diff->Items(2) ) {
229 $result .= sprintf "%d,%dd%d\n", $diff->Get(qw( Min1 Max1 Max2 ));
230 }
231 elsif( ! $diff->Items(1) ) {
232 $result .= sprintf "%da%d,%d\n", $diff->Get(qw( Max1 Min2 Max2 ));
233 }
234 else {
235 $sep = "---\n";
236 $result .= sprintf "%d,%dc%d,%d\n", $diff->Get(qw( Min1 Max1 Min2 Max2 ));
237 }
238 $result .= "< $_\n" for $diff->Items(1);
239 $result .= $sep;
240 $result .= "> $_\n" for $diff->Items(2);
241 }
242  
243 return $result ? $result : undef;
244 }
245  
246  
247  
248 =head2 install $pkgname %install_files
249  
250 install package vimlib files
251  
252 %install_file is a hash, which key is source file , value is target path of installation.
253  
254 =cut
255  
256 sub install {
257 my $pkgname = shift @ARGV;
258 my %install_to = @ARGV;
259  
260 my $meta = VIM::Packager::MetaReader->new->read_metafile();
261  
262 # we should check more details on those files which are going to be
263 # installed.
264 my $found_record = VIM::Packager::Record::find( $pkgname );
265 if( $found_record and -e $found_record ) {
266 my $r = VIM::Packager::Record::read( $found_record );
267 my $version = $r->{meta}->{version};
268 printf "Found installed package: %s v%s\n" , $pkgname , $version ;
269  
270 # uninstall older version
271 if( $version < $meta->{version} ) {
272 print "We require version up to " . $meta->{version} . "\n";
273 print "Uninstalling $pkgname v$version\n";
274 for my $f ( @{ $r->{files} } ) {
275 if( -e $f ) {
276 print "Removing $f\n";
277 unlink $f;
278 }
279 else {
280 print "Warning: Can not found file $f\n";
281 }
282 }
283 }
284 else {
285 print "Package $pkgname has been installed. Skipped.\n";
286 print "run \$ make uninstall before make install if you need to reinstall it\n";
287 return;
288 }
289 }
290  
291 print "Installing $pkgname " . $meta->{version} . "\n";
292 while( my ($from,$to) = each %install_to ){
293 my ( $v, $dir, $file ) = File::Spec->splitpath($to);
294  
295 print("$from doesnt exist.\n"),next unless -e $from;
296  
297 File::Path::mkpath [ $dir ] unless -e $dir ;
298  
299 if( -e $to ) {
300 my $mtime_to = (stat($to))[9];
301 my $mtime_from = (stat($from))[9];
302  
303 if ( $mtime_from > $mtime_to ) {
304 File::Copy::copy( $from , $to );
305 print STDOUT "Installing $from => $to \n";
306 }
307 else {
308 print STDOUT "Skip $from\n";
309 }
310 }
311 else {
312 File::Copy::copy( $from , $to );
313 print STDOUT "Installing $from => $to \n";
314 }
315 }
316  
317 my @files = values %install_to;
318  
319 print STDERR "Making checksum...\n";
320 my @e = Vimana::Record->mk_file_digests( @files );
321 use Vimana::Record;
322 Vimana::Record->add( {
323 version => 0.2, # record spec version
324 generated_by => 'VIM-Packager' . $Vimana::VERSION,
325 install_type => 'meta', # auto , make , rake ... etc
326 package => $pkgname,
327 files => \@e,
328 meta => $meta,
329 } );
330  
331 print "Updating doc tags\n";
332 system(qq|vim -c ':helptags \$VIMRUNTIME/doc' -c q |);
333 print "Done\n";
334 }
335  
336  
337 =head2 uninstall [pkgname]
338  
339 =cut
340  
341 sub uninstall {
342 my $pkgname = shift @ARGV;
343 my $f = VIM::Packager::Record::find( $pkgname );
344  
345 unless( $f and -e $f ) {
346 print "Can not found record of $pkgname\n";
347 return ;
348 }
349  
350 my $r = YAML::LoadFile( $f );
351 my @files = @{ $r->{files} };
352  
353 for ( @files ) {
354 print "Removing $_\n";
355 if( ! -e $_ ) {
356 print "Warning: can not found file $_.\n";
357 next;
358 }
359 unlink $_;
360 }
361  
362 print "Removing record $pkgname\n";
363 unlink $f;
364 }
365  
366  
367 =head2 bump_version
368  
369 you can export VIMPACKAGE_AUTO_COMMIT to do auto commit after version bumpped.
370  
371 export VIMPACKAGE_AUTO_COMMIT=1
372  
373 =cut
374  
375 sub bump_version {
376 my $meta = VIM::Packager::MetaReader->new->read_metafile();
377 my $previous_ver = $meta->{version};
378 my $version = $previous_ver + 0.01;
379  
380 my $file = $meta->{version_from};
381 if( -e $file ) {
382 open FH , "<" , $file;
383 my @lines = <FH>;
384 close FH;
385  
386 my $found;
387 for ( @lines ) {
388 if( /^"=?\s*Version:?\s+$previous_ver/i ) {
389 $found++;
390 print "Version tag found: $_";
391 $_ = qq{" Version: $version\n};
392 }
393 }
394  
395 open FH , ">" , $file;
396 print FH @lines;
397 close FH;
398  
399 if( $ENV{VIMPACKAGE_AUTO_COMMIT} and -e '.git' and $found ) {
400 # found git repository
401 print "Found .git directory\n";
402 print "Do auto-commit\n";
403 my $git = qx{which git};
404 chomp $git;
405 qx{$git commit $file -m"Bump version to $version."};
406 }
407  
408 print "Version bumped to $version\n";
409 print "Done.\n";
410 }
411 }
412  
413 1;