tauri-cli 3.0.0-alpha.2

Command line interface for building Tauri apps
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{
  VersionMetadata,
  helpers::{
    framework::{Framework, infer_from_package_json as infer_framework},
    npm::PackageManager,
    prompts, resolve_tauri_path, template,
  },
};
use std::{
  collections::BTreeMap,
  env::current_dir,
  fs::{read_to_string, remove_dir_all},
  io::IsTerminal,
  path::{Path, PathBuf},
};

use crate::{
  Result,
  error::{Context, ErrorExt},
};
use clap::Parser;
use handlebars::{Handlebars, to_json};
use include_dir::{Dir, include_dir};

const TEMPLATE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/templates/app");
const TAURI_CONF_TEMPLATE: &str = include_str!("../templates/tauri.conf.json");

#[derive(Debug, Parser)]
#[clap(about = "Initialize a Tauri project in an existing directory")]
pub struct Options {
  /// Skip prompting for values
  #[clap(long, env = "CI")]
  ci: bool,
  /// Force init to overwrite the src-tauri folder
  #[clap(short, long)]
  force: bool,
  /// Enables logging
  #[clap(short, long)]
  log: bool,
  /// Set target directory for init
  #[clap(short, long)]
  #[clap(default_value_t = current_dir().expect("failed to read cwd").display().to_string())]
  directory: String,
  /// Path of the Tauri project to use (relative to the cwd)
  #[clap(short, long)]
  tauri_path: Option<PathBuf>,
  /// Name of your Tauri application
  #[clap(short = 'A', long)]
  app_name: Option<String>,
  /// Window title of your Tauri application
  #[clap(short = 'W', long)]
  window_title: Option<String>,
  /// Web assets location, relative to <project-dir>/src-tauri
  #[clap(short = 'D', long)]
  frontend_dist: Option<String>,
  /// Url of your dev server
  #[clap(short = 'P', long)]
  dev_url: Option<String>,
  /// A shell command to run before `tauri dev` kicks in.
  #[clap(long)]
  before_dev_command: Option<String>,
  /// A shell command to run before `tauri build` kicks in.
  #[clap(long)]
  before_build_command: Option<String>,
}

#[derive(Default)]
struct InitDefaults {
  app_name: Option<String>,
  framework: Option<Framework>,
}

impl Options {
  fn load(mut self) -> Result<Self> {
    if !std::io::stdin().is_terminal() {
      self.ci = true;
    }
    let package_json_path = PathBuf::from(&self.directory).join("package.json");

    let init_defaults = if package_json_path.exists() {
      let package_json_text =
        read_to_string(&package_json_path).fs_context("failed to read", &package_json_path)?;
      let package_json: crate::PackageJson =
        serde_json::from_str(&package_json_text).context("failed to parse JSON")?;
      let (framework, _) = infer_framework(&package_json_text);
      InitDefaults {
        app_name: package_json.product_name.or(package_json.name),
        framework,
      }
    } else {
      Default::default()
    };

    self.app_name = self.app_name.map(|s| Ok(Some(s))).unwrap_or_else(|| {
      prompts::input(
        "What is your app name?",
        Some(
          init_defaults
            .app_name
            .clone()
            .unwrap_or_else(|| "Tauri App".to_string()),
        ),
        self.ci,
        true,
      )
    })?;

    self.window_title = self.window_title.map(|s| Ok(Some(s))).unwrap_or_else(|| {
      prompts::input(
        "What should the window title be?",
        Some(
          init_defaults
            .app_name
            .clone()
            .unwrap_or_else(|| "Tauri".to_string()),
        ),
        self.ci,
        true,
      )
    })?;

    self.frontend_dist = self.frontend_dist.map(|s| Ok(Some(s))).unwrap_or_else(|| prompts::input(
      r#"Where are your web assets (HTML/CSS/JS) located, relative to the "<current dir>/src-tauri/tauri.conf.json" file that will be created?"#,
      init_defaults.framework.as_ref().map(|f| f.frontend_dist()),
      self.ci,
      false,
    ))?;

    self.dev_url = self.dev_url.map(|s| Ok(Some(s))).unwrap_or_else(|| {
      prompts::input(
        "What is the url of your dev server?",
        init_defaults.framework.map(|f| f.dev_url()),
        self.ci,
        true,
      )
    })?;

    let detected_package_manager = PackageManager::from_project(&self.directory);

    self.before_dev_command = self
      .before_dev_command
      .map(|s| Ok(Some(s)))
      .unwrap_or_else(|| {
        prompts::input(
          "What command should Tauri run before `tauri dev` to start your frontend? (leave empty if not needed)",
          Some(default_dev_command(detected_package_manager).into()),
          self.ci,
          true,
        )
      })?;

    self.before_build_command = self
      .before_build_command
      .map(|s| Ok(Some(s)))
      .unwrap_or_else(|| {
        prompts::input(
          "What command should Tauri run before `tauri build` to build your frontend? (leave empty if not needed)",
          Some(default_build_command(detected_package_manager).into()),
          self.ci,
          true,
        )
      })?;

    Ok(self)
  }
}

