feat(core): validate devPath and distDir values (#1848)

* feat(core): validate `devPath` and `distDir` values

* fix tests
This commit is contained in:
Lucas Fernandes Nogueira 2021-05-17 14:47:05 -03:00 committed by GitHub
parent e08065d7fe
commit e97846aae9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 26 deletions

View File

@ -0,0 +1,7 @@
---
"tauri": patch
"tauri-utils": patch
"tauri-codegen": patch
---
Validate `tauri.conf.json > build > devPath` and `tauri.conf.json > build > distDir` values.

View File

@ -6,7 +6,7 @@ use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError};
use proc_macro2::TokenStream;
use quote::quote;
use std::path::PathBuf;
use tauri_utils::config::Config;
use tauri_utils::config::{Config, WindowUrl};
/// Necessary data needed by [`context_codegen`] to generate code for a Tauri application context.
pub struct ContextData {
@ -24,15 +24,31 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
config_parent,
root,
} = data;
let assets_path = if dev {
// if dev_path is a dev server, we don't have any assets to embed
if config.build.dev_path.starts_with("http") {
None
} else {
Some(config_parent.join(&config.build.dev_path))
}
let app_url = if dev {
&config.build.dev_path
} else {
Some(config_parent.join(&config.build.dist_dir))
&config.build.dist_dir
};
let assets_path = match app_url {
WindowUrl::External(_) => None,
WindowUrl::App(path) => {
if path.components().count() == 0 {
panic!(
"The `{}` configuration cannot be empty",
if dev { "devPath" } else { "distDir" }
)
}
let assets_path = config_parent.join(path);
if !assets_path.exists() {
panic!(
"The `{}` configuration is set to `{:?}` but this path doesn't exist",
if dev { "devPath" } else { "distDir" },
path
)
}
Some(assets_path)
}
_ => unimplemented!(),
};
// generate the assets inside the dist dir into a perfect hash function

View File

@ -2,6 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
//! The Tauri configuration used at runtime.
//! It is pulled from a `tauri.conf.json` file and the [`Config`] struct is generated at compile time.
//!
//! # Stability
//! This is a core functionality that is not considered part of the stable API.
//! If you use it, note that it may include breaking changes in the future.
use std::{collections::HashMap, path::PathBuf};
use serde::Deserialize;
@ -384,21 +391,21 @@ impl Default for TauriConfig {
pub struct BuildConfig {
/// the devPath config.
#[serde(default = "default_dev_path")]
pub dev_path: String,
pub dev_path: WindowUrl,
/// the dist config.
#[serde(default = "default_dist_path")]
pub dist_dir: String,
pub dist_dir: WindowUrl,
/// Whether we should inject the Tauri API on `window.__TAURI__` or not.
#[serde(default)]
pub with_global_tauri: bool,
}
fn default_dev_path() -> String {
"http://localhost:8080".to_string()
fn default_dev_path() -> WindowUrl {
WindowUrl::External(Url::parse("http://localhost:8080").unwrap())
}
fn default_dist_path() -> String {
"../dist".to_string()
fn default_dist_path() -> WindowUrl {
WindowUrl::App("../dist".into())
}
impl Default for BuildConfig {
@ -762,8 +769,8 @@ mod build {
impl ToTokens for BuildConfig {
fn to_tokens(&self, tokens: &mut TokenStream) {
let dev_path = str_lit(&self.dev_path);
let dist_dir = str_lit(&self.dist_dir);
let dev_path = &self.dev_path;
let dist_dir = &self.dist_dir;
let with_global_tauri = self.with_global_tauri;
literal_struct!(tokens, BuildConfig, dev_path, dist_dir, with_global_tauri);
@ -915,8 +922,8 @@ mod test {
// create a build config
let build = BuildConfig {
dev_path: String::from("http://localhost:8080"),
dist_dir: String::from("../dist"),
dev_path: WindowUrl::External(Url::parse("http://localhost:8080").unwrap()),
dist_dir: WindowUrl::App("../dist".into()),
with_global_tauri: false,
};
@ -925,7 +932,10 @@ mod test {
assert_eq!(b_config, build);
assert_eq!(d_bundle, tauri.bundle);
assert_eq!(d_updater, tauri.updater);
assert_eq!(d_path, String::from("http://localhost:8080"));
assert_eq!(
d_path,
WindowUrl::External(Url::parse("http://localhost:8080").unwrap())
);
assert_eq!(d_title, tauri.windows[0].title);
assert_eq!(d_windows, tauri.windows);
}

View File

@ -239,16 +239,18 @@ impl<P: Params> WindowManager<P> {
// setup content for dev-server
#[cfg(dev)]
fn get_url(&self) -> String {
if self.inner.config.build.dev_path.starts_with("http") {
self.inner.config.build.dev_path.clone()
} else {
"tauri://localhost".into()
match &self.inner.config.build.dev_path {
WindowUrl::External(url) => url.to_string(),
_ => "tauri://localhost".into(),
}
}
#[cfg(custom_protocol)]
fn get_url(&self) -> String {
"tauri://localhost".into()
match &self.inner.config.build.dist_dir {
WindowUrl::External(url) => url.to_string(),
_ => "tauri://localhost".into(),
}
}
fn prepare_pending_window(
@ -505,7 +507,7 @@ mod test {
assert_eq!(manager.get_url(), "tauri://localhost");
#[cfg(dev)]
assert_eq!(manager.get_url(), manager.config().build.dev_path);
assert_eq!(manager.get_url(), "http://localhost:4000/");
}
}