Skip to content

Commit

Permalink
feat(create): add support for tauri 2 (#2001)
Browse files Browse the repository at this point in the history
* rebase: pick changes to main

* chore: bump version
  • Loading branch information
fu050409 authored Jan 9, 2025
1 parent 5e053f6 commit d575d72
Show file tree
Hide file tree
Showing 229 changed files with 2,522 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sixty-mice-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-farm": patch
---

Add support for tauri v2
17 changes: 16 additions & 1 deletion crates/create-farm-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ where

Template::Tauri(Some(*sub_template))
}
Template::Tauri2(None) => {
let sub_templates = vec![
TauriSubTemplate::React,
TauriSubTemplate::Vue,
TauriSubTemplate::Svelte,
TauriSubTemplate::Vanilla,
TauriSubTemplate::Solid,
TauriSubTemplate::Preact,
];

let sub_template =
prompts::select("Select a Tauri2 template:", &sub_templates, Some(0))?.unwrap();

Template::Tauri2(Some(*sub_template))
}
Template::Electron(None) => {
let sub_templates = vec![
ElectronSubTemplate::React,
Expand Down Expand Up @@ -162,7 +177,7 @@ where
}
clean_dir(&target_dir)?;
} else {
let _ = fs::create_dir_all(&target_dir);
fs::create_dir_all(&target_dir)?;
}

