Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
55 changes: 36 additions & 19 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,40 +1,47 @@
name: CI

on:
push:
tags:
- "v*.*.*"
branches:
- main
pull_request:

permissions:
checks: write
pull-requests: write
env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: Swatinem/rust-cache@v2
with:
prefix-key: v1-rust
prefix-key: v1-rust-${{ matrix.os }}
shared-key: debug
cache-all-crates: true
- name: Setup Rust
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
- name: Install moonbit
components: rustfmt, clippy
- name: Install moonbit (Unix)
if: runner.os != 'Windows'
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.19
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.24+012953835
echo "$HOME/.moon/bin" >> $GITHUB_PATH
- name: Install moonbit (Windows)
if: runner.os == 'Windows'
shell: powershell
run: |
$env:MOONBIT_INSTALL_VERSION = "0.6.24+012953835"
irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
echo "$env:USERPROFILE\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Bundle core MoonBit library
run: moon bundle --target wasm
working-directory: core
Expand All @@ -46,28 +53,38 @@ jobs:
run: cargo build --all-features --all-targets
test:
needs: [build]
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: Swatinem/rust-cache@v2
with:
prefix-key: v1-rust
prefix-key: v1-rust-${{ matrix.os }}
shared-key: debug
cache-all-crates: false
- name: Setup Rust
uses: actions-rs/toolchain@v1
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
override: true
components: rustfmt, clippy
- uses: cargo-bins/cargo-binstall@main
- name: Install wasmtime-cli
run: cargo binstall --force --locked [email protected]
- name: Install moonbit
- name: Install moonbit (Unix)
if: runner.os != 'Windows'
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.19
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.24+012953835
echo "$HOME/.moon/bin" >> $GITHUB_PATH
- name: Install moonbit (Windows)
if: runner.os == 'Windows'
shell: powershell
run: |
$env:MOONBIT_INSTALL_VERSION = "0.6.24+012953835"
irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
echo "$env:USERPROFILE\.moon\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Bundle core MoonBit library
run: moon bundle --target wasm
working-directory: core
Expand All @@ -83,7 +100,7 @@ jobs:
publish:
needs: [test]
if: "startsWith(github.ref, 'refs/tags/v')"
runs-on: ubuntu-latest
runs-on: ubuntu-latest # Publish on Ubuntu only
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -96,7 +113,7 @@ jobs:
override: true
- name: Install moonbit
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.19
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.24
echo "$HOME/.moon/bin" >> $GITHUB_PATH
- name: Bundle core MoonBit library
run: moon bundle --target wasm
Expand Down
5 changes: 5 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tools]
rust = "stable"

