Skip to content

Commit

Permalink
ci: add-clarinet sdk ci (#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Jul 8, 2024
1 parent 3756243 commit 33eac47
Show file tree
Hide file tree
Showing 16 changed files with 3,873 additions and 3,793 deletions.
File renamed without changes.
75 changes: 75 additions & 0 deletions .github/workflows/ci-sdk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: CI - Clarinet SDK
on:
pull_request:
branches:
- main
paths-ignore:
- "**/CHANGELOG.md"
push:
branches:
- main
paths-ignore:
- "**/CHANGELOG.md"

workflow_dispatch:

jobs:
pre_run:
name: Cancel previous runs
runs-on: ubuntu-latest
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
persist-credentials: false

build_and_test_sdk:
name: Build and test clarinet-sdk packages
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Rust toolchain
run: |
rustup toolchain install stable --profile minimal --component rustfmt
rustup target add wasm32-unknown-unknown
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Setup cache keys
run: |
echo "RUST_VERSION_HASH=$(rustc --version | sha256sum | awk '{print $1}')" >> $GITHUB_ENV
echo "NODE_VERSION_HASH=$(node --version | sha256sum | awk '{print $1}')" >> $GITHUB_ENV
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/
~/target/release/build/
~/target/wasm32-unknown-unknown/build/
key: clarinet-sdk-cargo-${{ runner.os }}-${{ env.RUST_VERSION_HASH }}-${{ hashFiles('./Cargo.lock') }}

- name: Cache npm
uses: actions/cache@v4
with:
path: |
~/node_modules/
key: clarinet-sdk-npm-${{ runner.os }}-${{ env.NODE_VERSION_HASH }}-${{ hashFiles('./package-lock.json') }}

- name: Install wasm-pack
run: npm install -g wasm-pack

- name: Build Wasm packages
run: npm run build:wasm

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm run test
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/clarinet-files/src/wasm_fs_accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use js_sys::{Function as JsFunction, Promise};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::{from_value as decode_from_js, to_value as encode_to_js};
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;

#[derive(Serialize, Deserialize)]
Expand Down
22 changes: 12 additions & 10 deletions components/clarinet-sdk-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ It powers [@hirosystems/clarinet-sdk](https://npmjs.com/package/@hirosystems/cla

Install [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).

In the root directory of Clarinet, run the following command to build the packages for Node.js and the browser.
Under the hood, it will run `wasm-pack build` twice, once for each target.

```sh
npm run build:wasm
```

Alternatively, it's also possible to build the packages separately:

**Build for node**

```sh
Expand All @@ -23,11 +32,6 @@ wasm-pack build --release --scope hirosystems --out-dir pkg-node --target nodejs
wasm-pack build --release --scope hirosystems --out-dir pkg-browser --target web
```

Run this script to build **both versions**:

```sh
node build.mjs
```

### Use the local version of the package

Expand Down Expand Up @@ -68,14 +72,12 @@ The following script will build for both target, it will also rename the package
browser build.

```sh
cd components/clarinet-sdk-wasm
node build.mjs
npm run build:wasm
```

Once built, the packages can be released by running the following commands. Note that by default we
Once built, the packages can be released by running the following command. Note that by default we
release with the beta tag.

```sh
cd pkg-node && npm publish --tag beta && cd ..
cd pkg-browser && npm publish --tag beta && cd ..
npm run publish:sdk-wasm
```
31 changes: 18 additions & 13 deletions components/clarinet-sdk-wasm/build.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#!/usr/bin/node

import { spawn } from "node:child_process";
import { readFile, rm, writeFile } from "node:fs/promises";
import fs from "node:fs/promises";
import path from "node:path";

// directory of the current file
const rootDir = new URL(".", import.meta.url).pathname;

/**
* build
*/
async function build() {
console.log("Deleting pkg-node");
await rmIfExists("./pkg-node");
await rmIfExists(path.join(rootDir, "pkg-node"));
console.log("Deleting pkg-browser");
await rmIfExists("./pkg-browser");
await rmIfExists(path.join(rootDir, "pkg-browser"));

await Promise.all([
execCommand("wasm-pack", [
Expand Down Expand Up @@ -47,7 +51,9 @@ async function build() {
export const execCommand = async (command, args) => {
console.log(`Building ${args[5]}`);
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args);
const childProcess = spawn(command, args, {
cwd: rootDir,
});
childProcess.stdout.on("data", (data) => {
process.stdout.write(data.toString());
});
Expand All @@ -69,11 +75,11 @@ export const execCommand = async (command, args) => {

/**
* rmIfExists
* @param {string} path
* @param {string} dirPath
*/
async function rmIfExists(path) {
async function rmIfExists(dirPath) {
try {
await rm(path, { recursive: true, force: true });
await fs.rm(dirPath, { recursive: true, force: true });
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
Expand All @@ -85,14 +91,14 @@ async function rmIfExists(path) {
* updatePackageName
*/
async function updatePackageName() {
const filePath = "./pkg-browser/package.json";
const filePath = path.join(rootDir, "pkg-browser/package.json");

const fileData = await readFile(filePath, "utf-8");
const fileData = await fs.readFile(filePath, "utf-8");
const updatedData = fileData.replace(
'"name": "@hirosystems/clarinet-sdk-wasm"',
'"name": "@hirosystems/clarinet-sdk-wasm-browser"'
'"name": "@hirosystems/clarinet-sdk-wasm-browser"',
);
await writeFile(filePath, updatedData, "utf-8");
await fs.writeFile(filePath, updatedData, "utf-8");
console.log("✅ Package name updated successfully.");
}

Expand All @@ -101,8 +107,7 @@ try {
console.log("\n✅ Project successfully built.\n🚀 Ready to publish.");
console.log("Run the following commands to publish");
console.log("\n```");
console.log("$ cd pkg-node && npm publish --tag beta && cd ..");
console.log("$ cd pkg-browser && npm publish --tag beta && cd ..");
console.log("$ npm run publish:sdk-wasm");
console.log("```\n");
} catch (error) {
console.error("❌ Error building:", error);
Expand Down
5 changes: 5 additions & 0 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ impl SDK {
Ok(())
}

#[wasm_bindgen(js_name=getId)]
pub fn get_id(&mut self) -> String {
"hello-world-1".to_string()
}

#[wasm_bindgen(js_name=initSession)]
pub async fn init_session(&mut self, cwd: String, manifest_path: String) -> Result<(), String> {
let cwd_path = PathBuf::from(cwd);
Expand Down
1 change: 0 additions & 1 deletion components/clarinet-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
dist/
node_modules/
2 changes: 1 addition & 1 deletion components/clarinet-sdk/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"license": "GPL-3.0",
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm-browser": "2.8.0-beta1",
"@hirosystems/clarinet-sdk-wasm-browser": "^2.8.0-beta1",
"@stacks/transactions": "^6.13.0"
}
}
2 changes: 1 addition & 1 deletion components/clarinet-sdk/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"license": "GPL-3.0",
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm": "2.8.0-beta1",
"@hirosystems/clarinet-sdk-wasm": "^2.8.0-beta1",
"@stacks/transactions": "^6.13.0",
"kolorist": "^1.8.0",
"prompts": "^2.4.2",
Expand Down
1 change: 1 addition & 0 deletions components/clarinet-sdk/node/tests/simnet-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe("basic simnet interactions", () => {
});

it("exposes devnet stacks accounts", () => {
expect(simnet.getId()).toBe("hello-world-1");
const accounts = simnet.getAccounts();

expect(accounts).toHaveLength(4);
Expand Down
Loading

0 comments on commit 33eac47

Please sign in to comment.