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
#![forbid(unsafe_code)]

#[macro_use] mod macros;

use std::env::ArgsOs;
use std::fmt::{self, Display, Debug, Formatter, Write};
use std::ffi::OsString;
use std::hash::*;
use std::io;
use std::path::*;
use std::process::Command;



/// An opaque `cargo-local-install` error, currently meant for [Display] only.

pub struct Error(String, Option<Inner>);
impl Display for Error { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { write!(fmt, "{}", self.0) } }
impl Debug   for Error { fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { write!(fmt, "Error({:?})", self.0) } }
impl std::error::Error for Error {}

enum Inner { Io(io::Error) }
impl From<io::Error> for Inner { fn from(err: io::Error) -> Self { Inner::Io(err) } }

macro_rules! error {
    ( None,      $fmt:literal $($tt:tt)* ) => { Error(format!($fmt $($tt)*), None) };
    ( $err:expr, $fmt:literal $($tt:tt)* ) => { Error(format!($fmt $($tt)*), Some($err.into())) };
}



#[derive(Clone, Copy, PartialEq, Eq)]
enum LogMode {
    Quiet,
    Normal,
    Verbose,
}

/// Run an install after reading the executable name / subcommand.

/// Will `exit(...)`.

///

/// ## Example

/// ```no_run

/// fn main() {

///     let mut args = std::env::args_os();

///     let _cargo_exe  = args.next(); // "cargo.exe"

///     let _subcommand = args.next(); // "local-install"

///     cargo_local_install::exec_from_args_os_after_exe(args);

/// }

/// ```

pub fn exec_from_args_os_after_exe(args: ArgsOs) -> ! {
    run_from_args_os_after_exe(args).unwrap_or_else(|err| fatal!("{}", err));
    std::process::exit(0);
}

/// Run an install after reading the executable name / subcommand.

///

/// ## Example

/// ```no_run

/// fn main() {

///     let mut args = std::env::args_os();

///     let _cargo_exe  = args.next(); // "cargo.exe"

///     let _subcommand = args.next(); // "local-install"

///     cargo_local_install::run_from_args_os_after_exe(args).unwrap();

/// }

/// ```

