Skip to content

Commit

Permalink
feat(core): validate devPath and distDir values (#1848)
Browse files Browse the repository at this point in the history
* feat(core): validate `devPath` and `distDir` values

* fix tests
  • Loading branch information
lucasfernog authored May 17, 2021
1 parent e08065d commit e97846a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .changes/dev-path-dist-dir-validation.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 25 additions & 9 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
32 changes: 21 additions & 11 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
};

Expand All @@ -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);
}
Expand Down
14 changes: 8 additions & 6 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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/");
}
}

Expand Down

0 comments on commit e97846a

Please sign in to comment.