Skip to content

Commit

Permalink
fix: fix binary installation
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 22, 2024
1 parent 3e9608b commit 2fbdaf1
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 67 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,28 @@ jobs:
run: |
if [[ "${{ matrix.platform }}" == "ubuntu-latest" ]]; then
if [[ "${{ matrix.arch }}" == "x64" ]]; then
mkdir -p ./build/linux/x64
cp target/x86_64-unknown-linux-gnu/release/todoctor ./build/linux/x64/todoctor
mkdir -p ./bin/linux/x64
cp target/x86_64-unknown-linux-gnu/release/todoctor ./bin/linux/x64/todoctor
elif [[ "${{ matrix.arch }}" == "arm64" ]]; then
mkdir -p ./build/linux/arm64
cp target/aarch64-unknown-linux-gnu/release/todoctor ./build/linux/arm64/todoctor
mkdir -p ./bin/linux/arm64
cp target/aarch64-unknown-linux-gnu/release/todoctor ./bin/linux/arm64/todoctor
fi
elif [[ "${{ matrix.platform }}" == "macos-latest" ]]; then
if [[ "${{ matrix.arch }}" == "x64" ]]; then
mkdir -p ./build/macos/x64
cp target/x86_64-apple-darwin/release/todoctor ./build/macos/x64/todoctor
mkdir -p ./bin/macos/x64
cp target/x86_64-apple-darwin/release/todoctor ./bin/macos/x64/todoctor
elif [[ "${{ matrix.arch }}" == "arm64" ]]; then
mkdir -p ./build/macos/arm64
cp target/aarch64-apple-darwin/release/todoctor ./build/macos/arm64/todoctor
mkdir -p ./bin/macos/arm64
cp target/aarch64-apple-darwin/release/todoctor ./bin/macos/arm64/todoctor
fi
fi
shell: bash

- name: Move Binaries to Bin Folder (Windows)
if: runner.os == 'Windows'
run: |
mkdir build\windows\x64
copy target\x86_64-pc-windows-msvc\release\todoctor.exe build\windows\x64\todoctor.exe
mkdir bin\windows\x64
copy target\x86_64-pc-windows-msvc\release\todoctor.exe bin\windows\x64\todoctor.exe
shell: cmd

- name: Upload Binaries
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ node_modules/
target/

# Build
build/
!bin/todoctor.js
dist/
bin/

Expand Down
46 changes: 46 additions & 0 deletions bin/todoctor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node

import { spawn } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import os from 'node:os'
import fs from 'node:fs'

let filename = fileURLToPath(import.meta.url)
let dirname = path.dirname(filename)

let platform = os.platform()
let arch = os.arch()

let binaries = {
'win32:x64': 'windows/x64/todoctor.exe',
'darwin:arm64': 'macos/arm64/todoctor',
'linux:arm64': 'linux/arm64/todoctor',
'darwin:x64': 'macos/x64/todoctor',
'linux:x64': 'linux/x64/todoctor',
}

let key = `${platform}:${arch}`
let relativePath = binaries[key]

if (!relativePath) {
console.error(`Unsupported platform or architecture: ${platform}, ${arch}`)
process.exit(1)
}

let binaryPath = path.join(dirname, relativePath)

if (!fs.existsSync(binaryPath)) {
console.error(`Binary not found: ${binaryPath}`)
console.error(
'Please ensure that the postinstall script has run successfully.',
)
process.exit(1)
}

let args = process.argv.slice(2)
let child = spawn(binaryPath, args, { stdio: 'inherit' })

child.on('exit', code => {
process.exit(code)
})
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
"start": "pnpm run /^start:/",
"start:preview": "vite",
"build": "pnpm run /^build:/",
"build:lib": "cargo build --release && cp target/release/todoctor ./bin/",
"build:lib": "cargo build --release && node ./scripts/build",
"build:preview": "vite build",
"postinstall": "node ./scripts/install.js",
"release": "pnpm release:check && pnpm release:version",
"release:check": "pnpm test && pnpm run build",
"release:version": "changelogen --output changelog.md --release --push",
Expand All @@ -42,6 +41,15 @@
"test:types": "tsc --noEmit --pretty",
"test:unit": "cargo test"
},
"os": [
"darwin",
"linux",
"win32"
],
"cpu": [
"x64",
"arm64"
],
"publishConfig": {
"access": "public"
},
Expand Down
58 changes: 58 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from 'node:path'
import fs from 'node:fs'
import os from 'node:os'

let platform = os.platform()
let arch = os.arch()

let platformMap = {
win32: 'windows',
darwin: 'macos',
linux: 'linux',
}

let archMap = {
arm64: 'arm64',
x64: 'x64',
}

let platformName = platformMap[platform]
let archName = archMap[arch]

if (!platformName || !archName) {
console.error(`Unsupported platform or architecture: ${platform}, ${arch}`)
process.exit(1)
}

let binaryName = platform === 'win32' ? 'todoctor.exe' : 'todoctor'

let sourcePath = path.join(
import.meta.dirname,
'..',
'target',
'release',
binaryName,
)
let destDir = path.join(
import.meta.dirname,
'..',
'bin',
platformName,
archName,
)
let destPath = path.join(destDir, binaryName)

if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true })
}

try {
fs.copyFileSync(sourcePath, destPath)

if (platform !== 'win32') {
fs.chmodSync(destPath, 0o755)
}
} catch (error) {
console.error(`Error copying file: ${error.message}`)
process.exit(1)
}
48 changes: 0 additions & 48 deletions scripts/install.js

This file was deleted.

14 changes: 8 additions & 6 deletions src/get_dist_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ pub fn get_dist_path() -> Option<PathBuf> {
return None;
}
};
let real_exe_path = fs::canonicalize(&exe_path).unwrap_or(exe_path);

if let Ok(real_path) = fs::read_link(&exe_path) {
let project_root = real_path.parent()?.parent()?.to_path_buf();
let dist_path = project_root.join("dist");
if dist_path.exists() {
return Some(dist_path);
fn ascend_path(mut path: PathBuf, levels: usize) -> Option<PathBuf> {
for _ in 0..levels {
path = path.parent()?.to_path_buf();
}
Some(path)
}

let project_root = exe_path.parent()?.parent()?.to_path_buf();
let levels_up = 4;

let project_root = ascend_path(real_exe_path, levels_up)?;
let dist_path = project_root.join("dist");
if dist_path.exists() {
return Some(dist_path);
Expand Down

0 comments on commit 2fbdaf1

Please sign in to comment.