Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.wasm
ruby.wasm
/rubies
/build
/dist
2 changes: 1 addition & 1 deletion examples/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
ruby_wasm_ui (0.8.3)
ruby_wasm_ui (0.9.0)
js (~> 2.7)
listen (~> 3.8)
puma (~> 6.0)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>My App</title>
<script type="module">
import { DefaultRubyVM } from "https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.2/dist/browser/+esm";
const response = await fetch("../src.wasm");
const response = await fetch("./src.wasm");
const module = await WebAssembly.compileStreaming(response);
const { vm } = await DefaultRubyVM(module);
vm.evalAsync(`
Expand Down
76 changes: 47 additions & 29 deletions lib/ruby_wasm_ui/cli/command/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "fileutils"
require "open3"
require "bundler/setup"
require "ruby_wasm"
Expand Down Expand Up @@ -74,35 +75,6 @@ def ensure_ruby_wasm
end
end

def update_gitignore(entries_to_add)
gitignore_path = ".gitignore"

# Read existing .gitignore or create new content
if File.exist?(gitignore_path)
content = File.read(gitignore_path)
lines = content.lines.map(&:chomp)
else
lines = []
end

# Add entries that don't already exist
added_entries = []
entries_to_add.each do |entry|
unless lines.include?(entry)
lines << entry
added_entries << entry
end
end

# Write back to .gitignore
File.write(gitignore_path, lines.join("\n") + "\n")
if added_entries.any?
log_info("Added to .gitignore: #{added_entries.join(', ')}")
else
log_info("No new entries added to .gitignore (all entries already exist)")
end
end

protected

def log_info(message)
Expand Down Expand Up @@ -168,6 +140,52 @@ def build_ruby_wasm(ruby_version_str)
cli = RubyWasm::CLI.new(stdout: $stdout, stderr: $stderr)
cli.run(command)
end

def ensure_dist_directory
unless Dir.exist?("dist")
Dir.mkdir("dist")
log_info("Created dist directory")
end
end

def copy_non_ruby_files
log_info("Copying non-Ruby files from src to dist...")

copied_files = []
Dir.glob("src/**/*").each do |src_path|
next if File.directory?(src_path)
next if src_path.end_with?(".rb")

# Get relative path from src directory
relative_path = src_path.sub(/^src\//, "")
dest_path = File.join("dist", relative_path)

# Create destination directory if needed
dest_dir = File.dirname(dest_path)
FileUtils.mkdir_p(dest_dir) unless Dir.exist?(dest_dir)

# Copy file
FileUtils.cp(src_path, dest_path)
copied_files << relative_path
end

if copied_files.any?
log_success("✓ Copied #{copied_files.size} file(s): #{copied_files.join(', ')}")
else
log_info("No non-Ruby files to copy")
end
end

def pack_wasm(exit_on_error: true, log_prefix: "Packing")
command = "bundle exec rbwasm pack ruby.wasm --dir ./src::./src -o dist/src.wasm"
log_info("#{log_prefix}: #{command}")

success = run_command(command, exit_on_error: exit_on_error)
if success
log_success("✓ Pack completed")
end
success
end
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions lib/ruby_wasm_ui/cli/command/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def run(_argv)
puts ""

ensure_src_directory
ensure_ruby_wasm

# Initial build
log_info("Performing initial build...")
Expand Down Expand Up @@ -76,11 +77,11 @@ def cleanup
end

def build
command = "bundle exec rbwasm pack ruby.wasm --dir ./src::./src -o src.wasm"
log_info("Building: #{command}")
ensure_dist_directory

success = run_command(command, exit_on_error: false)
success = pack_wasm(exit_on_error: false, log_prefix: "Building")
if success
copy_non_ruby_files
log_success("✓ Build completed")
else
log_error("Build failed")
Expand Down Expand Up @@ -132,11 +133,11 @@ def start_server
# Static file server application
static_app = lambda do |env|
path = env["PATH_INFO"]
if path == "/"
file_path = File.join(Dir.pwd, "src", "index.html")
else
file_path = File.join(Dir.pwd, path)
end
# Remove leading slash and handle root path
relative_path = path == "/" ? "index.html" : path.sub(/^\//, "")

# Serve files from dist directory
file_path = File.join(Dir.pwd, "dist", relative_path)

if File.exist?(file_path) && File.file?(file_path)
content_type = Rack::Mime.mime_type(File.extname(file_path), "text/html")
Expand Down Expand Up @@ -187,7 +188,7 @@ def start_server
# Open browser after a short delay to ensure server is ready
Thread.new do
sleep 1
open_browser("http://localhost:#{port}/src/index.html")
open_browser("http://localhost:#{port}/index.html")
end

Rack::Handler::Puma.run(app, Port: port, Host: "0.0.0.0")
Expand Down
10 changes: 5 additions & 5 deletions lib/ruby_wasm_ui/cli/command/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ def run(_argv)

ensure_src_directory
ensure_ruby_wasm
ensure_dist_directory

# Pack WASM file
pack

# Copy non-Ruby files from src to dist
copy_non_ruby_files
end

private

def pack
command = "bundle exec rbwasm pack ruby.wasm --dir ./src::./src -o src.wasm"
log_info("Packing: #{command}")

run_command(command)
log_success("✓ Pack completed")
pack_wasm(exit_on_error: true, log_prefix: 'Packing')
end
end
end
Expand Down
33 changes: 31 additions & 2 deletions lib/ruby_wasm_ui/cli/command/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def run(_argv)
# Update .gitignore
puts ""
log_info("Step 3/4: Updating .gitignore...")
update_gitignore(["*.wasm", "/rubies", "/build"])
update_gitignore(["ruby.wasm", "/rubies", "/build", "/dist"])
log_success("✓ .gitignore updated")

# Create initial files
Expand All @@ -46,6 +46,35 @@ def run(_argv)

private

def update_gitignore(entries_to_add)
gitignore_path = ".gitignore"

# Read existing .gitignore or create new content
if File.exist?(gitignore_path)
content = File.read(gitignore_path)
lines = content.lines.map(&:chomp)
else
lines = []
end

# Add entries that don't already exist
added_entries = []
entries_to_add.each do |entry|
unless lines.include?(entry)
lines << entry
added_entries << entry
end
end

# Write back to .gitignore
File.write(gitignore_path, lines.join("\n") + "\n")
if added_entries.any?
log_info("Added to .gitignore: #{added_entries.join(', ')}")
else
log_info("No new entries added to .gitignore (all entries already exist)")
end
end

def create_initial_files
# Skip if src directory exists
if Dir.exist?("src")
Expand Down Expand Up @@ -76,7 +105,7 @@ def create_initial_files
<title>My App</title>
<script type="module">
import { DefaultRubyVM } from "https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.2/dist/browser/+esm";
const response = await fetch("../src.wasm");
const response = await fetch("./src.wasm");
const module = await WebAssembly.compileStreaming(response);
const { vm } = await DefaultRubyVM(module);
vm.evalAsync(`
Expand Down
Loading
Loading