Skip to content

Commit

Permalink
fix: conflict between Apple and Android targets (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Nov 14, 2023
1 parent 85e72b9 commit cfd674e
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-targets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-mobile2": minor
---

Fixes conflicts between Apple and Android targets. `Target::name_list` now returns `Vec<&str>`.
8 changes: 4 additions & 4 deletions src/android/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ pub enum Command {
Open,
#[structopt(name = "check", about = "Checks if code compiles for target(s)")]
Check {
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = Target::name_list())]
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = &Target::name_list())]
targets: Vec<String>,
},
#[structopt(name = "build", about = "Builds dynamic libraries for target(s)")]
Build {
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = Target::name_list())]
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = &Target::name_list())]
targets: Vec<String>,
#[structopt(flatten)]
profile: cli::Profile,
Expand Down Expand Up @@ -97,7 +97,7 @@ pub enum Command {
pub enum ApkSubcommand {
#[structopt(about = "build APKs (Android Package Kit)")]
Build {
#[structopt(name = "targets", possible_values = Target::name_list())]
#[structopt(name = "targets", possible_values = &Target::name_list())]
/// Which targets to build (all by default).
targets: Vec<String>,
#[structopt(flatten)]
Expand All @@ -110,7 +110,7 @@ pub enum ApkSubcommand {
pub enum AabSubcommand {
#[structopt(about = "build AABs (Android App Bundle)")]
Build {
#[structopt(name = "targets", possible_values = Target::name_list())]
#[structopt(name = "targets", possible_values = &Target::name_list())]
/// Which targets to build (all by default).
targets: Vec<String>,
#[structopt(flatten)]
Expand Down
4 changes: 4 additions & 0 deletions src/android/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ impl<'a> TargetTrait<'a> for Target<'a> {
})
}

fn name_list() -> Vec<&'a str> {
Self::all().keys().copied().collect::<Vec<_>>()
}

fn triple(&'a self) -> &'a str {
self.triple
}
Expand Down
6 changes: 3 additions & 3 deletions src/apple/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub enum Command {
Open,
#[structopt(name = "check", about = "Checks if code compiles for target(s)")]
Check {
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = Target::name_list())]
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = &Target::name_list())]
targets: Vec<String>,
},
#[structopt(name = "build", about = "Builds static libraries for target(s)")]
Build {
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = Target::name_list())]
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = &Target::name_list())]
targets: Vec<String>,
#[structopt(flatten)]
profile: cli::Profile,
Expand All @@ -79,7 +79,7 @@ pub enum Command {
Archive {
#[structopt(long = "build-number")]
build_number: Option<u32>,
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = Target::name_list())]
#[structopt(name = "targets", default_value = Target::DEFAULT_KEY, possible_values = &Target::name_list())]
targets: Vec<String>,
#[structopt(flatten)]
profile: cli::Profile,
Expand Down
2 changes: 1 addition & 1 deletion src/apple/device/devicectl/device_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ pub fn device_list<'a>(env: &Env) -> Result<BTreeSet<Device<'a>>, DeviceListErro
.run()
.map_err(DeviceListError::DetectionFailed)?;

let contents = read_to_string(&json_output_path_)?;
let contents = read_to_string(json_output_path_)?;
parse_device_list(contents)
}
2 changes: 1 addition & 1 deletion src/apple/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,6 @@ pub fn list_devices<'a>(env: &Env) -> Result<BTreeSet<Device<'a>>, String> {
}
}

pub fn list_simulators<'a>(env: &Env) -> Result<BTreeSet<Simulator>, String> {
pub fn list_simulators(env: &Env) -> Result<BTreeSet<Simulator>, String> {
simctl::device_list(env).map_err(|e| e.to_string())
}
4 changes: 4 additions & 0 deletions src/apple/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ impl<'a> TargetTrait<'a> for Target<'a> {
})
}

fn name_list() -> Vec<&'a str> {
Self::all().keys().copied().collect::<Vec<_>>()
}

fn triple(&'a self) -> &'a str {
self.triple
}
Expand Down
2 changes: 1 addition & 1 deletion src/apple/version_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl FromStr for VersionNumber {

fn from_str(v: &str) -> Result<Self, Self::Err> {
match v.split('.').count() {
1 | 2 | 3 => {
1..=3 => {
let triple = VersionTriple::from_str(v)?;
Ok(Self {
triple,
Expand Down
9 changes: 1 addition & 8 deletions src/target.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::util;
use once_cell_regex::exports::once_cell::sync::OnceCell;
use std::{
collections::BTreeMap,
fmt::{self, Debug, Display},
Expand All @@ -11,13 +10,7 @@ pub trait TargetTrait<'a>: Debug + Sized {

fn all() -> &'a BTreeMap<&'a str, Self>;

fn name_list() -> &'static [&'a str]
where
Self: 'static,
{
static INSTANCE: OnceCell<Vec<&str>> = OnceCell::new();
INSTANCE.get_or_init(|| Self::all().keys().copied().collect::<Vec<_>>())
}
fn name_list() -> Vec<&'a str>;

fn default_ref() -> &'a Self {
Self::all()
Expand Down

0 comments on commit cfd674e

Please sign in to comment.