Skip to content

Commit fd1d76c

Browse files
authored
bin: completions (#1035)
* bin: completions * fix powershell
1 parent ef5ce3f commit fd1d76c

File tree

7 files changed

+141
-2
lines changed

7 files changed

+141
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ hemtt-wss = { path = "../libs/wss" }
3535
arma3-wiki = { workspace = true }
3636
byteorder = { workspace = true }
3737
clap = { workspace = true, features = ["derive"] }
38+
clap_complete = "4.5.55"
3839
dialoguer = "0.11.0"
3940
dirs = { workspace = true }
4041
fs_extra = "1.3.0"

bin/src/commands/manage.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use std::{
2+
fs::{File, OpenOptions},
3+
io::{Read, Write},
4+
path::PathBuf,
5+
};
6+
7+
use clap::CommandFactory;
8+
use clap_complete::{Shell, generate_to};
9+
10+
use crate::{Cli, Error, report::Report};
11+
12+
#[derive(clap::Parser)]
13+
#[command(arg_required_else_help = true)]
14+
/// Manage HEMTT features installed on your system
15+
pub struct Command {
16+
#[command(subcommand)]
17+
commands: Subcommands,
18+
19+
#[clap(flatten)]
20+
global: crate::GlobalArgs,
21+
}
22+
23+
#[derive(clap::Subcommand)]
24+
enum Subcommands {
25+
/// Install shell completions
26+
Completions {
27+
#[arg(value_enum)]
28+
shell: Shell,
29+
},
30+
#[cfg(windows)]
31+
Powershell,
32+
}
33+
34+
/// Execute the manage command
35+
///
36+
/// # Errors
37+
/// Will not return an error
38+
pub fn execute(cmd: &Command) -> Result<Report, Error> {
39+
match &cmd.commands {
40+
Subcommands::Completions { shell } => {
41+
clap_complete::generate(
42+
*shell,
43+
&mut Cli::command(),
44+
env!("CARGO_PKG_NAME"),
45+
&mut std::io::stdout(),
46+
);
47+
}
48+
#[cfg(windows)]
49+
Subcommands::Powershell => {
50+
install_powershell_completions_sourced()?;
51+
}
52+
}
53+
std::process::exit(0);
54+
}
55+
56+
#[allow(dead_code)]
57+
fn install_powershell_completions_sourced() -> std::io::Result<()> {
58+
let script_path = dirs::config_dir()
59+
.expect("Could not locate config directory (APPDATA)")
60+
.join("hemtt")
61+
.join("_hemtt.ps1");
62+
63+
let script_dir = script_path
64+
.parent()
65+
.expect("Could not determine script directory");
66+
std::fs::create_dir_all(script_dir)?;
67+
68+
generate_to(
69+
Shell::PowerShell,
70+
&mut Cli::command(),
71+
env!("CARGO_PKG_NAME"),
72+
script_dir,
73+
)?;
74+
75+
let profile_path =
76+
get_powershell_profile_path().expect("Could not determine PowerShell profile path");
77+
78+
if let Some(parent) = profile_path.parent() {
79+
std::fs::create_dir_all(parent)?;
80+
}
81+
if !profile_path.exists() {
82+
File::create(&profile_path)?;
83+
}
84+
85+
let import_line = r#". "$env:APPDATA\hemtt\_hemtt.ps1""#;
86+
87+
let mut profile_content = String::new();
88+
File::open(&profile_path)?.read_to_string(&mut profile_content)?;
89+
90+
if profile_content.contains(import_line) {
91+
info!("PowerShell completions already configured in your profile.");
92+
return Ok(());
93+
}
94+
let mut file = OpenOptions::new().append(true).open(&profile_path)?;
95+
writeln!(
96+
file,
97+
"\n# hemtt manage completions powershell\n{import_line}"
98+
)?;
99+
100+
debug!("Completion script saved to: {}", script_path.display());
101+
info!("Restart PowerShell or run: . $PROFILE");
102+
103+
Ok(())
104+
}
105+
106+
fn get_powershell_profile_path() -> Option<PathBuf> {
107+
dirs::home_dir().map(|home| {
108+
home.join("Documents")
109+
.join("WindowsPowerShell")
110+
.join("Microsoft.PowerShell_profile.ps1")
111+
})
112+
}

bin/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod check;
44
pub mod dev;
55
pub mod launch;
66
pub mod localization;
7+
pub mod manage;
78
pub mod new;
89
pub mod release;
910
pub mod script;

bin/src/commands/wiki.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ enum Subcommands {
2929
pub fn execute(_cmd: &Command) -> Result<Report, Error> {
3030
// TODO right now just assumes force-pull since that's the only subcommand
3131
let _ = Database::empty(true);
32-
Ok(Report::new())
32+
std::process::exit(0);
3333
}

bin/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum Commands {
6161
Utils(commands::utils::Command),
6262
Value(commands::value::Command),
6363
Wiki(commands::wiki::Command),
64+
Manage(commands::manage::Command),
6465
#[cfg(windows)]
6566
Photoshoot(commands::photoshoot::Command),
6667
}
@@ -99,7 +100,13 @@ pub fn execute(cli: &Cli) -> Result<(), Error> {
99100
cli.global.verbosity,
100101
!matches!(
101102
cli.command,
102-
Some(Commands::Utils(_) | Commands::Wiki(_) | Commands::New(_) | Commands::Book(_))
103+
Some(
104+
Commands::Utils(_)
105+
| Commands::Wiki(_)
106+
| Commands::New(_)
107+
| Commands::Book(_)
108+
| Commands::Manage(_)
109+
)
103110
),
104111
)?;
105112
}
@@ -136,6 +143,7 @@ pub fn execute(cli: &Cli) -> Result<(), Error> {
136143
Commands::Utils(cmd) => commands::utils::execute(cmd),
137144
Commands::Value(cmd) => commands::value::execute(cmd),
138145
Commands::Wiki(cmd) => commands::wiki::execute(cmd),
146+
Commands::Manage(cmd) => commands::manage::execute(cmd),
139147
#[cfg(windows)]
140148
Commands::Photoshoot(cmd) => commands::photoshoot::execute(cmd),
141149
};

book/install.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,23 @@ mkdir -p "$binaryLocation"
5454
if ! echo "$PATH" | grep -q "$binaryLocation"; then
5555
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.zshrc $HOME/.profile"
5656
for config in $config_files; do
57+
addedByHEMTT=false
5758
if [ -f "$config" ]; then
5859
if ! grep -q -s "export PATH=$binaryLocation:\$PATH" "$config"; then
5960
echo "Appending $binaryLocation to $config"
6061
echo "" >>"$config"
6162
echo "# Added by HEMTT" >>"$config"
6263
echo "export PATH=$binaryLocation:\$PATH" >>"$config"
64+
addedByHEMTT=true
65+
fi
66+
fi
67+
if ! grep -q -s "source <(hemtt manage completions" "$config"; then
68+
echo "Adding completions to $config"
69+
echo "" >>"$config"
70+
if [ "$addedByHEMTT" = false ]; then
71+
echo "# Added by HEMTT" >>"$config"
72+
fi
73+
echo "source <(hemtt manage completions \$(basename \$SHELL))" >>"$config"
6374
fi
6475
fi
6576
done
@@ -73,4 +84,9 @@ else
7384
exit 1
7485
fi
7586

87+
if ! "$binaryLocation/hemtt" --version >/dev/null 2>&1; then
88+
echo "Error: HEMTT binary was installed, but is not compatible with your system"
89+
exit 1
90+
fi
91+
7692
echo "Installation complete. You can run HEMTT using 'hemtt'"

0 commit comments

Comments
 (0)