Skip to content

Commit

Permalink
feat(cli.rs): allow using cross instead of cargo, add target triple a…
Browse files Browse the repository at this point in the history
…rg (#1664)
  • Loading branch information
lucasfernog authored Apr 30, 2021
1 parent 894643c commit 5c1fe52
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-runner-arg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Adds `--runner [PROGRAM]` argument on the `dev` and `build` command, allowing using the specified program to run and build the application (example program: `cross`).
5 changes: 5 additions & 0 deletions .changes/cli-target-triple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Adds `--target [TARGET_TRIPLE]` option to the `build` command (example: `--target arm-unknown-linux-gnueabihf`).
5 changes: 5 additions & 0 deletions .changes/cli-targets-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Rename `--target` option on the `build` command to `--bundle`.
2 changes: 1 addition & 1 deletion examples/api/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tauri-build = { path = "../../../core/tauri-build" }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../../core/tauri", features =["api-all", "cli", "updater"]}
tauri = { path = "../../../core/tauri", features = ["api-all", "cli", "updater"] }

[features]
default = [ "custom-protocol" ]
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ description = "A very simple Tauri Appplication"
edition = "2018"

[build-dependencies]
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ]}
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ] }

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../../core/tauri", features =["api-all"]}
tauri = { path = "../../../core/tauri", features = ["api-all"] }

[features]
default = [ "custom-protocol" ]
Expand Down
2 changes: 1 addition & 1 deletion examples/multiwindow/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "Apache-2.0 OR MIT"
tauri-build = { path = "../../../core/tauri-build" }

[dependencies]
tauri = { path = "../../../core/tauri", features =["api-all"]}
tauri = { path = "../../../core/tauri", features = ["api-all"] }

[features]
default = [ "custom-protocol" ]
Expand Down
4 changes: 2 additions & 2 deletions examples/updater/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2018"
license = "Apache-2.0 OR MIT"

[build-dependencies]
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ]}
tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ] }

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = [ "derive" ] }
tauri = { path = "../../../core/tauri", features =["api-all", "updater"]}
tauri = { path = "../../../core/tauri", features = ["api-all", "updater"] }

