Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt latest changes in Tauri v2 Beta #109

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,160 changes: 626 additions & 534 deletions Cargo.lock

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
javascript = ["specta/typescript", "specta/js_doc"]
typescript = ["specta/typescript", "specta/js_doc"]
javascript = ["dep:specta-typescript"]
typescript = ["dep:specta-typescript"]

[dependencies]
# Public
specta = { workspace = true, features = ["function"] }
specta-typescript = { workspace = true, optional = true }
specta-util = { workspace = true }
tauri-specta-macros = { version = "=2.0.0-rc.5", path = "./macros" }
serde = "1"
serde_json = "1"
Expand All @@ -36,12 +38,19 @@ indoc = "2.0.5"

[workspace]
members = [
"examples/app/src-tauri",
"examples/custom-plugin/app/src-tauri",
"examples/custom-plugin/plugin",
"macros",
"examples/app/src-tauri",
"examples/custom-plugin/app/src-tauri",
"examples/custom-plugin/plugin",
"macros",
]

[workspace.dependencies]
tauri = { version = "2.0.0-beta.23" }
specta = { version = "=2.0.0-rc.12" }
tauri = { version = "=2.0.0-beta.25" }
specta = { version = "=2.0.0-rc.16" }
specta-util = { version = "0.0.3" }
specta-typescript = { version = "0.0.3" }

[patch.crates-io]
specta = { path = "../specta/specta" }
specta-util = { path = "../specta/specta-util" }
specta-typescript = { path = "../specta/specta-typescript" }
6 changes: 3 additions & 3 deletions docs/v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
> However, it's safe to use as long as you lock your versions.

```bash
cargo add tauri@=2.0.0-beta.22
cargo add specta@=2.0.0-rc.12
cargo add tauri@=2.0.0-beta.25
cargo add specta@=2.0.0-rc.16
cargo add tauri-specta@=2.0.0-rc.11 --features javascript,typescript
```

