Skip to content

Commit

Permalink
refactor(core): remove app module (#6895)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 8, 2023
1 parent 2cc282c commit 3245d14
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 285 deletions.
6 changes: 6 additions & 0 deletions .changes/move-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"api": patch
"tauri": patch
---

Moved the `app` feature to its own plugin in the plugins-workspace repository.
6 changes: 3 additions & 3 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions core/tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use serde_json::Value as JsonValue;

use std::sync::Arc;

mod app;
mod event;
#[cfg(os_any)]
mod operating_system;
Expand Down Expand Up @@ -54,7 +53,6 @@ impl<T: Serialize> From<T> for InvokeResponse {
#[derive(Deserialize)]
#[serde(tag = "module", content = "message")]
enum Module {
App(app::Cmd),
#[cfg(process_any)]
Process(process::Cmd),
#[cfg(os_any)]
Expand All @@ -77,12 +75,6 @@ impl Module {
package_info,
};
match self {
Self::App(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(process_any)]
Self::Process(cmd) => resolver.respond_async(async move {
cmd
Expand Down
58 changes: 0 additions & 58 deletions core/tauri/src/endpoints/app.rs

This file was deleted.

3 changes: 3 additions & 0 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ pub use {
scope::*,
};

/// The Tauri version.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg(target_os = "ios")]
#[doc(hidden)]
pub fn log_stdout() {
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
use serde::{de::Error as DeError, Deserialize, Deserializer};
use serde_repr::{Deserialize_repr, Serialize_repr};

#[cfg(path_all)]
#[cfg(any(path_all, test))]
mod commands;
mod error;
pub use error::*;
Expand Down Expand Up @@ -340,7 +340,7 @@ pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
#[allow(unused_mut)]
let mut builder = Builder::new("path");

#[cfg(path_all)]
#[cfg(any(path_all, test))]
{
builder = builder.invoke_handler(crate::generate_handler![
commands::resolve_directory,
Expand Down
80 changes: 37 additions & 43 deletions core/tauri/src/scope/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ impl Scope {

#[cfg(test)]
mod tests {
use serde::Serialize;

use super::RemoteDomainAccessScope;
use crate::{api::ipc::CallbackFn, test::MockRuntime, App, InvokePayload, Manager, Window};

Expand All @@ -186,10 +188,10 @@ mod tests {
(app, window)
}

fn assert_ipc_response(
fn assert_ipc_response<R: Serialize>(
window: &Window<MockRuntime>,
payload: InvokePayload,
expected: Result<&str, &str>,
expected: Result<R, &str>,
) {
let callback = payload.callback;
let error = payload.error;
Expand All @@ -208,8 +210,8 @@ mod tests {
}
};
let (expected_response, fn_name) = match expected {
Ok(payload) => (payload, callback),
Err(payload) => (payload, error),
Ok(payload) => (serde_json::to_value(payload).unwrap(), callback),
Err(payload) => (serde_json::to_value(payload).unwrap(), error),
};
let expected = format!(
"window[\"_{}\"]({})",
Expand All @@ -224,21 +226,19 @@ mod tests {
assert!(evaluated_script.contains(&expected));
}

fn app_version_payload() -> InvokePayload {
fn path_is_absolute_payload() -> InvokePayload {
let callback = CallbackFn(0);
let error = CallbackFn(1);

let mut payload = serde_json::Map::new();
let mut msg = serde_json::Map::new();
msg.insert(
"cmd".into(),
serde_json::Value::String("getAppVersion".into()),
payload.insert(
"path".into(),
serde_json::Value::String(std::env::current_dir().unwrap().display().to_string()),
);
payload.insert("message".into(), serde_json::Value::Object(msg));

InvokePayload {
cmd: "".into(),
tauri_module: Some("App".into()),
cmd: "plugin:path|is_absolute".into(),
tauri_module: None,
callback,
error,
inner: serde_json::Value::Object(payload),
Expand All @@ -262,12 +262,13 @@ mod tests {
fn scope_not_defined() {
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("app.tauri.app")
.add_window("other")
.add_plugin("path")
.enable_tauri_api()]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(&crate::window::ipc_scope_not_found_error_message(
"main",
"https://tauri.app/",
Expand All @@ -279,12 +280,13 @@ mod tests {
fn scope_not_defined_for_window() {
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("second")
.add_plugin("path")
.enable_tauri_api()]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(&crate::window::ipc_scope_window_error_message("main")),
);
}
Expand All @@ -293,12 +295,13 @@ mod tests {
fn scope_not_defined_for_url() {
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("github.com")
.add_window("main")
.add_plugin("path")
.enable_tauri_api()]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(&crate::window::ipc_scope_domain_error_message(
"https://tauri.app/",
)),
Expand All @@ -307,43 +310,37 @@ mod tests {

#[test]
fn subdomain_is_not_allowed() {
let (app, mut window) = test_context(vec![
let (_app, mut window) = test_context(vec![
RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path")
.enable_tauri_api(),
RemoteDomainAccessScope::new("sub.tauri.app")
.add_window("main")
.add_plugin("path")
.enable_tauri_api(),
]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
app_version_payload(),
Ok(app.package_info().version.to_string().as_str()),
);
assert_ipc_response(&window, path_is_absolute_payload(), Ok(true));

window.navigate("https://blog.tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(&crate::window::ipc_scope_domain_error_message(
"https://blog.tauri.app/",
)),
);

window.navigate("https://sub.tauri.app".parse().unwrap());
assert_ipc_response(
&window,
app_version_payload(),
Ok(app.package_info().version.to_string().as_str()),
);
assert_ipc_response(&window, path_is_absolute_payload(), Ok(true));

window.window.label = "test".into();
window.navigate("https://dev.tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(&crate::window::ipc_scope_not_found_error_message(
"test",
"https://dev.tauri.app/",
Expand All @@ -353,16 +350,13 @@ mod tests {

#[test]
fn subpath_is_allowed() {
let (app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
let (_app, window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path")
.enable_tauri_api()]);

window.navigate("https://tauri.app/inner/path".parse().unwrap());
assert_ipc_response(
&window,
app_version_payload(),
Ok(app.package_info().version.to_string().as_str()),
);
assert_ipc_response(&window, path_is_absolute_payload(), Ok(true));
}

#[test]
Expand All @@ -372,9 +366,9 @@ mod tests {
]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
app_version_payload(),
path_is_absolute_payload(),
Err(crate::window::IPC_SCOPE_DOES_NOT_ALLOW),
);
}
Expand All @@ -386,7 +380,7 @@ mod tests {
.add_plugin(PLUGIN_NAME)]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
plugin_test_payload(),
Err(&format!("plugin {PLUGIN_NAME} not found")),
Expand All @@ -400,7 +394,7 @@ mod tests {
]);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
assert_ipc_response::<()>(
&window,
plugin_test_payload(),
Err(crate::window::IPC_SCOPE_DOES_NOT_ALLOW),
Expand Down
33 changes: 0 additions & 33 deletions examples/api/src/views/App.svelte

This file was deleted.

6 changes: 0 additions & 6 deletions examples/api/src/views/Welcome.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
import { getName, getVersion, getTauriVersion } from '@tauri-apps/api/app'
import { relaunch, exit } from '@tauri-apps/api/process'
let version = '0.0.0'
Expand Down Expand Up @@ -35,11 +34,6 @@

<br />
<br />
<pre>
App name: <code>{appName}</code>
App version: <code>{version}</code>
Tauri version: <code>{tauriVersion}</code>
</pre>
<br />
<div class="flex flex-wrap gap-1 shadow-">
<button class="btn" on:click={closeApp}>Close application</button>
Expand Down
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

Loading

0 comments on commit 3245d14

Please sign in to comment.