Skip to content

Commit

Permalink
ref(target): replace sdkroots with platform
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed Jun 19, 2022
1 parent 5052874 commit d06cba8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum-as-inner = "0.5.0"
derive_is_enum_variant = "0.1.1"
enum_variant_macros = "0.2.0"
phf = "0.10.1"
serde = { version = "1.0.137", features = ["derive"] }

[dev-dependencies]
tracing-test = "0.2.1"
Expand Down
10 changes: 9 additions & 1 deletion src/pbxproj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,16 @@ fn test_demo1_representation() {
assert_eq!(Some(&String::from("Wordle")), targets[0].product_name);
assert_eq!(Some(&String::from("Wordle")), targets[0].name);
assert_eq!(PBXProductType::Application, targets[0].product_type);
assert_eq!(
PBXTargetPlatform::IOS,
targets[0].platfrom(project.objects())
);
assert_eq!(None, targets[0].build_tool_path);
assert_eq!(None, targets[0].build_arguments_string);
assert_eq!(None, targets[0].build_working_directory);
assert_eq!(None, targets[0].pass_build_settings_in_environment);
assert_eq!(3, targets[0].build_phases.len());

assert_eq!(
vec![
(&PBXBuildPhaseKind::Sources, 12), // 12
Expand Down Expand Up @@ -188,8 +193,11 @@ fn test_demo10_representation() {
assert_eq!(&PBXTargetKind::Native, targets[0].kind);
assert_eq!(Some(&String::from("Scrumdinger")), targets[0].product_name);
assert_eq!(Some(&String::from("Scrumdinger")), targets[0].name);
assert!(!targets[0].sdkroots(project.objects()).is_empty());
assert_eq!(PBXProductType::Application, targets[0].product_type);
assert_eq!(
PBXTargetPlatform::IOS,
targets[0].platfrom(project.objects())
);
assert_eq!(None, targets[0].build_tool_path);
assert_eq!(None, targets[0].build_arguments_string);
assert_eq!(None, targets[0].build_working_directory);
Expand Down
18 changes: 15 additions & 3 deletions src/pbxproj/object/target.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod dependency;
mod platform;
pub use dependency::*;

use anyhow::Result;

use crate::pbxproj::*;

pub use platform::PBXTargetPlatform;

/// `Abstraction` for building a specific targets (a library, binary, or test).
///
/// variants: `PBXAggregateTarget`, `PBXLegacyTarget`, and `PBXNativeTarget`
Expand Down Expand Up @@ -46,7 +49,7 @@ pub struct PBXTarget<'a> {

impl<'a> PBXTarget<'a> {
/// get target's sdk roots from all build configuration settings
pub fn sdkroots(&'a self, objects: &'a PBXObjectCollection) -> Vec<&'a String> {
pub fn platfrom(&'a self, objects: &'a PBXObjectCollection) -> PBXTargetPlatform {
if let Some(ref bclist) = self.build_configuration_list {
let mut sdkroots = bclist
.build_configurations
Expand All @@ -68,9 +71,18 @@ impl<'a> PBXTarget<'a> {
if sdkroots.is_empty() {
tracing::error!("No SDKROOT found for {:?}", self.name);
}
sdkroots
sdkroots.dedup();
let sdkroot = &sdkroots[0];
if sdkroots.len() > 1 {
tracing::warn!("Get more then one sdkroot for target {:?}", self.name);
tracing::warn!("Using {sdkroot:?} as sdkroot");
}
PBXTargetPlatform::from_sdk_root(sdkroot.as_str())
} else {
tracing::error!("No build configuration list for {:?}", self.name);
tracing::error!(
"No build configuration list for {:?}, platfrom is not identified",
self.name
);
Default::default()
}
}
Expand Down
80 changes: 80 additions & 0 deletions src/pbxproj/object/target/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use derive_is_enum_variant::is_enum_variant;
#[derive(Clone, Debug, Deserialize, Serialize, Hash, PartialEq, Eq, is_enum_variant)]
/// Target Platform based
pub enum PBXTargetPlatform {
/// iOS Platform
#[serde(rename = "iOS")]
IOS,
/// watchOS Platform
#[serde(rename = "watchOS")]
WatchOS,
/// tvOs Platform
#[serde(rename = "tvOS")]
TvOS,
/// macOS Platform
#[serde(rename = "macOS")]
MacOS,
/// Unknown or not support platform
Unknown,
}

impl Default for PBXTargetPlatform {
fn default() -> Self {
Self::Unknown
}
}

impl PBXTargetPlatform {
/// Get Target Platfrom from sdkroot
pub fn from_sdk_root(sdk_root: &str) -> Self {
match sdk_root {
"iphoneos" => Self::IOS,
"macosx" => Self::MacOS,
"appletvos" => Self::TvOS,
"watchos" => Self::WatchOS,
_ => Self::Unknown,
}
}
/// Get PBXTargetPlatform from simulator identifer
pub fn from_identifer(identifer: &str) -> Self {
let name = identifer.replace("com.apple.CoreSimulator.SimRuntime.", "");
let platform_str = name.split("-").next().unwrap().to_string();
match Self::from_str(&platform_str) {
Ok(res) => res,
Err(e) => {
tracing::error!("Platfrom from str: {e}");
Self::Unknown
}
}
}
}

impl FromStr for PBXTargetPlatform {
type Err = String;

fn from_str(s: &str) -> std::result::Result<Self, String> {
match s {
"iOS" => Ok(Self::IOS),
"watchOS" => Ok(Self::WatchOS),
"tvOS" => Ok(Self::TvOS),
"macOS" => Ok(Self::MacOS),
_ => Ok(Self::Unknown),
}
}
}

impl ToString for PBXTargetPlatform {
fn to_string(&self) -> String {
match self {
Self::IOS => "iOS",
Self::WatchOS => "watchOS",
Self::TvOS => "tvOS",
Self::MacOS => "macOS",
_ => "",
}
.into()
}
}

0 comments on commit d06cba8

Please sign in to comment.