use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Default)]
pub struct Asked {
pub example: String,
pub id: String,
pub device: bool,
pub release: bool,
pub features: String,
pub run: bool,
pub identity: Option<String>,
pub profile: Option<PathBuf>,
}
pub fn target(device: bool) -> &'static str {
if device { "aarch64-apple-ios" } else { "aarch64-apple-ios-sim" }
}
pub fn info_plist(name: &str, id: &str, device: bool) -> String {
let platform = if device { "iPhoneOS" } else { "iPhoneSimulator" };
let orientations = "<array><string>UIInterfaceOrientationPortrait</string><string>UIInterfaceOrientationPortraitUpsideDown</string>\
<string>UIInterfaceOrientationLandscapeLeft</string><string>UIInterfaceOrientationLandscapeRight</string></array>";
format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key><string>{name}</string>
<key>CFBundleIdentifier</key><string>{id}</string>
<key>CFBundleName</key><string>{name}</string>
<key>CFBundleDisplayName</key><string>{name}</string>
<key>CFBundlePackageType</key><string>APPL</string>
<key>CFBundleShortVersionString</key><string>1.0</string>
<key>CFBundleVersion</key><string>1</string>
<key>CFBundleInfoDictionaryVersion</key><string>6.0</string>
<key>CFBundleSupportedPlatforms</key><array><string>{platform}</string></array>
<key>LSRequiresIPhoneOS</key><true/>
<key>MinimumOSVersion</key><string>14.0</string>
<key>UIDeviceFamily</key><array><integer>1</integer><integer>2</integer></array>
<key>UILaunchScreen</key><dict/>
<key>UIApplicationSupportsIndirectInputEvents</key><true/>
<key>UIApplicationSceneManifest</key><dict><key>UIApplicationSupportsMultipleScenes</key><false/></dict>
<key>UISupportedInterfaceOrientations</key>{orientations}
<key>UISupportedInterfaceOrientations~ipad</key>{orientations}
</dict>
</plist>
"#
)
}
pub fn bundle(program: &Path, name: &str, id: &str, device: bool, out: &Path) -> std::io::Result<PathBuf> {
let app = out.join(format!("{name}.app"));
_ = std::fs::remove_dir_all(&app);
std::fs::create_dir_all(&app)?;
std::fs::copy(program, app.join(name))?;
std::fs::write(app.join("Info.plist"), info_plist(name, id, device))?;
Ok(app)
}
pub fn ran(command: &mut Command) -> Result<String, String> {
let done = command.output().map_err(|error| format!("{command:?}: {error}"))?;
let said = String::from_utf8_lossy(&done.stdout).into_owned();
if done.status.success() { Ok(said) } else { Err(format!("{command:?}: {}{said}", String::from_utf8_lossy(&done.stderr))) }
}
pub fn sign_for_simulator(app: &Path) -> Result<(), String> {
ran(Command::new("codesign").args(["--force", "--sign", "-"]).arg(app)).map(drop)
}
pub fn sign_for_device(app: &Path, identity: &str, profile: &Path) -> Result<(), String> {
std::fs::copy(profile, app.join("embedded.mobileprovision")).map_err(|error| error.to_string())?;
let decoded = ran(Command::new("security").args(["cms", "-D", "-i"]).arg(profile))?;
let plist = app.with_extension("profile.plist");
std::fs::write(&plist, decoded).map_err(|error| error.to_string())?;
let entitlements = ran(Command::new("/usr/libexec/PlistBuddy").args(["-x", "-c", "Print :Entitlements"]).arg(&plist))?;
let claimed = app.with_extension("entitlements");
std::fs::write(&claimed, entitlements).map_err(|error| error.to_string())?;
ran(Command::new("codesign").args(["--force", "--sign", identity, "--entitlements"]).arg(&claimed).arg(app)).map(drop)
}
pub fn newest_iphone() -> Result<String, String> {
let listed = ran(Command::new("xcrun").args(["simctl", "list", "devices", "available"]))?;
let (mut found, mut section): (Option<String>, Option<String>) = (None, None);
for line in listed.lines() {
if line.starts_with("--") {
found = section.take().filter(|first| !first.is_empty()).or(found);
section = line.contains("iOS").then(String::new);
continue;
}
let id = line.trim().strip_prefix("iPhone").and_then(|rest| rest.split(['(', ')']).map(str::trim).find(|piece| piece.len() == 36 && piece.matches('-').count() == 4));
if let (Some(id), Some(first)) = (id, section.as_mut().filter(|first| first.is_empty())) {
*first = id.to_owned();
}
}
let found = section.filter(|first| !first.is_empty()).or(found);
found.ok_or_else(|| format!("no iPhone simulator in:\n{listed}"))
}
pub fn boot(device: &str) -> Result<(), String> {
_ = ran(Command::new("xcrun").args(["simctl", "boot", device]));
ran(Command::new("xcrun").args(["simctl", "bootstatus", device, "-b"])).map(drop)
}
pub fn build(asked: &Asked, built: &Path) -> Result<PathBuf, String> {
let target = target(asked.device);
let features = if asked.features.is_empty() { "touch".to_string() } else { format!("touch,{}", asked.features) };
let mut cargo = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()));
cargo.args(["build", "--example", &asked.example, "--target", target, "--features", &features]).env("CARGO_TARGET_DIR", built);
if asked.release {
cargo.arg("--release");
}
let status = cargo.status().map_err(|error| error.to_string())?;
if !status.success() {
return Err("the build failed".into());
}
let program = built.join(target).join(if asked.release { "release" } else { "debug" }).join("examples").join(&asked.example);
let app = bundle(&program, &asked.example, &asked.id, asked.device, &built.join("ios")).map_err(|error| error.to_string())?;
match (&asked.identity, &asked.profile, asked.device) {
(Some(identity), Some(profile), true) => sign_for_device(&app, identity, profile)?,
(.., true) => return Err("a device build needs --identity and --profile".into()),
_ => sign_for_simulator(&app)?,
}
Ok(app)
}
pub fn run_in_simulator(app: &Path, id: &str) -> Result<(), String> {
let device = newest_iphone()?;
boot(&device)?;
_ = Command::new("open").args(["-a", "Simulator"]).status();
ran(Command::new("xcrun").args(["simctl", "install", &device]).arg(app))?;
let status = Command::new("xcrun").args(["simctl", "launch", "--console-pty", "--terminate-running-process", &device, id]).status();
status.map_err(|error| error.to_string()).map(drop)
}
fn asked() -> Result<Asked, String> {
let mut asked = Asked::default();
let mut words = std::env::args().skip(1);
while let Some(word) = words.next() {
match word.as_str() {
"--device" => asked.device = true,
"--release" => asked.release = true,
"--run" => asked.run = true,
"--features" => asked.features = words.next().unwrap_or_default(),
"--id" => asked.id = words.next().unwrap_or_default(),
"--identity" => asked.identity = words.next(),
"--profile" => asked.profile = words.next().map(PathBuf::from),
_ if !word.starts_with('-') && asked.example.is_empty() => asked.example = word,
_ => return Err(format!("unknown: {word}")),
}
}
if asked.example.is_empty() {
return Err("name an example: cargo run --example ios_bundle -- gallery --run".into());
}
if asked.id.is_empty() {
asked.id = format!("dev.taut.{}", asked.example.replace('_', "-"));
}
Ok(asked)
}
fn main() {
if !cfg!(target_os = "macos") {
return eprintln!("An iOS app is built on a Mac with Xcode.");
}
let done = asked().and_then(|asked| {
let built = std::env::var_os("CARGO_TARGET_DIR").map_or(Path::new(env!("CARGO_MANIFEST_DIR")).join("target"), PathBuf::from);
let app = build(&asked, &built)?;
println!("{}", app.display());
if asked.run && !asked.device { run_in_simulator(&app, &asked.id) } else { Ok(()) }
});
if let Err(error) = done {
eprintln!("{error}");
std::process::exit(1);
}
}