From 0441782e7f94ca535bc6ebfa42997c87bb4311c6 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 28 Jun 2023 08:59:20 +0200 Subject: [PATCH] cargo-apk: Support new `Artifact` format for `[lib]`, `[[bin]]` and `[[example]]` renames (#26) https://github.com/rust-mobile/cargo-subcommand/pull/17 --- Cargo.toml | 4 ++++ cargo-apk/README.md | 4 +++- cargo-apk/src/apk.rs | 20 +++++++++++--------- cargo-apk/src/main.rs | 17 +++++++++++++---- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 17a3449..c81137c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,7 @@ members = [ "examples", "cargo-apk", ] + +[patch.crates-io] +# Unreleased, including https://github.com/rust-mobile/cargo-subcommand/pull/17 +cargo-subcommand = { git = "https://github.com/rust-mobile/cargo-subcommand.git", rev = "281b281" } diff --git a/cargo-apk/README.md b/cargo-apk/README.md index edae6e1..5253b2a 100644 --- a/cargo-apk/README.md +++ b/cargo-apk/README.md @@ -5,11 +5,13 @@ Tool for creating Android packages. ## Installation From crates.io: + ```console $ cargo install cargo-apk ``` From source: + ```console $ cargo install --path . ``` @@ -80,7 +82,7 @@ runtime_libs = "path/to/libs_folder" # and keystore password respectively. The profile portion follows the same rules # as ``, it is the uppercased profile name with `-` replaced with `_`. # -# If present they take precedence over the signing information in the manifest. +# If present they take precedence over the signing information in the manifest. [package.metadata.android.signing.] path = "relative/or/absolute/path/to/my.keystore" keystore_password = "android" diff --git a/cargo-apk/src/apk.rs b/cargo-apk/src/apk.rs index f2549c4..f70f87c 100644 --- a/cargo-apk/src/apk.rs +++ b/cargo-apk/src/apk.rs @@ -1,6 +1,6 @@ use crate::error::Error; use crate::manifest::{Inheritable, Manifest, Root}; -use cargo_subcommand::{Artifact, CrateType, Profile, Subcommand}; +use cargo_subcommand::{Artifact, ArtifactType, CrateType, Profile, Subcommand}; use ndk_build::apk::{Apk, ApkConfig}; use ndk_build::cargo::{cargo_ndk, VersionCode}; use ndk_build::dylibs::get_libs_search_paths; @@ -162,19 +162,21 @@ impl<'a> ApkBuilder<'a> { let mut manifest = self.manifest.android_manifest.clone(); if manifest.package.is_empty() { - manifest.package = match artifact { - Artifact::Root(name) => format!("rust.{}", name.replace('-', "_")), - Artifact::Example(name) => format!("rust.example.{}", name.replace('-', "_")), + let name = artifact.name.replace('-', "_"); + manifest.package = match artifact.r#type { + ArtifactType::Lib => format!("rust.{}", name), + ArtifactType::Bin => format!("rust.{}", name), + ArtifactType::Example => format!("rust.example.{}", name), }; } if manifest.application.label.is_empty() { - manifest.application.label = artifact.name().to_string(); + manifest.application.label = artifact.name.to_string(); } manifest.application.activity.meta_data.push(MetaData { name: "android.app.lib_name".to_string(), - value: artifact.name().replace('-', "_"), + value: artifact.name.replace('-', "_"), }); let crate_path = self.cmd.manifest().parent().expect("invalid manifest path"); @@ -200,11 +202,11 @@ impl<'a> ApkBuilder<'a> { .manifest .apk_name .clone() - .unwrap_or_else(|| artifact.name().to_string()); + .unwrap_or_else(|| artifact.name.to_string()); let config = ApkConfig { ndk: self.ndk.clone(), - build_dir: self.build_dir.join(artifact), + build_dir: self.build_dir.join(artifact.build_dir()), apk_name, assets, resources, @@ -331,7 +333,7 @@ impl<'a> ApkBuilder<'a> { let apk = self.build(artifact)?; apk.install(self.device_serial.as_deref())?; - let target_dir = self.build_dir.join(artifact); + let target_dir = self.build_dir.join(artifact.build_dir()); self.ndk.ndk_gdb( target_dir, "android.app.NativeActivity", diff --git a/cargo-apk/src/main.rs b/cargo-apk/src/main.rs index c587477..95f8185 100644 --- a/cargo-apk/src/main.rs +++ b/cargo-apk/src/main.rs @@ -129,6 +129,15 @@ fn split_apk_and_cargo_args(input: Vec) -> (Args, Vec) { (args, split_args.cargo_args) } +fn iterator_single_item(mut iter: impl Iterator) -> Option { + let first_item = iter.next()?; + if iter.next().is_some() { + None + } else { + Some(first_item) + } +} + fn main() -> anyhow::Result<()> { env_logger::init(); let Cmd { @@ -160,14 +169,14 @@ fn main() -> anyhow::Result<()> { ApkSubCmd::Run { args, no_logcat } => { let cmd = Subcommand::new(args.subcommand_args)?; let builder = ApkBuilder::from_subcommand(&cmd, args.device)?; - anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args()); - builder.run(&cmd.artifacts()[0], no_logcat)?; + let artifact = iterator_single_item(cmd.artifacts()).ok_or(Error::invalid_args())?; + builder.run(artifact, no_logcat)?; } ApkSubCmd::Gdb { args } => { let cmd = Subcommand::new(args.subcommand_args)?; let builder = ApkBuilder::from_subcommand(&cmd, args.device)?; - anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args()); - builder.gdb(&cmd.artifacts()[0])?; + let artifact = iterator_single_item(cmd.artifacts()).ok_or(Error::invalid_args())?; + builder.gdb(artifact)?; } ApkSubCmd::Version => { println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));