Skip to content

Commit

Permalink
fix(cli): CLI path issues on mobile project initialization (#9009)
Browse files Browse the repository at this point in the history
* fix(cli): fix panic when `android init` using cargo or yarn

closes #8531

* clippy

* try with fullpath

* clippy

* move cli

* Update test-android.yml

* add to path instead

* clippy

* try moving

* use cargo subcommand

* delete unused logic [skip ci]

* truncate on init [skip ci]

* enhance binary/args check

* update change files

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Feb 28, 2024
1 parent cb92cfd commit f5f3ed5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 128 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-mobile-init-partition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri-cli': 'patch:bug'
'@tauri-apps/cli': 'patch:bug'
---

Fixes Android and iOS project initialization when the Tauri CLI is on a different disk partition.
12 changes: 10 additions & 2 deletions .github/workflows/test-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ jobs:
- name: build CLI
run: cargo build --manifest-path ./tooling/cli/Cargo.toml

- name: move CLI to cargo bin dir
if: matrix.os != "windows-latest"
run: mv ./tooling/cli/target/debug/cargo-tauri $HOME/.cargo/bin

- name: move CLI to cargo bin dir
if: matrix.os == "windows-latest"
run: mv ./tooling/cli/target/debug/cargo-tauri.exe $HOME/.cargo/bin

- name: build Tauri API
working-directory: ./tooling/api
Expand All @@ -95,12 +103,12 @@ jobs:

- name: init Android Studio project
working-directory: ./examples/api
run: ../../tooling/cli/target/debug/cargo-tauri android init
run: cargo tauri android init
env:
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}

- name: build APK
working-directory: ./examples/api
run: ../../tooling/cli/target/debug/cargo-tauri android build
run: cargo tauri android build
env:
NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
67 changes: 12 additions & 55 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 40 additions & 71 deletions tooling/cli/src/mobile/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use super::{get_app, Target};
use crate::{
helpers::{app_paths::tauri_dir, config::get as get_tauri_config, template::JsonMap},
helpers::{config::get as get_tauri_config, template::JsonMap},
interface::{AppInterface, Interface},
Result,
};
Expand All @@ -18,17 +18,13 @@ use cargo_mobile2::{
util::{
self,
cli::{Report, TextWrapper},
relativize_path,
},
};
use handlebars::{
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError, RenderErrorReason,
};

use std::{
env::{current_dir, var, var_os},
path::PathBuf,
};
use std::{env::var_os, path::PathBuf};

pub fn command(
target: Target,
Expand Down Expand Up @@ -87,7 +83,6 @@ pub fn exec(
#[allow(unused_variables)] reinstall_deps: bool,
skip_targets_install: bool,
) -> Result<App> {
let current_dir = current_dir()?;
let tauri_config = get_tauri_config(target.platform_target(), None)?;

let tauri_config_guard = tauri_config.lock().unwrap();
Expand All @@ -97,75 +92,49 @@ pub fn exec(

let (handlebars, mut map) = handlebars(&app);

// the CWD used when the the IDE runs the android-studio-script or the xcode-script
let ide_run_cwd = if target == Target::Android {
tauri_dir()
} else {
tauri_dir().join("gen/apple")
};

let mut args = std::env::args_os();
let mut binary = args

let (binary, mut build_args) = args
.next()
.map(|bin| {
let path = PathBuf::from(&bin);
if path.exists() {
let absolute_path = util::prefix_path(&current_dir, path);
return relativize_path(absolute_path, &ide_run_cwd).into_os_string();
let bin_path = PathBuf::from(&bin);
let mut build_args = vec!["tauri"];

if let Some(bin_stem) = bin_path.file_stem() {
let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
if r.is_match(&bin_stem.to_string_lossy()) {
if let Some(npm_execpath) = var_os("npm_execpath") {
let manager_stem = PathBuf::from(&npm_execpath)
.file_stem()
.unwrap()
.to_os_string();
let is_npm = manager_stem == "npm-cli";
let binary = if is_npm {
"npm".into()
} else if manager_stem == "npx-cli" {
"npx".into()
} else {
manager_stem
};

if is_npm {
build_args.insert(0, "run");
build_args.insert(1, "--");
}

return (binary, build_args);
}
} else if !cfg!(debug_assertions) && bin_stem == "cargo-tauri" {
return (std::ffi::OsString::from("cargo"), build_args);
}
}
bin

(bin, build_args)
})
.unwrap_or_else(|| std::ffi::OsString::from("cargo"));
let mut build_args = Vec::new();
for arg in args {
let path = PathBuf::from(&arg);
if path.exists() {
let absolute_path = util::prefix_path(&current_dir, path);
build_args.push(
relativize_path(absolute_path, &ide_run_cwd)
.to_string_lossy()
.into_owned(),
);
continue;
}
let is_mobile_cmd_arg = arg == "android" || arg == "ios";
build_args.push(arg.to_string_lossy().into_owned());
if is_mobile_cmd_arg {
break;
}
}
build_args.push(target.ide_build_script_name().into());

let binary_path = PathBuf::from(&binary);
let bin_stem = binary_path.file_stem().unwrap().to_string_lossy();
let r = regex::Regex::new("(nodejs|node)\\-?([1-9]*)*$").unwrap();
if r.is_match(&bin_stem) {
if let Some(npm_execpath) = var_os("npm_execpath").map(PathBuf::from) {
let manager_stem = npm_execpath.file_stem().unwrap().to_os_string();
let is_npm = manager_stem == "npm-cli";
let is_npx = manager_stem == "npx-cli";
binary = if is_npm {
"npm".into()
} else if is_npx {
"npx".into()
} else {
manager_stem
};
if !(build_args.is_empty() || is_npx) {
// remove script path, we'll use `npm_lifecycle_event` instead
build_args.remove(0);
}
if is_npm {
build_args.insert(0, "--".into());
}
if !is_npx {
build_args.insert(0, var("npm_lifecycle_event").unwrap());
}
if is_npm {
build_args.insert(0, "run".into());
}
}
}
.unwrap_or_else(|| (std::ffi::OsString::from("cargo"), vec!["tauri"]));

build_args.push(target.command_name());
build_args.push(target.ide_build_script_name());

map.insert("tauri-binary", binary.to_string_lossy());
map.insert("tauri-binary-args", &build_args);
Expand Down

0 comments on commit f5f3ed5

Please sign in to comment.