Skip to content

Commit

Permalink
rename domain to identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed May 24, 2024
1 parent a74e2d3 commit 319fd7b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 46 deletions.
6 changes: 5 additions & 1 deletion .changes/use-tauri-identifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
"cargo-mobile2": minor
---

**BREAKING CHANGE:** Take `config.domain` as package name in Android and bundle ID in iOS instead of combining it with the libname in Cargo.toml.
Use `config.identifier` as the package name in Android and bundle ID in iOS.

**BREAKING CHANGE:**
- In `Config`, renamed field `domain` to `identifier`.
- In `App`, renamed method `reverse_domain` to `reverse_identifier`.
4 changes: 2 additions & 2 deletions src/android/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<'a> Device<'a> {
self.install_apk(config, env, profile)
.map_err(RunError::ApkInstallFailed)?;
}
let activity = format!("{}/{}", config.app().reverse_domain(), activity);
let activity = format!("{}/{}", config.app().reverse_identifier(), activity);
self.adb(env)
.before_spawn(move |cmd| {
cmd.args(["shell", "am", "start", "-n", &activity]);
Expand All @@ -386,7 +386,7 @@ impl<'a> Device<'a> {
let stdout = loop {
let cmd = duct::cmd(
env.platform_tools_path().join("adb"),
["shell", "pidof", "-s", &config.app().reverse_domain()],
["shell", "pidof", "-s", &config.app().reverse_identifier()],
)
.vars(env.explicit_env())
.stderr_capture()
Expand Down
2 changes: 1 addition & 1 deletion src/apple/device/simctl/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn run(

handle.wait().map_err(RunError::DeployFailed)?;

let app_id = config.app().reverse_domain();
let app_id = config.app().reverse_identifier();
let mut launcher_cmd = duct::cmd("xcrun", ["simctl", "launch", id, &app_id])
.vars(env.explicit_env())
.dup_stdio();
Expand Down
28 changes: 14 additions & 14 deletions src/config/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub enum Error {
NameInvalid(name::Invalid),
#[error("app.lib_name invalid: {0}")]
LibNameInvalid(lib_name::Invalid),
#[error("`app.domain` {domain} isn't valid: {cause}")]
DomainInvalid {
domain: String,
#[error("`app.identifier` {identifier} isn't valid: {cause}")]
IdentifierInvalid {
identifier: String,
cause: domain::DomainError,
},
#[error("`app.asset-dir` {asset_dir} couldn't be normalized: {cause}")]
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct App {
name: String,
lib_name: Option<String>,
stylized_name: String,
domain: String,
identifier: String,
asset_dir: PathBuf,
#[serde(skip)]
template_pack: Pack,
Expand All @@ -82,7 +82,7 @@ impl Debug for App {
.field("root_dir", &self.root_dir)
.field("name", &self.name)
.field("stylized_name", &self.stylized_name)
.field("domain", &self.domain)
.field("identifier", &self.identifier)
.field("asset_dir", &self.asset_dir)
.field("template_pack", &self.template_pack)
.finish()
Expand All @@ -102,14 +102,14 @@ impl App {

let stylized_name = raw.stylized_name.unwrap_or_else(|| name.clone());

let domain = {
let domain = raw.domain;
domain::check_domain_syntax(&domain)
.map_err(|cause| Error::DomainInvalid {
domain: domain.clone(),
let identifier = {
let identifier = raw.identifier;
domain::check_domain_syntax(&identifier)
.map_err(|cause| Error::IdentifierInvalid {
identifier: identifier.clone(),
cause,
})
.map(|()| domain)
.map(|()| identifier)
}?;

if raw.asset_dir.as_deref() == Some(DEFAULT_ASSET_DIR) {
Expand Down Expand Up @@ -156,7 +156,7 @@ impl App {
name,
lib_name,
stylized_name,
domain,
identifier,
asset_dir,
template_pack,
target_dir_resolver: None,
Expand Down Expand Up @@ -213,8 +213,8 @@ impl App {
&self.stylized_name
}

pub fn reverse_domain(&self) -> String {
self.domain
pub fn reverse_identifier(&self) -> String {
self.identifier
.clone()
.split('.')
.rev()
Expand Down
44 changes: 25 additions & 19 deletions src/config/app/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,32 @@ use std::{
};

#[derive(Debug)]
enum DefaultDomainError {
enum DefaultIdentifierError {
FailedToGetGitEmailAddr(#[allow(unused)] std::io::Error),
FailedToParseEmailAddr,
}

fn default_domain(_wrapper: &TextWrapper) -> Result<Option<String>, DefaultDomainError> {
fn default_identifier(_wrapper: &TextWrapper) -> Result<Option<String>, DefaultIdentifierError> {
let email = Git::new(".".as_ref())
.user_email()
.map_err(DefaultDomainError::FailedToGetGitEmailAddr)?;
let domain = email
.map_err(DefaultIdentifierError::FailedToGetGitEmailAddr)?;
let identifier = email
.trim()
.split('@')
.last()
.ok_or(DefaultDomainError::FailedToParseEmailAddr)?;
.ok_or(DefaultIdentifierError::FailedToParseEmailAddr)?;
Ok(
if !COMMON_EMAIL_PROVIDERS.contains(&domain) && domain::check_domain_syntax(domain).is_ok()
if !COMMON_EMAIL_PROVIDERS.contains(&identifier)
&& domain::check_domain_syntax(identifier).is_ok()
{
#[cfg(not(feature = "brainium"))]
if domain == "brainiumstudios.com" {
if identifier == "brainiumstudios.com" {
crate::util::cli::Report::action_request(
"You have a Brainium email address, but you're using a non-Brainium installation of cargo-mobile2!",
"If that's not intentional, run `cargo install --force --git https://github.com/tauri-apps/cargo-mobile2 --features brainium`",
).print(_wrapper);
}
Some(domain.to_owned())
Some(identifier.to_owned())
} else {
None
},
Expand Down Expand Up @@ -74,7 +75,7 @@ impl Display for DefaultsError {
struct Defaults {
name: Option<String>,
stylized_name: String,
domain: String,
identifier: String,
}

impl Defaults {
Expand All @@ -89,7 +90,7 @@ impl Defaults {
Ok(Self {
name: name::transliterate(&dir_name.to_kebab_case()),
stylized_name: dir_name.to_title_case(),
domain: default_domain(wrapper)
identifier: default_identifier(wrapper)
.ok()
.flatten()
.unwrap_or_else(|| "example.com".to_owned()),
Expand Down Expand Up @@ -117,7 +118,7 @@ pub enum PromptError {
DefaultsFailed(DefaultsError),
NamePromptFailed(io::Error),
StylizedNamePromptFailed(io::Error),
DomainPromptFailed(io::Error),
IdentifierPromptFailed(io::Error),
ListTemplatePacksFailed(templating::ListError),
TemplatePackPromptFailed(io::Error),
}
Expand All @@ -130,7 +131,9 @@ impl Display for PromptError {
Self::StylizedNamePromptFailed(err) => {
write!(f, "Failed to prompt for stylized name: {}", err)
}
Self::DomainPromptFailed(err) => write!(f, "Failed to prompt for domain: {}", err),
Self::IdentifierPromptFailed(err) => {
write!(f, "Failed to prompt for identifier: {}", err)
}
Self::ListTemplatePacksFailed(err) => write!(f, "{}", err),
Self::TemplatePackPromptFailed(err) => {
write!(f, "Failed to prompt for template pack: {}", err)
Expand All @@ -145,7 +148,7 @@ pub struct Raw {
pub name: String,
pub lib_name: Option<String>,
pub stylized_name: Option<String>,
pub domain: String,
pub identifier: String,
pub asset_dir: Option<String>,
pub template_pack: Option<String>,
}
Expand All @@ -157,7 +160,7 @@ impl Raw {
name: defaults.name.ok_or_else(|| DetectError::NameNotDetected)?,
lib_name: None,
stylized_name: Some(defaults.stylized_name),
domain: defaults.domain,
identifier: defaults.identifier,
asset_dir: None,
template_pack: Some(super::DEFAULT_TEMPLATE_PACK.to_owned())
.filter(|pack| pack != super::IMPLIED_TEMPLATE_PACK),
Expand All @@ -168,14 +171,14 @@ impl Raw {
let defaults = Defaults::new(wrapper).map_err(PromptError::DefaultsFailed)?;
let (name, default_stylized) = Self::prompt_name(wrapper, &defaults)?;
let stylized_name = Self::prompt_stylized_name(&name, default_stylized)?;
let domain = Self::prompt_domain(wrapper, &defaults)?;
let identifier = Self::prompt_identifier(wrapper, &defaults)?;
let template_pack = Some(Self::prompt_template_pack(wrapper)?)
.filter(|pack| pack != super::IMPLIED_TEMPLATE_PACK);
Ok(Self {
name,
lib_name: None,
stylized_name: Some(stylized_name),
domain,
identifier,
asset_dir: None,
template_pack,
})
Expand Down Expand Up @@ -231,10 +234,13 @@ impl Raw {
.map_err(PromptError::StylizedNamePromptFailed)
}

fn prompt_domain(wrapper: &TextWrapper, defaults: &Defaults) -> Result<String, PromptError> {
fn prompt_identifier(
wrapper: &TextWrapper,
defaults: &Defaults,
) -> Result<String, PromptError> {
Ok(loop {
let response = prompt::default("Domain", Some(&defaults.domain), None)
.map_err(PromptError::DomainPromptFailed)?;
let response = prompt::default("Identifier", Some(&defaults.identifier), None)
.map_err(PromptError::IdentifierPromptFailed)?;
match domain::check_domain_syntax(response.as_str()) {
Ok(_) => break response,
Err(err) => {
Expand Down
6 changes: 3 additions & 3 deletions templates/apps/wry/Cargo.toml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "{{app.name}}-desktop"
path = "gen/bin/desktop.rs"

[package.metadata.cargo-android]
app-activity-name = "{{reverse-domain app.domain}}.MainActivity"
app-activity-name = "{{reverse-domain app.identifier}}.MainActivity"
app-dependencies = [
"androidx.webkit:webkit:1.6.1",
"androidx.appcompat:appcompat:1.6.1",
Expand All @@ -25,9 +25,9 @@ app-theme-parent = "Theme.MaterialComponents.DayNight.DarkActionBar"
vulkan-validation = false

[package.metadata.cargo-android.env-vars]
WRY_ANDROID_PACKAGE = "{{reverse-domain app.domain}}"
WRY_ANDROID_PACKAGE = "{{reverse-domain app.identifier}}"
WRY_ANDROID_LIBRARY = "{{snake-case app.name}}"
WRY_ANDROID_KOTLIN_FILES_OUT_DIR = "<android-project-dir>/app/src/main/kotlin/{{dot-to-slash (reverse-domain app.domain)}}/{{snake-case app.name}}"
WRY_ANDROID_KOTLIN_FILES_OUT_DIR = "<android-project-dir>/app/src/main/kotlin/{{dot-to-slash (reverse-domain app.identifier)}}"

[package.metadata.cargo-apple.ios]
frameworks = [ "WebKit" ]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package {{reverse-domain app.domain}}
package {{reverse-domain app.identifier}}

class MainActivity : WryActivity()
2 changes: 1 addition & 1 deletion templates/apps/wry/src/lib.rs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub extern "C" fn start_app() {
#[cfg(target_os = "android")]
{
tao::android_binding!(
{{reverse-domain-snake-case app.domain}},
{{reverse-domain-snake-case app.identifier}},
WryActivity,
wry::android_setup, // pass the wry::android_setup function to tao which will invoke when the event loop is created
_start_app
Expand Down
4 changes: 2 additions & 2 deletions templates/platforms/android-studio/app/build.gradle.kts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ plugins {
}

android {
namespace="{{reverse-domain app.domain}}.{{snake-case app.name}}"{{#if has-asset-packs}}
namespace="{{reverse-domain app.identifier}}"{{#if has-asset-packs}}
assetPacks += mutableSetOf({{quote-and-join-colon-prefix asset-packs}}){{/if}}
compileSdk = 33
defaultConfig {
applicationId = "{{reverse-domain app.domain}}.{{snake-case app.name}}"
applicationId = "{{reverse-domain app.identifier}}"
minSdk = {{android.min-sdk-version}}
targetSdk = 33
versionCode = 1
Expand Down
4 changes: 2 additions & 2 deletions templates/platforms/xcode/project.yml.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: {{app.name}}
options:
bundleIdPrefix: {{reverse-domain app.domain}}
bundleIdPrefix: {{reverse-domain app.identifier}}
deploymentTarget:
iOS: {{apple.ios-version}}
macOS: {{apple.macos-version}}
Expand All @@ -12,7 +12,7 @@ settingGroups:
app:
base:
PRODUCT_NAME: {{app.stylized-name}}
PRODUCT_BUNDLE_IDENTIFIER: {{reverse-domain app.domain}}.{{app.name}}
PRODUCT_BUNDLE_IDENTIFIER: {{reverse-domain app.identifier}}
{{#if apple.development-team}}
DEVELOPMENT_TEAM: {{apple.development-team}}
{{/if}}
Expand Down

0 comments on commit 319fd7b

Please sign in to comment.