Skip to content

Commit

Permalink
feat(config): add $schema to tauri.conf.json, closes #3464 (#4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 3, 2022
1 parent ec79e07 commit 715cbde
Show file tree
Hide file tree
Showing 31 changed files with 173 additions and 70 deletions.
7 changes: 7 additions & 0 deletions .changes/config-$schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri-utils": patch
"cli.rs": patch
"cli.js": patch
---

Added `$schema` support to `tauri.conf.json`.
6 changes: 5 additions & 1 deletion core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,9 @@ impl PackageConfig {
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Config {
/// The JSON schema for the Tauri config.
#[serde(rename = "$schema")]
pub schema: Option<String>,
/// Package settings.
#[serde(default)]
pub package: PackageConfig,
Expand Down Expand Up @@ -2887,12 +2890,13 @@ mod build {

impl ToTokens for Config {
fn to_tokens(&self, tokens: &mut TokenStream) {
let schema = quote!(None);
let package = &self.package;
let tauri = &self.tauri;
let build = &self.build;
let plugins = &self.plugins;

literal_struct!(tokens, Config, package, tauri, build, plugins);
literal_struct!(tokens, Config, schema, package, tauri, build, plugins);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn noop_assets() -> NoopAsset {
pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
crate::Context {
config: Config {
schema: None,
package: Default::default(),
tauri: TauriConfig {
pattern: PatternKind::Brownfield,
Expand Down
1 change: 1 addition & 0 deletions core/tauri/test/fixture/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../../../tooling/cli/schema.json",
"build": {
"distDir": "../dist",
"devPath": "http://localhost:4000"
Expand Down
1 change: 1 addition & 0 deletions core/tests/app-updater/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": [],
"devPath": []
Expand Down
1 change: 1 addition & 0 deletions examples/api/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": "../dist",
"devPath": "http://localhost:5000",
Expand Down
1 change: 1 addition & 0 deletions examples/commands/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/helloworld/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/isolation/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"package": {
"productName": "isolation",
"version": "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions examples/multiwindow/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/navigation/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": "public",
"devPath": "public",
Expand Down
1 change: 1 addition & 0 deletions examples/parent-window/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/resources/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": ["../index.html"],
"devPath": ["../index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/sidecar/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": ["../index.html"],
"devPath": ["../index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/splashscreen/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": "dist",
"devPath": "dist",
Expand Down
1 change: 1 addition & 0 deletions examples/state/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/streaming/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../tooling/cli/schema.json",
"build": {
"distDir": ["index.html"],
"devPath": ["index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/tauri-dynamic-lib/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": ["src/index.html"],
"devPath": ["src/index.html"],
Expand Down
1 change: 1 addition & 0 deletions examples/updater/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../tooling/cli/schema.json",
"build": {
"distDir": ["../index.html"],
"devPath": ["../index.html"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../../../tooling/cli/schema.json",
"build": {
"distDir": "../public",
"devPath": "../public",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../../../tooling/cli/schema.json",
"build": {
"distDir": "../public",
"devPath": "../public",
Expand Down
1 change: 1 addition & 0 deletions tooling/bench/tests/helloworld/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../../../tooling/cli/schema.json",
"build": {
"distDir": "../public",
"devPath": "../public",
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/node/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Automatically generated
/*.node
/*.node
schema.json
6 changes: 4 additions & 2 deletions tooling/cli/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"devDependencies": {
"@napi-rs/cli": "2.7.0",
"cross-env": "7.0.3",
"cross-spawn": "7.0.3",
"fs-extra": "10.1.0",
"jest": "28.0.3",
Expand All @@ -52,9 +53,10 @@
},
"scripts": {
"artifacts": "napi artifacts",
"build:release": "napi build --platform --release",
"build": "napi build --platform",
"build:release": "cross-env TARGET=node napi build --platform --release",
"build": "cross-env TARGET=node napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"prepack": "cp ../schema.json .",
"test": "jest --runInBand --forceExit --no-cache",
"version": "napi version",
"tauri": "node ./tauri.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../../../../../../../../tooling/cli/schema.json",
"build": {
"devPath": "../dist",
"distDir": "../dist",
Expand Down
9 changes: 8 additions & 1 deletion tooling/cli/node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,14 @@ convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
dependencies:
safe-buffer "~5.1.1"

cross-spawn@7.0.3, cross-spawn@^7.0.3:
cross-env@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
dependencies:
cross-spawn "^7.0.1"

cross-spawn@7.0.3, cross-spawn@^7.0.1, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
"description": "The config type mapped to `tauri.conf.json`.",
"type": "object",
"properties": {
"$schema": {
"description": "The JSON schema for the Tauri config.",
"type": [
"string",
"null"
]
},
"build": {
"description": "The build configuration.",
"default": {
Expand Down
51 changes: 51 additions & 0 deletions tooling/cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use include_dir::{include_dir, Dir};
use serde::Deserialize;

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

#[derive(Debug, Parser)]
#[clap(about = "Initializes a Tauri project")]
Expand Down Expand Up @@ -185,6 +186,56 @@ pub fn command(mut options: Options) -> Result<()> {
to_json(options.window_title.unwrap_or_else(|| "Tauri".to_string())),
);

let mut config = serde_json::from_str(
&handlebars
.render_template(TAURI_CONF_TEMPLATE, &data)
.expect("Failed to render tauri.conf.json template"),
)
.unwrap();
if crate::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("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()),
);
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")?;
}
Expand Down
2 changes: 2 additions & 0 deletions tooling/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use clap::{FromArgMatches, IntoApp, Parser, Subcommand};

use std::ffi::OsString;

const TARGET: Option<&str> = option_env!("TARGET");

pub(crate) trait CommandExt {
fn pipe(&mut self) -> Result<&mut Self>;
}
Expand Down
66 changes: 1 addition & 65 deletions tooling/cli/templates/app/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,65 +1 @@
{
"package": {
"productName": "{{ app_name }}",
"version": "0.1.0"
},
"build": {
"distDir": "{{ dist_dir }}",
"devPath": "{{ dev_path }}",
"beforeDevCommand": "",
"beforeBuildCommand": ""
},
"tauri": {
"bundle": {
"active": true,
"targets": "all",
"identifier": "com.tauri.dev",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"resources": [],
"externalBin": [],
"copyright": "",
"category": "DeveloperTool",
"shortDescription": "",
"longDescription": "",
"deb": {
"depends": []
},
"macOS": {
"frameworks": [],
"exceptionDomain": "",
"signingIdentity": null,
"providerShortName": null,
"entitlements": null
},
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"updater": {
"active": false
},
"allowlist": {
"all": true
},
"windows": [
{
"title": "{{ window_title }}",
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
}
],
"security": {
"csp": null
}
}
}
{{{ tauri_config }}}
Loading

0 comments on commit 715cbde

Please sign in to comment.