// Render the template
Expand Down
1 change: 1 addition & 0 deletions crates/create-farm-rs/src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl PackageManager {
Template::Lit,
Template::Preact,
Template::Nestjs,
Template::Tauri2(None),
Template::Tauri(None),
Template::Electron(None)
],
Expand Down
16 changes: 16 additions & 0 deletions crates/create-farm-rs/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub enum Template {
Solid,
Preact,
Nestjs,
Tauri2(Option<TauriSubTemplate>),
Tauri(Option<TauriSubTemplate>),
Electron(Option<ElectronSubTemplate>),
}
Expand All @@ -175,6 +176,8 @@ impl Display for Template {
Template::Solid => write!(f, "solid"),
Template::Preact => write!(f, "preact"),
Template::Nestjs => write!(f, "nestjs"),
Template::Tauri2(None) => write!(f, "tauri2"),
Template::Tauri2(Some(sub_template)) => write!(f, "tauri2-{sub_template}"),
Template::Tauri(None) => write!(f, "tauri"),
Template::Tauri(Some(sub_template)) => write!(f, "tauri-{sub_template}"),
Template::Electron(None) => write!(f, "electron"),
Expand All @@ -197,6 +200,7 @@ impl FromStr for Template {
"solid" => Ok(Template::Solid),
"preact" => Ok(Template::Preact),
"nestjs" => Ok(Template::Nestjs),
"tauri2" => Ok(Template::Tauri2(None)),
"tauri" => Ok(Template::Tauri(None)),
"electron" => Ok(Template::Electron(None)),
_ => Err(format!(
Expand All @@ -222,7 +226,16 @@ impl Displayable for Template {
Template::Svelte => "\x1b[38;2;255;137;54mSvelte - (https://svelte.dev/)\x1b[0m",
Template::Lit => "\x1b[33mLit - (https://lit.dev/)\x1b[0m",
Template::Preact => "\x1b[36mPreact - (https://preactjs.com/)\x1b[0m",
Template::Tauri2(None) => "\x1b[38;2;255;137;54mTauri2 - (https://tauri.app/)\x1b[0m",
Template::Tauri(None) => "\x1b[38;2;255;137;54mTauri - (https://tauri.app/)\x1b[0m",
Template::Tauri2(Some(sub_template)) => match sub_template {
TauriSubTemplate::React => "\x1b[38;2;255;215;0mTauri2 with React\x1b[0m",
TauriSubTemplate::Vue => "\x1b[38;2;255;215;0mTauri2 with Vue\x1b[0m",
TauriSubTemplate::Vanilla => "\x1b[38;2;255;215;0mTauri2 with Vanilla\x1b[0m",
TauriSubTemplate::Svelte => "\x1b[38;2;255;215;0mTauri2 with Svelte\x1b[0m",
TauriSubTemplate::Solid => "\x1b[38;2;255;215;0mTauri2 with Solid\x1b[0m",
TauriSubTemplate::Preact => "\x1b[38;2;255;215;0mTauri2 with Preact\x1b[0m",
},
Template::Tauri(Some(sub_template)) => match sub_template {
TauriSubTemplate::React => "\x1b[38;2;255;215;0mTauri with React\x1b[0m",
TauriSubTemplate::Vue => "\x1b[38;2;255;215;0mTauri with Vue\x1b[0m",
Expand Down Expand Up @@ -258,6 +271,7 @@ impl<'a> Template {
Template::Solid,
Template::Preact,
Template::Nestjs,
Template::Tauri2(None),
Template::Tauri(None),
Template::Electron(None),
];
Expand Down Expand Up @@ -357,6 +371,8 @@ impl<'a> Template {
};

let current_template_name = match self {
Template::Tauri2(None) => "tauri2".to_string(),
Template::Tauri2(Some(sub_template)) => format!("tauri2/{}", sub_template.to_simple_string()),
Template::Tauri(None) => "tauri".to_string(),
Template::Tauri(Some(sub_template)) => format!("tauri/{}", sub_template.to_simple_string()),
Template::Electron(None) => "electron".to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"]
}
7 changes: 7 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tauri + Preact + Typescript

This template should help get you started developing with Tauri, Preact and Typescript in Vite.

## Recommended IDE Setup

- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)
27 changes: 27 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/farm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from "@farmfe/core";
import preact from '@preact/preset-vite';

// @ts-ignore process is a nodejs global
const host = process.env.TAURI_DEV_HOST;

// https://www.farmfe.org/docs/config/configuring-farm
export default defineConfig({
plugins: [preact()],

clearScreen: false,
server: {
port: 1420,
strictPort: true,
host: host || false,
hmr: host
? {
protocol: "ws",
host,
port: 1421,
watchOptions: {
ignored: ["**/node_modules/**"],
}
}
: undefined,
},
});
24 changes: 24 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
14 changes: 14 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/farm.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tauri + Preact + Typescript</title>
</head>

<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "<FARM-TEMPLATE-NAME>",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "farm",
"build": "tsc && farm build",
"preview": "farm preview",
"tauri": "tauri"
},
"dependencies": {
"preact": "^10.25.1",
"@tauri-apps/api": "^2",
"@tauri-apps/plugin-opener": "^2"
},
"devDependencies": {
"@farmfe/cli": "^1.0.4",
"@farmfe/core": "^1.6.3",
"@preact/preset-vite": "^2.9.3",
"core-js": "^3.39.0",
"typescript": "^5.6.2",
"@tauri-apps/cli": "^2"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "<FARM-TEMPLATE-NAME>"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
# The `_lib` suffix may seem redundant but it is necessary
# to make the lib name unique and wouldn't conflict with the bin name.
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
name = "<FARM-TEMPLATE-NAME>_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:default",
"opener:default"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Generated by Tauri
# will have schema files for capabilities auto-completion
/gen/schemas
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions crates/create-farm-rs/templates/tauri2/preact/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
preact_lib::run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "<FARM-TEMPLATE-NAME>",
"version": "0.1.0",
"identifier": "com.tauri.dev",
"build": {
"beforeDevCommand": "pnpm dev",
"devUrl": "http://localhost:1420",
"beforeBuildCommand": "pnpm build",
"frontendDist": "../dist"
},
"app": {
"windows": [
{
"title": "<FARM-TEMPLATE-NAME>",
"width": 800,
"height": 600
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}
Loading

0 comments on commit d575d72

Please sign in to comment.