From 9031bbf73231538f593679b695d81944ed7f5bb9 Mon Sep 17 00:00:00 2001 From: Brian H Date: Tue, 24 Sep 2024 16:17:21 -0600 Subject: [PATCH] Generate rust bindings Signed-off-by: Brian H --- Cargo.lock | 27 ++ Cargo.toml | 2 + Makefile | 4 + example/.gitignore | 4 + example/Cargo.lock | 621 +++++++++++++++++++++++++++++++++++++++ example/Cargo.toml | 16 + example/README.md | 4 + example/spin.toml | 21 ++ example/src/lib.rs | 17 ++ src/commands/add.rs | 7 +- src/commands/bindings.rs | 64 +++- src/common/constants.rs | 3 +- src/main.rs | 6 +- 13 files changed, 783 insertions(+), 13 deletions(-) create mode 100644 Makefile create mode 100644 example/.gitignore create mode 100644 example/Cargo.lock create mode 100644 example/Cargo.toml create mode 100644 example/README.md create mode 100644 example/spin.toml create mode 100644 example/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 4096897..061034e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4165,6 +4165,8 @@ dependencies = [ "url", "wasm-pkg-loader", "wasmparser 0.217.0", + "wit-bindgen-core", + "wit-bindgen-rust", "wit-component 0.217.0", "wit-parser 0.217.0", ] @@ -6106,6 +6108,31 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "wit-bindgen-core" +version = "0.32.0" +source = "git+https://github.com/fibonacci1729/wit-bindgen?branch=deps#83aa28293258fd6c3697e044b3a3d69397a2fe81" +dependencies = [ + "anyhow", + "heck 0.5.0", + "wit-parser 0.217.0", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.32.0" +source = "git+https://github.com/fibonacci1729/wit-bindgen?branch=deps#83aa28293258fd6c3697e044b3a3d69397a2fe81" +dependencies = [ + "anyhow", + "heck 0.5.0", + "indexmap 2.5.0", + "prettyplease", + "syn 2.0.77", + "wasm-metadata 0.217.0", + "wit-bindgen-core", + "wit-component 0.217.0", +] + [[package]] name = "wit-component" version = "0.209.1" diff --git a/Cargo.toml b/Cargo.toml index 354c62b..0bc5cae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,8 @@ wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools" } wasm-pkg-loader = "0.4.1" futures = "0.3.30" semver = "1.0.23" +wit-bindgen-rust = { git = "https://github.com/fibonacci1729/wit-bindgen", branch = "deps" } +wit-bindgen-core = { git = "https://github.com/fibonacci1729/wit-bindgen", branch = "deps" } [target.'cfg(target_os = "linux")'.dependencies] # This needs to be an explicit dependency to enable diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a06d0b --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: install +install: + cargo build --release + spin pluginify -i \ No newline at end of file diff --git a/example/.gitignore b/example/.gitignore new file mode 100644 index 0000000..7e7c427 --- /dev/null +++ b/example/.gitignore @@ -0,0 +1,4 @@ +src/bindings +target/ +.spin/ +.wit/ \ No newline at end of file diff --git a/example/Cargo.lock b/example/Cargo.lock new file mode 100644 index 0000000..03956e3 --- /dev/null +++ b/example/Cargo.lock @@ -0,0 +1,621 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + +[[package]] +name = "async-trait" +version = "0.1.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bytes" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "example" +version = "0.1.0" +dependencies = [ + "anyhow", + "spin-sdk", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + +[[package]] +name = "indexmap" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" +dependencies = [ + "equivalent", + "hashbrown", + "serde", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "leb128" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "routefinder" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0971d3c8943a6267d6bd0d782fdc4afa7593e7381a92a3df950ff58897e066b5" +dependencies = [ + "smartcow", + "smartstring", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "smartcow" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "656fcb1c1fca8c4655372134ce87d8afdf5ec5949ebabe8d314be0141d8b5da2" +dependencies = [ + "smartstring", +] + +[[package]] +name = "smartstring" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29" +dependencies = [ + "autocfg", + "static_assertions", + "version_check", +] + +[[package]] +name = "spdx" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47317bbaf63785b53861e1ae2d11b80d6b624211d42cb20efcd210ee6f8a14bc" +dependencies = [ + "smallvec", +] + +[[package]] +name = "spin-executor" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2df1a5e2cc70a628c9ea6914770c234cc4a292218091e6707ae8be68b4a5de76" +dependencies = [ + "futures", + "once_cell", + "wit-bindgen", +] + +[[package]] +name = "spin-macro" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef3d03e5a205a641d85ace3af1604b39dba63d3ffe3865a71bda02fb482ae60a" +dependencies = [ + "anyhow", + "bytes", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "spin-sdk" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed97f54a15f2d8b1fa15e436d88bacb95a5b379a3e0f8fbd8042eb8696ca048a" +dependencies = [ + "anyhow", + "async-trait", + "bytes", + "form_urlencoded", + "futures", + "http", + "once_cell", + "routefinder", + "serde", + "serde_json", + "spin-executor", + "spin-macro", + "thiserror", + "wit-bindgen", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasm-encoder" +version = "0.38.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ad2b51884de9c7f4fe2fd1043fccb8dcad4b1e29558146ee57a144d15779f3f" +dependencies = [ + "leb128", +] + +[[package]] +name = "wasm-encoder" +version = "0.41.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "972f97a5d8318f908dded23594188a90bcd09365986b1163e66d70170e5287ae" +dependencies = [ + "leb128", +] + +[[package]] +name = "wasm-metadata" +version = "0.10.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18ebaa7bd0f9e7a5e5dd29b9a998acf21c4abed74265524dd7e85934597bfb10" +dependencies = [ + "anyhow", + "indexmap", + "serde", + "serde_derive", + "serde_json", + "spdx", + "wasm-encoder 0.41.2", + "wasmparser 0.121.2", +] + +[[package]] +name = "wasmparser" +version = "0.118.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77f1154f1ab868e2a01d9834a805faca7bf8b50d041b4ca714d005d0dab1c50c" +dependencies = [ + "indexmap", + "semver", +] + +[[package]] +name = "wasmparser" +version = "0.121.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dbe55c8f9d0dbd25d9447a5a889ff90c0cc3feaa7395310d3d826b2c703eaab" +dependencies = [ + "bitflags", + "indexmap", + "semver", +] + +[[package]] +name = "wit-bindgen" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b76f1d099678b4f69402a421e888bbe71bf20320c2f3f3565d0e7484dbe5bc20" +dependencies = [ + "bitflags", + "wit-bindgen-rust-macro", +] + +[[package]] +name = "wit-bindgen-core" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75d55e1a488af2981fb0edac80d8d20a51ac36897a1bdef4abde33c29c1b6d0d" +dependencies = [ + "anyhow", + "wit-component", + "wit-parser", +] + +[[package]] +name = "wit-bindgen-rust" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01ff9cae7bf5736750d94d91eb8a49f5e3a04aff1d1a3218287d9b2964510f8" +dependencies = [ + "anyhow", + "heck", + "wasm-metadata", + "wit-bindgen-core", + "wit-component", +] + +[[package]] +name = "wit-bindgen-rust-macro" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "804a98e2538393d47aa7da65a7348116d6ff403b426665152b70a168c0146d49" +dependencies = [ + "anyhow", + "proc-macro2", + "quote", + "syn 2.0.77", + "wit-bindgen-core", + "wit-bindgen-rust", + "wit-component", +] + +[[package]] +name = "wit-component" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a35a2a9992898c9d27f1664001860595a4bc99d32dd3599d547412e17d7e2" +dependencies = [ + "anyhow", + "bitflags", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "wasm-encoder 0.38.1", + "wasm-metadata", + "wasmparser 0.118.2", + "wit-parser", +] + +[[package]] +name = "wit-parser" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "316b36a9f0005f5aa4b03c39bc3728d045df136f8c13a73b7db4510dec725e08" +dependencies = [ + "anyhow", + "id-arena", + "indexmap", + "log", + "semver", + "serde", + "serde_derive", + "serde_json", + "unicode-xid", +] diff --git a/example/Cargo.toml b/example/Cargo.toml new file mode 100644 index 0000000..df1679d --- /dev/null +++ b/example/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "example" +authors = ["Brian H "] +description = "" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyhow = "1" +spin-sdk = "3.0.1" +wit-bindgen = "0.32.0" + +[workspace] \ No newline at end of file diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..8bd5820 --- /dev/null +++ b/example/README.md @@ -0,0 +1,4 @@ +``` +spin deps add registry --registry registry-by-karthik.fermyon.app --version ^0.1.0 component:markdown-renderer +spin deps generate-bindings -L rust -o src/bindings -c example +``` diff --git a/example/spin.toml b/example/spin.toml new file mode 100644 index 0000000..678f7a3 --- /dev/null +++ b/example/spin.toml @@ -0,0 +1,21 @@ +spin_manifest_version = 2 + +[application] +name = "example" +version = "0.1.0" +authors = ["Brian H "] +description = "" + +[[trigger.http]] +route = "/..." +component = "example" + +[component.example] +source = "target/wasm32-wasi/release/example.wasm" +allowed_outbound_hosts = [] +[component.example.build] +command = "cargo build --target wasm32-wasi --release" +watch = ["src/**/*.rs", "Cargo.toml"] + +[component.example.dependencies] +"component:markdown-renderer/markdown-fns" = { version = "^0.1.0", registry = "registry-by-karthik.fermyon.app", package = "component:markdown-renderer" } diff --git a/example/src/lib.rs b/example/src/lib.rs new file mode 100644 index 0000000..7d285e4 --- /dev/null +++ b/example/src/lib.rs @@ -0,0 +1,17 @@ +use spin_sdk::http::{IntoResponse, Request, Response}; +use spin_sdk::http_component; + +mod bindings; + +/// A simple Spin HTTP component. +#[http_component] +fn handle_example(req: Request) -> anyhow::Result { + let output_body = bindings::deps::component::markdown_renderer::markdown_fns::render("Hello, Fermyon"); + + println!("Handling request to {:?}", req.header("spin-full-url")); + Ok(Response::builder() + .status(200) + .header("content-type", "text/plain") + .body(output_body) + .build()) +} diff --git a/src/commands/add.rs b/src/commands/add.rs index 0c2751f..7cfaf0e 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -13,7 +13,7 @@ use tokio::fs; use wit_parser::{PackageId, Resolve}; use crate::common::{ - constants::{SPIN_COMPONENTS_WIT_DIRECTORY, SPIN_DEPS_WIT_FILE_NAME, SPIN_WIT_DIRECTORY}, + constants::{SPIN_DEPS_WIT_FILE_NAME, SPIN_WIT_DIRECTORY}, interact::{select_multiple_prompt, select_prompt}, manifest::{edit_component_deps_in_manifest, get_component_ids, get_spin_manifest_path}, wit::{ @@ -61,15 +61,14 @@ impl AddCommand { Some("dependency-world".to_string()), )?; - let component_dir = PathBuf::from(SPIN_WIT_DIRECTORY) - .join(SPIN_COMPONENTS_WIT_DIRECTORY) - .join(selected_component); + let component_dir = PathBuf::from(SPIN_WIT_DIRECTORY).join(selected_component); let output_wit = component_dir.join(SPIN_DEPS_WIT_FILE_NAME); let base_resolve_file = if std::fs::exists(&output_wit)? { Some(&output_wit) } else { + fs::create_dir_all(&component_dir).await?; None }; diff --git a/src/commands/bindings.rs b/src/commands/bindings.rs index a1cb367..2fa9c3c 100644 --- a/src/commands/bindings.rs +++ b/src/commands/bindings.rs @@ -1,5 +1,10 @@ use anyhow::Result; use clap::{Args, ValueEnum}; +use std::path::PathBuf; +use tokio::fs; +use wit_parser::Resolve; + +use crate::common::constants::{SPIN_DEPS_WIT_FILE_NAME, SPIN_WIT_DIRECTORY}; #[derive(Debug, Clone, ValueEnum)] pub enum BindingsLanguage { @@ -8,13 +13,64 @@ pub enum BindingsLanguage { } #[derive(Args, Debug)] -pub struct BindingsCommand { - pub lang: Option, - pub component_id: Option, +pub struct GenerateBindingsCommand { + #[clap(short = 'L', long)] + pub language: BindingsLanguage, + #[clap(short = 'o', long)] + pub output: PathBuf, + #[clap(short = 'c', long)] + pub component_id: String, } -impl BindingsCommand { +impl GenerateBindingsCommand { pub async fn run(&self) -> Result<()> { + let wit_path = PathBuf::from(SPIN_WIT_DIRECTORY) + .join(&self.component_id) + .join(SPIN_DEPS_WIT_FILE_NAME); + + if !std::fs::exists(&wit_path)? { + // TODO: warn that the file does not exist + return Ok(()); + } + + let mut resolve = Resolve::default(); + let package_id = resolve.push_file(&wit_path)?; + + let world_id = resolve.select_world(package_id, Some("deps"))?; + + match &self.language { + BindingsLanguage::Rust => { + let opts = wit_bindgen_rust::Opts { + generate_all: true, + // Uncomment this once spin-sdk is updated and remove dependency on wit_bindgen in Cargo.toml + //runtime_path: Some("::spin_sdk::wit_bindgen".to_string()), + ..Default::default() + }; + + let mut generator = opts.build(); + + let mut files = wit_bindgen_core::source::Files::default(); + generator.generate(&resolve, world_id, &mut files)?; + + fs::create_dir_all(&self.output).await?; + + let mut mod_output = String::new(); + + for (name, contents) in files.iter() { + let output_path = self.output.join(name); + let mod_file = PathBuf::from(name); + let mod_name = mod_file.file_stem().unwrap().to_string_lossy(); + std::fmt::write(&mut mod_output, format_args!("pub mod {mod_name};\n"))?; + fs::write(output_path, contents).await?; + } + + fs::write(self.output.join("mod.rs"), mod_output).await?; + } + BindingsLanguage::Ts => { + todo!("generate ts") + } + } + Ok(()) } } diff --git a/src/common/constants.rs b/src/common/constants.rs index e2175a0..1356d46 100644 --- a/src/common/constants.rs +++ b/src/common/constants.rs @@ -1,3 +1,2 @@ -pub const SPIN_WIT_DIRECTORY: &str = ".wit"; -pub const SPIN_COMPONENTS_WIT_DIRECTORY: &str = "components"; +pub const SPIN_WIT_DIRECTORY: &str = ".wit/components"; pub const SPIN_DEPS_WIT_FILE_NAME: &str = "deps.wit"; diff --git a/src/main.rs b/src/main.rs index 1b5fd68..4249048 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand}; mod commands; mod common; -use commands::{add::AddCommand, bindings::BindingsCommand}; +use commands::{add::AddCommand, bindings::GenerateBindingsCommand}; /// Main CLI structure for command-line argument parsing. #[derive(Parser)] @@ -20,7 +20,7 @@ enum Commands { #[command(subcommand)] Add(AddCommand), /// Generates dependency bindings for selected component - Bindings(BindingsCommand), + GenerateBindings(GenerateBindingsCommand), } #[tokio::main] @@ -29,7 +29,7 @@ async fn main() -> Result<()> { match app.command { Commands::Add(cmd) => cmd.run().await?, - Commands::Bindings(cmd) => cmd.run().await?, + Commands::GenerateBindings(cmd) => cmd.run().await?, } Ok(())