fn default_dev_command(pm: PackageManager) -> &'static str {
  match pm {
    PackageManager::Yarn => "yarn dev",
    PackageManager::YarnBerry => "yarn dev",
    PackageManager::Npm => "npm run dev",
    PackageManager::Pnpm => "pnpm dev",
    PackageManager::Bun => "bun dev",
    PackageManager::Deno => "deno task dev",
  }
}

fn default_build_command(pm: PackageManager) -> &'static str {
  match pm {
    PackageManager::Yarn => "yarn build",
    PackageManager::YarnBerry => "yarn build",
    PackageManager::Npm => "npm run build",
    PackageManager::Pnpm => "pnpm build",
    PackageManager::Bun => "bun build",
    PackageManager::Deno => "deno task build",
  }
}

pub fn command(mut options: Options) -> Result<()> {
  options = options.load()?;

  let template_target_path = PathBuf::from(&options.directory).join("src-tauri");
  let metadata = serde_json::from_str::<VersionMetadata>(include_str!("../metadata-v2.json"))
    .context("failed to parse version metadata")?;

  if template_target_path.exists() && !options.force {
    log::warn!(
      "Tauri dir ({:?}) not empty. Run `init --force` to overwrite.",
      template_target_path
    );
  } else {
    let _ = remove_dir_all(&template_target_path);
    let mut handlebars = Handlebars::new();
    handlebars.register_escape_fn(handlebars::no_escape);

    let mut data = tauri_dependencies_data(options.tauri_path.as_deref(), &metadata);
    data.insert(
      "frontend_dist",
      to_json(options.frontend_dist.as_deref().unwrap_or("../dist")),
    );
    data.insert("dev_url", to_json(options.dev_url));
    data.insert(
      "app_name",
      to_json(options.app_name.as_deref().unwrap_or("Tauri App")),
    );
    data.insert(
      "window_title",
      to_json(options.window_title.as_deref().unwrap_or("Tauri")),
    );
    data.insert("before_dev_command", to_json(options.before_dev_command));
    data.insert(
      "before_build_command",
      to_json(options.before_build_command),
    );

    let mut config = serde_json::from_str(
      &handlebars
        .render_template(TAURI_CONF_TEMPLATE, &data)
        .expect("Failed to render tauri.conf.json template"),
    )
    .unwrap();
    if option_env!("TARGET") == Some("node") {
      let mut dir = current_dir().expect("failed to read cwd");
      let mut count = 0;
      let mut cli_node_module_path = None;
      let cli_path = "node_modules/@tauri-apps/cli";

      // only go up three folders max
      while count <= 2 {
        let test_path = dir.join(cli_path);
        if test_path.exists() {
          let mut node_module_path = PathBuf::from("..");
          for _ in 0..count {
            node_module_path.push("..");
          }
          node_module_path.push(cli_path);
          node_module_path.push("config.schema.json");
          cli_node_module_path.replace(node_module_path);
          break;
        }
        count += 1;
        match dir.parent() {
          Some(parent) => {
            dir = parent.to_path_buf();
          }
          None => break,
        }
      }

      if let Some(cli_node_module_path) = cli_node_module_path {
        let mut map = serde_json::Map::default();
        map.insert(
          "$schema".into(),
          serde_json::Value::String(
            cli_node_module_path
              .display()
              .to_string()
              .replace('\\', "/"),
          ),
        );
        let merge_config = serde_json::Value::Object(map);
        json_patch::merge(&mut config, &merge_config);
      }
    }

    data.insert(
      "tauri_config",
      to_json(serde_json::to_string_pretty(&config).unwrap()),
    );

    template::render(&handlebars, &data, &TEMPLATE_DIR, &options.directory)
      .with_context(|| "failed to render Tauri template")?;
  }

  Ok(())
}

