Skip to content

Commit

Permalink
Merge pull request #64 from miyagawa/first-install
Browse files Browse the repository at this point in the history
The first carmel install (without snapshot) should install the latest. Fix #53
  • Loading branch information
miyagawa committed May 11, 2022
2 parents fa00d9a + cd07d3b commit 9519f1b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
46 changes: 34 additions & 12 deletions lib/Carmel/App.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ sub cmd_update {

print "---> Checking updates...\n";

$self->update_or_install($snapshot, @args);
}

sub update_or_install {
my($self, $snapshot, @args) = @_;

my $builder = $self->builder;
my $requirements = $self->requirements;

Expand All @@ -130,7 +136,7 @@ sub cmd_update {
}
}

if ($dist->pathname ne $pathname) {
if ($dist->pathname ne $pathname && $snapshot) {
$snapshot->remove_distributions(sub {
my $dist = shift;
$dist->provides_module($module);
Expand Down Expand Up @@ -163,7 +169,7 @@ sub cmd_update {
if (@args) {
for my $arg (@args) {
my($module, $version) = split '@', $arg, 2;
my $dist = $snapshot->find($module);
my $dist = $snapshot ? $snapshot->find($module) : undef;
if ($dist) {
$check->($module, $dist->pathname, 1, $version ? "== $version" : undef);
} elsif (defined $requirements->requirements_for_module($module)) {
Expand All @@ -176,17 +182,27 @@ sub cmd_update {
my $missing = $requirements->clone;

my @checks;
$self->resolve(sub {
my $artifact = shift;
for my $pkg (keys %{$artifact->provides}) {
$missing->clear_requirement($pkg);
}
push @checks, [ $artifact->package, $artifact->install->{pathname}, 0 ];
});
my $resolver = $self->resolver(
root => $self->requirements->clone,
snapshot => $snapshot,
found => sub {
my $artifact = shift;
for my $pkg (keys %{$artifact->provides}) {
$missing->clear_requirement($pkg);
}
push @checks, [ $artifact->package, $artifact->install->{pathname}, 0 ];
},
missing => sub {
my($module, $want_version) = @_;
$missing->add_string_requirement($module => $want_version);
},
);
$resolver->resolve;

# specified in cpanfile but not in snapshot, possibly core
# snapshot not supplied (first carmel install), or
# specified in cpanfile but not in snapshot, possibly core module
for my $module ($missing->required_modules) {
push @checks, [ $module, '', 1 ];
push @checks, [ $module, '', 0 ];
}

progress \@checks, sub { $check->(@{$_[0]}) };
Expand All @@ -201,7 +217,13 @@ sub cmd_install {

die "Usage: carmel install\n" if @args;

$self->update_dependencies($self->requirements, $self->snapshot);
my $snapshot = $self->snapshot;
if ($snapshot) {
$self->update_dependencies($self->requirements, $snapshot);
} else {
print "---> Installing modules...\n";
$self->update_or_install($snapshot);
}
}

sub update_dependencies {
Expand Down
17 changes: 10 additions & 7 deletions xt/cli/update_core.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Module::CoreList;
plan skip_all => "HTTP::Tiny is not in core or is possibly the latest"
if ($Module::CoreList::version{$]}{"HTTP::Tiny"} || 999) > 0.080;

subtest 'carmel update core-module with empty cpanfile' => sub {
subtest 'carmel install now installs core module if there is a new version' => sub {
my $app = cli();

$app->write_cpanfile(<<EOF);
Expand All @@ -17,27 +17,30 @@ EOF

$app->run_ok("install");
$app->run_ok("list");
is $app->stdout, '';

$app->run_ok("update", "HTTP::Tiny");
$app->run_ok("list", "HTTP::Tiny");
like $app->stdout, qr/HTTP::Tiny \(/;
};

subtest 'carmel update with empty cpanfile' => sub {
subtest 'carmel update updates core modules' => sub {
my $app = cli();

# this creates an empty snapshot
$app->write_cpanfile('');
$app->run_ok("install");

# now add HTTP::Tiny, it will use the core version
# BUG: this is actually an inconsistent behavior from first-run
$app->write_cpanfile(<<EOF);
requires 'HTTP::Tiny';
EOF

$app->run_ok("install");
$app->run_ok("list");
unlike $app->stdout, qr/HTTP::Tiny \(/;

# now, carmel update will update it because it's in cpanfile
$app->run_ok("update");
$app->run_ok("list", "HTTP::Tiny");
like $app->stdout, qr/HTTP::Tiny \(/;
};


done_testing;

0 comments on commit 9519f1b

Please sign in to comment.