Skip to content

Commit b0d24f7

Browse files
committed
feat(go): prepare-wit-files.sh
1 parent 54b142a commit b0d24f7

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

crates/pluginlab/wit/plugin-api.wit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ interface host-state-plugin {
2828
}
2929

3030
world plugin-api {
31+
// The wasip2 target of TinyGo assumes that the component is targeting wasi:cli/command@0.2.0 world (part of wasi:cli),
32+
// so it needs to include the imports from that world.
33+
// It's only included for the versions of the wit files destined for TinyGo.
34+
// include wasi:cli/imports@0.2.0; // SPECIFIC TinyGo - DO NOT CHANGE THIS LINE
3135
import http-client;
3236
import host-state-plugin;
3337
export plugin;

go_modules/wit/host-api.wit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Code generated by `prepare-wit-files.sh`, from `crates/pluginlab/wit/*.wit`. DO NOT EDIT!
2+
13
package repl:api;
24

35
interface host-state {

go_modules/wit/plugin-api.wit

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Code generated by `prepare-wit-files.sh`, from `crates/pluginlab/wit/*.wit`. DO NOT EDIT!
2+
13
interface plugin {
24
use transport.{plugin-response};
35

@@ -30,7 +32,8 @@ interface host-state-plugin {
3032
world plugin-api {
3133
// The wasip2 target of TinyGo assumes that the component is targeting wasi:cli/command@0.2.0 world (part of wasi:cli),
3234
// so it needs to include the imports from that world.
33-
include wasi:cli/imports@0.2.0;
35+
// It's only included for the versions of the wit files destined for TinyGo.
36+
include wasi:cli/imports@0.2.0; // SPECIFIC TinyGo - DO NOT CHANGE THIS LINE
3437
import http-client;
3538
import host-state-plugin;
3639
export plugin;

go_modules/wit/shared.wit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Code generated by `prepare-wit-files.sh`, from `crates/pluginlab/wit/*.wit`. DO NOT EDIT!
2+
13
interface transport {
24
record plugin-response {
35
status: repl-status,

justfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ build-c-plugins:
4444
#!/usr/bin/env bash
4545
just list-c-plugins|xargs -I {} just build-c-plugin {}
4646

47+
# Prepare the wit files for Go
48+
prepare-wit-files-for-go:
49+
#!/usr/bin/env bash
50+
./scripts/prepare-wit-files.sh -i crates/pluginlab/wit -o go_modules/wit -s "SPECIFIC TinyGo"
51+
4752
# Bundle wit files into a single file `repl:api.wasm`
48-
go-wit-build:
53+
go-wit-build: prepare-wit-files-for-go
4954
#!/usr/bin/env bash
5055
cd go_modules/
5156
wkg wit build

scripts/prepare-wit-files.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
3+
# Script to prepare WIT files by copying and making modifications
4+
# Usage: ./scripts/prepare-wit-files.sh -i <input_dir> -o <output_dir> -s <search_string>
5+
6+
set -e
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
BLUE='\033[0;34m'
13+
NC='\033[0m' # No Color
14+
15+
# Default values
16+
INPUT_DIR=""
17+
OUTPUT_DIR=""
18+
SEARCH_STRING=""
19+
SCRIPT_NAME=$(basename "$0")
20+
21+
# Function to show usage
22+
show_usage() {
23+
echo -e "${BLUE}Usage:${NC}"
24+
echo -e " $SCRIPT_NAME -i <input_dir> -o <output_dir> -s <search_string>"
25+
echo ""
26+
echo -e "${BLUE}Options:${NC}"
27+
echo -e " -i Input directory containing .wit files"
28+
echo -e " -o Output directory for processed .wit files"
29+
echo -e " -s Search string to uncomment (e.g., 'SPECIFIC TinyGo')"
30+
echo ""
31+
echo -e "${BLUE}Examples:${NC}"
32+
echo -e " $SCRIPT_NAME -i ./crates/pluginlab/wit -o ./go_modules/wit -s 'SPECIFIC TinyGo'"
33+
echo -e " $SCRIPT_NAME -i ./src/wit -o ./dist/wit -s 'SPECIFIC Rust'"
34+
echo ""
35+
echo -e "${BLUE}Description:${NC}"
36+
echo -e " Copies .wit files from input directory to output directory and:"
37+
echo -e " 1. Adds a header comment indicating the file is generated"
38+
echo -e " 2. Uncomments lines containing the specified search string"
39+
}
40+
41+
# Parse command line arguments
42+
while getopts "i:o:s:h" opt; do
43+
case $opt in
44+
i) INPUT_DIR="$OPTARG" ;;
45+
o) OUTPUT_DIR="$OPTARG" ;;
46+
s) SEARCH_STRING="$OPTARG" ;;
47+
h) show_usage; exit 0 ;;
48+
\?) echo -e "${RED}Invalid option: -$OPTARG${NC}" >&2; show_usage; exit 1 ;;
49+
:) echo -e "${RED}Option -$OPTARG requires an argument.${NC}" >&2; show_usage; exit 1 ;;
50+
esac
51+
done
52+
53+
# Check if required arguments are provided
54+
if [ -z "$INPUT_DIR" ] || [ -z "$OUTPUT_DIR" ] || [ -z "$SEARCH_STRING" ]; then
55+
echo -e "${RED}Error: Missing required arguments${NC}"
56+
show_usage
57+
exit 1
58+
fi
59+
60+
# Check if input directory exists
61+
if [ ! -d "$INPUT_DIR" ]; then
62+
echo -e "${RED}Error: Input directory '$INPUT_DIR' does not exist${NC}"
63+
exit 1
64+
fi
65+
66+
# Create output directory if it doesn't exist
67+
mkdir -p "$OUTPUT_DIR"
68+
69+
echo -e "${GREEN}Preparing WIT files...${NC}"
70+
echo -e "${BLUE}Input:${NC} $INPUT_DIR"
71+
echo -e "${BLUE}Output:${NC} $OUTPUT_DIR"
72+
echo -e "${BLUE}Search:${NC} $SEARCH_STRING"
73+
echo ""
74+
75+
# Copy all .wit files from input to output
76+
echo -e "${YELLOW}Copying WIT files...${NC}"
77+
cp "$INPUT_DIR"/*.wit "$OUTPUT_DIR/"
78+
79+
# Process each .wit file
80+
for wit_file in "$OUTPUT_DIR"/*.wit; do
81+
if [ -f "$wit_file" ]; then
82+
filename=$(basename "$wit_file")
83+
echo -e "${YELLOW}Processing $filename...${NC}"
84+
85+
# Create a temporary file
86+
temp_file=$(mktemp)
87+
88+
# Add the header comment
89+
echo "// Code generated by \`$SCRIPT_NAME\`, from \`$INPUT_DIR/*.wit\`. DO NOT EDIT!" > "$temp_file"
90+
echo "" >> "$temp_file"
91+
92+
# Add the rest of the file content, uncommenting lines with search string
93+
sed "s|^\\([[:space:]]*\\)//[[:space:]]*\\(.*$SEARCH_STRING.*\\)|\\1\\2|" "$wit_file" >> "$temp_file"
94+
95+
# Replace the original file with the modified content
96+
mv "$temp_file" "$wit_file"
97+
98+
echo -e "${GREEN}✓ Processed $filename${NC}"
99+
fi
100+
done
101+
102+
echo ""
103+
echo -e "${GREEN}✓ All WIT files prepared successfully!${NC}"
104+
echo -e "${YELLOW}Files copied to: $OUTPUT_DIR${NC}"

0 commit comments

Comments
 (0)