-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow publishing binaries via NPM (#12)
- Loading branch information
Showing
11 changed files
with
342 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
on: | ||
push: | ||
branches-ignore: | ||
- 'trunk' | ||
push: | ||
branches-ignore: | ||
- "trunk" | ||
|
||
jobs: | ||
tests: | ||
name: 'Run tests' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: './go.mod' | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: './.nvmrc' | ||
cache: npm | ||
- run: npm ci | ||
- run: npm test | ||
tests: | ||
name: "Run tests" | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: "Setup Go" | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "./go.mod" | ||
- name: "Setup Node" | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version-file: "./.nvmrc" | ||
cache: npm | ||
- run: npm ci | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/npm | ||
/out | ||
/test | ||
gen-elm-wrappers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
: ${BINARY_VERSION?} | ||
|
||
rm -rf npm | ||
mkdir npm | ||
|
||
package_name=gen-elm-wrappers | ||
|
||
# general approach taken from <https://sentry.engineering/blog/publishing-binaries-on-npm> | ||
|
||
list-binaries-to-build() { | ||
perl <targets.txt -ne 'print if not /^#/' | ||
} | ||
|
||
cp -r npm-main-template npm/$package_name | ||
cp README.md npm/$package_name/README.md | ||
printf '{' >> npm/$package_name/arch-packages.json | ||
comma='' | ||
|
||
list-binaries-to-build | while read GOOS GOARCH ; do | ||
BINARY_EXT='' | ||
# keep in sync with build-binaries.sh | ||
case $GOOS in | ||
aix) | ||
process_platform=aix | ||
;; | ||
darwin) | ||
process_platform=darwin | ||
;; | ||
freebsd) | ||
process_platform=freebsd | ||
;; | ||
linux) | ||
process_platform=linux | ||
;; | ||
openbsd) | ||
process_platform=openbsd | ||
;; | ||
windows) | ||
process_platform=win32 | ||
BINARY_EXT=.exe | ||
;; | ||
*) | ||
continue | ||
esac | ||
|
||
case $GOARCH in | ||
386) | ||
process_arch=ia32 | ||
;; | ||
amd64) | ||
process_arch=x64 | ||
;; | ||
arm) | ||
process_arch=arm | ||
;; | ||
arm64) | ||
process_arch=arm64 | ||
;; | ||
s390x) | ||
process_arch=s390 | ||
;; | ||
loong64) | ||
process_arch=loong64 | ||
;; | ||
s390x) | ||
process_arch=s390x | ||
;; | ||
*) | ||
continue | ||
esac | ||
|
||
arch_package_name=@dave4420/$package_name-$process_platform-$process_arch | ||
mkdir -p npm/$arch_package_name/bin | ||
cat <<PACKAGE_JSON > npm/$arch_package_name/package.json | ||
{ | ||
"name": "$arch_package_name", | ||
"version": "$BINARY_VERSION", | ||
"os": [ "$process_platform" ], | ||
"cpu": [ "$process_arch" ] | ||
} | ||
PACKAGE_JSON | ||
cat <<README > npm/$arch_package_name/README.md | ||
You should not need to install this package directly. | ||
Instead, | ||
- either install [$package_name](https://www.npmjs.com/package/$package_name) | ||
- or manually [download the binary for your architecture](https://github.com/dave4420/gen-elm-wrappers/releases/tag/$BINARY_VERSION) | ||
README | ||
cp \ | ||
out/gen-elm-wrappers-$GOOS-$GOARCH-$BINARY_VERSION$BINARY_EXT \ | ||
npm/$arch_package_name/bin/gen-elm-wrappers$BINARY_EXT | ||
printf '%s\n "%s": "%s"' \ | ||
"$comma" \ | ||
"${process_platform}-${process_arch}" \ | ||
$arch_package_name \ | ||
>> npm/$package_name/arch-packages.json | ||
comma=',' | ||
done | ||
|
||
printf '\n}\n' >> npm/$package_name/arch-packages.json | ||
|
||
node -e ' | ||
const pj = require("./package.json"); | ||
pj.version = process.env.BINARY_VERSION; | ||
delete pj.private; | ||
pj.scripts = { | ||
"postinstall": "node ./install.js" | ||
}; | ||
pj.bin = "bin/cli.js"; | ||
delete pj.devDependencies; | ||
pj.optionalDependencies = {}; | ||
fs.readdirSync("npm").forEach((dir) => { | ||
if (dir === "'$package_name'") { | ||
return; | ||
} | ||
pj.optionalDependencies[dir] = process.env.BINARY_VERSION; | ||
}); | ||
process.stdout.write(JSON.stringify(pj, null, 2)); | ||
process.stdout.write("\n"); | ||
' > npm/$package_name/package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
set -exuo pipefail | ||
IFS=$'\n\t' | ||
|
||
cd npm | ||
|
||
for package in $(find . -type d -exec sh -c '[ -f {}/package.json ]' \; -print) ; do | ||
# ordered so that the package with the shortest name is published last, | ||
# after its dependencies | ||
if ! npm publish $package ; then | ||
printf 'Error code %d; hopefully already published?\n' $? | ||
fi | ||
npm view "$( | ||
node -e " | ||
const package = require('$package/package.json'); | ||
process.stdout.write(`${package.name}@${package.version}\n`); | ||
" | ||
)" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env node | ||
|
||
// Lookup table for all platforms and binary distribution packages | ||
const BINARY_DISTRIBUTION_PACKAGES = require("./arch-packages.json"); | ||
|
||
// Windows binaries end with .exe so we need to special case them. | ||
const binaryName = | ||
process.platform === "win32" ? "gen-elm-wrappers.exe" : "gen-elm-wrappers"; | ||
|
||
// Determine package name for this platform | ||
const platformSpecificPackageName = | ||
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]; | ||
|
||
function getBinaryPath() { | ||
try { | ||
// Resolving will fail if the optionalDependency was not installed | ||
return require.resolve(`${platformSpecificPackageName}/bin/${binaryName}`); | ||
} catch (e) { | ||
return require("path").join(__dirname, "..", binaryName); | ||
} | ||
} | ||
|
||
require("child_process").execFileSync(getBinaryPath(), process.argv.slice(2), { | ||
stdio: "inherit", | ||
}); |
Oops, something went wrong.