Skip to content

Commit

Permalink
refactor: commands to determine local and remote actions properly
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Feb 20, 2021
1 parent cdbb50a commit 5eec58b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
57 changes: 36 additions & 21 deletions src/cli/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ impl<'a> Actions<'a> {
};

// Process Fish shell package structure and read the Packet manifest
let manifest = self
.paket
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_name, is_pkg_local)?;
let manifest =
self.paket
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_fmt.pkg_name, is_pkg_local)?;

if let (Some(toml_pkg), Some(toml_events)) = (manifest.package, manifest.events) {
if let Some(toml_pkg) = manifest.package {
// Copy all corresponding package files to Fish shell directories
self.paket
.scan_pkg_dir(&pkg_dir, &toml_pkg.include, |src, dest| {
Expand All @@ -68,8 +68,12 @@ impl<'a> Actions<'a> {
})?;

// Emit an `after_install` Fish shell event if there is an associated Paket event
self.paket
.emit_event(&toml_pkg.name, &toml_events, PaketEvents::AfterInstall)?;
if let Some(toml_events) = manifest.events {
self.paket
.emit_event(&toml_pkg.name, &toml_events, PaketEvents::AfterInstall)?;
}
} else {
bail!("`paket.toml` file could not be parsed correctly.")
};

println!("Package was installed successfully.");
Expand Down Expand Up @@ -116,11 +120,11 @@ impl<'a> Actions<'a> {
};

// Process Fish shell package structure and read the Packet manifest
let manifest = self
.paket
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_name, is_pkg_local)?;
let manifest =
self.paket
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_fmt.pkg_name, is_pkg_local)?;

if let (Some(toml_pkg), Some(toml_events)) = (manifest.package, manifest.events) {
if let Some(toml_pkg) = manifest.package {
// Copy all corresponding package files to Fish shell directories
self.paket
.scan_pkg_dir(&pkg_dir, &toml_pkg.include, |src, dest| {
Expand All @@ -129,8 +133,12 @@ impl<'a> Actions<'a> {
})?;

// Emit an `after_update` Fish shell event if there is an associated Paket event
self.paket
.emit_event(&toml_pkg.name, &toml_events, PaketEvents::AfterUpdate)?;
if let Some(toml_events) = manifest.events {
self.paket
.emit_event(&toml_pkg.name, &toml_events, PaketEvents::AfterUpdate)?;
}
} else {
bail!("`paket.toml` file could not be parsed correctly.")
};

println!("Package was updated successfully.");
Expand All @@ -150,12 +158,12 @@ impl<'a> Actions<'a> {
let pkg_dir = if is_pkg_path {
let pkg_path = pkg_path.unwrap_or_default();
println!(
"Removing installed package using directory `{}` as reference...",
"Uninstalling package using directory `{}` as reference...",
pkg_path.display()
);
pkg_path
} else {
println!("Removing package `{}`...", &pkg_name);
println!("Uninstalling package `{}`...", &pkg_name);

// Process Fish shell package structure
let pkg_dir = self.git.base_dir.join(&pkg_name);
Expand All @@ -170,14 +178,19 @@ impl<'a> Actions<'a> {
};

// Process Fish shell package structure and read the Packet manifest
let manifest = self
.paket
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_name, is_pkg_path)?;

if let (Some(toml_pkg), Some(toml_events)) = (manifest.package, manifest.events) {
// Emit a `before_uninstall` Fish shell event if there is an associated Paket event
let manifest =
self.paket
.emit_event(&toml_pkg.name, &toml_events, PaketEvents::BeforeUninstall)?;
.read_pkg_dir_with_manifest(&pkg_dir, &pkg_fmt.pkg_name, is_pkg_path)?;

if let Some(toml_pkg) = manifest.package {
// Emit an `after_update` Fish shell event if there is an associated Paket event
if let Some(toml_events) = manifest.events {
self.paket.emit_event(
&toml_pkg.name,
&toml_events,
PaketEvents::BeforeUninstall,
)?;
}

// Remove all corresponding package files from Fish shell directories
self.paket
Expand All @@ -187,6 +200,8 @@ impl<'a> Actions<'a> {
}
Ok(())
})?;
} else {
bail!("`paket.toml` file could not be parsed correctly.")
};

if !is_pkg_path {
Expand Down
14 changes: 9 additions & 5 deletions src/paket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a> Paket {
pkg_dir.exists() && pkg_dir.is_dir() && pkg_dir.read_dir().unwrap().next().is_some()
}

/// Read a package directory along with its Paket manifest file (paket.toml).
/// Read a valid package directory along with its Paket manifest file (paket.toml).
pub fn read_pkg_dir_with_manifest(
&'a self,
pkg_dir: &PathBuf,
Expand All @@ -156,6 +156,11 @@ impl<'a> Paket {
) -> Result<config::TomlManifest> {
let pkg_dir = pkg_dir.clone();
let pkg_toml_path = pkg_dir.join("paket.toml").canonicalize().with_context(|| {
let pkg_name = if is_local {
pkg_dir.as_os_str().to_str().unwrap_or_default()
} else {
pkg_name
};
format!(
"`paket.toml` file was not found on package `{}` or inaccessible.",
pkg_name
Expand Down Expand Up @@ -189,7 +194,7 @@ impl<'a> Paket {
// checking for remote packages only
if !is_local && pkg_name != toml_pkg.name {
bail!(
"`{}` package name in `paket.toml` doesn't match with given input package name.",
"package name `{}` in `paket.toml` doesn't match with given input package name.",
pkg_name
)
}
Expand Down Expand Up @@ -309,9 +314,8 @@ impl<'a> Paket {
let pkgname_e = event_parts[0].trim_start();
if pkgname_e != pkgname {
bail!(
"package `{}` name in event is different than the base package name `{}`.",
pkgname_e,
pkgname
"Paket event value `{}` defined in `paket.toml` has invalid format. \nUse the format `[package_name]_[event_name]` without the brackets and spaces (underscores instead). \nAlso make sure that the whole value matches with your package's Fish shell event (--on-event).",
pkg_event_val
);
}

Expand Down

0 comments on commit 5eec58b

Please sign in to comment.