[env]
PATH = "/home/{{env.USER}}/.moon/bin:{{env.PATH}}"
83 changes: 77 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,80 @@ fn main() {
- `core` is a git submodule containing the MoonBit core library (https://github.com/moonbitlang/core)
- `bundled-core` is the MoonBit core library (source and compiled for wasm), included in this repository to avoid users of the crate from having to build `core` themselves.

To update and build the MoonBit core library:
### System Requirements

**NOTE**: requires the 0.6.19 version of MoonBit currently
#### Debian/Ubuntu
The following system packages are required:
```bash
# Essential build tools and C compiler for Rust build dependencies
sudo apt-get update && sudo apt-get install -y build-essential

# Optional but recommended: curl for downloading tools
sudo apt-get install -y curl

# Git for submodule management
sudo apt-get install -y git
```

**Note**: The project has been tested on Debian 12 (Bookworm) with kernel 6.1.0-38-amd64 and works correctly with the standard Debian package repositories.

### Development Setup

1. **Install mise** (development environment manager):
```bash
# Install mise if not already present
curl https://mise.run | sh
```

2. **Install development tools** via mise:
```bash
# This installs Rust stable toolchain as configured in .mise.toml
mise install
```

3. **Install MoonBit** (version 0.6.24):
```bash
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.24
# Add to PATH (the installer does this automatically for new shells)
export PATH="$HOME/.moon/bin:$PATH"
```

4. **Initialize git submodules** and build the core library:
```bash
git submodule update --init --recursive
cd core && moon bundle --target wasm && cd ..
```

5. **Install test dependencies**:
```bash
# Install cargo-binstall for faster binary installation
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash

# Install wasmtime-cli with required features
cargo binstall --force --locked [email protected] -y
```

### Building and Testing

```bash
# Format check
cargo fmt -- --check

# Lint check
cargo clippy -- -Dwarnings

# Build all features and targets
cargo build --all-features --all-targets

# Run tests (requires wasmtime-cli)
cargo test -p moonbit-component-generator -- --nocapture --test-threads=1
```
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s -- 0.6.19

### Updating the MoonBit Core Library

To update and rebuild the MoonBit core library bundle:

```bash
git submodule update --recursive
./update-bundle.sh
```
Expand All @@ -85,6 +153,9 @@ The bundled core library is included in the compiled crate using the `include_di
It is also pushed into the repository (`bundled-core` directory) to avoid users of the crate from having to build the MoonBit core themselves as part
of the Rust build process.

Running the tests require `wasmtime-cli` to be installed with the following features enabled:
- `component-model`
- `wasi-config`
### Notes

- Running tests requires `wasmtime-cli` version 33.0.0 with the following features enabled:
- `component-model`
- `wasi-config`
- The MoonBit compiler runs in WASM on V8, so the first compilation in a session may take longer due to V8 initialization
80 changes: 80 additions & 0 deletions bundled-core/.githooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# MoonBit Git Hooks

This directory contains Git hooks to ensure code quality and consistency in the MoonBit core library.

## Setup

To enable the hooks for this repository, run:

```bash
./.githooks/setup.sh
```

Or manually configure:

```bash
git config core.hooksPath .githooks
chmod +x .githooks/*
```

## Available Hooks

### pre-commit

Runs before each commit to ensure code quality:

- ✅ **moon check** - Validates code syntax, types, and formatting
- ❌ **Blocks commit** if any issues are found
- 💡 **Suggests fixes** like running `moon fmt`

### Usage

The hooks run automatically when you commit:

```bash
git commit -m "your message"
# → Runs moon check automatically
```

To bypass hooks temporarily (not recommended for production):

```bash
git commit --no-verify -m "your message"
```

## Troubleshooting

### Hook fails with "moon command not found"

Install the MoonBit toolchain:
- Visit: https://www.moonbitlang.com/download/
- Follow the installation instructions for your platform

### Hook fails due to formatting issues

Run the auto-formatter:

```bash
moon fmt
```

### Hook fails due to type errors

Fix the reported type errors and try committing again. The hook output will show specific error locations.

## Contributing

When adding new hooks:

1. Create the hook file in `.githooks/`
2. Make it executable: `chmod +x .githooks/hook-name`
3. Update this README
4. Test the hook thoroughly

## Philosophy

These hooks enforce quality standards to:
- 🐛 **Catch errors early** before they reach CI/CD
- 🎨 **Maintain consistent formatting** across the codebase
- ⚡ **Speed up development** by providing immediate feedback
- 🤝 **Help contributors** follow project conventions
88 changes: 88 additions & 0 deletions bundled-core/.githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

# Enhanced pre-commit hook for MoonBit
# This version also runs formatting and provides more detailed feedback
# To use this instead of the basic version, rename it to 'pre-commit'

set -e

echo "🌙 Running MoonBit pre-commit checks (enhanced)..."

# Check if moon is available
if ! command -v moon &> /dev/null; then
echo "❌ Error: 'moon' command not found. Please install MoonBit toolchain."
echo " Visit: https://www.moonbitlang.com/download/"
exit 1
fi

# Check for staged files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(mbt|mbti)$' || true)

# If no mbt/mbti files are staged, exit
if [ -z "$STAGED_FILES" ]; then
echo "ℹ️ No MoonBit files staged for commit."
exit 0
fi

echo "📁 Found staged MoonBit files:"
echo "$STAGED_FILES" | sed 's/^/ - /'
echo ""

# Run formatting first
echo "🎨 Running 'moon fmt'..."
if ! moon fmt; then
echo "❌ Formatting failed! Please check the output above."
exit 1
fi

# Check if formatting changed any files
CHANGED_FILES=$(git diff --name-only | grep -E '\.(mbt|mbti)$' || true)
if [ -n "$CHANGED_FILES" ]; then
echo "⚠️ Formatting changes detected. Please stage the formatted files:"
echo "$CHANGED_FILES" | sed 's/^/ - /'
echo ""
echo "💡 Run: git add . && git commit"
exit 1
fi

# Run moon check and capture output
echo "📋 Running 'moon check'..."
CHECK_OUTPUT=$(moon check --target all 2>&1)
CHECK_EXIT_CODE=$?

if [ $CHECK_EXIT_CODE -ne 0 ]; then
echo ""
echo "❌ Pre-commit hook failed!"
echo " 'moon check' found issues in your code:"
echo ""
echo "📝 Error details:"
echo "$CHECK_OUTPUT" | sed 's/^/ /'
echo ""
echo "💡 Common fixes:"
echo " - Check for type errors and fix them"
echo " - Ensure all imports are valid"
echo " - Run 'moon fmt' if formatting issues persist"
echo " - Review the error messages above for specific issues"
exit 1
else
echo " $CHECK_OUTPUT"
fi

# Optional: Run tests on changed modules (uncomment if desired)
# echo "🧪 Running tests for changed modules..."
# for file in $STAGED_FILES; do
# MODULE_DIR=$(dirname "$file")
# if [ -f "$MODULE_DIR/moon.pkg.json" ]; then
# echo " Testing $MODULE_DIR..."
# if ! (cd "$MODULE_DIR" && moon test); then
# echo "❌ Tests failed in $MODULE_DIR"
# exit 1
# fi
# fi
# done

echo "✅ All checks passed! Proceeding with commit..."
echo " 📁 Files: $(echo "$STAGED_FILES" | wc -l | xargs) MoonBit files"
echo " 🎨 Formatted: ✓"
echo " 📋 Checked: ✓"
echo ""
Loading