Browse Source

fix(cli.rs): distDir validation

Lucas Nogueira 3 years ago
parent
commit
26dab690b7

+ 3 - 0
tooling/cli.rs/Cargo.lock

@@ -1679,6 +1679,7 @@ dependencies = [
  "schemars_derive",
  "serde",
  "serde_json",
+ "url",
 ]
 
 [[package]]
@@ -2007,6 +2008,7 @@ dependencies = [
  "toml_edit",
  "unicode-width",
  "ureq",
+ "url",
  "valico",
  "zeroize",
 ]
@@ -2231,6 +2233,7 @@ dependencies = [
  "idna",
  "matches",
  "percent-encoding",
+ "serde",
 ]
 
 [[package]]

+ 4 - 2
tooling/cli.rs/Cargo.toml

@@ -30,7 +30,7 @@ notify = "4.0"
 shared_child = "1.0"
 toml_edit = "0.10"
 json-patch = "0.2"
-schemars = "0.8"
+schemars = { version = "0.8", features = ["url"] }
 toml = "0.5"
 valico = "3.6"
 handlebars = "4.1"
@@ -50,6 +50,7 @@ zeroize = "1.4"
 glob = "0.3"
 heck = "0.3"
 dialoguer = "0.9"
+url = { version = "2.2", features = [ "serde" ] }
 
 [target."cfg(windows)".dependencies]
 encode_unicode = "0.3"
@@ -58,7 +59,8 @@ encode_unicode = "0.3"
 heck = "0.3"
 
 [build-dependencies]
-schemars = "0.8"
+schemars = { version = "0.8", features = ["url"] }
 serde = { version = "1.0", features = [ "derive" ] }
 serde_json = "1.0"
 serde_with = "1.11"
+url = { version = "2.2", features = [ "serde" ] }

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

@@ -10,6 +10,7 @@ use schemars::JsonSchema;
 use serde::{Deserialize, Serialize};
 use serde_json::Value as JsonValue;
 use serde_with::skip_serializing_none;
+use url::Url;
 
 use std::{collections::HashMap, path::PathBuf};
 
@@ -786,12 +787,38 @@ fn default_dialog() -> Option<bool> {
   Some(true)
 }
 
+/// The window webview URL options.
+#[derive(PartialEq, Debug, Clone, Deserialize, Serialize, JsonSchema)]
+#[serde(untagged)]
+#[non_exhaustive]
+pub enum WindowUrl {
+  /// An external URL.
+  External(Url),
+  /// An app URL.
+  App(PathBuf),
+}
+
+impl Default for WindowUrl {
+  fn default() -> Self {
+    Self::App("index.html".into())
+  }
+}
+
+impl std::fmt::Display for WindowUrl {
+  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+    match self {
+      Self::External(url) => write!(f, "{}", url),
+      Self::App(path) => write!(f, "{}", path.display()),
+    }
+  }
+}
+
 /// The `dev_path` and `dist_dir` options.
 #[derive(Debug, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
 #[serde(untagged, deny_unknown_fields)]
 pub enum AppUrl {
   /// The app's external URL, or the path to the directory containing the app assets.
-  Url(String),
+  Url(WindowUrl),
   /// An array of files to embed on the app.
   Files(Vec<PathBuf>),
 }
@@ -834,11 +861,13 @@ pub struct BuildConfig {
 }
 
 fn default_dev_path() -> AppUrl {
-  AppUrl::Url("".to_string())
+  AppUrl::Url(WindowUrl::External(
+    Url::parse("http://localhost:8080").unwrap(),
+  ))
 }
 
 fn default_dist_dir() -> AppUrl {
-  AppUrl::Url("../dist".to_string())
+  AppUrl::Url(WindowUrl::App(PathBuf::from("../dist")))
 }
 
 /// The tauri.conf.json mapper.

+ 21 - 3
tooling/cli.rs/schema.json

@@ -7,7 +7,7 @@
     "build": {
       "description": "The build configuration.",
       "default": {
-        "devPath": "",
+        "devPath": "http://localhost:8080/",
         "distDir": "../dist",
         "withGlobalTauri": false
       },
@@ -245,7 +245,11 @@
       "anyOf": [
         {
           "description": "The app's external URL, or the path to the directory containing the app assets.",
-          "type": "string"
+          "allOf": [
+            {
+              "$ref": "#/definitions/WindowUrl"
+            }
+          ]
         },
         {
           "description": "An array of files to embed on the app.",
@@ -276,7 +280,7 @@
         },
         "devPath": {
           "description": "The path or URL to use on development.",
-          "default": "",
+          "default": "http://localhost:8080/",
           "allOf": [
             {
               "$ref": "#/definitions/AppUrl"
@@ -1348,6 +1352,20 @@
       },
       "additionalProperties": false
     },
+    "WindowUrl": {
+      "description": "The window webview URL options.",
+      "anyOf": [
+        {
+          "description": "An external URL.",
+          "type": "string",
+          "format": "uri"
+        },
+        {
+          "description": "An app URL.",
+          "type": "string"
+        }
+      ]
+    },
     "WindowsConfig": {
       "type": "object",
       "properties": {

+ 2 - 3
tooling/cli.rs/src/build.rs

@@ -5,7 +5,7 @@
 use crate::helpers::{
   app_paths::{app_dir, tauri_dir},
   command_env,
-  config::{get as get_config, AppUrl},
+  config::{get as get_config, AppUrl, WindowUrl},
   execute_with_output,
   manifest::rewrite_manifest,
   updater_signature::sign_file_from_env_variables,
@@ -90,8 +90,7 @@ pub fn command(options: Options) -> Result<()> {
     }
   }
 
-  if let AppUrl::Url(url) = &config_.build.dist_dir {
-    let web_asset_path = PathBuf::from(url);
+  if let AppUrl::Url(WindowUrl::App(web_asset_path)) = &config_.build.dist_dir {
     if !web_asset_path.exists() {
       return Err(anyhow::anyhow!(
           "Unable to find your web assets, did you forget to build your web app? Your distDir is set to \"{:?}\".",