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 31, 2023
1 parent b1ae086 commit d24a00f
Show file tree
Hide file tree
Showing 17 changed files with 536 additions and 213 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/orbiter",
"args": [
"init",
"zsh"
],
"cwd": "${workspaceFolder}"
}
]
}
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
repo: starship/starship
from_release: true
exec: '**/starship'
install: '**/starship init zsh > init-starship.zsh'
src: 'init-starship.zsh'
install:
zsh: '**/starship init zsh > init-starship.zsh'
src:
zsh: 'init-starship.zsh'

- id: ripgrep
resource:
Expand All @@ -37,8 +39,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 +112,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 +153,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(())
}
}
24 changes: 24 additions & 0 deletions src/hooks/load.rs
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
use crate::utils::{config::ShellSpecificEvaluatable, shells::SupportedShell};

pub fn load(
current_shell: &SupportedShell,
load_cmd: &ShellSpecificEvaluatable,
) -> Result<(), Box<dyn std::error::Error>> {
Ok(match load_cmd {
ShellSpecificEvaluatable::Generic(generic) => println!("{}", generic),
ShellSpecificEvaluatable::ShellSpecific(shell_specific) => {
let shell_specific_evaluatable = match current_shell {
SupportedShell::Sh => &shell_specific.sh,
SupportedShell::Bash => &shell_specific.bash,
SupportedShell::Zsh => &shell_specific.zsh,
SupportedShell::Fish => &shell_specific.fish,
SupportedShell::PowerShell => &shell_specific.powershell,
SupportedShell::WinCmd => &shell_specific.wincmd,
};

match shell_specific_evaluatable {
Some(evaluatable) => println!("{}", evaluatable),
None => (),
}
}
})
}
Loading

0 comments on commit d24a00f

Please sign in to comment.