Skip to content

Commit

Permalink
add shell specific command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
zfwf committed Dec 30, 2023
1 parent b1ae086 commit c53acab
Show file tree
Hide file tree
Showing 16 changed files with 485 additions and 212 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
repo: ajeetdsouza/zoxide
from_release: true
exec: '**/zoxide'
install: '**/zoxide init zsh > init-zoxide.zsh'
src: 'init-zoxide.zsh'
install:
zsh: '**/zoxide init zsh > init-zoxide.zsh'
bash: '**/zoxide init bash > init-zoxide.bash'
fish: '**/zoxide init fish > init-zoxide.fish'
src:
zsh: 'init-zoxide.zsh'
load: 'alias cd=z'

- id: fd
Expand Down Expand Up @@ -106,8 +110,10 @@
resource:
repo: direnv/direnv
from_release: true
install: 'mv direnv* direnv; chmod +x ./direnv; ./direnv hook zsh > zhook.zsh'
src: zhook.zsh
install:
zsh: 'mv direnv* direnv; chmod +x ./direnv; ./direnv hook zsh > zhook.zsh'
src:
zsh: zhook.zsh
load: export DIRENV_LOG_FORMAT=""
exec: '**/direnv'

Expand Down Expand Up @@ -145,12 +151,14 @@
- id: zsh-autosuggestions
resource:
repo: zsh-users/zsh-autosuggestions
src: zsh-autosuggestions.zsh
src:
zsh: zsh-autosuggestions.zsh

- id: fast-syntax-highlighting
resource:
repo: z-shell/F-Sy-H
src: f-sy-h.plugin.zsh
src:
zsh: f-sy-h.plugin.zsh
```
### Order of Execution
Expand Down
40 changes: 26 additions & 14 deletions src/hooks/extract.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
use infer;
use std::path::Path;

use crate::utils::script::*;
use crate::utils::{script::*, shells::SupportedShell};

pub fn extract(cmd: &str) -> Result<(), Box<dyn std::error::Error>> {
run_cmd(&cmd)?;
pub fn extract(
current_shell: &SupportedShell,
cmd: &str,
) -> Result<(), Box<dyn std::error::Error>> {
run_cmd(current_shell, &cmd)?;

Ok(())
}

pub fn extract_asset(asset_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
pub fn extract_asset(
current_shell: &SupportedShell,
asset_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
let asset_path_string = &asset_path.display().to_string();
let infer_kind = infer::get_from_path(asset_path)?;
if let Some(kind) = &infer_kind {
match kind.extension() {
"zip" => {
run_cmd(&format!("unzip {}", asset_path_string))?;
run_cmd(current_shell, &format!("unzip {}", asset_path_string))?;
}
"gz" => {
run_cmd(&format!("tar xvf {}", asset_path_string))?;
run_cmd(current_shell, &format!("tar xvf {}", asset_path_string))?;
}
"deb" => {
run_cmd(&format!(
"ar xv {}; ls *.tar.* | xargs -n 1 tar xvf",
asset_path_string
))?;
run_cmd(
current_shell,
&format!(
"ar xv {}; ls *.tar.* | xargs -n 1 tar xvf",
asset_path_string
),
)?;
}
_ => {}
}
} else if let Some(ext) = &asset_path.extension() {
println!("ext {:?}", ext);
match ext.to_str().unwrap() {
"dmg" => {
run_cmd(&format!(
r#"
run_cmd(
current_shell,
&format!(
r#"
_extract_dmg() {{
local dmg_name="{}"
echo "dmg_name $dmg_name"
Expand All @@ -49,8 +60,9 @@ pub fn extract_asset(asset_path: &Path) -> Result<(), Box<dyn std::error::Error>
_extract_dmg
"#,
asset_path_string
))?;
asset_path_string
),
)?;
}
_ => {}
}
Expand Down
55 changes: 14 additions & 41 deletions src/hooks/init.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,17 @@
use crate::utils::{config::AdaptiveInit, script::*};
use log::debug;
use crate::utils::{
config::{ShellSpecificCommand, SupportedShellSpecificCommand},
script::*,
shells::SupportedShell,
};

pub fn init(adaptive_init: &AdaptiveInit) -> Result<String, Box<dyn std::error::Error>> {
let init_result = match adaptive_init {
AdaptiveInit::Run(cmd) => run_cmd_with_output(&cmd)?,
AdaptiveInit::OSSpecific {
linux,
macos,
windows,
} => {
let os = std::env::consts::OS;
match os {
"linux" => {
if let Some(cmd) = &linux {
run_cmd_with_output(&cmd)?
} else {
"".to_owned()
}
}
"macos" => {
if let Some(cmd) = &macos {
run_cmd_with_output(&cmd)?
} else {
"".to_owned()
}
}
"windows" => {
if let Some(cmd) = &windows {
run_cmd_with_output(&cmd)?
} else {
"".to_owned()
}
}
_ => {
debug!("init hook: unsupported os os={}", os);
"".to_owned()
}
}
pub fn init(
current_shell: &SupportedShell,
init_cmd: &ShellSpecificCommand,
) -> Result<String, Box<dyn std::error::Error>> {
match init_cmd {
ShellSpecificCommand::Generic(generic) => run_cmd_with_output(current_shell, generic),
ShellSpecificCommand::ShellSpecific(shell_specific) => {
run_shell_cmd(current_shell, shell_specific)
}
};

Ok(init_result)
}
}
45 changes: 10 additions & 35 deletions src/hooks/install.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
use crate::utils::{config::AdaptiveInstall, script::*};
use crate::utils::{config::ShellSpecificCommand, script::*, shells::SupportedShell};

pub fn install(adaptive_install: &AdaptiveInstall) -> Result<(), Box<dyn std::error::Error>> {
match adaptive_install {
AdaptiveInstall::Run(cmd) => {
run_cmd_with_output(&cmd)?;
pub fn install(
current_shell: &SupportedShell,
install_cmd: &ShellSpecificCommand,
) -> Result<String, Box<dyn std::error::Error>> {
match install_cmd {
ShellSpecificCommand::Generic(generic) => run_cmd_with_output(current_shell, generic),
ShellSpecificCommand::ShellSpecific(shell_specific) => {
run_shell_cmd(current_shell, shell_specific)
}
AdaptiveInstall::OSSpecific {
linux,
macos,
windows,
} => {
let os = std::env::consts::OS;
match os {
"linux" => {
if let Some(cmd) = &linux {
run_cmd(&cmd)?;
}
}
"macos" => {
if let Some(cmd) = &macos {
run_cmd(&cmd)?;
}
}
"windows" => {
if let Some(cmd) = &windows {
run_cmd(&cmd)?;
}
}
_ => {
println!("install hook: unsupported os os={}", os);
}
}
}
};

Ok(())
}
}
12 changes: 12 additions & 0 deletions src/hooks/load.rs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
use crate::utils::{config::ShellSpecificCommand, script::*, shells::SupportedShell};

pub fn load(
current_shell: &SupportedShell,
load_cmd: &ShellSpecificCommand,
) -> Result<String, Box<dyn std::error::Error>> {
match load_cmd {
ShellSpecificCommand::Generic(generic) => run_cmd_with_output(current_shell, generic),
ShellSpecificCommand::ShellSpecific(shell_specific) => {
run_shell_cmd(current_shell, shell_specific)
}
}
}
Loading

0 comments on commit c53acab

Please sign in to comment.