From dd8ad41ad90ed19d6686c42b10be38c6562e4962 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Thu, 19 Dec 2024 14:57:13 +0100 Subject: [PATCH 1/2] Templates: add a Rust template for non-trigger components This new template makes it easy to create a component that can be used as a dependency in another component. Until now, that required fully manually setting up a component and adding it to the Spin manifest, etc. Signed-off-by: Till Schneidereit --- templates/component-rust/content/.gitignore | 2 ++ templates/component-rust/content/Cargo.toml.tmpl | 16 ++++++++++++++++ templates/component-rust/content/component.wit | 9 +++++++++ templates/component-rust/content/spin.toml | 13 +++++++++++++ templates/component-rust/content/src/lib.rs | 14 ++++++++++++++ .../metadata/snippets/component.txt | 6 ++++++ .../component-rust/metadata/spin-template.toml | 12 ++++++++++++ 7 files changed, 72 insertions(+) create mode 100644 templates/component-rust/content/.gitignore create mode 100644 templates/component-rust/content/Cargo.toml.tmpl create mode 100644 templates/component-rust/content/component.wit create mode 100644 templates/component-rust/content/spin.toml create mode 100644 templates/component-rust/content/src/lib.rs create mode 100644 templates/component-rust/metadata/snippets/component.txt create mode 100644 templates/component-rust/metadata/spin-template.toml diff --git a/templates/component-rust/content/.gitignore b/templates/component-rust/content/.gitignore new file mode 100644 index 0000000000..386474fa59 --- /dev/null +++ b/templates/component-rust/content/.gitignore @@ -0,0 +1,2 @@ +target/ +.spin/ diff --git a/templates/component-rust/content/Cargo.toml.tmpl b/templates/component-rust/content/Cargo.toml.tmpl new file mode 100644 index 0000000000..86ecd618ce --- /dev/null +++ b/templates/component-rust/content/Cargo.toml.tmpl @@ -0,0 +1,16 @@ +[package] +name = "{{project-name | kebab_case}}" +authors = ["{{authors}}"] +description = "{{project-description}}" +version = "0.1.0" +rust-version = "1.78" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyhow = "1" +wit-bindgen = "0.36.0" + +[workspace] diff --git a/templates/component-rust/content/component.wit b/templates/component-rust/content/component.wit new file mode 100644 index 0000000000..8f2a310d59 --- /dev/null +++ b/templates/component-rust/content/component.wit @@ -0,0 +1,9 @@ +package component:{{project-name | kebab_case}}; + +interface {{project-name | kebab_case}} { + hello: func() -> string; +} + +world component { + export {{project-name | kebab_case}}; +} diff --git a/templates/component-rust/content/spin.toml b/templates/component-rust/content/spin.toml new file mode 100644 index 0000000000..60cd38fb46 --- /dev/null +++ b/templates/component-rust/content/spin.toml @@ -0,0 +1,13 @@ +spin_manifest_version = 2 + +[application] +name = "{{project-name | kebab_case}}" +version = "0.1.0" +authors = ["{{authors}}"] +description = "{{project-description}}" + +[component.{{project-name | kebab_case}}] +source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" +[component.{{project-name | kebab_case}}.build] +command = "cargo build --target wasm32-wasip2 --release" +watch = ["src/**/*.rs", "Cargo.toml"] diff --git a/templates/component-rust/content/src/lib.rs b/templates/component-rust/content/src/lib.rs new file mode 100644 index 0000000000..2ed1d18550 --- /dev/null +++ b/templates/component-rust/content/src/lib.rs @@ -0,0 +1,14 @@ +impl Guest for Exports { + fn hello() -> String { + "Hello from {{project-name | snake_case}}".to_string() + } +} + +// Boilerplate below here +use crate::exports::component::{{project-name | snake_case}}::{{project-name | snake_case}}::Guest; +wit_bindgen::generate!({ + world: "component", + path: "component.wit", +}); +struct Exports; +export!(Exports); diff --git a/templates/component-rust/metadata/snippets/component.txt b/templates/component-rust/metadata/snippets/component.txt new file mode 100644 index 0000000000..de861a892b --- /dev/null +++ b/templates/component-rust/metadata/snippets/component.txt @@ -0,0 +1,6 @@ +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" +[component.{{project-name | kebab_case}}.build] +command = "cargo build --target wasm32-wasip2 --release" +workdir = "{{ output-path }}" +watch = ["src/**/*.rs", "Cargo.toml"] diff --git a/templates/component-rust/metadata/spin-template.toml b/templates/component-rust/metadata/spin-template.toml new file mode 100644 index 0000000000..7c2cb98c58 --- /dev/null +++ b/templates/component-rust/metadata/spin-template.toml @@ -0,0 +1,12 @@ +manifest_version = "1" +id = "component-rust" +description = "Pure Rust component to be used as a dependency for other component" +tags = ["rust"] + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } From 59b56b620a57188e3124e125cbb1bb42a10525b7 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Thu, 16 Jan 2025 15:42:19 +0100 Subject: [PATCH 2/2] Address review comments Signed-off-by: Till Schneidereit --- templates/component-rust/content/Cargo.toml.tmpl | 6 +++--- templates/component-rust/content/spin.toml | 13 ------------- .../component-rust/metadata/spin-template.toml | 9 +++++---- 3 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 templates/component-rust/content/spin.toml diff --git a/templates/component-rust/content/Cargo.toml.tmpl b/templates/component-rust/content/Cargo.toml.tmpl index 86ecd618ce..82b2092bb4 100644 --- a/templates/component-rust/content/Cargo.toml.tmpl +++ b/templates/component-rust/content/Cargo.toml.tmpl @@ -1,9 +1,9 @@ [package] name = "{{project-name | kebab_case}}" authors = ["{{authors}}"] -description = "{{project-description}}" +description = "{{component-description}}" version = "0.1.0" -rust-version = "1.78" +rust-version = "1.82" edition = "2021" [lib] @@ -11,6 +11,6 @@ crate-type = ["cdylib"] [dependencies] anyhow = "1" -wit-bindgen = "0.36.0" +wit-bindgen = "0.37.0" [workspace] diff --git a/templates/component-rust/content/spin.toml b/templates/component-rust/content/spin.toml deleted file mode 100644 index 60cd38fb46..0000000000 --- a/templates/component-rust/content/spin.toml +++ /dev/null @@ -1,13 +0,0 @@ -spin_manifest_version = 2 - -[application] -name = "{{project-name | kebab_case}}" -version = "0.1.0" -authors = ["{{authors}}"] -description = "{{project-description}}" - -[component.{{project-name | kebab_case}}] -source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" -[component.{{project-name | kebab_case}}.build] -command = "cargo build --target wasm32-wasip2 --release" -watch = ["src/**/*.rs", "Cargo.toml"] diff --git a/templates/component-rust/metadata/spin-template.toml b/templates/component-rust/metadata/spin-template.toml index 7c2cb98c58..e6cb8c2de0 100644 --- a/templates/component-rust/metadata/spin-template.toml +++ b/templates/component-rust/metadata/spin-template.toml @@ -1,12 +1,13 @@ manifest_version = "1" id = "component-rust" -description = "Pure Rust component to be used as a dependency for other component" +description = "Library (dependency) component using Rust" tags = ["rust"] -[add_component] -skip_files = ["spin.toml"] +[new_application] +supported = false + [add_component.snippets] component = "component.txt" [parameters] -project-description = { type = "string", prompt = "Description", default = "" } +component-description = { type = "string", prompt = "Description", default = "" }