diff options
author | GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> | 2023-02-05 15:53:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-05 15:53:34 +0100 |
commit | 802d7dfee77a3f58ee14a7ef6e2454e5aae62cbb (patch) | |
tree | 1c63107fafa7b535acd1a31a2e317e7535f85b62 | |
parent | 24cabc6019e702ac888a33311532a9832863b572 (diff) | |
download | FlightCore-802d7dfee77a3f58ee14a7ef6e2454e5aae62cbb.tar.gz FlightCore-802d7dfee77a3f58ee14a7ef6e2454e5aae62cbb.zip |
feat: Auto-generate TS bindings (#140)
* feat: Initial trial to auto-generate TS bindings
That way instead of manually duplicating code, we can just run
`cargo test` to generate them.
* fix: Update forgotten imports
* refactor: Move FlightCoreVersion to autogen bind
TypeScript binding autogenerated from Rust code
* refactor: Move ReleaseInfo to autogen binding
TypeScript binding autogenerated from Rust code
* docs: Explain how to generate TS binds from Rust
* feat: Check for binding changes in CI
Checks for uncommitted binding changes in CI and fails if they differ
* style: Formatting fixes
-rw-r--r-- | .github/workflows/push-test.yml | 34 | ||||
-rw-r--r-- | docs/DEVELOPMENT.md | 20 | ||||
-rw-r--r-- | src-tauri/Cargo.lock | 30 | ||||
-rw-r--r-- | src-tauri/Cargo.toml | 2 | ||||
-rw-r--r-- | src-tauri/bindings/FlightCoreVersion.ts | 3 | ||||
-rw-r--r-- | src-tauri/bindings/NorthstarMod.ts | 3 | ||||
-rw-r--r-- | src-tauri/bindings/ReleaseInfo.ts | 3 | ||||
-rw-r--r-- | src-tauri/src/github/release_notes.rs | 7 | ||||
-rw-r--r-- | src-tauri/src/lib.rs | 4 | ||||
-rw-r--r-- | src-vue/src/components/ThunderstoreModCard.vue | 2 | ||||
-rw-r--r-- | src-vue/src/plugins/store.ts | 6 | ||||
-rw-r--r-- | src-vue/src/utils/FlightCoreVersion.d.ts | 5 | ||||
-rw-r--r-- | src-vue/src/utils/NorthstarMod.d.ts | 7 | ||||
-rw-r--r-- | src-vue/src/utils/ReleaseInfo.d.ts | 6 | ||||
-rw-r--r-- | src-vue/src/views/ChangelogView.vue | 2 | ||||
-rw-r--r-- | src-vue/src/views/mods/LocalModsView.vue | 2 |
16 files changed, 109 insertions, 27 deletions
diff --git a/.github/workflows/push-test.yml b/.github/workflows/push-test.yml index 5e4597d8..aaad5a6d 100644 --- a/.github/workflows/push-test.yml +++ b/.github/workflows/push-test.yml @@ -31,6 +31,40 @@ jobs: command: fmt args: --manifest-path src-tauri/Cargo.toml --all -- --check + # Ensure committed bindings correct + autogen-ts-bindings-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - name: install dependencies (ubuntu only) + run: | + sudo apt-get update + sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf + + - name: Move original TypeScript bindings + run: | + cp -r src-tauri/bindings original-bindings + rm -r src-tauri/bindings + + - name: Create empty dist folder # Otherwise tests fail + run: mkdir src-vue/dist + + - name: Run Tests + run: cargo test --manifest-path src-tauri/Cargo.toml + + - name: Compare TypeScript Bindings + run: | + diff -r src-tauri/bindings original-bindings + if [[ $? -ne 0 ]]; then + echo "Generated bindings are different from committed bindings" + exit 1 + fi + test-tauri: needs: ensure-same-version strategy: diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index 58714a09..8dbb1cea 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -158,6 +158,26 @@ else { ``` +### Auto-generating TypeScript bindings for Rust types + +This codebases uses [`ts-rs`](https://crates.io/crates/ts-rs) to generate TypeScript interfaces from Rust code. + +To generate new bindings, [use the appropriate macros](https://docs.rs/ts-rs/6.2.1/ts_rs/#get-started) + +```Rust +use ts_rs::TS; + +#[derive(TS)] +#[ts(export)] +struct User { + user_id: i32, + first_name: String, + last_name: String, +} +``` + +then simply run `cargo test`. The generated bindings are placed in `src-tauri/bindings/`. Make sure to add and commit them as well! + ## Other This repo uses [EditorConfig](https://editorconfig.org/) to define some basic formatting rules. Find a plugin for your IDE [here](https://editorconfig.org/#download). diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 0376d592..aea73676 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3,6 +3,12 @@ version = 3 [[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" + +[[package]] name = "addr2line" version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -100,6 +106,7 @@ dependencies = [ "tauri-build", "tauri-plugin-store", "tokio", + "ts-rs", "zip", ] @@ -4167,6 +4174,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] +name = "ts-rs" +version = "6.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4added4070a4fdf9df03457206cd2e4b12417c8560a2954d91ffcbe60177a56a" +dependencies = [ + "thiserror", + "ts-rs-macros", +] + +[[package]] +name = "ts-rs-macros" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f807fdb3151fee75df7485b901a89624358cd07a67a8fb1a5831bf5a07681ff" +dependencies = [ + "Inflector", + "proc-macro2", + "quote", + "syn", + "termcolor", +] + +[[package]] name = "typenum" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index dcdf8d3b..dbd6f605 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -44,6 +44,8 @@ async-recursion = "1.0.0" game-scanner = "1.1.4" # For parsing timestamps chrono = "0.4.23" +# TypeScript bindings +ts-rs = "6.1" [features] # by default Tauri runs in production mode diff --git a/src-tauri/bindings/FlightCoreVersion.ts b/src-tauri/bindings/FlightCoreVersion.ts new file mode 100644 index 00000000..9017a840 --- /dev/null +++ b/src-tauri/bindings/FlightCoreVersion.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface FlightCoreVersion { tag_name: string, published_at: string, }
\ No newline at end of file diff --git a/src-tauri/bindings/NorthstarMod.ts b/src-tauri/bindings/NorthstarMod.ts new file mode 100644 index 00000000..ed9d9297 --- /dev/null +++ b/src-tauri/bindings/NorthstarMod.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface NorthstarMod { name: string, thunderstore_mod_string: string | null, enabled: boolean, directory: string, }
\ No newline at end of file diff --git a/src-tauri/bindings/ReleaseInfo.ts b/src-tauri/bindings/ReleaseInfo.ts new file mode 100644 index 00000000..249f986e --- /dev/null +++ b/src-tauri/bindings/ReleaseInfo.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface ReleaseInfo { name: string, published_at: string, body: string, }
\ No newline at end of file diff --git a/src-tauri/src/github/release_notes.rs b/src-tauri/src/github/release_notes.rs index 758f7ace..6dee4576 100644 --- a/src-tauri/src/github/release_notes.rs +++ b/src-tauri/src/github/release_notes.rs @@ -1,15 +1,18 @@ use crate::constants::APP_USER_AGENT; use serde::{Deserialize, Serialize}; use std::vec::Vec; +use ts_rs::TS; -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, TS)] +#[ts(export)] pub struct ReleaseInfo { pub name: String, pub published_at: String, pub body: String, } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, TS)] +#[ts(export)] pub struct FlightCoreVersion { tag_name: String, published_at: String, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 62cc07ae..e43e5935 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -13,6 +13,7 @@ use platform_specific::linux; use serde::{Deserialize, Serialize}; use sysinfo::SystemExt; +use ts_rs::TS; use zip::ZipArchive; use northstar::get_northstar_version_number; @@ -31,7 +32,8 @@ pub struct GameInstall { pub install_type: InstallType, } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, TS)] +#[ts(export)] pub struct NorthstarMod { pub name: String, pub thunderstore_mod_string: Option<String>, diff --git a/src-vue/src/components/ThunderstoreModCard.vue b/src-vue/src/components/ThunderstoreModCard.vue index a202aa50..d125e9f5 100644 --- a/src-vue/src/components/ThunderstoreModCard.vue +++ b/src-vue/src/components/ThunderstoreModCard.vue @@ -70,7 +70,7 @@ import {ThunderstoreMod} from "../utils/thunderstore/ThunderstoreMod"; import {ThunderstoreModVersion} from "../utils/thunderstore/ThunderstoreModVersion"; import {invoke, shell} from "@tauri-apps/api"; import {ThunderstoreModStatus} from "../utils/thunderstore/ThunderstoreModStatus"; -import {NorthstarMod} from "../utils/NorthstarMod"; +import {NorthstarMod} from "../../../src-tauri/bindings/NorthstarMod"; import {GameInstall} from "../utils/GameInstall"; import {ElNotification} from "element-plus"; import { NorthstarState } from "../utils/NorthstarState"; diff --git a/src-vue/src/plugins/store.ts b/src-vue/src/plugins/store.ts index 2eae843a..d4371fb8 100644 --- a/src-vue/src/plugins/store.ts +++ b/src-vue/src/plugins/store.ts @@ -5,16 +5,16 @@ import { InstallType } from "../utils/InstallType"; import { invoke } from "@tauri-apps/api"; import { GameInstall } from "../utils/GameInstall"; import { ReleaseCanal } from "../utils/ReleaseCanal"; -import { FlightCoreVersion } from "../utils/FlightCoreVersion"; +import { FlightCoreVersion } from "../../../src-tauri/bindings/FlightCoreVersion"; import { ElNotification, NotificationHandle } from 'element-plus'; import { NorthstarState } from '../utils/NorthstarState'; import { appDir } from '@tauri-apps/api/path'; import { open } from '@tauri-apps/api/dialog'; import { Store } from 'tauri-plugin-store-api'; import { router } from "../main"; -import ReleaseInfo from "../utils/ReleaseInfo"; +import { ReleaseInfo } from "../../../src-tauri/bindings/ReleaseInfo"; import { ThunderstoreMod } from '../utils/thunderstore/ThunderstoreMod'; -import { NorthstarMod } from "../utils/NorthstarMod"; +import { NorthstarMod } from "../../../src-tauri/bindings/NorthstarMod"; import { searchModule } from './modules/search'; const persistentStore = new Store('flight-core-settings.json'); diff --git a/src-vue/src/utils/FlightCoreVersion.d.ts b/src-vue/src/utils/FlightCoreVersion.d.ts deleted file mode 100644 index 2516bf25..00000000 --- a/src-vue/src/utils/FlightCoreVersion.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -// derived from release_notes.rs -export interface FlightCoreVersion { - tag_name: string, - published_at: string, -} diff --git a/src-vue/src/utils/NorthstarMod.d.ts b/src-vue/src/utils/NorthstarMod.d.ts deleted file mode 100644 index 5e119683..00000000 --- a/src-vue/src/utils/NorthstarMod.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Matches Rust struct (in lib.rs). -export interface NorthstarMod { - name: string, - thunderstore_mod_string?: string, - enabled: bool, - directory: string, -} diff --git a/src-vue/src/utils/ReleaseInfo.d.ts b/src-vue/src/utils/ReleaseInfo.d.ts deleted file mode 100644 index 162f7917..00000000 --- a/src-vue/src/utils/ReleaseInfo.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Matches Rust struct (in release_notes mod). -export default interface ReleaseInfo { - name: string; - published_at: string; - body: string; -} diff --git a/src-vue/src/views/ChangelogView.vue b/src-vue/src/views/ChangelogView.vue index b9b91568..9335220e 100644 --- a/src-vue/src/views/ChangelogView.vue +++ b/src-vue/src/views/ChangelogView.vue @@ -23,7 +23,7 @@ <script lang="ts"> import { defineComponent } from 'vue'; -import ReleaseInfo from '../utils/ReleaseInfo'; +import { ReleaseInfo } from "../../../src-tauri/bindings/ReleaseInfo"; import { marked } from "marked"; diff --git a/src-vue/src/views/mods/LocalModsView.vue b/src-vue/src/views/mods/LocalModsView.vue index 470ab4f7..6e2d4be0 100644 --- a/src-vue/src/views/mods/LocalModsView.vue +++ b/src-vue/src/views/mods/LocalModsView.vue @@ -24,7 +24,7 @@ import { invoke } from '@tauri-apps/api'; import { ElNotification } from 'element-plus'; import { defineComponent } from 'vue'; import { GameInstall } from '../../utils/GameInstall'; -import { NorthstarMod } from '../../utils/NorthstarMod'; +import { NorthstarMod } from "../../../../src-tauri/bindings/NorthstarMod"; export default defineComponent({ name: 'LocalModsView', |