Skip to content

Commit

Permalink
Support updating of plugins
Browse files Browse the repository at this point in the history
- Adds a new `--update` option to both the `lock` and `source` commands.
- Updating Git sources will now fetch the remote and recheckout the
  configured reference.
- Updating remote sources will redownload them (same as `--reinstall`).
- Fixes some issues with changing `branch`, `tag`, and `rev` fields in
  plugin configuration. Sheldon now correctly checks the new reference
  out and will try fetch from the remote if the reference is missing
  locally.
  • Loading branch information
rossmacarthur committed Jul 28, 2020
1 parent 57b3375 commit 5a8254d
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 117 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -176,6 +176,12 @@ are all okay. It will always regenerate the lock file.
sheldon lock
```

To update all plugin sources you can use the `--update` flag.

```sh
sheldon lock --update
```

To force a reinstall of all plugin sources you can use the `--reinstall` flag.

```sh
Expand Down
75 changes: 64 additions & 11 deletions src/cli.rs
Expand Up @@ -11,7 +11,7 @@ use url::Url;

use crate::{
config::{GistRepository, GitHubRepository, GitProtocol, GitReference, RawPlugin, Shell},
context::Settings,
context::{LockMode, Settings},
edit::Plugin,
log::{Output, Verbosity},
};
Expand Down Expand Up @@ -119,8 +119,12 @@ enum RawCommand {
/// Install the plugins sources and generate the lock file.
#[structopt(help_message = HELP_MESSAGE)]
Lock {
/// Reinstall all plugin sources.
/// Update all plugin sources.
#[structopt(long)]
update: bool,

/// Reinstall all plugin sources.
#[structopt(long, conflicts_with = "update")]
reinstall: bool,
},

Expand All @@ -130,8 +134,13 @@ enum RawCommand {
/// Regenerate the lock file.
#[structopt(long)]
relock: bool,
/// Reinstall all plugin sources (implies --relock).

/// Update all plugin sources (implies --relock).
#[structopt(long)]
update: bool,

/// Reinstall all plugin sources (implies --relock).
#[structopt(long, conflicts_with = "update")]
reinstall: bool,
},
}
Expand Down Expand Up @@ -199,9 +208,9 @@ pub enum Command {
/// Remove a plugin from the config file.
Remove { name: String },
/// Install the plugins sources and generate the lock file.
Lock { reinstall: bool },
Lock { mode: LockMode },
/// Generate and print out the script.
Source { reinstall: bool, relock: bool },
Source { relock: bool, mode: LockMode },
}

/// Resolved command line options with defaults set.
Expand All @@ -215,6 +224,17 @@ pub struct Opt {
pub command: Command,
}

impl LockMode {
fn from_lock_opts(update: bool, reinstall: bool) -> Self {
match (update, reinstall) {
(true, false) => Self::Update,
(false, true) => Self::Reinstall,
(false, false) => Self::Normal,
(true, true) => unreachable!(),
}
}
}

impl Plugin {
fn from_add(add: Add) -> (String, Self) {
let Add {
Expand Down Expand Up @@ -330,11 +350,18 @@ impl Opt {
}
RawCommand::Edit => Command::Edit,
RawCommand::Remove { name } => Command::Remove { name },
RawCommand::Lock { reinstall } => Command::Lock { reinstall },
RawCommand::Source { relock, reinstall } => Command::Source {
relock: relock || reinstall,
RawCommand::Lock { update, reinstall } => {
let mode = LockMode::from_lock_opts(update, reinstall);
Command::Lock { mode }
}
RawCommand::Source {
relock,
update,
reinstall,
},
} => {
let mode = LockMode::from_lock_opts(update, reinstall);
Command::Source { relock, mode }
}
};

Self {
Expand Down Expand Up @@ -456,7 +483,10 @@ SUBCOMMANDS:
lock_file: None,
clone_dir: None,
download_dir: None,
command: RawCommand::Lock { reinstall: false },
command: RawCommand::Lock {
update: false,
reinstall: false
},
}
);
}
Expand Down Expand Up @@ -493,7 +523,10 @@ SUBCOMMANDS:
lock_file: Some("/test/plugins.lock".into()),
clone_dir: Some("/repos".into()),
download_dir: Some("/downloads".into()),
command: RawCommand::Lock { reinstall: false },
command: RawCommand::Lock {
update: false,
reinstall: false
},
}
);
}
Expand Down Expand Up @@ -918,6 +951,7 @@ USAGE:
{name} lock [FLAGS]
FLAGS:
--update Update all plugin sources
--reinstall Reinstall all plugin sources
-h, --help Show this message and exit",
name = crate_name!(),
Expand All @@ -928,6 +962,15 @@ FLAGS:
assert_eq!(err.info, None);
}

#[test]
fn raw_opt_lock_with_update_and_reinstall_expect_conflict() {
setup();
assert_eq!(
raw_opt_err(&["lock", "--update", "--reinstall"]).kind,
structopt::clap::ErrorKind::ArgumentConflict
);
}

#[test]
fn raw_opt_source_help() {
setup();
Expand All @@ -944,6 +987,7 @@ USAGE:
FLAGS:
--relock Regenerate the lock file
--update Update all plugin sources (implies --relock)
--reinstall Reinstall all plugin sources (implies --relock)
-h, --help Show this message and exit",
name = crate_name!(),
Expand All @@ -953,4 +997,13 @@ FLAGS:
assert_eq!(err.kind, structopt::clap::ErrorKind::HelpDisplayed);
assert_eq!(err.info, None);
}

#[test]
fn raw_opt_source_with_update_and_reinstall_expect_conflict() {
setup();
assert_eq!(
raw_opt_err(&["source", "--update", "--reinstall"]).kind,
structopt::clap::ErrorKind::ArgumentConflict
);
}
}
15 changes: 13 additions & 2 deletions src/context.rs
Expand Up @@ -45,6 +45,17 @@ pub struct EditContext {
pub shell: Option<Shell>,
}

/// Behaviour when locking a config file.
#[derive(Debug)]
pub enum LockMode {
/// Apply any changed configuration.
Normal,
/// Apply any changed configuration and update all plugins.
Update,
/// Apply any changed configuration and reinstall all plugins.
Reinstall,
}

/// Contextual information to use while running the main tasks (lock and
/// source).
#[derive(Debug)]
Expand All @@ -53,8 +64,8 @@ pub struct LockContext {
pub settings: Settings,
/// The output style.
pub output: Output,
/// Whether to reinstall plugin sources.
pub reinstall: bool,
/// The relock mode.
pub mode: LockMode,
}

macro_rules! setting_access {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Expand Up @@ -258,19 +258,19 @@ impl Sheldon {
};
Self::remove(&ctx, name)
}
Command::Lock { reinstall } => {
Command::Lock { mode } => {
let ctx = LockContext {
settings,
output,
reinstall,
mode,
};
Self::lock(&ctx, &mut warnings)
}
Command::Source { relock, reinstall } => {
Command::Source { relock, mode } => {
let ctx = LockContext {
settings,
output,
reinstall,
mode,
};
Self::source(&ctx, relock, &mut warnings)
}
Expand Down

0 comments on commit 5a8254d

Please sign in to comment.