Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scaffold script #112

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ in Rust.

### QuickStart

If you want to create a scaffold (with github action support) automatically you can run:

```sh
deno run --allow-read=. --allow-write=. https://deno.land/x/deno_bindgen/scaffold/scaffold.ts my_project_name
```

Annotate on top of Rust `fn`, `struct` and `enum` to make them available to Deno.

```rust
Expand Down
112 changes: 112 additions & 0 deletions scaffold/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Release libs

on:
workflow_dispatch:
inputs:
tag:
description: "tag name"
required: true

jobs:
build:
name: Release libs
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v2
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

- uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Build
uses: actions-rs/cargo@v1
with:
command: build
args: --release

- name: Build MacOS x86_64
if: runner.os == 'MacOS'
run: |
mv target/release/lib$NAME_HERE.dylib lib$NAME_HERE_x86_64.dylib

- name: Upload MacOS x86_64
if: runner.os == 'MacOS'
uses: svenstaro/upload-release-action@v2
with:
file: lib$NAME_HERE_x86_64.dylib
tag: ${{ github.event.inputs.tag }}
overwrite: true

- name: Upload Linux
if: runner.os == 'Linux'
uses: svenstaro/upload-release-action@v2
with:
file: target/release/lib$NAME_HERE.so
tag: ${{ github.event.inputs.tag }}
overwrite: true

- name: Upload Windows
if: runner.os == 'Windows'
uses: svenstaro/upload-release-action@v2
with:
file: target/release/$NAME_HERE.dll
tag: ${{ github.event.inputs.tag }}
overwrite: true

#uncomment to build for macos arm
#- uses: goto-bus-stop/setup-zig@v1
# with:
# version: 0.9.0

#- name: Build MacOS aarch64
# if: runner.os == 'Linux'
# run: |
# rustup target add aarch64-apple-darwin
# cargo install cargo-zigbuild
# cargo zigbuild --release --target aarch64-apple-darwin
# mv target/aarch64-apple-darwin/release/lib$NAME_HERE.dylib lib$NAME_HERE_aarch64.dylib

#- name: Upload MacOS aarch64
# if: runner.os == 'Linux'
# uses: svenstaro/upload-release-action@v2
# with:
# file: lib$NAME_HERE_aarch64.dylib
# tag: ${{ github.event.inputs.tag }}
# overwrite: true


#uncomment to automaticly generate bindings
#- name: Setup Deno
# if: runner.os == 'Linux'
# uses: denoland/setup-deno@v1

#- name: Generate bindings
# if: runner.os == 'Linux'
# run: |
# libUrl=https://github.com/$GITHUB_REPOSITORY/releases/download/${{ github.event.inputs.tag }}
# touch src/lib.rs # Required to trigger cargo build correctly (to generate bindings.json)
# deno run -A -r https://deno.land/x/deno_bindgen/cli.ts --release=$libUrl

#- name: Commit bindings
# if: runner.os == 'Linux'
# run: |
# git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
# git config --local user.name "github-actions[bot]"
# git add bindings/bindings.ts
# git commit -m "update bindings to ${{ github.event.inputs.tag }}"

#- name: Push changes
# if: runner.os == 'Linux'
# uses: ad-m/github-push-action@master
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# branch: ${{ github.ref }}


77 changes: 77 additions & 0 deletions scaffold/scaffold.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { basename } from "https://deno.land/[email protected]/path/mod.ts";

const libPath = Deno.args[0];
if (!libPath) throw new Error("no project name provided");
const libName = basename(libPath);

// 1- Create cargo fixture
try {
Deno.statSync(libPath);
console.error("Path already exists: ", libPath);
Deno.exit(1);
} catch { /**/ }
Deno.mkdirSync(libPath + "/src", { recursive: true });

// 2- Create lib.rs
Deno.writeTextFileSync(
libPath + "/src/lib.rs",
`\
// add.rs
use deno_bindgen::deno_bindgen;

#[deno_bindgen]
pub struct Input {
a: i32,
b: i32,
}

#[deno_bindgen]
fn add(input: Input) -> i32 {
input.a + input.b
}`,
);

// 3- Create Cargo.toml
Deno.writeTextFileSync(
libPath + "/Cargo.toml",
`\
[package]
name = "${libName}"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
deno_bindgen = "0.8.1"
serde = { version = "1", features = ["derive"] }

[lib]
crate-type = ["cdylib"]`,
);

// 4- Create mod.ts
Deno.writeTextFileSync(
libPath + "/mod.ts",
`\
// add.ts
import { add } from "./bindings/bindings.ts";

console.log(
add({ a: 1, b: 2 }),
);`,
);

// 5- Create github action
Deno.mkdirSync(libPath + "/.github/workflows", { recursive: true });
const githubAction = await fetch(
import.meta.resolve("./action.yml"),
).then((r) => r.text()).then((a) => a.replaceAll("$NAME_HERE", libName));
Deno.writeTextFileSync(
libPath + "/.github/workflows/release.yml",
githubAction,
);

console.log("1- cd ", libPath);
console.log("2- Run deno_bindgen to develop locally");
console.log("3- Run deno run --unstable mod.ts");