Skip to content

Commit

Permalink
feat(init): add target menu to init
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Sep 18, 2023
1 parent fd9d635 commit aad5dc9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
68 changes: 60 additions & 8 deletions cargo-dist/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::ops::Not;

use axoproject::errors::AxoprojectError;
use axoproject::WorkspaceInfo;
use axoproject::{errors::AxoprojectError, platforms::triple_to_display_name};
use camino::Utf8PathBuf;
use cargo_dist_schema::PrRunMode;
use semver::Version;
Expand Down Expand Up @@ -205,7 +203,7 @@ fn get_new_dist_metadata(
ci: None,
installers: None,
tap: None,
targets: cfg.targets.is_empty().not().then(|| cfg.targets.clone()),
targets: None,
dist: None,
include: None,
auto_includes: None,
Expand Down Expand Up @@ -301,6 +299,63 @@ fn get_new_dist_metadata(
}
}

{
// Start with builtin targets
let default_platforms = crate::default_desktop_targets();
let mut known = default_platforms.clone();
// If the config doesn't have targets at all, generate them
let config_vals = meta.targets.as_deref().unwrap_or(&default_platforms);
let cli_vals = cfg.targets.as_slice();
// Add anything custom they did to the list (this will do some reordering if they hand-edited)
for val in config_vals.iter().chain(cli_vals) {
if !known.contains(val) {
known.push(val.clone());
}
}

// Prettify/sort things
let desc = move |triple: &str| -> String {
let pretty = triple_to_display_name(triple).unwrap_or("[unknown]");
format!("{pretty} - {triple}")
};
known.sort_by_cached_key(|k| desc(k).to_uppercase());

let mut defaults = vec![];
let mut keys = vec![];
for item in &known {
// If this target is in their config, keep it
// If they passed it on the CLI, flip it on
let config_had_it = config_vals.contains(item);
let cli_had_it = cli_vals.contains(item);

let default = config_had_it || cli_had_it;
defaults.push(default);

keys.push(desc(item));
}

// Prompt the user
let prompt = r#"what platforms do you want to build for?"#;
let selected = if args.yes {
defaults
.iter()
.enumerate()
.filter_map(|(idx, enabled)| enabled.then_some(idx))
.collect()
} else {
let res = MultiSelect::with_theme(&theme)
.items(&keys)
.defaults(&defaults)
.with_prompt(prompt)
.interact()?;
eprintln!();
res
};

// Apply the results
meta.targets = Some(selected.into_iter().map(|i| known[i].clone()).collect());
}

// Enable CI backends
{
// FIXME: when there is more than one option this should be a proper
Expand Down Expand Up @@ -339,10 +394,7 @@ fn get_new_dist_metadata(
}

// Prompt the user
let prompt = r#"enable Github CI integration?
this creates a CI action which automates creating a Github Release,
builds all your binaries/archives, and then uploads them to the Release
it also unlocks the ability to generate installers which fetch those artifacts"#;
let prompt = r#"enable Github CI and Releases?"#;
let default = defaults[github_key];

let github_selected = if args.yes {
Expand Down
17 changes: 17 additions & 0 deletions cargo-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,20 @@ fn generate_installer(dist: &DistGraph, style: &InstallerImpl) -> Result<()> {
}
Ok(())
}

/// Get the default list of targets
pub fn default_desktop_targets() -> Vec<String> {
vec![
// Everyone can build x64!
axoproject::platforms::TARGET_X64_LINUX_GNU.to_owned(),
axoproject::platforms::TARGET_ARM64_LINUX_GNU.to_owned(),
axoproject::platforms::TARGET_X64_MAC.to_owned(),
// Apple is really easy to cross from Apple
axoproject::platforms::TARGET_ARM64_MAC
.to_owned()
.to_owned(),
// other cross-compiles not yet supported
// axoproject::platforms::TARGET_X64_WINDOWS.to_owned(),
// axoproject::platforms::TARGET_ARM64_WINDOWS.to_owned(),
]
}
22 changes: 1 addition & 21 deletions cargo-dist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,12 @@ fn cmd_plan(cli: &Cli, _args: &PlanArgs) -> Result<(), miette::Report> {
}

fn cmd_init(cli: &Cli, args: &InitArgs) -> Result<(), miette::Report> {
// This command is more automagic, so provide default targets if none are chosen
let targets = if cli.target.is_empty() {
default_desktop_targets()
} else {
cli.target.clone()
};
let config = cargo_dist::config::Config {
needs_coherent_announcement_tag: false,
artifact_mode: cargo_dist::config::ArtifactMode::All,
no_local_paths: cli.no_local_paths,
allow_all_dirty: cli.allow_dirty,
targets,
targets: cli.target.clone(),
ci: cli.ci.iter().map(|ci| ci.to_lib()).collect(),
installers: cli.installer.iter().map(|ins| ins.to_lib()).collect(),
announcement_tag: cli.tag.clone(),
Expand Down Expand Up @@ -254,20 +248,6 @@ fn cmd_generate_ci(cli: &Cli, args: &GenerateCiArgs) -> Result<(), miette::Repor
)
}

fn default_desktop_targets() -> Vec<String> {
vec![
// Everyone can build x64!
"x86_64-unknown-linux-gnu".to_owned(),
"x86_64-apple-darwin".to_owned(),
"x86_64-pc-windows-msvc".to_owned(),
// Apple is really easy to cross from Apple
"aarch64-apple-darwin".to_owned(),
// other cross-compiles not yet supported
// "aarch64-gnu-unknown-linux".to_owned(),
// "aarch64-pc-windows-msvc".to_owned(),
]
}

fn cmd_help_md(_args: &Cli, _sub_args: &HelpMarkdownArgs) -> Result<(), miette::Report> {
let mut out = Term::stdout();
print_help_markdown(&mut out).into_diagnostic()
Expand Down

0 comments on commit aad5dc9

Please sign in to comment.