shithub: ricket

Download patch

ref: 753017483f993c4cdec445aead2a074ed99d0b50
parent: 2c9e7e5c36f1315a2de8d5ff4d8583c100c42821
author: Skyrbunny <tallesttower1@gmail.com>
date: Thu Sep 14 07:29:42 EDT 2023

Added Ori's changes.

--- a/README.md
+++ b/README.md
@@ -6,12 +6,22 @@
 
 Ricket is a frontend and toolset for [wazero](https://github.com/tetratelabs/wazero) tailored specifically for use with Plan 9.
 
+Special thanks to Ori on the #cat-v IRC channel for making this more Plan 9-like.
+
+Installing on a 9front system is easy:
+
+```shell
+git/clone https://github.com/SlashScreen/ricket
+cd ricket
+mk install
+```
+
 ## Project status
 
-Currently it's experimental.
+Ricket is now in beta- it should work, but it may have bugs. If something goes wrong, file a bug report!
 
 ## Project goals
 
 - [x] Be able to run WASI programs at all
 - [X] Be able to run WASI programs on Plan 9
-- [ ] Be able to package WASI programs as standalone apps
+- [X] Be able to package WASI programs as standalone apps
--- a/mkfile
+++ b/mkfile
@@ -1,9 +1,16 @@
-ARCH = amd64
-
-install:V:
-	cp ricket /$ARCH/bin/ricket
+install:V Q:
+    mkdir -p /$objtype/bin/ricket
+	cp ricket /$objtype/bin/ricket/run
+    cp package.rc /$objtype/bin/ricket/package
 	cp ricket.troff /sys/man/1/ricket
+    echo Ricket is now installed on your system.
 
-clean:V:
-    rm /$ARCH/bin/ricket
-    rm /sys/man/1/ricket
+clean:V Q:
+    rm -rf /$objtype/bin/ricket
+    rm -f /sys/man/1/ricket
+    echo Bye, ricket!
+
+build:V Q:
+    echo Please note that go must be installed on the system for this to work.
+    echo Attempting to build ricket from source.
+    GOARCH=$objtype go build .
--- /dev/null
+++ b/package.rc
@@ -1,0 +1,10 @@
+#!/bin/rc
+
+flagfmt='o:out'
+args='wasmfile'
+if(! eval `''{aux/getflags $*} || ! $#* 0)
+	exec aux/usage
+if(~ $#out 0)
+	out = wasm.out
+{echo '#!/bin/ricket/run'; cat $1} > $out
+chmod +x $out
binary files a/ricket b/ricket differ
--- a/ricket.go
+++ b/ricket.go
@@ -4,47 +4,24 @@
 	"context"
 	"crypto/rand"
 	_ "embed"
-	"fmt"
-	"io"
 	"log"
 	"os"
-	"path"
-	"runtime"
 
 	"github.com/tetratelabs/wazero"
 	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
 )
 
-const arch = runtime.GOARCH
-
 func main() {
-	if len(os.Args) == 1 {
-		fmt.Println("Not enough arguments. Type \"ricket help\" for usage.")
-		return
-	}
-
-	switch os.Args[1] {
-	case "run":
-		run_program()
-	case "package":
-		package_file()
-	case "help", "?":
+	if len(os.Args) == 0 {
 		help()
-	default:
-		fmt.Printf("Unknown command: %s", os.Args[1])
+	} else {
+		run(os.Args[0])
 	}
 }
 
-func run_program() {
-	// Check for arguments
-	if len(os.Args) < 3 {
-		log.Println("No path to WASM file provided.")
-		return
-	}
-
-	ctx := context.Background()
-
+func run(path string) {
 	// Instantiate runtime
+	ctx := context.Background()
 	r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter())
 	defer r.Close(ctx)
 
@@ -51,11 +28,21 @@
 	wasi_snapshot_preview1.MustInstantiate(ctx, r)
 
 	// Read program
-	wasm, err := os.ReadFile(os.Args[2])
+	wasm, err := os.ReadFile(path)
 	if err != nil {
 		log.Panicf("failed to read WASM file: %v\n", err)
 	}
 
+	// if present, strip off #! line
+	if len(wasm) > 2 && string(wasm[0:2]) == "#!" {
+		for i := 2; i < len(wasm); i++ {
+			if wasm[i] == byte('\n') {
+				wasm = wasm[i+1:]
+				break
+			}
+		}
+	}
+
 	var wasmArgs []string
 	if len(os.Args) > 3 {
 		wasmArgs = os.Args[3:]
@@ -79,113 +66,9 @@
 	}
 }
 
-func package_file() {
-	// Check for arguments
-	if len(os.Args) < 5 {
-		log.Println("Improper arguments. See `ricket help` or `man ricket`.")
-		return
-	}
-
-	wasm_path := os.Args[2]
-	bin_dir := os.Args[4]
-	program_name := os.Args[3]
-
-	err := os.MkdirAll(bin_dir+"/"+program_name+"/bin", 0777)
-	if err != nil {
-		fmt.Printf("Error while making destination directory: %s\n", err)
-		return
-	}
-
-	{ // Step 1: Copy wasm file
-		_, wasm_filename := path.Split(wasm_path) // make sure we only get the wasm bit
-		dst := fmt.Sprintf("%s/%s/bin/%s", bin_dir, program_name, wasm_filename)
-		dest_file, err := os.Create(dst)
-		if err != nil {
-			fmt.Printf("Error while creating wasm file directory: %s\n", err)
-			return
-		}
-		wasm_file, err := os.Open(wasm_path)
-		if err != nil {
-			fmt.Printf("Error while copying wasm file: %s\n", err)
-			return
-		}
-
-		io.Copy(dest_file, wasm_file)
-	}
-
-	{ // Step 2: Copy ricket file if necessary
-		omit := len(os.Args) == 6 && os.Args[5] == "-o"
-		if !omit {
-			ricket_path, err := os.Executable()
-			if err != nil {
-				fmt.Printf("Error while copying ricket file: %s\n", err)
-				return
-			}
-			ricket_exec, err := os.Open(ricket_path)
-			if err != nil {
-				fmt.Printf("Error while copying ricket file: %s\n", err)
-				return
-			}
-			dest_file, err := os.Create(fmt.Sprintf("%s/%s/bin/ricket", bin_dir, program_name))
-			if err != nil {
-				fmt.Printf("Error while copying ricket file: %s\n", err)
-				return
-			}
-
-			io.Copy(dest_file, ricket_exec)
-		}
-	}
-
-	{ // Step 3: Write RC file
-		dst := fmt.Sprintf("%s/%s/%s", bin_dir, program_name, program_name)
-		rc, err := os.Create(dst)
-		if err != nil {
-			fmt.Printf("Error while creating rc file: %s\n", err)
-			return
-		}
-		rc.Write([]byte(format_rc(wasm_path)))
-	}
-
-	{ // Step 4: Write install file
-		output := format_install(program_name)
-		dst, err := os.Create(fmt.Sprintf("%s/%s/mkfile", bin_dir, program_name))
-		if err != nil {
-			fmt.Printf("Error while writing install file: %s\n", err)
-			return
-		}
-		dst.Write([]byte(output))
-	}
-}
-
 func help() {
 	println(`
-usage:
-	ricket run path [ args ... ] - run a .wasm file at <path>, passing in any arguments.
-	ricket package path name bin_dir [ -o ] - package a .wasm file at <path> into a standalone program called <name> at <bin_dir>, <-o>ptionally <-o>mitting the copied ricket executable.
-	ricket help | ? - open this page. Plan 9 users should instead run 'man ricket'.
+usage: ricket/run path [ args ... ]
+run 'man ricket' for rmore info.
 	`)
-}
-
-func format_rc(full_path string) string {
-	_, path := path.Split(full_path)
-	return fmt.Sprintf(`#!/bin/rc
-bin/ricket run bin/%s $*
-`, path)
-}
-
-func format_install(name string) string {
-	return fmt.Sprintf(
-		`
-ARCH = %s
-USER = glenda
-PROJECT = %s
-
-install:V:
-	mkdir -p /$ARCH/$PROJECT/bin
-	for (f in bin/*) cp $f /$ARCH/$PROJECT/bin
-	cp $PROJECT /$ARCH/$PROJECT
-	chmod +x /$ARCH/$PROJECT/$PROJECT
-	echo bind -b /$ARCH/$PROJECT /bin >> /usr/$USER/lib/profile
-`,
-		arch, name)
 }
--- a/ricket.troff
+++ b/ricket.troff
@@ -13,26 +13,24 @@
 ]
 
 .PP
-.B ricket package path name bin_dir
+.B ricket package
 [
-.B -o 
-.I omit
+.B -o
+.I output
 ]
+path
 
-.PP
-.B ricket
-.IR help | ? 
 
 .SH DESCRIPTION
 
 .PP
-.I ricket 
-is a WASI runtime for Plan 9 from Bell Labs, which allows for running CLI 
+.I ricket
+is a WASI runtime for Plan 9 from Bell Labs, which allows for running CLI
 applications written in languages that cannot natively compile to Plan 9.
 
 .PP
 .B ricket run
-will run the .wasm file at the path specified by 
+will run the .wasm file at the path specified by
 .BR path .
 Any
 .B args
@@ -39,59 +37,15 @@
 will be passed into the program.
 
 .PP
-.B ricket package
-will bundle the .wasm file at the path specified by
-.B path
-with a copy of the 
-.I ricket 
-executable, creating a standalone application with the name
-.B name
-and deposit the contents at
-.BR bin_dir . 
-Passing in
+.B ricket/package
+will change the wasm file in
+.BR path .
+to make it act as an executable, assuming ricket is installed on the system.
 .B -o
-will omit the 
-.I ricket 
-executable, relying on an already-installed and globally-accessible 
-.IR ricket .
-This will make the program smaller, at the cost of no longer being standalone.
-.PP
-In-depth, this process creates these files:
-.RS
-1. A copy of the
-.I ricket 
-executable (by default).
+is the optional output file path.
 
-2. A copy of the desired .wasm file.
-
-3. An .rc file that calls the 
-.I ricket 
-executable (or otherwise installed if
-.B -o
-is passed) with the relevant arguments.
-
-4. a mkfile that will finish installation into your system. 
-Simply type
-.I mk install
-to finish.
-.RE
-The program itself is the .rc file, allowing the user to simply type
-.I my_program
-instead of
-.I ricket run my_program.wasm
-, and allow for easy (if large) distribution.
-
-.PP
-.B ricket
-.IR help | ? 
-will open a brief help page. This is intended for non-Plan 9 users of 
-.IR ricket ; 
-Plan 9 users should use
-.B man ricket
-instead to open this page.
-
 .SH BUGS
 
 While not strictly a bug,
 .I ricket
-must interperet the WASM file, which can be slow as dirt. Perhaps JIT compilation will be availible in the future.
\ No newline at end of file
+must interperet the WASM file, which can be slow as dirt. Perhaps JIT compilation will be availible in the future.