chiark / gitweb /
nailing-cargo: wip Cargo.lock manipulation
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 10 May 2020 10:43:42 +0000 (11:43 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 10 May 2020 10:43:42 +0000 (11:43 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
nailing-cargo

index 32da8fca69599398aff44688b16e8319207ece6a..eecf01c828a0bd443ce5511bb12dd3bed5756da6 100755 (executable)
@@ -67,8 +67,11 @@ use File::Compare;
 use TOML::Tiny::Faithful;
 
 our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
-$worksphere =~ s{/[^/]+$}{} or die "$self: cwd \`$worksphere' unsupported!\n";
+$worksphere =~ s{/([^/]+)$}{}
+  or die "$self: cwd \`$worksphere' unsupported!\n";
+our $subdir = $1;
 our $lockfile = "../.nailing-cargo.lock";
+our $oot_cargo_lock_faff;
 
 our @configs;
 our $verbose=1;
@@ -249,6 +252,73 @@ sub calculate () {
     close N or die "$self: close new $nailing: $!\n";
   }
 }
+s
+our $oot_dir;
+our @out_command;
+
+sub calculate_oot () {
+  my $oot = $nail->{oot};
+  return unless $oot && (defined $oot->{dir} or defined $oot->{use});
+  if (@ARGV && $ARGV[0] =~ m/generate-lockfile|update/) {
+    $oot_cargo_lock_faff = 1;
+  }
+  my ($pre,$post);
+  my @xargs;
+  if (!$oot_cargo_lock_faff) {
+    @xargs = $xxx_subdir;
+    ($pre, $post) = ('cd "$1"; shift', '');
+  } else {
+    @xargs = $xxx_builddir, $xxx_subdir, $xxx_src_abs_dir;
+    ($pre, $post) = (<<'END', <<'END');
+        cd "$1"; shift;
+        mkdir -p -- "$1"; cd "$1"; shift;
+        cp -- "$1"/Cargo.toml "$1"/Cargo.lock .; shift;
+        mkdir -p src; >src/lib.rs;
+END
+        rm -r src Cargo.toml;
+END
+    $pre  =~ s/^\s+//mg; $pre  =~ s/^\s+\n/ /g;
+    $post =~ s/^\s+//mg; $post =~ s/^\s+\n/ /g;
+  }
+  my $use = $oot->{use} // die "$self: [oot] specified, need oot.use\n";
+  my $getuser = sub {
+    scalar($oot->{user} // die "$self: oot.use $use requires oot.user\n")
+  };
+  my @command;
+  my $sh_ec = sub {
+    if (!length $post) {
+      @command = @_, 'sh','-ec',$pre.' exec "$@"','--',@xargs;
+    } else {
+      @command = @_, 'sh','-ec',$pre.' "$@"; '.$post,'--',@xargs;
+    }
+  };
+  my $command_sh = sub {
+    my $quoted = join ' ', map {
+      return $_ if !m/\W/;
+      s/\'/\'\\'\'/g;
+      "'$_'"
+    } @ARGV;
+    @command = @_, "set -e; $pre $quoted; $post";
+  };
+  if ($use eq 'really') {
+    my $user = $getuser->();
+    my @pw = getpwnam $user or "die $self: oot.user lookup failed\n";
+    my $homedir = $pw[7];
+    $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
+  } elsif ($use eq 'ssh') {
+    my $user = $getuser->();
+    $user .= '@localhost' unless $user =~ m/\@/;
+    $command_sh->('ssh',$user);
+  } elsif ($use eq 'command_sh') {
+    $command_sh->(@{ $oot->{command} // die "$self: need oot.command\n" });
+  } elsif ($use eq 'command_args') {
+    $sh_ec->(@{ $oot->{command} // die "$self: need oot.command\n" });
+  } else {
+    die "$self: oot.use mode $use not recognised\n";
+  }
+  die unless @command;
+  @ARGV = @command;
+}
 
 our $want_uninstall;
 
@@ -344,6 +414,8 @@ while (@ARGV && $ARGV[0] =~ m/^-/) {
        $noact++;
       } elsif (s{^-D}{-}) {
        $dump++;
+      } elsif (s{^-L}{-}) {
+       $oot_cargo_lock_faff=1;
       } else {
        die "$self: unknown short option(s) $_\n";
       }
@@ -359,11 +431,12 @@ takelock();
 readnail();
 readorigs();
 calculate();
+calculate_oot();
 
 if ($dump) {
   eval '
     use Data::Dumper;
-    print STDERR Dumper(\%manifests, \%packagemap);
+    print STDERR Dumper(\%manifests, \%packagemap, \@ARGV);
   ' or die $@;
 }
 
@@ -381,6 +454,9 @@ my $estatus = invoke();
 
 uninstall();
 $want_uninstall = 1;
-print STDERR "$self: unnailed.\n" if $verbose;
+
+get_cargo_lock() if $oot_cargo_lock_faff;
+
+print STDERR "$self: unnailed.  status $estatus.\n" if $verbose;
 
 exit $estatus;