[features]
default = [ "custom-protocol" ]
Expand Down
3 changes: 3 additions & 0 deletions tooling/cli.rs/config_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ fn default_dialog() -> Option<bool> {
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BuildConfig {
/// The binary used to build and run the application.
pub runner: Option<String>,
/// the app's dev server URL, or the path to the directory containing an index.html file
#[serde(default = "default_dev_path")]
pub dev_path: String,
Expand Down Expand Up @@ -629,6 +631,7 @@ pub struct Config {

fn default_build() -> BuildConfig {
BuildConfig {
runner: None,
dev_path: default_dev_path(),
dist_dir: default_dist_dir(),
before_dev_command: None,
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli.rs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@
"default": "../dist",
"type": "string"
},
"runner": {
"description": "The binary used to build and run the application.",
"type": [
"string",
"null"
]
},
"withGlobalTauri": {
"description": "Whether we should inject the Tauri API on `window.__TAURI__` or not.",
"default": false,
Expand Down
28 changes: 23 additions & 5 deletions tooling/cli.rs/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ mod rust;

#[derive(Default)]
pub struct Build {
runner: Option<String>,
debug: bool,
verbose: bool,
targets: Option<Vec<String>>,
target: Option<String>,
bundles: Option<Vec<String>>,
config: Option<String>,
}

Expand All @@ -40,8 +42,18 @@ impl Build {
self
}

pub fn targets(mut self, targets: Vec<String>) -> Self {
self.targets = Some(targets);
pub fn runner(mut self, runner: String) -> Self {
self.runner.replace(runner);
self
}

pub fn target(mut self, target: String) -> Self {
self.target.replace(target);
self
}

pub fn bundles(mut self, bundles: Vec<String>) -> Self {
self.bundles.replace(bundles);
self
}

Expand Down Expand Up @@ -90,7 +102,13 @@ impl Build {
));
}

rust::build_project(self.debug)?;
let runner_from_config = config_.build.runner.clone();
let runner = self
.runner
.or(runner_from_config)
.unwrap_or_else(|| "cargo".to_string());

rust::build_project(runner, &self.target, self.debug)?;

let app_settings = rust::AppSettings::new(&config_)?;

Expand Down Expand Up @@ -135,7 +153,7 @@ impl Build {
settings_builder = settings_builder.verbose();
}

if let Some(names) = self.targets {
if let Some(names) = self.bundles {
let mut types = vec![];
for name in names {
if name == "none" {
Expand Down
13 changes: 9 additions & 4 deletions tooling/cli.rs/src/build/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,23 @@ struct CargoConfig {
build: Option<CargoBuildConfig>,
}

pub fn build_project(debug: bool) -> crate::Result<()> {
pub fn build_project(runner: String, target: &Option<String>, debug: bool) -> crate::Result<()> {
let mut args = vec!["build", "--features=custom-protocol"];

if let Some(target) = target {
args.push("--target");
args.push(target);
}

if !debug {
args.push("--release");
}

let status = Command::new("cargo").args(args).status()?;
let status = Command::new(&runner).args(args).status()?;
if !status.success() {
return Err(anyhow::anyhow!(format!(
"Result of `cargo build` operation was unsuccessful: {}",
status
"Result of `{} build` operation was unsuccessful: {}",
runner, status
)));
}

Expand Down
28 changes: 24 additions & 4 deletions tooling/cli.rs/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ subcommands:
about: Tauri dev.
setting: TrailingVarArg
args:
- runner:
short: r
long: runner
about: binary to use to run the application
takes_value: true
- config:
short: c
long: config
Expand All @@ -19,13 +24,23 @@ subcommands:
short: e
long: exit-on-panic
about: Exit on panic
- target:
short: t
long: target
about: target triple to build against
multiple: true
- args:
about: Args passed to the binary
index: 1
multiple: true
- build:
about: Tauri build.
args:
- runner:
short: r
long: runner
about: binary to use to build the application
takes_value: true
- debug:
short: d
long: debug
Expand All @@ -34,17 +49,22 @@ subcommands:
short: v
long: verbose
about: Enables verbose logging
- target:
short: t
long: target
about: list of target triples to build against
- bundle:
short: b
long: bundle
about: list of bundles to package
takes_value: true
multiple: true
- config:
short: c
long: config
about: config JSON to merge with tauri.conf.json
takes_value: true
- target:
short: t
long: target
about: target triple to build against
multiple: true
- sign:
about: Tauri updates signer.
args:
Expand Down
39 changes: 34 additions & 5 deletions tooling/cli.rs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fn kill_before_dev_process() {

#[derive(Default)]
pub struct Dev {
runner: Option<String>,
target: Option<String>,
exit_on_panic: bool,
config: Option<String>,
args: Vec<String>,
Expand All @@ -44,6 +46,16 @@ impl Dev {
Default::default()
}

pub fn runner(mut self, runner: String) -> Self {
self.runner.replace(runner);
self
}

pub fn target(mut self, target: String) -> Self {
self.target.replace(target);
self
}

pub fn config(mut self, config: String) -> Self {
self.config.replace(config);
self
Expand Down Expand Up @@ -101,13 +113,26 @@ impl Dev {
.build
.dev_path
.to_string();
let runner_from_config = config
.lock()
.unwrap()
.as_ref()
.unwrap()
.build
.runner
.clone();
let runner = self
.runner
.clone()
.or(runner_from_config)
.unwrap_or_else(|| "cargo".to_string());

rewrite_manifest(config.clone())?;

let (child_wait_tx, child_wait_rx) = channel();
let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));

process = self.start_app(child_wait_rx.clone());
process = self.start_app(&runner, child_wait_rx.clone());

let (tx, rx) = channel();

Expand Down Expand Up @@ -149,20 +174,24 @@ impl Dev {
// So the app should only be started when a file other than tauri.conf.json is changed
let _ = child_wait_tx.send(());
process.kill()?;
process = self.start_app(child_wait_rx.clone());
process = self.start_app(&runner, child_wait_rx.clone());
}
}
}
}
}

fn start_app(&self, child_wait_rx: Arc<Mutex<Receiver<()>>>) -> Arc<SharedChild> {
let mut command = Command::new("cargo");
fn start_app(&self, runner: &str, child_wait_rx: Arc<Mutex<Receiver<()>>>) -> Arc<SharedChild> {
let mut command = Command::new(runner);
command.args(&["run", "--no-default-features"]);
if let Some(target) = &self.target {
command.args(&["--target", target]);
}
if !self.args.is_empty() {
command.arg("--").args(&self.args);
}
let child = SharedChild::spawn(&mut command).expect("failed to run cargo");
let child =
SharedChild::spawn(&mut command).unwrap_or_else(|_| panic!("failed to run {}", runner));
let child_arc = Arc::new(child);

let child_clone = child_arc.clone();
Expand Down
22 changes: 19 additions & 3 deletions tooling/cli.rs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ fn init_command(matches: &ArgMatches) -> Result<()> {
}

fn dev_command(matches: &ArgMatches) -> Result<()> {
let runner = matches.value_of("runner");
let target = matches.value_of("target");
let exit_on_panic = matches.is_present("exit-on-panic");
let config = matches.value_of("config");
let args: Vec<String> = matches
Expand All @@ -99,6 +101,12 @@ fn dev_command(matches: &ArgMatches) -> Result<()> {

let mut dev_runner = dev::Dev::new().exit_on_panic(exit_on_panic).args(args);

if let Some(runner) = runner {
dev_runner = dev_runner.runner(runner.to_string());
}
if let Some(target) = target {
dev_runner = dev_runner.target(target.to_string());
}
if let Some(config) = config {
dev_runner = dev_runner.config(config.to_string());
}
Expand All @@ -107,20 +115,28 @@ fn dev_command(matches: &ArgMatches) -> Result<()> {
}

fn build_command(matches: &ArgMatches) -> Result<()> {
let runner = matches.value_of("runner");
let target = matches.value_of("target");
let debug = matches.is_present("debug");
let verbose = matches.is_present("verbose");
let targets = matches.values_of_lossy("target");
let bundles = matches.values_of_lossy("bundles");
let config = matches.value_of("config");

let mut build_runner = build::Build::new();
if let Some(runner) = runner {
build_runner = build_runner.runner(runner.to_string());
}
if let Some(target) = target {
build_runner = build_runner.target(target.to_string());
}
if debug {
build_runner = build_runner.debug();
}
if verbose {
build_runner = build_runner.verbose();
}
if let Some(targets) = targets {
build_runner = build_runner.targets(targets);
if let Some(bundles) = bundles {
build_runner = build_runner.bundles(bundles);
}
if let Some(config) = config {
build_runner = build_runner.config(config.to_string());
Expand Down

0 comments on commit 5c1fe52

Please sign in to comment.