Expand Down Expand Up @@ -187,7 +187,7 @@ let builder = ts::builder()
.types(TypeCollection::default().register::<Custom>()); // < call `register` as much as you want.
```

`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.12/specta/type/trait.NamedType.html) as otherwise you would run into the following problem:
`register` only supports [named types](https://docs.rs/specta/2.0.0-rc.16/specta/type/trait.NamedType.html) as otherwise you would run into the following problem:
```rust
// vvv - What would this be without a name?
export ... = {};
Expand Down
4 changes: 2 additions & 2 deletions examples/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@tauri-apps/cli": "next",
"typescript": "^5.5.2",
"vite": "^5.3.2"
"typescript": "^5.5.4",
"vite": "^5.3.5"
}
}
2 changes: 2 additions & 0 deletions examples/app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ specta = { workspace = true }
serde = { version = "1.0", features = ["derive"] }
tauri = { workspace = true, features = [] }
tauri-specta = { path = "../../../", features = ["typescript", "javascript"] }
specta-util = { workspace = true }
specta-typescript = { workspace = true }
tauri-plugin-os = "^2.0.0-beta.3"
thiserror = "1"

Expand Down
8 changes: 6 additions & 2 deletions examples/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
)]

use serde::{Deserialize, Serialize};
use specta::{Type, TypeCollection};
use specta::Type;
use specta_util::TypeCollection;
use tauri_specta::*;
use thiserror::Error;

Expand Down Expand Up @@ -129,7 +130,10 @@ fn main() {
])
.events(tauri_specta::collect_events![crate::DemoEvent, EmptyEvent])
.types(TypeCollection::default().register::<Custom>())
.config(specta::ts::ExportConfig::default().formatter(specta::ts::formatter::prettier))
.config(
specta_typescript::ExportConfig::default()
.formatter(specta_typescript::formatter::prettier),
)
.types(TypeCollection::default().register::<Testing>())
.statics(StaticCollection::default().register("universalConstant", 42))
.header("/* These are my Tauri Specta Bindings! */");
Expand Down
4 changes: 2 additions & 2 deletions examples/app/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCurrent } from "@tauri-apps/api/webviewWindow";
import { getCurrentWebview } from "@tauri-apps/api/webview";
import { commands, events } from "./bindings";

const appWindow = getCurrent();
const appWindow = getCurrentWebview();

let greetInputEl: HTMLInputElement | null;
let greetMsgEl: HTMLElement | null;
Expand Down
6 changes: 3 additions & 3 deletions examples/custom-plugin/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"license": "MIT",
"dependencies": {
"@tauri-apps/api": "next",
"solid-js": "^1.8.18",
"solid-js": "^1.8.19",
"tauri-specta-custom-plugin": "workspace:*"
},
"devDependencies": {
"@tauri-apps/cli": "next",
"typescript": "^5.5.2",
"vite": "^5.3.2",
"typescript": "^5.5.4",
"vite": "^5.3.5",
"vite-plugin-solid": "^2.10.2"
}
}
3 changes: 3 additions & 0 deletions examples/custom-plugin/plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ serde = "1"
specta = { workspace = true }
tauri = { workspace = true }
tauri-specta = { path = "../../../", features = ["typescript"] }

[dev-dependencies]
specta-typescript = { workspace = true }
9 changes: 9 additions & 0 deletions examples/custom-plugin/plugin/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.

/** user-defined commands **/

export const commands = {
/**
* Adds two numbers, returning the result.
Expand All @@ -9,12 +12,18 @@ return await TAURI_INVOKE("plugin:specta-example|add_numbers", { a, b });
}
}

/** user-defined events **/

export const events = __makeEvents__<{
randomNumber: RandomNumber
}>({
randomNumber: "plugin:specta-example:random-number"
})

/** user-defined statics **/



/** user-defined types **/

export type RandomNumber = number
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-plugin/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"codegen": "cargo test && pnpm build"
},
"devDependencies": {
"typescript": "^5.5.2"
"typescript": "^5.5.4"
}
}
5 changes: 4 additions & 1 deletion examples/custom-plugin/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ mod test {
fn export_types() {
specta_builder!()
.path("./bindings.ts")
.config(specta::ts::ExportConfig::default().formatter(specta::ts::formatter::prettier))
.config(
specta_typescript::ExportConfig::default()
.formatter(specta_typescript::formatter::prettier),
)
.export_for_plugin(PLUGIN_NAME)
.expect("failed to export specta types");
}
Expand Down
25 changes: 2 additions & 23 deletions macros/src/collect_commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use proc_macro2::Ident;
use quote::quote;
use syn::{
parse::{Parse, ParseStream},
Expand All @@ -8,33 +7,19 @@ use syn::{
};

pub struct Input {
type_map: Option<Ident>,
paths: Punctuated<Path, Token![,]>,
}

mod kw {
syn::custom_keyword!(type_map);
}

impl Parse for Input {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
type_map: {
if input.peek(kw::type_map) && input.peek2(Token![:]) {
input.parse::<kw::type_map>()?;
input.parse::<Token![:]>()?;
Some(input.parse()?)
} else {
None
}
},
paths: Punctuated::parse_terminated(input)?,
})
}
}

pub fn proc_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let Input { type_map, paths } = parse_macro_input!(input as Input);
let Input { paths } = parse_macro_input!(input as Input);

let tauri_paths = paths.iter().map(|p| {
let Path {
Expand All @@ -47,18 +32,12 @@ pub fn proc_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
quote!(#leading_colon #(#segments)::*)
});

let type_map = type_map
.map(|i| quote!(#i))
.unwrap_or_else(|| quote!(::specta::r#type::TypeMap::default()));

let body = quote! {(
::specta::function::collect_functions![type_map; #paths],
::specta::function::collect_functions![#paths],
::tauri::generate_handler![#(#tauri_paths),*],
)};

quote! {{
let mut type_map = #type_map;

#body
}}
.into()
Expand Down
Loading
Loading