forked from perl-carton/carton
/
CLI.pm
413 lines (326 loc) · 10.4 KB
/
CLI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package Carton::CLI;
use strict;
use warnings;
use Cwd;
use Config;
use Getopt::Long;
use Term::ANSIColor qw(colored);
use Carton;
use Carton::Util;
use Carton::Error;
use Carton::Tree;
use Try::Tiny;
use constant { SUCCESS => 0, INFO => 1, WARN => 2, ERROR => 3 };
our $Colors = {
SUCCESS, => 'green',
WARN, => 'yellow',
INFO, => 'cyan',
ERROR, => 'red',
};
sub new {
my $class = shift;
bless {
color => 1,
verbose => 0,
}, $class;
}
sub carton {
my $self = shift;
$self->{carton} ||= Carton->new;
}
sub work_file {
my($self, $file) = @_;
return "$self->{work_dir}/$file";
}
sub run {
my($self, @args) = @_;
$self->{work_dir} = $ENV{PERL_CARTON_HOME} || (Cwd::cwd() . "/.carton");
mkdir $self->{work_dir}, 0777 unless -e $self->{work_dir};
local @ARGV = @args;
my @commands;
my $p = Getopt::Long::Parser->new(
config => [ "no_ignore_case", "pass_through" ],
);
$p->getoptions(
"h|help" => sub { unshift @commands, 'help' },
"v|version" => sub { unshift @commands, 'version' },
"color!" => \$self->{color},
"verbose!" => \$self->{verbose},
);
push @commands, @ARGV;
my $cmd = shift @commands || 'usage';
my $call = $self->can("cmd_$cmd");
if ($call) {
try {
$self->$call(@commands);
} catch {
/Carton::Error::CommandExit/ and return;
die $_;
}
} else {
$self->error("Could not find command '$cmd'\n");
}
}
sub commands {
my $self = shift;
no strict 'refs';
map { s/^cmd_//; $_ }
grep /^cmd_(.*)/, sort keys %{__PACKAGE__."::"};
}
sub cmd_usage {
my $self = shift;
$self->print(<<HELP);
Usage: carton <command>
where <command> is one of:
@{[ join ", ", $self->commands ]}
Run carton -h <command> for help.
HELP
}
sub parse_options {
my($self, $args, @spec) = @_;
Getopt::Long::GetOptionsFromArray($args, @spec);
}
sub printf {
my $self = shift;
my $type = pop;
my($temp, @args) = @_;
$self->print(sprintf($temp, @args), $type);
}
sub print {
my($self, $msg, $type) = @_;
$msg = colored $msg, $Colors->{$type} if defined $type && $self->{color};
my $fh = $type && $type >= WARN ? *STDERR : *STDOUT;
print {$fh} $msg;
}
sub error {
my($self, $msg) = @_;
$self->print($msg, ERROR);
Carton::Error::CommandExit->throw;
}
sub cmd_help {
my $self = shift;
my $module = $_[0] ? ("Carton::Doc::" . ucfirst $_[0]) : "Carton";
system "perldoc", $module;
}
sub cmd_version {
my $self = shift;
$self->print("carton $Carton::VERSION\n");
}
sub cmd_bundle {
my($self, @args) = @_;
$self->parse_options(\@args, "p|path=s" => sub { $self->carton->{path} = $_[1] });
my $local_mirror = $self->carton->local_mirror;
if (my $build_file = $self->has_build_file) {
$self->print("Bundling modules using $build_file\n");
$self->carton->download_from_build_file($build_file, $local_mirror);
$self->carton->update_mirror_index($local_mirror);
} else {
$self->error("Can't locate build file\n");
}
$self->printf("Complete! Modules were bundled into %s (DarkPAN)\n", $local_mirror, SUCCESS);
}
sub cmd_install {
my($self, @args) = @_;
$self->parse_options(
\@args,
"p|path=s" => sub { $self->carton->{path} = $_[1] },
"deployment!" => \$self->{deployment},
"cached!" => \$self->{use_local_mirror},
);
my $lock = $self->find_lock;
my $local_mirror = $self->carton->local_mirror;
$self->carton->configure(
lock => $lock,
mirror_file => $self->mirror_file, # $lock object?
( $self->{use_local_mirror} && -d $local_mirror ? (mirror => $local_mirror) : () ),
);
my $build_file = $self->has_build_file;
if (@args) {
$self->print("Installing modules from the command line\n");
$self->carton->install_modules(\@args);
$self->carton->update_lock_file($self->lock_file);
} elsif ($self->{deployment} or not $build_file) {
$self->print("Installing modules using carton.lock (deployment mode)\n");
$self->carton->install_from_lock;
} elsif ($build_file) {
$self->print("Installing modules using $build_file\n");
$self->carton->install_from_build_file($build_file);
$self->carton->update_lock_file($self->lock_file);
} else {
$self->error("Can't locate build file or carton.lock\n");
}
$self->printf("Complete! Modules were installed into %s\n", $self->carton->{path}, SUCCESS);
}
sub cmd_uninstall {
my($self, @args) = @_;
$self->parse_options(\@args, "p|path=s", sub { $self->carton->{path} = $_[1] });
my $lock = $self->find_lock
or $self->error("Can't find carton.lock: Run `carton install`");
my $index = $self->carton->build_index($lock->{modules});
my @meta;
for my $module (@args) {
if (exists $index->{$module}) {
push @meta, $index->{$module}{meta};
} else {
$self->print("Can't locate module $module\n", WARN);
}
}
my %root;
if ($self->has_build_file) {
for my $dep ($self->carton->list_dependencies) {
my($mod, $ver) = split /~/, $dep;
if (exists $index->{$mod}) {
$root{ $index->{$mod}{meta}{name} } = 1;
}
}
}
# only can uninstall root dependencies
my $tree = $self->carton->build_tree($lock->{modules}, \%root);
for my $root ($tree->children) {
if (grep $_->{name} eq $root->key, @meta) {
$tree->remove_child($root);
}
}
my @missing = grep !$tree->find_child($_), keys %{$lock->{modules}};
for my $module (@missing) {
my $meta = $lock->{modules}{$module};
$self->print("Uninstalling $meta->{dist}\n");
$self->carton->uninstall($lock, $module);
}
for my $meta (@meta) {
unless (grep $meta->{name} eq $_, @missing) {
$self->print("$meta->{name} is dependent by some other modules. Can't uninstall it.\n", WARN);
}
}
$self->carton->update_lock_file($self->lock_file);
if (@missing) {
$self->printf("Complete! Modules and its dependencies were uninstalled from %s\n",
$self->carton->{path}, SUCCESS);
}
}
sub mirror_file {
my $self = shift;
return $self->work_file("02packages.details.txt");
}
sub has_build_file {
my $self = shift;
my $file = (grep -e, qw( Build.PL Makefile.PL ))[0]
or return;
return $file;
}
sub cmd_show {
my($self, @args) = @_;
my $lock = $self->find_lock
or $self->error("Can't find carton.lock: Run `carton install`\n");
my $index = $self->carton->build_index($lock->{modules});
for my $module (@args) {
my $meta = $index->{$module}{meta}
or $self->error("Couldn't locate $module in carton.lock\n");
$self->print( Carton::Util::to_json($meta) );
}
}
sub cmd_tree {
my $self = shift;
$self->cmd_list("--tree", @_);
}
sub cmd_list {
my($self, @args) = @_;
my $tree_mode;
$self->parse_options(\@args, "tree!" => \$tree_mode);
my $lock = $self->find_lock
or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
if ($tree_mode) {
my $tree = $self->carton->build_tree($lock->{modules});
$self->carton->walk_down_tree($tree, sub {
my($module, $depth) = @_;
my $line = " " x $depth . "$module->{dist}\n";
$self->print($line);
});
} else {
for my $module (values %{$lock->{modules} || {}}) {
$self->print("$module->{dist}\n");
}
}
}
sub cmd_check {
my($self, @args) = @_;
my $file = $self->has_build_file
or $self->error("Can't find a build file: nothing to check.\n");
$self->parse_options(\@args, "p|path=s", sub { $self->carton->{path} = $_[1] });
my $lock = $self->carton->build_lock;
my @deps = $self->carton->list_dependencies;
my $res = $self->carton->check_satisfies($lock, \@deps);
my $ok = 1;
if (@{$res->{unsatisfied}}) {
$self->print("Following dependencies are not satisfied. Run `carton install` to install them.\n", WARN);
for my $dep (@{$res->{unsatisfied}}) {
$self->print("$dep->{module} " . ($dep->{version} ? "($dep->{version})" : "") . "\n");
}
$ok = 0;
}
if ($res->{superflous}) {
$self->printf("Following modules are found in %s but couldn't be tracked from your $file\n",
$self->carton->{path}, WARN);
$self->carton->walk_down_tree($res->{superflous}, sub {
my($module, $depth) = @_;
my $line = " " x $depth . "$module->{dist}\n";
$self->print($line);
}, 1);
$ok = 0;
}
if ($ok) {
$self->printf("Dependencies specified in your $file are satisfied and matches with modules in %s.\n",
$self->carton->{path}, SUCCESS);
}
}
sub cmd_update {
# "cleanly" update distributions in extlib
# rebuild the tree, update modules with DFS
die <<EOF;
carton update is not implemented yet.
The command is supposed to update all the dependencies to the latest
version as if you don't have the current local environment doesn't
exist.
For now, you can remove the local environment and re-run carton install
to get the similar functionality.
EOF
}
sub cmd_exec {
my($self, @args) = @_;
# allows -Ilib
@args = map { /^(-[I])(.+)/ ? ($1,$2) : $_ } @args;
my $system; # for unit testing
my @include;
$self->parse_options(\@args, 'I=s@', \@include, "system", \$system);
my $path = $self->carton->{path};
my $lib = join ",", @include, "$path/lib/perl5", ".";
local $ENV{PERL5OPT} = "-Mlib::core::only -Mlib=$lib";
local $ENV{PATH} = "$path/bin:$ENV{PATH}";
$system ? system(@args) : exec(@args);
}
sub find_lock {
my $self = shift;
if (-e $self->lock_file) {
return $self->lock_data; # TODO object
}
return;
}
sub lock_data {
my $self = shift;
my $lock;
try {
$lock = Carton::Util::load_json($self->lock_file);
} catch {
if (/No such file/) {
$self->error("Can't locate carton.lock\n");
} else {
$self->error("Can't parse carton.lock: $_\n");
}
};
return $lock;
}
sub lock_file {
my $self = shift;
return 'carton.lock';
}
1;