pub fn run_from_args_os_after_exe(args: ArgsOs) -> Result<(), Error> {
    let mut args = args.peekable();

    let mut dry_run     = false;
    let mut log_mode    = LogMode::Normal;
    let mut locked      = None;
    let mut root        = None;
    let mut target_dir  = None;
    let mut path        = None;

    let mut options     = Vec::<(OsString, Vec<OsString>)>::new(); // will get reordered for improved caching

    let mut crates      = Vec::<OsString>::new();

    let mut any = false;
    while let Some(arg) = args.next() {
        any = true;
        let lossy = arg.to_string_lossy();
        match &*lossy {
            "--help"        => return help(),
            //"--version"     => return version(), // XXX: Conflicts with version selection flag


            // We want to warn if `--locked` wasn't passed, since you probably wanted it

            "--locked"      => locked = Some(true ),
            "--unlocked"    => locked = Some(false), // new to cargo-local-install


            // Custom-handled flags

            "--root"        => root         = Some(PathBuf::from(args.next().ok_or_else(|| error!(None, "--root must specify a directory"))?)),
            "--target-dir"  => target_dir   = Some(canonicalize(PathBuf::from(args.next().ok_or_else(|| error!(None, "--target-dir must specify a directory"))?))?),
            "--path"        => path         = Some(canonicalize(PathBuf::from(args.next().ok_or_else(|| error!(None, "--path must specify a directory"))?))?),
            "--list"        => return Err(error!(None, "not yet implemented: --list (should this list global cache or local bins?)")),
            "--no-track"    => return Err(error!(None, "not yet implemented: --no-track (the entire point of this crate is tracking...)")),
            "-Z"            => return Err(error!(None, "not yet implemented: -Z flags")),
            "--frozen"      => return Err(error!(None, "not yet implemented: --frozen (last I checked this never worked in cargo install anyways?)")), // https://github.com/rust-lang/cargo/issues/7169#issuecomment-515195574

            "--offline"     => return Err(error!(None, "not yet implemented: --offline")),
            "--dry-run"     => dry_run = true, // new to cargo-local-install


            // pass-through single-arg commands

            "-q" | "--quiet" => {
                log_mode = LogMode::Quiet;
                options.push((arg, Vec::new()));
            },
            "-v" | "--verbose" => {
                log_mode = LogMode::Verbose;
                options.push((arg, Vec::new()));
            },
            "-j" | "--jobs" |
            "-f" | "--force" |
            "--all-features" | "--no-default-features" |
            "--debug" | "--bins" | "--examples"
            => {
                options.push((arg, Vec::new()));
            },

            // pass-through single-arg commands

            "--version" |
            "--git" | "--branch" | "--tag" | "--rev" |
            "--profile" | "--target" |
            "--index" | "--registry" |
            "--color"
            => {
                let arg2 = args.next().ok_or_else(|| error!(None, "{} requires an argument", lossy))?;
                options.push((arg, vec![arg2]));
            },

            // pass-through multi-arg commands

            "--features"    => return Err(error!(None, "not yet implemented: {}", lossy)),
            "--bin"         => return Err(error!(None, "not yet implemented: {}", lossy)),
            "--example"     => return Err(error!(None, "not yet implemented: {}", lossy)),

            "--" => {
                crates.extend(args);
                break;
            },

            flag if flag.starts_with("-") => return Err(error!(None, "unrecognized flag: {}", flag)),
            _krate => crates.push(arg),
        }
    }
    if !any {
        let _ = print_usage(std::io::stderr().lock());
        return Err(error!(None, "no arguments were specified"));
    }
    let quiet   = log_mode == LogMode::Quiet;
    let verbose = log_mode == LogMode::Verbose;

    let locked = locked.unwrap_or_else(|| {
        warnln!("either specify --locked to use the same dependencies the crate was built with, or --unlocked to get rid of this warning");
        false
    });
    if locked {
        options.push(("--locked".into(), Vec::new()));
    }

    if crates.is_empty() { return Err(error!(None, "no crates specified")) }

    let global_dir = {
        let var = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
        let mut d = PathBuf::from(std::env::var_os(var).ok_or_else(|| error!(None, "couldn't determine target dir, {} not set", var))?);
        d.push(".cargo");
        d.push("local-install");
        d
    };
    let crates_cache_dir = global_dir.join("crates");

    let target_dir = target_dir.map_or_else(|| Ok(global_dir.join("target")), |td| canonicalize(td))?;
    options.push(("--target-dir".into(), vec![target_dir.into()]));
    if let Some(path) = path { options.push(("--path".into(), vec![canonicalize(path)?.into()])); }
    options.sort();
    let options = options;

    let root = root.unwrap_or_else(|| PathBuf::new());
    let dst_bin = root.join("bin");
    std::fs::create_dir_all(&dst_bin).map_err(|err| error!(err, "unable to create {}: {}", dst_bin.display(), err))?;

    for krate in crates.into_iter() {
        let context = Context { dry_run, quiet, verbose, crates_cache_dir: crates_cache_dir.as_path(), dst_bin: dst_bin.as_path() };
        install_crate(context, krate, options.iter())?;
    }

    warnln!("be sure to add `{}` to your PATH to be able to run the installed binaries", dst_bin.display());
    Ok(())
}

struct Context<'a> {
    pub dry_run:            bool,
    pub quiet:              bool,
    pub verbose:            bool,
    pub crates_cache_dir:   &'a Path,
    pub dst_bin:            &'a Path,
}

