From 3ddce21f1d99ddef52e9b95327d02a699d168ac6 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 3 Nov 2023 15:11:14 +0100 Subject: [PATCH 1/9] Demo npm --- integrations/npm/README.md | 1 + integrations/npm/bin/buffrs-linux-x64 | 0 integrations/npm/package.json | 13 +++++++++ integrations/npm/src/main.js | 41 +++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 integrations/npm/README.md create mode 100644 integrations/npm/bin/buffrs-linux-x64 create mode 100644 integrations/npm/package.json create mode 100644 integrations/npm/src/main.js diff --git a/integrations/npm/README.md b/integrations/npm/README.md new file mode 100644 index 00000000..202ed1db --- /dev/null +++ b/integrations/npm/README.md @@ -0,0 +1 @@ +# buffrs diff --git a/integrations/npm/bin/buffrs-linux-x64 b/integrations/npm/bin/buffrs-linux-x64 new file mode 100644 index 00000000..e69de29b diff --git a/integrations/npm/package.json b/integrations/npm/package.json new file mode 100644 index 00000000..32a0e4d3 --- /dev/null +++ b/integrations/npm/package.json @@ -0,0 +1,13 @@ +{ + "name": "buffrs", + "version": "0.0.1", + "description": "buffrs", + "main": "src/main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Mara Schulke ", + "license": "MIT", + "dependencies": {}, + "devDependencies": {} +} diff --git a/integrations/npm/src/main.js b/integrations/npm/src/main.js new file mode 100644 index 00000000..6afd8fdd --- /dev/null +++ b/integrations/npm/src/main.js @@ -0,0 +1,41 @@ +#!/usr/bin/env node + +import { spawnSync } from "child_process"; + +/** + * Returns the executable path which is located inside `node_modules` + * The naming convention is app-${os}-${arch} + * If the platform is `win32` or `cygwin`, executable will include a `.exe` extension. + * @see https://nodejs.org/api/os.html#osarch + * @see https://nodejs.org/api/os.html#osplatform + * @example "x/xx/node_modules/app-darwin-arm64" + */ +function getExePath() { + const arch = process.arch; + let os = process.platform as string; + let extension = ""; + if (["win32", "cygwin"].includes(process.platform)) { + os = "windows"; + extension = ".exe"; + } + + try { + // Since the binary will be located inside `node_modules`, we can simply call `require.resolve` + return require.resolve(`buffrs/bin/buffrs-${os}-${arch}${extension}`); + } catch (e) { + throw new Error( + `Couldn't find application binary inside node_modules for ${os}-${arch}` + ); + } +} + +/** + * Runs the application with args using nodejs spawn + */ +function run() { + const args = process.argv.slice(2); + const processResult = spawnSync(getExePath(), args, { stdio: "inherit" }); + process.exit(processResult.status ?? 0); +} + +run(); From 293a74a2622356b0721c318100079a277ae9cfc8 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Wed, 15 Nov 2023 12:14:25 +0100 Subject: [PATCH 2/9] Release prebuilt binaries on gh release --- .github/workflows/bin.yaml | 65 +++++++++++++++++++++++ .github/workflows/{book.yml => book.yaml} | 0 .github/workflows/{ci.yml => ci.yaml} | 0 3 files changed, 65 insertions(+) create mode 100644 .github/workflows/bin.yaml rename .github/workflows/{book.yml => book.yaml} (100%) rename .github/workflows/{ci.yml => ci.yaml} (100%) diff --git a/.github/workflows/bin.yaml b/.github/workflows/bin.yaml new file mode 100644 index 00000000..302d217d --- /dev/null +++ b/.github/workflows/bin.yaml @@ -0,0 +1,65 @@ +name: Binaries + +on: + release: + types: [created] + +jobs: + build: + name: Build and Release Binaries + runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + - os: ubuntu-latest + target: aarch64-unknown-linux-gnu + - os: ubuntu-latest + target: arm-unknown-linux-gnueabihf + - os: macos-latest + target: x86_64-apple-darwin + - os: macos-latest + target: aarch64-apple-darwin + - os: windows-latest + target: x86_64-pc-windows-msvc + - os: windows-latest + target: i686-pc-windows-msvc + steps: + - uses: actions/checkout@v2 + + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + target: ${{ matrix.target }} + override: true + + - name: Cache + uses: Swatinem/rust-cache@v2 + + - name: Build + run: cargo build --release --target ${{ matrix.target }} + + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.target }} + path: target/${{ matrix.target }}/release/* + + publish: + needs: build + runs-on: ubuntu-latest + steps: + - name: Download Artifacts + uses: actions/download-artifact@v2 + with: + path: artifacts + + - name: Release Binaries + uses: softprops/action-gh-release@v1 + with: + files: artifacts/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/book.yml b/.github/workflows/book.yaml similarity index 100% rename from .github/workflows/book.yml rename to .github/workflows/book.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yaml similarity index 100% rename from .github/workflows/ci.yml rename to .github/workflows/ci.yaml From d29680efc6da75b5ea6ecfb950f099817c3fc29d Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:06:40 +0100 Subject: [PATCH 3/9] Implement npm package that fetches binaries --- .github/workflows/bin.yaml | 2 + {integrations => pkgs}/npm/README.md | 0 .../buffrs-linux-x64 => pkgs/npm/bin/.gitkeep | 0 pkgs/npm/download.js | 50 +++++++++++++++++++ pkgs/npm/package-lock.json | 15 ++++++ {integrations => pkgs}/npm/package.json | 2 +- {integrations => pkgs}/npm/src/main.js | 6 ++- 7 files changed, 72 insertions(+), 3 deletions(-) rename {integrations => pkgs}/npm/README.md (100%) rename integrations/npm/bin/buffrs-linux-x64 => pkgs/npm/bin/.gitkeep (100%) create mode 100644 pkgs/npm/download.js create mode 100644 pkgs/npm/package-lock.json rename {integrations => pkgs}/npm/package.json (80%) rename {integrations => pkgs}/npm/src/main.js (90%) diff --git a/.github/workflows/bin.yaml b/.github/workflows/bin.yaml index 302d217d..c2026be4 100644 --- a/.github/workflows/bin.yaml +++ b/.github/workflows/bin.yaml @@ -3,6 +3,8 @@ name: Binaries on: release: types: [created] + repository_dispatch: + types: [manual-trigger] jobs: build: diff --git a/integrations/npm/README.md b/pkgs/npm/README.md similarity index 100% rename from integrations/npm/README.md rename to pkgs/npm/README.md diff --git a/integrations/npm/bin/buffrs-linux-x64 b/pkgs/npm/bin/.gitkeep similarity index 100% rename from integrations/npm/bin/buffrs-linux-x64 rename to pkgs/npm/bin/.gitkeep diff --git a/pkgs/npm/download.js b/pkgs/npm/download.js new file mode 100644 index 00000000..86c559c9 --- /dev/null +++ b/pkgs/npm/download.js @@ -0,0 +1,50 @@ +const fs = require('fs'); +const https = require('https'); +const os = require('os'); +const pkg = require('./package.json'); + +const download = (url, dest) => { + return new Promise((resolve, reject) => { + const file = fs.createWriteStream(dest); + https.get(url, (response) => { + if (response.statusCode !== 200) { + reject(new Error(`Failed to download: ${response.statusCode}`)); + return; + } + response.pipe(file); + }); + + file.on('finish', () => { + file.close(resolve); + }); + + file.on('error', (err) => { + fs.unlink(dest, () => reject(err)); + }); + }); +}; + +const install = async () => { + const arch = os.arch(); + const version = pkg.version; + + let platform = os.platform(); + let extension = ""; + + if (["win32", "cygwin"].includes(process.platform)) { + os = "windows"; + extension = ".exe"; + } + + const url = `https://github.com/helsing-ai/buffrs/releases/download/v${version}/buffrs-${platform}-${arch}${extension}`; + const file = `./bin/buffrs-${platform}-${arch}${extension}`; + + try { + await download(url, file); + console.log('Buffrs downloaded successfully'); + } catch (e) { + console.error('Failed to download binary:', e); + } +}; + +install() diff --git a/pkgs/npm/package-lock.json b/pkgs/npm/package-lock.json new file mode 100644 index 00000000..0fb51138 --- /dev/null +++ b/pkgs/npm/package-lock.json @@ -0,0 +1,15 @@ +{ + "name": "buffrs", + "version": "0.0.1", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "buffrs", + "version": "0.0.1", + "hasInstallScript": true, + "license": "MIT", + "devDependencies": {} + } + } +} diff --git a/integrations/npm/package.json b/pkgs/npm/package.json similarity index 80% rename from integrations/npm/package.json rename to pkgs/npm/package.json index 32a0e4d3..f3d2e27c 100644 --- a/integrations/npm/package.json +++ b/pkgs/npm/package.json @@ -4,7 +4,7 @@ "description": "buffrs", "main": "src/main.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "preinstall": "node download.js" }, "author": "Mara Schulke ", "license": "MIT", diff --git a/integrations/npm/src/main.js b/pkgs/npm/src/main.js similarity index 90% rename from integrations/npm/src/main.js rename to pkgs/npm/src/main.js index 6afd8fdd..77b2f94a 100644 --- a/integrations/npm/src/main.js +++ b/pkgs/npm/src/main.js @@ -12,8 +12,10 @@ import { spawnSync } from "child_process"; */ function getExePath() { const arch = process.arch; - let os = process.platform as string; + const os = process.platform as string; + let extension = ""; + if (["win32", "cygwin"].includes(process.platform)) { os = "windows"; extension = ".exe"; @@ -24,7 +26,7 @@ function getExePath() { return require.resolve(`buffrs/bin/buffrs-${os}-${arch}${extension}`); } catch (e) { throw new Error( - `Couldn't find application binary inside node_modules for ${os}-${arch}` + `Couldn't find buffrs binary inside node_modules for ${os}-${arch}` ); } } From 9dbc0b785f25c6f9316bb47711503c521da18c6a Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:08:20 +0100 Subject: [PATCH 4/9] Debug workflow --- .github/workflows/bin.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bin.yaml b/.github/workflows/bin.yaml index c2026be4..866af6f2 100644 --- a/.github/workflows/bin.yaml +++ b/.github/workflows/bin.yaml @@ -3,8 +3,8 @@ name: Binaries on: release: types: [created] - repository_dispatch: - types: [manual-trigger] + push: + branches: ["mara/npx"] jobs: build: From 2ea2f185c08747265e1b7b818f725cee9a8a576d Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:13:46 +0100 Subject: [PATCH 5/9] Make ci great again --- .github/workflows/book.yaml | 3 ++- .github/workflows/ci.yaml | 17 ++++++++++------- .github/workflows/nix.yaml | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/nix.yaml diff --git a/.github/workflows/book.yaml b/.github/workflows/book.yaml index f8e39e2d..bcf65770 100644 --- a/.github/workflows/book.yaml +++ b/.github/workflows/book.yaml @@ -3,7 +3,8 @@ name: Book on: push: branches: ["main"] - workflow_dispatch: + paths: + - 'docs/**' permissions: contents: read diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 73601e9e..71149147 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,7 +3,17 @@ name: CI on: push: branches: ["main"] + paths: + - 'Cargo.lock' + - 'Cargo.toml' + - 'deny.toml' + - 'src/**' pull_request: + paths: + - 'Cargo.lock' + - 'Cargo.toml' + - 'deny.toml' + - 'src/**' env: MINIMUM_LINE_COVERAGE_PERCENT: 35 @@ -64,10 +74,3 @@ jobs: - uses: Swatinem/rust-cache@v2 - run: cargo install typos-cli || true - run: typos - nix: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: cachix/install-nix-action@v22 - - uses: DeterminateSystems/magic-nix-cache-action@main - - run: nix flake check diff --git a/.github/workflows/nix.yaml b/.github/workflows/nix.yaml new file mode 100644 index 00000000..79cbb208 --- /dev/null +++ b/.github/workflows/nix.yaml @@ -0,0 +1,21 @@ +name: Nix Flake + +on: + push: + branches: ["main"] + paths: + - 'flake.nix' + - 'flake.lock' + pull_request: + paths: + - 'flake.nix' + +jobs: + nix: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: cachix/install-nix-action@v22 + - uses: DeterminateSystems/magic-nix-cache-action@main + - run: nix flake check + - 'flake.lock' From e04bdc8e56bb33e9db1606939cbb8a8a894c1932 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:16:34 +0100 Subject: [PATCH 6/9] Vendor openssl --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index b1e6e2a4..5ad42349 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ bytes = "1.0" clap = { version = "4.3", features = ["cargo", "derive"] } diff-struct = { version = "0.5.3", optional = true } flate2 = "1" -git2 = { version = "0.18.1", optional = true } +git2 = { version = "0.18.1", features = ["vendored-openssl"], optional = true } hex = "0.4.3" home = "0.5.5" human-panic = "1" From 0ed7abc28eb28975dcb83a2a21b5c82d2b0170d8 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:20:36 +0100 Subject: [PATCH 7/9] Vendor more stuff --- Cargo.lock | 10 ++++++++++ Cargo.toml | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index aa94babe..ca7c02af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1077,6 +1077,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "openssl-src" +version = "300.1.6+3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439fac53e092cd7442a3660c85dde4643ab3b5bd39040912388dcdabf6b88085" +dependencies = [ + "cc", +] + [[package]] name = "openssl-sys" version = "0.9.93" @@ -1085,6 +1094,7 @@ checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index 5ad42349..c3a0abd8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ bytes = "1.0" clap = { version = "4.3", features = ["cargo", "derive"] } diff-struct = { version = "0.5.3", optional = true } flate2 = "1" -git2 = { version = "0.18.1", features = ["vendored-openssl"], optional = true } +git2 = { version = "0.18.1", features = ["vendored-openssl", "vendored-libgit2"], optional = true } hex = "0.4.3" home = "0.5.5" human-panic = "1" From 0a479d7f3141ab9f6249e8cbcc5898c519b27406 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:46:17 +0100 Subject: [PATCH 8/9] Fix binary workflow for linux --- .github/workflows/bin.yaml | 13 ++++++++++++- registry/Cargo.toml | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bin.yaml b/.github/workflows/bin.yaml index 866af6f2..9b4c10ed 100644 --- a/.github/workflows/bin.yaml +++ b/.github/workflows/bin.yaml @@ -6,9 +6,12 @@ on: push: branches: ["mara/npx"] +env: + PKG_CONFIG_ALLOW_CROSS: 1 + jobs: build: - name: Build and Release Binaries + name: Build Binaries runs-on: ubuntu-latest strategy: matrix: @@ -41,6 +44,13 @@ jobs: - name: Cache uses: Swatinem/rust-cache@v2 + - name: Setup Runner + if: runner.os == 'Linux' + run: | + sudo apt-get update + sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf + export PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig + - name: Build run: cargo build --release --target ${{ matrix.target }} @@ -51,6 +61,7 @@ jobs: path: target/${{ matrix.target }}/release/* publish: + name: Publish Binaries needs: build runs-on: ubuntu-latest steps: diff --git a/registry/Cargo.toml b/registry/Cargo.toml index 46cea14a..a87f3c8d 100644 --- a/registry/Cargo.toml +++ b/registry/Cargo.toml @@ -6,7 +6,7 @@ description = "Registry for buffrs, a modern protocol buffer package manager" license = "Apache-2.0" [dependencies] -buffrs = { path = "../", version = "0.6.4" } +buffrs = { path = "../", version = "0.7" } prost = "0.12.1" tonic = "0.10.2" clap = { version = "4.3", features = ["cargo", "derive", "env"] } From 2bca717b9f74bf6d8c730e0463557cac8fb4fa81 Mon Sep 17 00:00:00 2001 From: Mara Schulke Date: Fri, 24 Nov 2023 14:54:55 +0100 Subject: [PATCH 9/9] Install openssl? --- .github/workflows/bin.yaml | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bin.yaml b/.github/workflows/bin.yaml index 9b4c10ed..9d3c9b4e 100644 --- a/.github/workflows/bin.yaml +++ b/.github/workflows/bin.yaml @@ -44,12 +44,25 @@ jobs: - name: Cache uses: Swatinem/rust-cache@v2 - - name: Setup Runner + - name: Setup Linux if: runner.os == 'Linux' run: | sudo apt-get update sudo apt-get install -y gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf - export PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig + echo "PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig" >> $GITHUB_ENV + + - name: Setup macOS + if: runner.os == 'macOS' + run: | + brew install openssl + echo "PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig" >> $GITHUB_ENV + echo "OPENSSL_INCLUDE_DIR=/usr/local/opt/openssl/include" >> $GITHUB_ENV + echo "OPENSSL_LIB_DIR=/usr/local/opt/openssl/lib" >> $GITHUB_ENV + + - name: Setup Windows + if: runner.os == 'Windows' + run: + echo "skipped" - name: Build run: cargo build --release --target ${{ matrix.target }}