-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Marijn Suijten <[email protected]>
- Loading branch information
Showing
3 changed files
with
101 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,109 @@ | ||
use cargo_apk::{ApkBuilder, Error}; | ||
use cargo_subcommand::Subcommand; | ||
use std::process::Command; | ||
use clap::Parser; | ||
|
||
#[derive(Parser)] | ||
struct Cmd { | ||
#[clap(subcommand)] | ||
apk: ApkCmd, | ||
} | ||
|
||
#[derive(clap::Subcommand)] | ||
enum ApkCmd { | ||
/// Helps cargo build apks for Android | ||
Apk { | ||
#[clap(subcommand)] | ||
cmd: ApkSubCmd, | ||
}, | ||
} | ||
|
||
#[derive(Parser)] | ||
struct Args { | ||
#[clap(flatten)] | ||
subcommand_args: cargo_subcommand::Args, | ||
/// Use device with the given serial (see `adb devices`) | ||
#[clap(short, long)] | ||
device: Option<String>, | ||
} | ||
|
||
#[derive(clap::Subcommand)] | ||
#[clap(trailing_var_arg = true)] | ||
enum ApkSubCmd { | ||
/// Analyze the current package and report errors, but don't build object files nor an apk | ||
#[clap(visible_alias = "c")] | ||
Check { | ||
#[clap(flatten)] | ||
args: Args, | ||
}, | ||
/// Compile the current package and create an apk | ||
#[clap(visible_alias = "b")] | ||
Build { | ||
#[clap(flatten)] | ||
args: Args, | ||
}, | ||
/// Invoke `cargo` under the detected NDK environment | ||
#[clap(name = "--")] | ||
Ndk { | ||
cargo_cmd: String, | ||
#[clap(flatten)] | ||
args: Args, | ||
}, | ||
/// Run a binary or example apk of the local package | ||
#[clap(visible_alias = "r")] | ||
Run { | ||
#[clap(flatten)] | ||
args: Args, | ||
/// Do not print or follow `logcat` after running the app | ||
#[clap(short, long)] | ||
no_logcat: bool, | ||
}, | ||
/// Start a gdb session attached to an adb device with symbols loaded | ||
Gdb { | ||
#[clap(flatten)] | ||
args: Args, | ||
}, | ||
/// Print the version of cargo-apk | ||
Version, | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
let args = std::env::args(); | ||
let mut device_serial = None; | ||
let mut no_logcat = false; | ||
let cmd = Subcommand::new(args, "apk", |name, value| match name { | ||
"--device" => { | ||
if let Some(value) = value { | ||
println!("Running on {}", value); | ||
device_serial = Some(value.to_owned()); | ||
Ok(true) | ||
} else { | ||
Err(cargo_subcommand::Error::InvalidArgs) | ||
} | ||
} | ||
"--no-logcat" => { | ||
no_logcat = true; | ||
Ok(true) | ||
let Cmd { | ||
apk: ApkCmd::Apk { cmd }, | ||
} = Cmd::parse(); | ||
match cmd { | ||
ApkSubCmd::Check { args } => { | ||
let cmd = Subcommand::new(args.subcommand_args)?; | ||
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?; | ||
builder.check()?; | ||
} | ||
_ => Ok(false), | ||
}) | ||
.map_err(Error::Subcommand)?; | ||
|
||
let builder = ApkBuilder::from_subcommand(&cmd, device_serial, no_logcat)?; | ||
|
||
match cmd.cmd() { | ||
"check" | "c" => builder.check()?, | ||
"build" | "b" => { | ||
ApkSubCmd::Build { args } => { | ||
let cmd = Subcommand::new(args.subcommand_args)?; | ||
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?; | ||
for artifact in cmd.artifacts() { | ||
builder.build(artifact)?; | ||
} | ||
} | ||
"run" | "r" => { | ||
anyhow::ensure!(cmd.artifacts().len() == 1, Error::invalid_args()); | ||
builder.run(&cmd.artifacts()[0])?; | ||
ApkSubCmd::Ndk { cargo_cmd, args } => { | ||
let cmd = Subcommand::new(args.subcommand_args)?; | ||
let builder = ApkBuilder::from_subcommand(&cmd, args.device)?; | ||
builder.default(&cargo_cmd)?; | ||
} | ||
"--" => { | ||
builder.default()?; | ||
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)?; | ||
} | ||
"gdb" => { | ||
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])?; | ||
} | ||
"help" => { | ||
if let Some(arg) = cmd.args().get(0) { | ||
match &**arg { | ||
"build" | "b" | "check" | "c" | "run" | "r" | "test" | "t" | "doc" => { | ||
run_cargo(&cmd)? | ||
} | ||
"gdb" => print_gdb_help(), | ||
_ => print_help(), | ||
} | ||
} else { | ||
print_help(); | ||
} | ||
} | ||
"version" => { | ||
ApkSubCmd::Version => { | ||
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); | ||
} | ||
_ => print_help(), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn run_cargo(cmd: &Subcommand) -> Result<(), Error> { | ||
Command::new("cargo") | ||
.arg(cmd.cmd()) | ||
.args(cmd.args()) | ||
.status()?; | ||
Ok(()) | ||
} | ||
|
||
fn print_help() { | ||
println!( | ||
r#"cargo-apk | ||
Helps cargo build apk's for android | ||
USAGE: | ||
cargo apk [SUBCOMMAND] | ||
SUBCOMMAND: | ||
check, c Checks that the current package builds without creating an apk | ||
build, b Compiles the current package and creates an apk | ||
run, r Run a binary or example of the local package | ||
gdb Start a gdb session attached to an adb device with symbols loaded | ||
version Print the version of cargo-apk | ||
FLAGS: | ||
--no-logcat Don't print and follow `logcat` after running the application. | ||
OPTIONS: | ||
--device <serial> Use device with the given serial. See `adb devices` for a list of | ||
connected Android devices. | ||
"# | ||
); | ||
} | ||
|
||
fn print_gdb_help() { | ||
println!( | ||
r#"cargo-apk gdb | ||
Start a gdb session attached to an adb device with symbols loaded | ||
USAGE: | ||
cargo apk gdb | ||
"# | ||
); | ||
} |