fn install_crate<'o, Opts: Iterator<Item = &'o (OsString, Vec<OsString>)>>(context: Context, krate: OsString, options: Opts) -> Result<(), Error> {
    let Context { dry_run, quiet, verbose, crates_cache_dir, dst_bin } = context;

    let mut trace = format!("cargo install");
    let mut cmd = Command::new("cargo");
    cmd.arg("install");
    for (flag, args) in options {
        write!(&mut trace, " {}", flag.to_str().unwrap()).unwrap();
        cmd.arg(flag);
        for arg in args.into_iter() {
            write!(&mut trace, " {:?}", arg).unwrap();
            cmd.arg(arg);
        }
    }

    let hash = {
        // real trace will have "--root ...", but that depends on hash!

        let trace_for_hash = format!("{} -- {}", trace, krate.to_string_lossy());
        #[allow(deprecated)] let mut hasher = std::hash::SipHasher::new();
        trace_for_hash.hash(&mut hasher);
        format!("{:016x}", hasher.finish())
    };

    let krate_build_dir = crates_cache_dir.join(hash);
    write!(&mut trace, " --root {:?}", krate_build_dir.display()).unwrap();
    cmd.arg("--root").arg(&krate_build_dir);

    trace.push_str(" -- ");
    trace.push_str(&krate.to_string_lossy());
    cmd.arg("--");
    cmd.arg(krate);

    if verbose { statusln!("Running", "`{}`", trace) }
    if !dry_run {
        let status = cmd.status().map_err(|err| error!(err, "failed to execute {}: {}", trace, err))?;
        match status.code() {
            Some(0) => { if verbose { statusln!("Succeeded", "`{}`", trace) } },
            Some(n) => return Err(error!(None, "{} failed (exit code {})", trace, n)),
            None    => return Err(error!(None, "{} failed (signal)", trace)),
        }
    } else { // dry_run

        statusln!("Skipped", "`{}` (--dry-run)", trace);
        return Ok(()); // XXX: Would be nice to log copied bins, but without building them we don't know what they are

    }

    let src_bin_path = krate_build_dir.join("bin");
    let src_bins = src_bin_path.read_dir().map_err(|err| error!(err, "unable to enumerate source bins at {}: {}", src_bin_path.display(), err))?;
    for src_bin in src_bins {
        let src_bin = src_bin.map_err(|err| error!(err, "error enumerating source bins at {}: {}", src_bin_path.display(), err))?;
        let dst_bin = dst_bin.join(src_bin.file_name());
        let file_type = src_bin.file_type().map_err(|err| error!(err, "error determining file type for {}: {}", src_bin.path().display(), err))?;
        if !file_type.is_file() { continue }
        let src_bin = src_bin.path();

        if !quiet { statusln!("Replacing", "`{}`", dst_bin.display()) }
        #[cfg(windows)] {
            let _ = std::fs::remove_file(&dst_bin);
            if let Err(err) = std::os::windows::fs::symlink_file(&src_bin, &dst_bin) {
                if !quiet { warnln!("Unable link `{}` to `{}`: {}", dst_bin.display(), src_bin.display(), err) }
            } else {
                if !quiet { statusln!("Linked", "`{}` to `{}`", dst_bin.display(), src_bin.display()) }
                continue
            }
        }
        #[cfg(unix)] {
            let _ = std::fs::remove_file(&dst_bin);
            if let Err(err) = std::os::unix::fs::symlink(&src_bin, &dst_bin) {
                if !quiet { warnln!("Unable link `{}` to `{}`: {}", dst_bin.display(), src_bin.display(), err) }
            } else {
                if !quiet { statusln!("Linked", "`{}` to `{}`", dst_bin.display(), src_bin.display()) }
                continue
            }
        }
        std::fs::copy(&src_bin, &dst_bin).map_err(|err| error!(err, "error replacing `{}` with `{}`: {}", dst_bin.display(), src_bin.display(), err))?;
        if !quiet { statusln!("Replaced", "`{}` with `{}`", dst_bin.display(), src_bin.display()) }
    }

    warnln!("be sure to add `{}` to your PATH to be able to run the installed binaries", dst_bin.display());
    Ok(())
}

fn help() -> Result<(), Error> {
    print_usage(&mut std::io::stdout().lock()).map_err(|err| error!(err, "unable to write help text to stdout: {}", err))
}

