Skip to content

Commit

Permalink
automatically generate completions during build
Browse files Browse the repository at this point in the history
  • Loading branch information
BoostCookie committed Nov 15, 2023
1 parent 8953675 commit 2647156
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 122 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ nix = "0.27.1"

[build-dependencies]
anyhow = "1.0.72"
clap = { version = "4.3.19", features = ["derive"] }
clap_complete = "4.3.19"
itertools = "0.11.0"
serde = { version = "1.0.175", features = ["derive"] }
serde_json = "1.0.103"
Expand Down
25 changes: 25 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use std::env;
use std::fs::File;
use std::path::Path;
use std::path::PathBuf;
use clap::CommandFactory;
use clap_complete::{generate_to, shells::{Bash, Zsh, Fish, Elvish, PowerShell}};

include!("src/cli.rs");

fn main() -> Result<()> {
let target_platform = std::env::var("TARGET").unwrap();
Expand Down Expand Up @@ -57,5 +61,26 @@ fn main() -> Result<()> {

built::write_built_file().expect("Failed to acquire build-time information");

let mut cmd = Juliaup::command();
let path = generate_to(Bash, &mut cmd, "juliaup", &out_path)
.expect("Failed to generate bash completion");
println!("cargo:warning=Generated bash completion: {:?}", path);

let path = generate_to(Zsh, &mut cmd, "juliaup", &out_path)
.expect("Failed to generate zsh completion");
println!("cargo:warning=Generated zsh completion: {:?}", path);

let path = generate_to(Fish, &mut cmd, "juliaup", &out_path)
.expect("Failed to generate fish completion");
println!("cargo:warning=Generated fish completion: {:?}", path);

let path = generate_to(Elvish, &mut cmd, "juliaup", &out_path)
.expect("Failed to generate elvish completion");
println!("cargo:warning=Generated elvish completion: {:?}", path);

let path = generate_to(PowerShell, &mut cmd, "juliaup", &out_path)
.expect("Failed to generate powershell completion");
println!("cargo:warning=Generated powershell completion: {:?}", path);

Ok(())
}
123 changes: 1 addition & 122 deletions src/bin/juliaup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use clap::Parser;
use juliaup::cli::{Juliaup, OverrideSubCmd, ConfigSubCmd, SelfSubCmd};
use juliaup::command_api::run_command_api;
#[cfg(not(windows))]
use juliaup::command_config_symlinks::run_command_config_symlinks;
Expand Down Expand Up @@ -27,128 +28,6 @@ use juliaup::{
};
use log::info;

#[derive(Parser)]
#[clap(name = "Juliaup", version)]
/// The Julia Version Manager
enum Juliaup {
/// Set the default Julia version
Default { channel: String },
/// Add a specific Julia version or channel to your system. Access via `julia +{channel}` e.g. `julia +1.6`
Add { channel: String },
/// Link an existing Julia binary to a custom channel name
Link {
channel: String,
file: String,
args: Vec<String>,
},
/// List all available channels
#[clap(alias = "ls")]
List {},
#[clap(subcommand, name = "override")]
OverrideSubCmd(OverrideSubCmd),
#[clap(alias = "up")]
/// Update all or a specific channel to the latest Julia version
Update { channel: Option<String> },
#[clap(alias = "rm")]
/// Remove a Julia version from your system
Remove { channel: String },
#[clap(alias = "st")]
/// Show all installed Julia versions
Status {},
/// Garbage collect uninstalled Julia versions
Gc {},
#[clap(subcommand, name = "config")]
/// Juliaup configuration
Config(ConfigSubCmd),
#[clap(hide = true)]
Api { command: String },
#[clap(name = "46029ef5-0b73-4a71-bff3-d0d05de42aac", hide = true)]
InitialSetupFromLauncher {},
#[clap(name = "0cf1528f-0b15-46b1-9ac9-e5bf5ccccbcf", hide = true)]
UpdateVersionDb {},
#[clap(name = "info", hide = true)]
Info {},
#[clap(subcommand, name = "self")]
SelfSubCmd(SelfSubCmd),
// This is used for the cron jobs that we create. By using this UUID for the command
// We can identify the cron jobs that were created by juliaup for uninstall purposes
#[cfg(feature = "selfupdate")]
#[clap(name = "4c79c12db1d34bbbab1f6c6f838f423f", hide = true)]
SecretSelfUpdate {},
}

#[derive(Parser)]
/// Manage directory overrides
enum OverrideSubCmd {
Status {},
Set {
channel: String,
#[clap(long, short)]
path: Option<String>,
},
Unset {
#[clap(long, short)]
nonexistent: bool,
#[clap(long, short)]
path: Option<String>,
},
}

#[derive(Parser)]
/// Manage this juliaup installation
enum SelfSubCmd {
#[cfg(not(feature = "selfupdate"))]
/// Update the Julia versions database
Update {},
#[cfg(feature = "selfupdate")]
/// Update the Julia versions database and juliaup itself
Update {},
#[cfg(feature = "selfupdate")]
/// Configure the channel to use for juliaup updates
Channel { channel: String },
#[cfg(feature = "selfupdate")]
/// Uninstall this version of juliaup from the system
Uninstall {},
}

#[derive(Parser)]
enum ConfigSubCmd {
#[cfg(not(windows))]
#[clap(name = "channelsymlinks")]
/// Create a separate symlink per channel
ChannelSymlinks {
/// New Value
value: Option<bool>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "backgroundselfupdateinterval")]
/// The time between automatic background updates of Juliaup in minutes, use 0 to disable.
BackgroundSelfupdateInterval {
/// New value
value: Option<i64>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "startupselfupdateinterval")]
/// The time between automatic updates at Julia startup of Juliaup in minutes, use 0 to disable.
StartupSelfupdateInterval {
/// New value
value: Option<i64>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "modifypath")]
/// Add the Julia binaries to your PATH by manipulating various shell startup scripts.
ModifyPath {
/// New value
value: Option<bool>,
},
/// The time between automatic updates of the versions database in minutes, use 0 to disable.
#[clap(name = "versionsdbupdateinterval")]
VersionsDbUpdateInterval {
/// New value
value: Option<i64>,
},
}

