-
Notifications
You must be signed in to change notification settings - Fork 1
cre: build package #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cc13697
cre: build package
fmonper1 224fbbc
pin rimraf version
fmonper1 7621846
fix lint error on script
fmonper1 a2773e8
wip build package
fmonper1 fb396c0
working a lot nicer now
fmonper1 924ae36
delete useless scripts
fmonper1 55d191a
revet change to compile-to-wasm
fmonper1 7f16bf8
comments cleanup
fmonper1 d4ca8de
separete repo scripts from cli scripts
fmonper1 0de4a1c
add back deleted file
fmonper1 141f433
update liner rules
fmonper1 9c54eb7
add postinstall script
fmonper1 5a7a0ec
add post install script and bash script to generate the necessary tar…
fmonper1 5193ad3
make package compilable to tarball
fmonper1 b64ba0b
remove build types tsconfig
fmonper1 e4bff54
Merge branch 'main' into build-package
fmonper1 6a9acfd
remove postinstall script, extend build script to add postinstall scr…
fmonper1 89d93fc
Merge branch 'main' into build-package
fmonper1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
#!/usr/bin/env node | ||
|
||
import { execFileSync } from 'node:child_process' | ||
import { join } from 'node:path' | ||
import { resolve } from 'node:path' | ||
|
||
function getBinaryPath() { | ||
// Lookup table for all platforms and binary distribution packages | ||
const BINARY_DISTRIBUTION_PACKAGES = { | ||
'linux-x64': '@chainlink/cre-ts-linux-x64', | ||
'linux-arm64': '@chainlink/cre-ts-linux-arm', | ||
'darwin-x64': '@chainlink/cre-ts-darwin-x64', | ||
'darwin-arm64': '@chainlink/cre-ts-darwin-arm64', | ||
'win32-x64': '@chainlink/cre-ts-windows-x64', | ||
} | ||
|
||
// Windows binaries end with .exe so we need to special case them. | ||
const binaryName = process.platform === 'win32' ? 'cre-build.exe' : 'cre-build' | ||
|
||
// Determine package name for this platform | ||
const platformSpecificPackageName = | ||
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`] | ||
|
||
try { | ||
// Resolving will fail if the optionalDependency was not installed | ||
return resolve(`node_modules/${platformSpecificPackageName}/bin/${binaryName}`) | ||
} catch (e) { | ||
return join(__dirname, binaryName) | ||
} | ||
} | ||
|
||
|
||
execFileSync(getBinaryPath(), process.argv.slice(2), { | ||
stdio: 'inherit', | ||
}) | ||
This file contains hidden or 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 hidden or 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,38 @@ | ||
|
||
#!/bin/bash | ||
|
||
# Platform configurations: "bun-target:platform-arch:os:cpu" | ||
platforms=( | ||
"bun-darwin-arm64:darwin-arm64:darwin:arm64" | ||
"bun-darwin-x64:darwin-x64:darwin:x64" | ||
"bun-linux-x64:linux-x64:linux:x64" | ||
"bun-linux-arm64:linux-arm64:linux:arm64" | ||
"bun-windows-x64:windows-x64:win32:x64" | ||
) | ||
|
||
# Loop through each platform configuration | ||
for platform_config in "${platforms[@]}"; do | ||
# Split the configuration string | ||
IFS=':' read -r bun_target platform_arch os cpu <<< "$platform_config" | ||
|
||
echo "Building for $platform_arch..." | ||
|
||
# Build the binary | ||
bun build ./cli/run.ts --target="$bun_target" --compile --outfile "dist/bin/$platform_arch/bin/cre-build" | ||
|
||
chmod +x "dist/bin/$platform_arch/bin/cre-build" | ||
|
||
# Create package.json for this platform | ||
cat > "dist/bin/$platform_arch/package.json" << EOF | ||
{ | ||
"name": "@chainlink/cre-build-$platform_arch", | ||
"version": "0.0.1", | ||
"os": ["$os"], | ||
"cpu": ["$cpu"] | ||
} | ||
EOF | ||
|
||
echo "✓ Built $platform_arch binary" | ||
done | ||
|
||
echo "All platform binaries built successfully!" |
This file contains hidden or 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,12 @@ | ||
sh build-pkg.sh | ||
|
||
bun pm pack --destination ./dist | ||
# tar -cvzf backup.tgz ./dist | ||
|
||
# Pack each platform-specific folder in dist/bin | ||
for folder in dist/bin/*/; do | ||
if [ -d "$folder" ] && [ -f "$folder/package.json" ]; then | ||
echo "Packing $folder..." | ||
(cd "$folder" && bun pm pack --destination ../..) | ||
fi | ||
done |
This file contains hidden or 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,18 @@ | ||
bun run build:javy:sdk:wasm | ||
|
||
bun tsc --project tsconfig.build.json | ||
|
||
sh build-bins.sh | ||
|
||
cp bin/* dist/bin/ | ||
cp install.js dist/install.js | ||
|
||
chmod +x dist/bin/cre-ts | ||
|
||
bun rimraf tsconfig.types.tsbuildinfo | ||
|
||
cp package.json dist/package.json | ||
|
||
cd dist | ||
|
||
bun pm pkg set scripts.postinstall="node ./install.js" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,7 @@ | ||
import { main as compileToJs } from './compile-to-js' | ||
import { main as compileToWasm } from './compile-to-wasm-cmd' | ||
|
||
export const main = async () => { | ||
await compileToJs() | ||
await compileToWasm() | ||
} |
This file contains hidden or 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,28 @@ | ||
import fg from 'fast-glob' | ||
import { $ } from 'bun' | ||
|
||
export const main = async () => { | ||
console.info('\n\n---> Compile TS source files to JS\n\n') | ||
|
||
const workflowsSourcePaths = await fg('src/workflows/**/*.ts') | ||
const workflows = Array.from(workflowsSourcePaths).map((path) => `./${path}`) | ||
|
||
await Bun.build({ | ||
entrypoints: [...workflows], | ||
outdir: './dist/workflows', | ||
target: 'node', | ||
format: 'esm', | ||
root: './src/workflows', | ||
}) | ||
|
||
console.info('\n---> Bundling individual workflow files\n\n') | ||
|
||
// Get all the generated JS files in dist/workflows | ||
const workflowJSFiles = await fg('dist/workflows/**/*.js') | ||
|
||
for (const jsFile of workflowJSFiles) { | ||
await $`bun build ${jsFile} --bundle --outfile=${jsFile}` | ||
} | ||
|
||
console.info('Bundling: Done!') | ||
} |
This file contains hidden or 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,51 @@ | ||
import { execFileSync } from 'node:child_process' | ||
import path from 'node:path' | ||
import fg from 'fast-glob' | ||
|
||
export const main = async () => { | ||
console.info('\n\n---> Compile JS workflows to WASM \n\n') | ||
|
||
const workflowJSFiles = fg.sync('dist/workflows/**/*.js') | ||
|
||
for (const jsFile of workflowJSFiles) { | ||
if (jsFile.includes('abi')) continue | ||
|
||
const wasmFile = jsFile.replace(/\.js$/, '.wasm') | ||
|
||
console.log(`\n\n🔨 Building WASM for: ${jsFile}`) | ||
|
||
const javyBinary = | ||
process.platform === 'darwin' | ||
? './.bin/javy-arm-macos-v5.0.4' | ||
jeffrifwaldsmartcontract marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: './.bin/javy-arm-linux-v5.0.4' | ||
|
||
const javyPath = path.join(process.cwd(), 'node_modules', javyBinary) | ||
|
||
/** | ||
* -C wit=src/workflows/workflow.wit — points to the WIT file (definition of what will be available for the Host). | ||
* -C wit-world=workflow — specifies the WIT world name (world "workflow" which is defined in the .wit file). | ||
* -C plugin=... — uses your custom runtime (bundled javy chainlink sdk plugin) | ||
*/ | ||
|
||
execFileSync( | ||
javyPath, | ||
[ | ||
'build', | ||
'-C', | ||
'wit=src/workflows/workflow.wit', | ||
'-C', | ||
'wit-world=workflow', | ||
'-C', | ||
'plugin=node_modules/@chainlink/cre-sdk/javy-chainlink-sdk.plugin.wasm', | ||
jsFile, | ||
'-o', | ||
wasmFile, | ||
], | ||
{ | ||
stdio: 'inherit', | ||
}, | ||
) | ||
} | ||
|
||
console.info('Done!') | ||
} |
This file contains hidden or 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,39 @@ | ||
#!/usr/bin/env bun | ||
import { main as compileCmd } from './compile-cmd' | ||
import { main as compileToJs } from './compile-to-js' | ||
import { main as compileToWasm } from './compile-to-wasm-cmd' | ||
|
||
const availableScripts = { | ||
'compile-to-js': compileToJs, | ||
'compile-to-wasm': compileToWasm, | ||
compile: compileCmd, | ||
} | ||
|
||
const main = async () => { | ||
const scriptName = process.argv[2] | ||
|
||
if (!scriptName) { | ||
console.error('Usage: bun run.ts <script-name>') | ||
console.error('Available scripts:') | ||
Object.keys(availableScripts).forEach((script) => { | ||
console.error(` ${script}`) | ||
}) | ||
process.exit(1) | ||
} | ||
|
||
try { | ||
const script = availableScripts[scriptName] | ||
|
||
if (!script) { | ||
console.error(`Script ${scriptName} not found`) | ||
process.exit(1) | ||
} | ||
|
||
await script() | ||
} catch (error) { | ||
console.error(`Failed to load script ${scriptName}:`, error) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
main() |
This file contains hidden or 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,122 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import zlib from 'zlib' | ||
import https from 'https' | ||
|
||
const __dirname = import.meta.dirname; | ||
|
||
// Lookup table for all platforms and binary distribution packages | ||
const BINARY_DISTRIBUTION_PACKAGES = { | ||
'linux-x64': '@chainlink/cre-ts-linux-x64', | ||
'linux-arm64': '@chainlink/cre-ts-linux-arm', | ||
'darwin-x64': '@chainlink/cre-ts-darwin-x64', | ||
'darwin-arm64': '@chainlink/cre-ts-darwin-arm64', | ||
'win32-x64': '@chainlink/cre-ts-windows-x64', | ||
} | ||
|
||
// Adjust the version you want to install. You can also make this dynamic. | ||
const BINARY_DISTRIBUTION_VERSION = '1.0.0' | ||
|
||
// Windows binaries end with .exe so we need to special case them. | ||
const binaryName = process.platform === 'win32' ? 'cre-build.exe' : 'cre-build' | ||
|
||
// Determine package name for this platform | ||
const platformSpecificPackageName = | ||
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`] | ||
|
||
// Compute the path we want to emit the fallback binary to | ||
const fallbackBinaryPath = path.join(__dirname, binaryName) | ||
|
||
function makeRequest(url) { | ||
fmonper1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return new Promise((resolve, reject) => { | ||
https | ||
.get(url, (response) => { | ||
if (response.statusCode >= 200 && response.statusCode < 300) { | ||
const chunks = [] | ||
response.on('data', (chunk) => chunks.push(chunk)) | ||
response.on('end', () => { | ||
resolve(Buffer.concat(chunks)) | ||
}) | ||
} else if ( | ||
response.statusCode >= 300 && | ||
response.statusCode < 400 && | ||
response.headers.location | ||
) { | ||
// Follow redirects | ||
makeRequest(response.headers.location).then(resolve, reject) | ||
} else { | ||
reject( | ||
new Error( | ||
`npm responded with status code ${response.statusCode} when downloading the package!` | ||
) | ||
) | ||
} | ||
}) | ||
.on('error', (error) => { | ||
reject(error) | ||
}) | ||
}) | ||
} | ||
|
||
function extractFileFromTarball(tarballBuffer, filepath) { | ||
// Tar archives are organized in 512 byte blocks. | ||
// Blocks can either be header blocks or data blocks. | ||
// Header blocks contain file names of the archive in the first 100 bytes, terminated by a null byte. | ||
// The size of a file is contained in bytes 124-135 of a header block and in octal format. | ||
// The following blocks will be data blocks containing the file. | ||
let offset = 0 | ||
while (offset < tarballBuffer.length) { | ||
const header = tarballBuffer.subarray(offset, offset + 512) | ||
offset += 512 | ||
|
||
const fileName = header.toString('utf-8', 0, 100).replace(/\0.*/g, '') | ||
const fileSize = parseInt(header.toString('utf-8', 124, 136).replace(/\0.*/g, ''), 8) | ||
|
||
if (fileName === filepath) { | ||
return tarballBuffer.subarray(offset, offset + fileSize) | ||
} | ||
|
||
// Clamp offset to the uppoer multiple of 512 | ||
offset = (offset + fileSize + 511) & ~511 | ||
} | ||
} | ||
|
||
async function downloadBinaryFromNpm() { | ||
fmonper1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Download the tarball of the right binary distribution package | ||
const tarballDownloadBuffer = await makeRequest( | ||
`https://registry.npmjs.org/${platformSpecificPackageName}/-/${platformSpecificPackageName}-${BINARY_DISTRIBUTION_VERSION}.tgz` | ||
) | ||
|
||
const tarballBuffer = zlib.unzipSync(tarballDownloadBuffer) | ||
|
||
// Extract binary from package and write to disk | ||
fs.writeFileSync( | ||
fallbackBinaryPath, | ||
extractFileFromTarball(tarballBuffer, `package/bin/${binaryName}`), | ||
{ mode: 0o755 } // Make binary file executable | ||
) | ||
} | ||
|
||
function isPlatformSpecificPackageInstalled() { | ||
try { | ||
// Resolving will fail if the optionalDependency was not installed | ||
require.resolve(`${platformSpecificPackageName}/bin/${binaryName}`) | ||
return true | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
if (!platformSpecificPackageName) { | ||
throw new Error('Platform not supported!') | ||
} | ||
|
||
// Skip downloading the binary if it was already installed via optionalDependencies | ||
if (!isPlatformSpecificPackageInstalled()) { | ||
console.log('Platform specific package not found. Will manually download binary.') | ||
downloadBinaryFromNpm() | ||
} else { | ||
console.log( | ||
'Platform specific package already installed. Will fall back to manually downloading binary.' | ||
) | ||
Comment on lines
+119
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought: I'm confused with this log If the package is already installed, the install script isn't expected to do anything, right? |
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add EOF new line