Skip to content

Commit

Permalink
feat: add pkg to backend init
Browse files Browse the repository at this point in the history
  • Loading branch information
mistydemeo committed Aug 28, 2024
1 parent ef55666 commit e5ba17f
Showing 1 changed file with 45 additions and 63 deletions.
108 changes: 45 additions & 63 deletions cargo-dist/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Deserialize;
use crate::{
config::{
self, CiStyle, CompressionImpl, Config, DistMetadata, HostingStyle, InstallPathStrategy,
InstallerStyle, PublishStyle, ZipStyle,
InstallerStyle, MacPkgConfig, PublishStyle, ZipStyle,
},
do_generate,
errors::{DistError, DistResult},
Expand Down Expand Up @@ -665,7 +665,6 @@ fn get_new_dist_metadata(
InstallerStyle::Npm,
InstallerStyle::Homebrew,
InstallerStyle::Msi,
InstallerStyle::Pkg,
]
} else {
eprintln!("{notice} no CI backends enabled, most installers have been hidden");
Expand Down Expand Up @@ -782,66 +781,6 @@ fn get_new_dist_metadata(
}
}

// Special handling of the pkg installer
if meta
.installers
.as_deref()
.unwrap_or_default()
.contains(&InstallerStyle::Pkg)
{
let pkg_is_new = !orig_meta
.installers
.as_deref()
.unwrap_or_default()
.contains(&InstallerStyle::Pkg);

if pkg_is_new && orig_meta.mac_pkg_config.is_none() {
let prompt = r#"you've enabled a Mac .pkg installer. This requires a unique bundle ID;
please enter one now. This is in reverse-domain name format.
For more information, consult the Apple documentation:
https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1"#;
let default = "".to_string();

let identifier: String = if args.yes {
default
} else {
let res = Input::with_theme(&theme)
.with_prompt(prompt)
.allow_empty(true)
.interact_text()?;
eprintln!();
res
};
let identifier = identifier.trim();
if identifier.is_empty() {
return Err(DistError::MacPkgBundleIdentifierMissing {});
}

let prompt = r#"Please enter the installation prefix this .pkg should use."#;
let prefix = if args.yes {
None
} else {
let res: String = Input::with_theme(&theme)
.with_prompt(prompt)
.allow_empty(true)
.interact_text()?;
eprintln!();

let trimmed = res.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_owned())
}
};

meta.mac_pkg_config = Some(config::MacPkgConfig {
identifier: identifier.to_owned(),
install_location: prefix,
})
}
}

meta.publish_jobs = if publish_jobs.is_empty() {
None
} else {
Expand Down Expand Up @@ -1034,14 +973,14 @@ fn apply_dist_to_metadata(metadata: &mut toml_edit::Item, meta: &DistMetadata) {
github_release,
package_libraries,
install_libraries,
mac_pkg_config,
// These settings are complex enough that we don't support editing them in init
extra_artifacts: _,
github_custom_runners: _,
github_custom_job_permissions: _,
bin_aliases: _,
system_dependencies: _,
github_build_setup: _,
mac_pkg_config: _,
} = &meta;

// Forcibly inline the default install_path if not specified,
Expand Down Expand Up @@ -1083,6 +1022,13 @@ fn apply_dist_to_metadata(metadata: &mut toml_edit::Item, meta: &DistMetadata) {
installers.as_ref(),
);

apply_optional_mac_pkg(
table,
"mac-pkg-config",
"\n# Configuration for the Mac .pkg installer\n",
mac_pkg_config.as_ref(),
);

apply_optional_value(
table,
"tap",
Expand Down Expand Up @@ -1494,3 +1440,39 @@ where
table.remove(key);
}
}

/// Similar to [`apply_optional_value`][] but specialized to `MacPkgConfig`, since we're not able to work with structs dynamically
fn apply_optional_mac_pkg(
table: &mut toml_edit::Table,
key: &str,
desc: &str,
val: Option<&MacPkgConfig>,
) {
if let Some(mac_pkg_config) = val {
let MacPkgConfig {
identifier,
install_location,
} = mac_pkg_config;

let new_item = &mut table[key];
let mut new_table = toml_edit::table();
if let Some(new_table) = new_table.as_table_mut() {
apply_optional_value(
new_table,
"identifier",
"# A unique identifier, in tld.domain.package format\n",
Some(identifier),
);
apply_optional_value(
new_table,
"install-location",
"# The location to which the software should be installed\n",
install_location.as_ref(),
);
new_table.decor_mut().set_prefix(desc);
}
new_item.or_insert(new_table);
} else {
table.remove(key);
}
}

0 comments on commit e5ba17f

Please sign in to comment.