fn print_usage(mut o: impl io::Write) -> io::Result<()> {
    let o = &mut o;
    writeln!(o, "cargo-local-install")?;
    writeln!(o, "Install a Rust binary. Default installation location is ./bin")?;
    writeln!(o)?;
    writeln!(o, "USAGE:")?;
    writeln!(o, "    cargo local-install [OPTIONS] [--] [crate]...")?;
    writeln!(o, "    cargo-local-install [OPTIONS] [--] [crate]...")?;
    writeln!(o)?;
    writeln!(o, "OPTIONS:")?;
    // pass-through options to `cargo install`

    writeln!(o, "    -q, --quiet                                      No output printed to stdout")?;
    writeln!(o, "        --version <VERSION>                          Specify a version to install")?;
    writeln!(o, "        --git <URL>                                  Git URL to install the specified crate from")?;
    writeln!(o, "        --tag <TAG>                                  Tag to use when installing from git")?;
    writeln!(o, "        --rev <SHA>                                  Specific commit to use when installing from git")?;
    writeln!(o, "        --path <PATH>                                Filesystem path to local crate to install")?;
    // writeln!(o, "        --list                                       list all installed packages and their versions // not supported

    writeln!(o, "    -j, --jobs <N>                                   Number of parallel jobs, defaults to # of CPUs")?;
    writeln!(o, "    -f, --force                                      Force overwriting existing crates or binaries")?;
    // writeln!(o, "        --no-track                                   Do not save tracking information")?; // not supported

    // writeln!(o, "        --features <FEATURES>...                     Space or comma separated list of features to activate")?; // nyi

    writeln!(o, "        --all-features                               Activate all available features")?;
    writeln!(o, "        --no-default-features                        Do not activate the `default` feature")?;
    writeln!(o, "        --profile <PROFILE-NAME>                     Install artifacts with the specified profile")?;
    writeln!(o, "        --debug                                      Build in debug mode instead of release mode")?;
    // writeln!(o, "        --bin <NAME>...                              Install only the specified binary")?; // nyi

    writeln!(o, "        --bins                                       Install all binaries")?;
    // writeln!(o, "        --example <NAME>...                          Install only the specified example")?; // nyi

    writeln!(o, "        --examples                                   Install all examples")?;
    writeln!(o, "        --target <TRIPLE>                            Build for the target triple")?;
    writeln!(o, "        --target-dir <DIRECTORY>                     Directory for all generated artifacts")?;
    writeln!(o, "        --root <DIR>                                 Directory to install packages into")?;
    writeln!(o, "        --index <INDEX>                              Registry index to install from")?;
    writeln!(o, "        --registry <REGISTRY>                        Registry to use")?;
    writeln!(o, "    -v, --verbose                                    Use verbose output (-vv very verbose/build.rs output)")?;
    writeln!(o, "        --color <WHEN>                               Coloring: auto, always, never")?;
    // writeln!(o, "        --frozen                                     Require Cargo.lock and cache are up to date")?; // not supported

    writeln!(o, "        --locked                                     Require Cargo.lock is up to date")?;
    // writeln!(o, "        --offline                                    Run without accessing the network")?; // not supported

    // CUSTOM FLAGS:

    writeln!(o, "        --unlocked                                   Don't require an up-to-date Cargo.lock")?;
    writeln!(o, "        --dry-run                                    Print `cargo install ...` spam but don't actually install")?;
    // writeln!(o, "    -Z <FLAG>...")?; // nyi

    writeln!(o)?;
    writeln!(o, "ARGS:")?;
    writeln!(o, "    <crate>...")?;
    writeln!(o)?;
    writeln!(o, "This command wraps `cargo install` to solve a couple of problems with using")?;
    writeln!(o, "the basic command directly:")?;
    writeln!(o)?;
    writeln!(o, "* The global `~/.cargo/bin` directory can contain only a single installed")?;
    writeln!(o, "  version of a package at a time - if you've got one project relying on")?;
    writeln!(o, "  `cargo web 0.5` and another prjoect relying on `cargo web 0.6`, you're SOL.")?;
    writeln!(o)?;
    writeln!(o, "* Forcing local installs with `--root my/project` to avoid global version")?;
    writeln!(o, "  conflicts means you must rebuild the entire dependency for each project,")?;
    writeln!(o, "  even when you use the exact same version for 100 other projects before.")?;
    writeln!(o)?;
    writeln!(o, "* When building similar binaries, the lack of target directory caching means")?;
    writeln!(o, "  the entire dependency tree must still be rebuilt from scratch.")?;
    Ok(())
}

#[allow(dead_code)]
fn version() {
    // TODO: (git hash, mod status, date) via build.rs nonsense?

    println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
}

fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf, Error> {
    let path = path.as_ref();
    let path = std::fs::canonicalize(path).map_err(|err| error!(err, "unable to canonicalize {}: {}", path.display(), err))?;
    let mut o = PathBuf::new();
    for component in path.components() {
        if let Component::Prefix(pre) = component {
            match pre.kind() {
                Prefix::VerbatimDisk(disk)  => o.push(format!("{}:", disk as char)),
                _other                      => o.push(component),
            }
        } else {
            o.push(component);
        }
    }
    Ok(o)
}