fn main() -> Result<()> {
human_panic::setup_panic!(human_panic::Metadata {
name: "Juliaup".into(),
Expand Down
123 changes: 123 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use clap::Parser;

#[derive(Parser)]
#[clap(name = "Juliaup", version)]
/// The Julia Version Manager
pub enum Juliaup {
/// Set the default Julia version
Default { channel: String },
/// Add a specific Julia version or channel to your system. Access via `julia +{channel}` e.g. `julia +1.6`
Add { channel: String },
/// Link an existing Julia binary to a custom channel name
Link {
channel: String,
file: String,
args: Vec<String>,
},
/// List all available channels
#[clap(alias = "ls")]
List {},
#[clap(subcommand, name = "override")]
OverrideSubCmd(OverrideSubCmd),
#[clap(alias = "up")]
/// Update all or a specific channel to the latest Julia version
Update { channel: Option<String> },
#[clap(alias = "rm")]
/// Remove a Julia version from your system
Remove { channel: String },
#[clap(alias = "st")]
/// Show all installed Julia versions
Status {},
/// Garbage collect uninstalled Julia versions
Gc {},
#[clap(subcommand, name = "config")]
/// Juliaup configuration
Config(ConfigSubCmd),
#[clap(hide = true)]
Api { command: String },
#[clap(name = "46029ef5-0b73-4a71-bff3-d0d05de42aac", hide = true)]
InitialSetupFromLauncher {},
#[clap(name = "0cf1528f-0b15-46b1-9ac9-e5bf5ccccbcf", hide = true)]
UpdateVersionDb {},
#[clap(name = "info", hide = true)]
Info {},
#[clap(subcommand, name = "self")]
SelfSubCmd(SelfSubCmd),
// This is used for the cron jobs that we create. By using this UUID for the command
// We can identify the cron jobs that were created by juliaup for uninstall purposes
#[cfg(feature = "selfupdate")]
#[clap(name = "4c79c12db1d34bbbab1f6c6f838f423f", hide = true)]
SecretSelfUpdate {},
}

#[derive(Parser)]
/// Manage directory overrides
pub enum OverrideSubCmd {
Status {},
Set {
channel: String,
#[clap(long, short)]
path: Option<String>,
},
Unset {
#[clap(long, short)]
nonexistent: bool,
#[clap(long, short)]
path: Option<String>,
},
}

#[derive(Parser)]
/// Manage this juliaup installation
pub enum SelfSubCmd {
#[cfg(not(feature = "selfupdate"))]
/// Update the Julia versions database
Update {},
#[cfg(feature = "selfupdate")]
/// Update the Julia versions database and juliaup itself
Update {},
#[cfg(feature = "selfupdate")]
/// Configure the channel to use for juliaup updates
Channel { channel: String },
#[cfg(feature = "selfupdate")]
/// Uninstall this version of juliaup from the system
Uninstall {},
}

#[derive(Parser)]
pub enum ConfigSubCmd {
#[cfg(not(windows))]
#[clap(name = "channelsymlinks")]
/// Create a separate symlink per channel
ChannelSymlinks {
/// New Value
value: Option<bool>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "backgroundselfupdateinterval")]
/// The time between automatic background updates of Juliaup in minutes, use 0 to disable.
BackgroundSelfupdateInterval {
/// New value
value: Option<i64>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "startupselfupdateinterval")]
/// The time between automatic updates at Julia startup of Juliaup in minutes, use 0 to disable.
StartupSelfupdateInterval {
/// New value
value: Option<i64>,
},
#[cfg(feature = "selfupdate")]
#[clap(name = "modifypath")]
/// Add the Julia binaries to your PATH by manipulating various shell startup scripts.
ModifyPath {
/// New value
value: Option<bool>,
},
/// The time between automatic updates of the versions database in minutes, use 0 to disable.
#[clap(name = "versionsdbupdateinterval")]
VersionsDbUpdateInterval {
/// New value
value: Option<i64>,
},
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Context;

pub mod cli;
pub mod command_add;
pub mod command_api;
pub mod command_config_backgroundselfupdate;
Expand Down

0 comments on commit 2647156

Please sign in to comment.