/// Builds the template variables for the Tauri crate dependencies of the generated `Cargo.toml`.
///
/// `tauri_dep`, `tauri_build_dep` and `tauri_runtime_wry_dep` are always set.
///
/// When `tauri_path` is provided the dependencies point to the crates inside that directory
/// and `patch_tauri_dep`, `tauri_utils_dep` and `tauri_plugin_dep` are also set so the template
/// renders a `[patch.crates-io]` section overriding the Tauri crates pulled in by other dependencies.
/// Without a path the template never renders that section, so those variables are not needed.
fn tauri_dependencies_data(
  tauri_path: Option<&Path>,
  metadata: &VersionMetadata,
) -> BTreeMap<&'static str, serde_json::Value> {
  let mut data = BTreeMap::new();
  if let Some(tauri_path) = tauri_path {
    let path_dep = |crate_dir: &str| {
      to_json(format!(
        "{{  path = {:?} }}",
        resolve_tauri_path(tauri_path, crate_dir)
      ))
    };
    data.insert("tauri_dep", path_dep("crates/tauri"));
    data.insert("tauri_build_dep", path_dep("crates/tauri-build"));
    data.insert(
      "tauri_runtime_wry_dep",
      path_dep("crates/tauri-runtime-wry"),
    );
    data.insert("patch_tauri_dep", to_json(true));
    data.insert("tauri_utils_dep", path_dep("crates/tauri-utils"));
    data.insert("tauri_plugin_dep", path_dep("crates/tauri-plugin"));
  } else {
    data.insert(
      "tauri_dep",
      to_json(format!(r#"{{ version = "{}" }}"#, metadata.tauri)),
    );
    data.insert(
      "tauri_build_dep",
      to_json(format!(r#"{{ version = "{}" }}"#, metadata.tauri_build)),
    );
    data.insert(
      "tauri_runtime_wry_dep",
      to_json(format!(
        r#"{{ version = "{}" }}"#,
        metadata.tauri_runtime_wry
      )),
    );
  }
  data
}

#[cfg(test)]
mod tests {
  use super::*;

  /// The version metadata shipped with the CLI, the same source `command` uses.
  fn metadata() -> VersionMetadata {
    serde_json::from_str(include_str!("../metadata-v2.json"))
      .expect("failed to parse version metadata")
  }

  /// Renders the app `Cargo.toml` template with the same handlebars setup used by `command`.
  fn render_manifest(data: &BTreeMap<&'static str, serde_json::Value>) -> toml::Table {
    let template = TEMPLATE_DIR
      .get_file("src-tauri/Cargo.crate-manifest")
      .expect("app template is missing src-tauri/Cargo.crate-manifest")
      .contents_utf8()
      .expect("Cargo manifest template is not UTF-8");

    let mut handlebars = Handlebars::new();
    handlebars.register_escape_fn(handlebars::no_escape);
    let rendered = handlebars
      .render_template(template, data)
      .expect("failed to render Cargo manifest template");

    assert!(
      !rendered.contains("{{") && !rendered.contains("}}"),
      "rendered manifest contains handlebars braces:\n{rendered}"
    );

    toml::from_str(&rendered)
      .unwrap_or_else(|e| panic!("rendered manifest is not valid TOML: {e}\n{rendered}"))
  }

  fn dependency<'a>(manifest: &'a toml::Table, section: &str, name: &str) -> &'a toml::Table {
    manifest
      .get(section)
      .and_then(|s| s.get(name))
      .and_then(|d| d.as_table())
      .unwrap_or_else(|| panic!("[{section}] is missing the `{name}` table"))
  }

  fn assert_path_dependency(dep: &toml::Table, tauri_path: &Path, crate_dir: &str) {
    assert!(
      !dep.contains_key("version"),
      "path dependency must not carry a version: {dep:?}"
    );
    let path = dep
      .get("path")
      .and_then(|p| p.as_str())
      .unwrap_or_else(|| panic!("dependency is missing a string `path`: {dep:?}"));
    assert_eq!(Path::new(path), resolve_tauri_path(tauri_path, crate_dir));
  }

  fn assert_manifest_uses_path(manifest: &toml::Table, tauri_path: &Path) {
    assert_path_dependency(
      dependency(manifest, "dependencies", "tauri"),
      tauri_path,
      "crates/tauri",
    );
    assert_path_dependency(
      dependency(manifest, "build-dependencies", "tauri-build"),
      tauri_path,
      "crates/tauri-build",
    );
    assert_path_dependency(
      dependency(manifest, "dependencies", "tauri-runtime-wry"),
      tauri_path,
      "crates/tauri-runtime-wry",
    );

    let patch = manifest
      .get("patch")
      .and_then(|p| p.get("crates-io"))
      .and_then(|p| p.as_table())
      .expect("[patch.crates-io] must be rendered for path dependencies");
    assert_eq!(
      patch.len(),
      4,
      "unexpected [patch.crates-io] entries: {patch:?}"
    );
    for (name, crate_dir) in [
      ("tauri", "crates/tauri"),
      ("tauri-utils", "crates/tauri-utils"),
      ("tauri-plugin", "crates/tauri-plugin"),
      ("tauri-runtime-wry", "crates/tauri-runtime-wry"),
    ] {
      let dep = patch
        .get(name)
        .and_then(|d| d.as_table())
        .unwrap_or_else(|| panic!("[patch.crates-io] is missing `{name}`"));
      assert_path_dependency(dep, tauri_path, crate_dir);
    }
  }

  #[test]
  fn version_dependencies() {
    let metadata = metadata();
    let data = tauri_dependencies_data(None, &metadata);
    assert!(
      !data.contains_key("patch_tauri_dep"),
      "patch_tauri_dep must only be set for path dependencies"
    );

    let manifest = render_manifest(&data);

    let tauri = dependency(&manifest, "dependencies", "tauri");
    assert_eq!(
      tauri.get("version").and_then(|v| v.as_str()),
      Some(metadata.tauri.as_str())
    );
    assert!(!tauri.contains_key("path"));

    let tauri_build = dependency(&manifest, "build-dependencies", "tauri-build");
    assert_eq!(
      tauri_build.get("version").and_then(|v| v.as_str()),
      Some(metadata.tauri_build.as_str())
    );
    assert!(!tauri_build.contains_key("path"));

    let tauri_runtime_wry = dependency(&manifest, "dependencies", "tauri-runtime-wry");
    assert_eq!(
      tauri_runtime_wry.get("version").and_then(|v| v.as_str()),
      Some(metadata.tauri_runtime_wry.as_str())
    );
    assert!(!tauri_runtime_wry.contains_key("path"));

    assert!(
      !manifest.contains_key("patch"),
      "[patch.crates-io] must not be rendered for version dependencies"
    );
  }

  #[test]
  fn relative_path_dependencies() {
    let tauri_path = Path::new("tauri-src");
    let manifest = render_manifest(&tauri_dependencies_data(Some(tauri_path), &metadata()));
    assert_manifest_uses_path(&manifest, tauri_path);
  }

  #[test]
  fn absolute_path_dependencies() {
    // absolute paths are written as-is; on Windows this covers backslash escaping
    let tauri_path = std::env::current_dir().unwrap().join("tauri-src");
    assert!(tauri_path.is_absolute());
    let manifest = render_manifest(&tauri_dependencies_data(Some(&tauri_path), &metadata()));
    assert_manifest_uses_path(&manifest, &tauri_path);
  }
}