Skip to content

Commit

Permalink
Merge pull request #577 from ugexe/nlogan/output-shell-commands
Browse files Browse the repository at this point in the history
Output commands run by Zef::Service::Shell::* wrappers
  • Loading branch information
ugexe authored Nov 16, 2024
2 parents 45f3ac7 + 0b834c9 commit c8ec8af
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/Zef/Client.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class Zef::Client {
die "failed to create directory: {$tmp.absolute}"
unless ($tmp.IO.e || mkdir($tmp));

my $meta6-prefix = '' R// $!extractor.ls-files($candi).sort.first({ .IO.basename eq 'META6.json' });
my $meta6-prefix = '' R// $!extractor.ls-files($candi, :$!logger).sort.first({ .IO.basename eq 'META6.json' });

self.logger.emit({
level => WARN,
Expand Down
18 changes: 15 additions & 3 deletions lib/Zef/Extract.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ class Zef::Extract does Extractor does Pluggable {
=head2 method ls-files
method ls-files(IO() $archive-file --> Array[Str])
method ls-files(IO() $archive-file, Supplier :$logger --> Array[Str])
On success it returns an C<Array> of relative paths that are available to be extracted from C<$archive-file>.
An optional C<:$logger> can be supplied to receive events about what is occurring.
=end pod


Expand Down Expand Up @@ -159,12 +161,22 @@ class Zef::Extract does Extractor does Pluggable {
#| each string is a relative path representing a file that can be extracted from the given $candi.uri
#| Note this differs from other 'Extract' adapters .extract() which take a $uri as the first
#| parameter, not a $candi
method ls-files($candi --> Array[Str]) {
method ls-files($candi, Supplier :$logger --> Array[Str]) {
my $stdout = Supplier.new;
my $stderr = Supplier.new;
if ?$logger {
$stdout.Supply.act: -> $out { $logger.emit({ level => VERBOSE, stage => EXTRACT, phase => LIVE, candi => $candi, message => $out }) }
$stderr.Supply.act: -> $err { $logger.emit({ level => ERROR, stage => EXTRACT, phase => LIVE, candi => $candi, message => $err }) }
}

my $path := $candi.uri;
my $extractors := self!extractors($path);
my $name-paths := $extractors.map(*.ls-files($path)).first(*.defined).map(*.IO);
my $name-paths := $extractors.map(*.ls-files($path, :$stdout, :$stderr)).first(*.defined).map(*.IO);
my @files = $name-paths.map({ .is-absolute ?? $path.IO.child(.relative($path)).cleanup.relative($path) !! $_ });

$stdout.done();
$stderr.done();

my Str @results = @files.map(*.Str);
return @results;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Zef/Service/Shell/DistributionBuilder.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ class Zef::Service::Shell::DistributionBuilder does Builder {

my @exec = |($*EXECUTABLE.absolute, |@includes.grep(*.defined).map({ "-I{$_}" }), '-e', "$cmd");

$stdout.emit("Command: {@exec.join(' ')}");

my $ENV := %*ENV;
my $passed;
react {
my $proc = Zef::zrun-async(@exec, :w);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout.lines { $stdout.emit($_) }
whenever $proc.stderr.lines { $stderr.emit($_) }
whenever $proc.start(:$ENV, :cwd($dist.path)) { $passed = $_.so }
Expand Down
3 changes: 1 addition & 2 deletions lib/Zef/Service/Shell/LegacyBuild.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ class Zef::Service::Shell::LegacyBuild does Builder {
my $cmd = "require '$build-file'; ::('Build').new.build('$dist.path.IO.absolute()') ?? exit(0) !! exit(1);";
my @exec = |($*EXECUTABLE.absolute, |@includes.grep(*.defined).map({ "-I{$_}" }), '-e', "$cmd");

$stdout.emit("Command: {@exec.join(' ')}");

my $ENV := %*ENV;
my $passed;
react {
my $proc = Zef::zrun-async(@exec);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout.lines { $stdout.emit($_) }
whenever $proc.stderr.lines { $stderr.emit($_) }
whenever $proc.start(:$ENV, :cwd($dist.path)) { $passed = $_.so }
Expand Down
1 change: 1 addition & 0 deletions lib/Zef/Service/Shell/Test.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Zef::Service::Shell::Test does Tester {
my $passed;
react {
my $proc = Zef::zrun-async($*EXECUTABLE.absolute, @includes.map({ slip '-I', $_ }), $rel-test-file);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout.lines { $stdout.emit($_) }
whenever $proc.stderr.lines { $stderr.emit($_) }
whenever $proc.start(:cwd($path)) { $passed = $_.so }
Expand Down
1 change: 1 addition & 0 deletions lib/Zef/Service/Shell/curl.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Zef::Service::Shell::curl does Fetcher does Probeable {
my $ENV := %*ENV;
my $cmd := self!command();
my $proc = Zef::zrun-async($cmd, '--silent', '-L', '-z', $save-as.absolute, '-o', $save-as.absolute, $uri);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand Down
27 changes: 17 additions & 10 deletions lib/Zef/Service/Shell/git.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
=head2 method ls-files
method ls-files(IO() $repo-path --> Array[Str])
method ls-files(IO() $repo-path, Supplier :$stdout, Supplier :$stderr --> Array[Str])
On success it returns an C<Array> of relative paths that are available to be extracted from C<$repo-path>.
A C<Supplier> can be supplied as C<:$stdout> and C<:$stderr> to receive any output.
=end pod

Expand Down Expand Up @@ -128,7 +129,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
#| Fetch the given url.
#| First attempts to clone the repository, but if it already exists (or fails) it attempts to pull down new changes
method fetch(Str() $uri, IO() $save-as, Supplier :$stdout, Supplier :$stderr --> IO::Path) {
return self!clone(self!repo-url($uri), $save-as) || self!pull($save-as);
return self!clone(self!repo-url($uri), $save-as, :$stdout, :$stderr) || self!pull($save-as, :$stdout, :$stderr);
}

#| Extracts the given path.
Expand All @@ -137,19 +138,19 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
die "target repo directory {$repo-path.absolute} does not contain a .git/ folder"
unless $repo-path.child('.git').d;

my $sha1 = self!rev-parse(self!fetch($repo-path)).head;
my $sha1 = self!rev-parse(self!fetch($repo-path, :$stdout, :$stderr), :$stdout, :$stderr).head;
die "target repo directory {$repo-path.absolute} failed to locate checkout revision"
unless $sha1;

my $checkout-to = $extract-to.child($sha1);
die "target repo directory {$extract-to.absolute} does not exist and could not be created"
unless ($checkout-to.e && $checkout-to.d) || mkdir($checkout-to);

return self!checkout($repo-path, $checkout-to, $sha1);
return self!checkout($repo-path, $checkout-to, $sha1, :$stdout, :$stderr);
}

#| Returns an array of strings, where each string is a relative path representing a file that can be extracted from the given $repo-path
method ls-files(IO() $repo-path --> Array[Str]) {
method ls-files(IO() $repo-path, Supplier :$stdout, Supplier :$stderr --> Array[Str]) {
die "target repo directory {$repo-path.absolute} does not contain a .git/ folder"
unless $repo-path.child('.git').d;

Expand All @@ -159,6 +160,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
my $cwd := $repo-path.absolute;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', 'ls-tree', '-r', '--name-only', self!checkout-name($repo-path));
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { $output.append($_) }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -171,7 +173,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
}

#| On success returns an IO::Path to where a `git clone ...` has put files
method !clone($url, IO() $save-as --> IO::Path) {
method !clone($url, IO() $save-as, Supplier :$stdout, Supplier :$stderr --> IO::Path) {
die "target download directory {$save-as.absolute} does not exist and could not be created"
unless $save-as.d || mkdir($save-as);

Expand All @@ -180,6 +182,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
my $cwd := $save-as.parent;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', 'clone', $url, $save-as.basename, '--quiet');
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -189,7 +192,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
}

#| Does a `git pull` on an existing local git repo
method !pull(IO() $repo-path --> IO::Path) {
method !pull(IO() $repo-path, Supplier :$stdout, Supplier :$stderr --> IO::Path) {
die "target download directory {$repo-path.absolute} does not contain a .git/ folder"
unless $repo-path.child('.git').d;

Expand All @@ -198,6 +201,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
my $cwd := $repo-path.absolute;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', 'pull', '--quiet');
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -207,7 +211,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
}

#| Does a `git fetch` on an existing local git repo. Not really related to self.fetch(...)
method !fetch(IO() $repo-path --> IO::Path) {
method !fetch(IO() $repo-path, Supplier :$stdout, Supplier :$stderr --> IO::Path) {
die "target download directory {$repo-path.absolute} does not contain a .git/ folder"
unless $repo-path.child('.git').d;

Expand All @@ -216,6 +220,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
my $cwd := $repo-path.absolute;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', 'fetch', '--quiet');
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -225,12 +230,13 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
}

#| Does a `git checkout ...`, allowing git source urls to have e.g. trailing @tag
method !checkout(IO() $repo-path, IO() $extract-to, $target --> IO::Path) {
method !checkout(IO() $repo-path, IO() $extract-to, $target, Supplier :$stdout, Supplier :$stderr --> IO::Path) {
my $passed;
react {
my $cwd := $repo-path.absolute;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', '--work-tree', $extract-to, 'checkout', $target, '.');
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -240,7 +246,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
}

#| Does a `git rev-parse ...` (used to get a sha1 for saving a copy of a specific checkout)
method !rev-parse(IO() $save-as --> Array[Str]) {
method !rev-parse(IO() $save-as, Supplier :$stdout, Supplier :$stderr --> Array[Str]) {
die "target repo directory {$save-as.absolute} does not contain a .git/ folder"
unless $save-as.child('.git').d;

Expand All @@ -250,6 +256,7 @@ class Zef::Service::Shell::git does Fetcher does Extractor does Probeable {
my $cwd := $save-as.absolute;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('git', 'rev-parse', self!checkout-name($save-as));
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { $output.append($_) }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand Down
7 changes: 5 additions & 2 deletions lib/Zef/Service/Shell/tar.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ class Zef::Service::Shell::tar does Extractor {
=head2 method ls-files
method ls-files(IO() $archive-file --> Array[Str])
method ls-files(IO() $archive-file, Supplier :$stdout, Supplier :$stderr --> Array[Str])
On success it returns an C<Array> of relative paths that are available to be extracted from C<$archive-file>.
A C<Supplier> can be supplied as C<:$stdout> and C<:$stderr> to receive any output.
=end pod

Expand Down Expand Up @@ -121,6 +122,7 @@ class Zef::Service::Shell::tar does Extractor {
my $cwd := $archive-file.parent;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('tar', '-zxvf', $archive-file.basename, '-C', $extract-to.relative($cwd));
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -130,7 +132,7 @@ class Zef::Service::Shell::tar does Extractor {
}

#| Returns an array of strings, where each string is a relative path representing a file that can be extracted from the given $archive-file
method ls-files(IO() $archive-file) {
method ls-files(IO() $archive-file, Supplier :$stdout, Supplier :$stderr) {
die "archive file does not exist: {$archive-file.absolute}"
unless $archive-file.e && $archive-file.f;

Expand All @@ -140,6 +142,7 @@ class Zef::Service::Shell::tar does Extractor {
my $cwd := $archive-file.parent;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('tar', '-zt', '-f', $archive-file.basename);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { $output.append($_) }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand Down
7 changes: 5 additions & 2 deletions lib/Zef/Service/Shell/unzip.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ class Zef::Service::Shell::unzip does Extractor {
=head2 method ls-files
method ls-files(IO() $archive-file --> Array[Str])
method ls-files(IO() $archive-file, Supplier :$stdout, Supplier :$stderr --> Array[Str])
On success it returns an C<Array> of relative paths that are available to be extracted from C<$archive-file>.
A C<Supplier> can be supplied as C<:$stdout> and C<:$stderr> to receive any output.
=end pod

Expand Down Expand Up @@ -98,6 +99,7 @@ class Zef::Service::Shell::unzip does Extractor {
my $cwd := $archive-file.parent;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('unzip', '-o', '-qq', $archive-file.basename, '-d', $extract-to.absolute);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand All @@ -107,7 +109,7 @@ class Zef::Service::Shell::unzip does Extractor {
}

#| Returns an array of strings, where each string is a relative path representing a file that can be extracted from the given $archive-file
method ls-files(IO() $archive-file) {
method ls-files(IO() $archive-file, Supplier :$stdout, Supplier :$stderr) {
die "archive file does not exist: {$archive-file.absolute}"
unless $archive-file.e && $archive-file.f;

Expand All @@ -117,6 +119,7 @@ class Zef::Service::Shell::unzip does Extractor {
my $cwd := $archive-file.parent;
my $ENV := %*ENV;
my $proc = Zef::zrun-async('unzip', '-Z', '-1', $archive-file.basename);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { $output.append($_) }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand Down
1 change: 1 addition & 0 deletions lib/Zef/Service/Shell/wget.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Zef::Service::Shell::wget does Fetcher does Probeable {
my $ENV := %*ENV;
my $cmd := self!command();
my $proc = Zef::zrun-async($cmd, '-P', $cwd, '--quiet', $uri, '-O', $save-as.absolute);
$stdout.emit("Command: {$proc.command}");
whenever $proc.stdout(:bin) { }
whenever $proc.stderr(:bin) { }
whenever $proc.start(:$ENV, :$cwd) { $passed = $_.so }
Expand Down

0 comments on commit c8ec8af

Please sign in to comment.