Skip to content

Commit

Permalink
workflow & scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kmamal committed Nov 4, 2022
1 parent 9798643 commit 4354f6a
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 75 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build and upload

on: workflow_dispatch

jobs:
build:
name: ${{ matrix.platform.name }} Node.js v${{ matrix.node }}

strategy:
fail-fast: false
matrix:
platform:
- { name: 'Linux (x64)' ,os: ubuntu-22.04 }
- { name: 'Windows (x64)' ,os: windows-2022 }
- { name: 'Mac (x64)' ,os: macos-12 }
- { name: 'Mac (arm64)' ,os: macos-12 ,arch: arm64 }
node: [ 14, 16, 18 ]

runs-on: ${{ matrix.platform.os }}

steps:
- uses: actions/checkout@v3
with:
submodules: true

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- if: ${{ startsWith(matrix.platform.os, 'ubuntu-') }}
run: npm run install-deps-ubuntu

- run: npm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CROSS_COMPILE_ARCH: ${{ matrix.platform.arch }}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
example/*
test/*
.github
.travis.yml
appveyor.yml
build/*
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
},
"scripts": {
"test": "standard | snazzy && tape test/*.js | faucet",
"rebuild": "node-gyp rebuild --verbose",
"prebuild": "prebuild --all --strip",
"install": "prebuild-install || node-gyp rebuild"
"install": "cd scripts && node install.mjs",
"download-release": "cd scripts && node download-release.mjs",
"build": "cd scripts && node build.mjs",
"upload-release": "cd scripts && node upload-release.mjs",
"release": "cd scripts && node release.mjs",
"clean": "cd scripts && node clean.mjs",
"install-deps-ubuntu": "./scripts/install-deps-ubuntu.sh"
},
"dependencies": {
"bindings": "^1.5.0",
Expand Down
23 changes: 0 additions & 23 deletions scripts/after_success.sh

This file was deleted.

8 changes: 0 additions & 8 deletions scripts/before_install.sh

This file was deleted.

21 changes: 21 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Fs from 'node:fs'
import { execSync } from 'node:child_process'
import C from './util/common.js'

await Promise.all([
C.dir.build,
C.dir.dist,
C.dir.publish,
].map(async (dir) => {
await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
}))

console.log("build in", C.dir.build)

let archFlag = ''
if (process.env.CROSS_COMPILE_ARCH) {
archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
}

process.chdir(C.dir.root)
execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, { stdio: 'inherit' })
16 changes: 16 additions & 0 deletions scripts/clean.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Fs from 'node:fs'
import Path from 'node:path'
import C from './util/common.js'

const dirs = [
Path.join(C.dir.root, 'node_modules'),
C.dir.build,
C.dir.dist,
C.dir.publish,
]

console.log("delete")
await Promise.all(dirs.map(async (dir) => {
console.log(" ", dir)
await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
}))
17 changes: 17 additions & 0 deletions scripts/download-release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Fs from 'node:fs'
import { once } from 'node:events'
import C from './util/common.js'
import { fetch } from './util/fetch.js'
import Tar from 'tar'

const url = `https://github.com/${C.owner}/${C.repo}/releases/download/v${C.version}/${C.assetName}`

console.log("fetch", url)
const response = await fetch(url)

console.log("unpack to", C.dir.release)
await Fs.promises.rm(C.dir.build, { recursive: true }).catch(() => {})
await Fs.promises.mkdir(C.dir.release, { recursive: true })
const tar = Tar.extract({ gzip: true, C: C.dir.release })
response.stream().pipe(tar)
await once(tar, 'finish')
4 changes: 4 additions & 0 deletions scripts/install-deps-ubuntu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

sudo apt update
sudo apt install build-essential libxi-dev \
libglu1-mesa-dev libglew-dev pkg-config
11 changes: 11 additions & 0 deletions scripts/install.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

if (!process.env.GL_FROM_SOURCE) {
try {
await import('./download-release.mjs')
process.exit()
} catch (error) {}
} else {
console.log("skip download and build from source")
}

await import('./build.mjs')
18 changes: 0 additions & 18 deletions scripts/install.sh

This file was deleted.

10 changes: 10 additions & 0 deletions scripts/release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { execSync } from 'node:child_process'

await import('./clean.mjs')

process.env.GL_FROM_SOURCE = 1
execSync('npm install', {
stdio: 'inherit',
})

await import('./upload-release.mjs')
10 changes: 10 additions & 0 deletions scripts/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Path from 'node:path'
import { execSync } from 'node:child_process'
import { fstat } from 'node:fs'

process.chdir(Path.resolve(__dirname, '../tests'))
const files = await fstat.readdir('.')
for (const name of files) {
if (!name.endsWith('.test.js')) { continue }
execSync(`node ${name}`, { stdio: 'inherit' })
}
23 changes: 0 additions & 23 deletions scripts/test.sh

This file was deleted.

105 changes: 105 additions & 0 deletions scripts/upload-release.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import Fs from 'node:fs'
import Path from 'node:path'
import C from './util/common.js'
import { fetch } from './util/fetch.js'
import Tar from 'tar'

console.log("gather files in", C.dir.dist)
await Fs.promises.rm(C.dir.dist, { recursive: true }).catch(() => {})
await Fs.promises.mkdir(C.dir.dist, { recursive: true })
await Fs.promises.copyFile(
Path.join(C.dir.release, 'webgl.node'),
Path.join(C.dir.dist, 'webgl.node'),
)

if (C.platform === 'win32') {
const names = await Fs.promises.readdir(C.dir.release)
await Promise.all(names.map(async (name) => {
if (!name.endsWith('.dll')) { return }
await Fs.promises.copyFile(
Path.join(C.dir.release, name),
Path.join(C.dir.dist, name),
)
}))
}

const commonHeaders = {
"Accept": 'application/vnd.github+json',
"Authorization": `Bearer ${process.env.GITHUB_TOKEN}`,
'User-Agent': `@kmamal/gl@${C.version}`,
}

let response

getRelease: {
console.log("get release", C.version)

try {
response = await fetch(
`https://api.github.com/repos/${C.owner}/${C.repo}/releases/tags/v${C.version}`,
{ headers: commonHeaders },
)
console.log("release exists", C.version)
break getRelease
} catch (error) {
console.log(error.message)
}

console.log("create release", C.version)

response = await fetch(
`https://api.github.com/repos/${C.owner}/${C.repo}/releases`,
{
method: 'POST',
headers: commonHeaders,
body: JSON.stringify({
tag_name: `v${C.version}`, // eslint-disable-line camelcase
name: `v${C.version}`,
}),
},
)
}
const releaseId = (await response.json()).id

console.log("create archive", C.assetName)
await Fs.promises.rm(C.dir.publish, { recursive: true }).catch(() => {})
await Fs.promises.mkdir(C.dir.publish, { recursive: true })
const assetPath = Path.join(C.dir.publish, C.assetName)

process.chdir(C.dir.dist)
await Tar.create(
{ gzip: true, file: assetPath },
await Fs.promises.readdir('.'),
)
const buffer = await Fs.promises.readFile(assetPath)

response = await fetch(
`https://api.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets`,
{ headers: commonHeaders },
)

const list = await response.json()
const asset = list.find((x) => x.name === C.assetName)
if (asset) {
console.log("delete asset", C.assetName)
await fetch(
`https://api.github.com/repos/${C.owner}/${C.repo}/releases/assets/${asset.id}`,
{
method: 'DELETE',
headers: commonHeaders,
},
)
}

console.log("upload", C.assetName)
await fetch(
`https://uploads.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets?name=${C.assetName}`,
{
method: 'POST',
headers: {
...commonHeaders,
'Content-Type': 'application/gzip',
},
body: buffer,
},
)
30 changes: 30 additions & 0 deletions scripts/util/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Fs = require('node:fs')
const Path = require('node:path')

const dir = {}
dir.root = Path.resolve(__dirname, '../..')
dir.build = Path.join(dir.root, 'build')
dir.release = Path.join(dir.build, 'Release')
dir.dist = Path.join(dir.root, 'dist')
dir.publish = Path.join(dir.root, 'publish')

const pkgPath = Path.join(dir.root, 'package.json')
const pkg = JSON.parse(Fs.readFileSync(pkgPath).toString())
const version = pkg.version.slice(0, pkg.version.indexOf('-'))
const [ , owner, repo ] = pkg.repository.url.match(/([^/:]+)\/([^/]+).git$/u)

const { platform, arch } = process
const abi = process.versions.modules
const targetArch = process.env.CROSS_COMPILE_ARCH || arch
const assetName = `gl-v${version}-node_v${abi}-${platform}-${targetArch}.tar.gz`

module.exports = {
dir,
version,
owner,
repo,
platform,
arch,
targetArch,
assetName,
}
Loading

0 comments on commit 4354f6a

Please sign in to comment.