Skip to content

Commit

Permalink
feat: ability to install a package from directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Feb 15, 2021
1 parent e02fe5f commit 6d3814e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
**/*.gz
**/.DS_Store
release
TODO
.vscode
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 22 additions & 16 deletions src/cli/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,31 @@ impl<'a> Actions<'a> {
let pkgfmt = PkgNameFmt::from(&pkg_name)?;
let pkg_name = &pkgfmt.get_short_name();
let pkg_tag = Some(pkgfmt.pkg_tag.as_ref());

let branch_tag = pkg_tag.unwrap_or("");
println!("Installing package `{}@{}`...", &pkg_name, branch_tag);

if self.pkt.pkg_exists(pkg_name) {
bail!(
"package `{}` is already installed. Try to use the `up` command to upgrade it.",
pkg_name
);
}
// Check for package naming convention or directory path
let pkg_dir = if let Some(pkg_path) = pkgfmt.get_pkg_path() {
println!("Installing package from directory `{:?}`...", pkg_path);
pkg_path
} else {
println!("Installing package `{}@{}`...", &pkg_name, branch_tag);

if self.pkt.pkg_exists(pkg_name) {
bail!(
"package `{}` is already installed. Try to use the `up` command to upgrade it.",
pkg_name
);
}

self.git.clone(pkg_name, pkg_tag, git_provider)?;
self.git.clone(pkg_name, pkg_tag, git_provider)?;

// Process Fish shell package structure
let pkg_dir = self.git.base_dir.join(&pkg_name);
if !self.pkt.pkg_exists(pkg_name) {
bail!("package `{}` was not cloned with success.", pkg_name);
}
// Process Fish shell package structure
let pkg_dir = self.git.base_dir.join(&pkg_name);
if !self.pkt.pkg_exists(pkg_name) {
bail!("package `{}` was not cloned with success.", pkg_name);
}
pkg_dir
};

self.pkt
.read_pkg_dir(&pkg_dir, &pkg_name, |src_path, dest_path| {
Expand All @@ -60,8 +67,7 @@ impl<'a> Actions<'a> {
}

println!("Package was installed successfully.");
println!("Now reload your current Fish shell session or source your config file:");
println!("source ~/.config/fish/config.fish");
println!("Now just reload your current Fish shell session.");

Ok(())
}
Expand Down
35 changes: 19 additions & 16 deletions src/paket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ pub struct Paket {
impl Paket {
/// Create a new instance of `Paket`.
pub fn new() -> Result<Self> {
// Check if Git and Fish shell binaries are available
Command::new("git", None)
.spawn()
.with_context(|| "`git` was not found! Please check if the latest `git` binary is installed on system.".to_string())?;
Command::new("fish", None)
.spawn()
.with_context(|| "`fish` was not found! Please check if the latest `fish` binary is installed on system.".to_string())?;
// Check if Git and Fish shell binaries are available on system
Command::new("git", None).spawn().with_context(|| {
"`git` was not found! Please check if the latest binary is installed on system."
.to_string()
})?;
Command::new("fish", None).spawn().with_context(|| {
"`fish` was not found! Please check if the latest binary is installed on system."
.to_string()
})?;

// Check if `paket` is running on top of a Fish shell session
let pid = process::getppid().to_string();
Expand All @@ -65,7 +67,7 @@ impl Paket {
fn configure_paths() -> Result<PaketPaths> {
// User's home directory
let home_dir = dirs::home_dir()
.expect("user home directory was not found or inaccessible.")
.expect("User home directory was not found or inaccessible.")
.canonicalize()?;

// User's home config directory
Expand All @@ -76,44 +78,44 @@ impl Paket {
}
let config_dir = config_dir
.canonicalize()
.with_context(|| "home config directory was not found or inaccessible.")?;
.with_context(|| "Home config directory was not found or inaccessible.")?;

// Fish config directories
let fish_dir = config_dir
.join("fish")
.canonicalize()
.with_context(|| "fish config directory was not found or inaccessible.")?;
.with_context(|| "Fish config directory was not found or inaccessible.")?;

// Fish config snippets directory
let fish_snippets_dir = fish_dir.join("conf.d");
if !fish_snippets_dir.exists() {
fs::create_dir_all(&fish_snippets_dir)
.with_context(|| "fish snippets directory can not be created.")?;
.with_context(|| "Fish snippets directory can not be created.")?;
}

// Fish config completions directory
let fish_completions_dir = fish_dir.join("completions");
if !fish_completions_dir.exists() {
fs::create_dir_all(&fish_completions_dir)
.with_context(|| "fish completions directory can not be created.")?;
.with_context(|| "Fish completions directory can not be created.")?;
}

// Fish config functions directory
let fish_functions_dir = fish_dir.join("functions");
if !fish_functions_dir.exists() {
fs::create_dir_all(&fish_functions_dir)
.with_context(|| "fish functions directory can not be created.")?;
.with_context(|| "Fish functions directory can not be created.")?;
}

// Paket config directory
let paket_dir = config_dir.join("paket");
if !paket_dir.exists() {
fs::create_dir_all(&paket_dir)
.with_context(|| "paket config directory can not be created.")?;
.with_context(|| "Paket config directory can not be created.")?;
}
let paket_dir = paket_dir
.canonicalize()
.with_context(|| "paket config directory was not found or inaccessible.")?;
.with_context(|| "Paket config directory was not found or inaccessible.")?;

Ok(PaketPaths {
config_dir,
Expand Down Expand Up @@ -174,7 +176,8 @@ impl Paket {

// Read `include` toml property of `package` section
let pkg_include = &toml_pkg.include.unwrap_or_default();
// TODO: support Git glob-like file's reading on `include` toml array. Plain file paths only for now.
// TODO: support Git glob-like file's reading on `include` toml array.
// Plain file paths only for now.

// `configuration snippets` -> conf.d/*.fish
// `completions` -> completions/*.fish
Expand Down
56 changes: 47 additions & 9 deletions src/pkg/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::result::Result;
use std::path::PathBuf;

use crate::result::{Context, Result};

/// Defaines the package name format based on a fomatted package name string.
pub struct PkgNameFmt {
Expand All @@ -8,14 +10,45 @@ pub struct PkgNameFmt {
pub pkg_name: String,
/// Contain the package version name (Git branch or tag).
pub pkg_tag: String,

pkg_path: Option<PathBuf>,
}

impl<'a> PkgNameFmt {
/// Return a `PkgNameFmt` instance but making sure that current package name format is valid.
/// Format: username/package_name@(tag_name|branch_name)
pub fn from(pkg_name: &'a str) -> Result<Self> {
if pkg_name.is_empty() {
bail!("provide a package name.");
bail!("provide a package name or a local Git package directory path.");
}

// Default Git tag for package repository
let mut pkg_tag = "master";

// Check if current `pkg_name` is an Git based package path directory
let pkg_path = std::path::Path::new(pkg_name);
if pkg_path.is_dir() {
let pkg_path = pkg_path
.canonicalize()
.with_context(|| "Package path directory doesn't exist or inaccessible.")?;

// TODO: we could also check here if current dir path is a valid git repository

// We take the dirname as package name
let pkg_name = match pkg_path.iter().last() {
Some(v) => v.to_str().unwrap().into(),
None => bail!(
"directory name for path \"{:?}\" was not determined",
pkg_path,
),
};

return Ok(Self {
user_name: String::new(),
pkg_name,
pkg_tag: pkg_tag.into(),
pkg_path: Some(pkg_path),
});
}

let pkg_parts: Vec<&str> = pkg_name.splitn(2, '/').collect();
Expand All @@ -25,29 +58,34 @@ impl<'a> PkgNameFmt {
);
}

let username = pkg_parts[0].trim().to_string();
let username = pkg_parts[0].trim();
let pkg_name_parts: Vec<&str> = pkg_parts[1].splitn(2, '@').collect();
if username.is_empty() || pkg_name_parts.is_empty() {
bail!("provide a valid user and package name format. E.g username/package_name");
}

let pkg_name = pkg_name_parts[0].trim().to_string();
let pkg_name = pkg_name_parts[0].trim();
if pkg_name.is_empty() {
bail!("provide a valid package name value. E.g username/package_name");
}

let mut pkg_tag = "master".to_string();
if pkg_name_parts.len() == 2 && !pkg_name_parts[1].is_empty() {
pkg_tag = pkg_name_parts[1].trim().to_string();
pkg_tag = pkg_name_parts[1].trim();
}

Ok(Self {
user_name: username,
pkg_name,
pkg_tag,
user_name: username.into(),
pkg_name: pkg_name.into(),
pkg_tag: pkg_tag.into(),
pkg_path: None,
})
}

/// Return if the current package is a valid Git-based package directory path.
pub fn get_pkg_path(&self) -> Option<PathBuf> {
self.pkg_path.clone()
}

/// Return the user and package name concatenated. E.g `username/package_name`.
pub fn get_short_name(&self) -> String {
[&self.user_name, "/", &self.pkg_name].concat()
Expand Down

0 comments on commit 6d3814e

Please sign in to comment.