shithub: ricket

Download patch

ref: 04dd2a91ca3d5d9d8dd833f35408ab9f00dc3c50
parent: 669cccaa2d0e9a1508e0180094b6fbf00f10e14e
author: Slashscreen <SlashScreen@users.noreply.github.com>
date: Thu Aug 10 13:02:22 EDT 2023

Draft of package functionalty

--- a/README.md
+++ b/README.md
@@ -14,3 +14,4 @@
 
 - [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
--- /dev/null
+++ b/bin/install.rc
@@ -1,0 +1,5 @@
+
+#!/bin/rc
+mv test /amd64/test/bin
+bind -b /amd64/test/bin /bin
+	
\ No newline at end of file
binary files /dev/null b/bin/test/ricket differ
--- /dev/null
+++ b/bin/test/test
@@ -1,0 +1,3 @@
+
+#!/bin/rc
+ricket run test.wasm $*
binary files /dev/null b/bin/test/test.wasm differ
binary files /dev/null b/ricket.exe differ
--- a/ricket.go
+++ b/ricket.go
@@ -5,8 +5,11 @@
 	"crypto/rand"
 	_ "embed"
 	"fmt"
+	"io"
+	"io/fs"
 	"log"
 	"os"
+	"path"
 
 	"github.com/tetratelabs/wazero"
 	"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
@@ -75,7 +78,77 @@
 }
 
 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]
+
+	os.Mkdir(bin_dir+"/"+program_name, fs.ModeAppend)
+
+	{ // Step 1: Copy wasm file
+		_, wasm_filename := path.Split(wasm_path) // make sure we only get the wasm bit
+		dst := fmt.Sprintf("%s/%s/%s", bin_dir, program_name, wasm_filename)
+		dest_file, err := os.Create(dst)
+		if err != nil {
+			fmt.Printf("Error while copying wasm file: %s", err)
+			return
+		}
+		wasm_file, err := os.Open(wasm_path)
+		if err != nil {
+			fmt.Printf("Error while copying wasm file: %s", 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", err)
+				return
+			}
+			ricket_exec, err := os.Open(ricket_path)
+			if err != nil {
+				fmt.Printf("Error while copying ricket file: %s", err)
+				return
+			}
+			dest_file, err := os.Create(fmt.Sprintf("%s/%s/ricket", bin_dir, program_name))
+			if err != nil {
+				fmt.Printf("Error while copying ricket file: %s", 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", 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/install.rc", bin_dir))
+		if err != nil {
+			fmt.Printf("Error while writing install file: %s", err)
+			return
+		}
+		dst.Write([]byte(output))
+	}
 }
 
 func help() {
@@ -86,3 +159,17 @@
 	ricket help | ? - open this page. Plan 9 users should instead run 'man ricket'.
 	`)
 }
+
+func format_rc(full_path string) string {
+	_, path := path.Split(full_path)
+	return fmt.Sprintf(`#!/bin/rc
+ricket run %s $*
+`, path)
+}
+
+func format_install(name string) string {
+	return fmt.Sprintf(`#!/bin/rc
+mv %s /amd64/%s/bin
+bind -b /amd64/%s/bin /bin
+	`, name, name, name)
+} // TODO: Other architectures
--- a/ricket.troff
+++ b/ricket.troff
@@ -70,8 +70,10 @@
 .B -o
 is passed) with the relevant arguments.
 
-4. an mk file that will finish installation into your system. see
-.BI mk (1)
+4. an install script that will finish installation into your system. 
+Simply type
+.I install
+to finish.
 .RE
 The program itself is the .rc file, allowing the user to simply type
 .I my_program