Explorar el Código

feat(cli.rs): allow using cross instead of cargo, add target triple arg (#1664)

Lucas Fernandes Nogueira hace 4 años
padre
commit
5c1fe52c2b

+ 5 - 0
.changes/cli-runner-arg.md

@@ -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 - 0
.changes/cli-target-triple.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Adds `--target [TARGET_TRIPLE]` option to the `build` command (example: `--target arm-unknown-linux-gnueabihf`).

+ 5 - 0
.changes/cli-targets-refactor.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Rename `--target` option on the `build` command to `--bundle`.

+ 1 - 1
examples/api/src-tauri/Cargo.toml

@@ -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" ]

+ 2 - 2
examples/helloworld/src-tauri/Cargo.toml

@@ -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" ]

+ 1 - 1
examples/multiwindow/src-tauri/Cargo.toml

@@ -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" ]

+ 2 - 2
examples/updater/src-tauri/Cargo.toml

@@ -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" ]

+ 3 - 0
tooling/cli.rs/config_definition.rs

@@ -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,
@@ -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,

+ 7 - 0
tooling/cli.rs/schema.json

@@ -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,

+ 23 - 5
tooling/cli.rs/src/build.rs

@@ -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>,
 }
 
@@ -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
   }
 
@@ -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_)?;
 
@@ -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" {

+ 9 - 4
tooling/cli.rs/src/build/rust.rs

@@ -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
     )));
   }
 

+ 24 - 4
tooling/cli.rs/src/cli.yml

@@ -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
@@ -19,6 +24,11 @@ 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
@@ -26,6 +36,11 @@ subcommands:
         - 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
@@ -34,10 +49,10 @@ 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:
@@ -45,6 +60,11 @@ subcommands:
                     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:

+ 34 - 5
tooling/cli.rs/src/dev.rs

@@ -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>,
@@ -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
@@ -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();
 
@@ -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();

+ 19 - 3
tooling/cli.rs/src/main.rs

@@ -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
@@ -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());
   }
@@ -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());