From 5929db83fd56281cff3ac6c8c717bf839106caf0 Mon Sep 17 00:00:00 2001 From: Tal Kol Date: Fri, 29 Apr 2022 18:57:25 +0100 Subject: [PATCH] Better build script --- .gitignore | 1 + README.md | 2 + build/.gitignore | 1 + build/build.sh | 10 --- build/build.ts | 71 +++++++++++++++ package-lock.json | 221 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 ++- 7 files changed, 304 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 build/.gitignore delete mode 100644 build/build.sh create mode 100644 build/build.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/README.md b/README.md index f280536..b146c3b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ To setup your machine for development, please make sure you have the following: * Develop * FunC contracts are located in `contracts/*.fc` + * Standalone root contracts are located in `contracts/*.fc` and imported utility code in `contracts/lib/*.fc` + * This structure assists with build and deployment and assumed by the included scripts * Tests are located in `test/*.spec.ts` * Build diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..8a8659e --- /dev/null +++ b/build/.gitignore @@ -0,0 +1 @@ +*.fif \ No newline at end of file diff --git a/build/build.sh b/build/build.sh deleted file mode 100644 index ebc5ec4..0000000 --- a/build/build.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -## cleanup -rm -rf *.fif - -## build main.fc -cat ../contracts/lib/*.fc ../contracts/main.fc | func -APSI -o main.fif - -## print all fif files that were built -echo "Build results:" && stat -f "%z %N" *.fif \ No newline at end of file diff --git a/build/build.ts b/build/build.ts new file mode 100644 index 0000000..8e228ec --- /dev/null +++ b/build/build.ts @@ -0,0 +1,71 @@ +// This is a simple generic build script in TypeScript that should work for most projects without modification +// It assumes that it is running from the repo root, and the directories are organized this way: +// ./build/ - directory for build artifacts exists +// ./contracts/*.fc - root contracts that are deployed separately are here +// ./contracts/lib/*.fc - utility code that should be imported as compilation dependency is here + +import * as fs from "fs"; +import * as path from "path"; +import * as process from "process"; +import * as child_process from "child_process"; +import fg from "fast-glob"; + +console.log(`=================================================================`); +console.log(`Build script running, let's find some FunC contracts to compile..`); + +// make sure func compiler is available +let funcVersion = ""; +try { + funcVersion = child_process.execSync("func -V").toString(); +} catch (e) {} +if (!funcVersion.includes(`Func build information`)) { + console.log(`\nFATAL ERROR: 'func' executable is not found, is it installed and in path?`); + process.exit(1); +} + +const rootContracts = fg.sync(["contracts/*.fc", "contracts/*.func"]); +for (const rootContract of rootContracts) { + // compile a new file + console.log(`\n* Found root contract '${rootContract}' - let's compile it:`); + const contractName = path.parse(rootContract).name; + + // delete existing build artifacts + if (fs.existsSync(`build/${contractName}.fif`)) { + console.log(` - Deleting old build artifact 'build/${contractName}.fif'`); + fs.unlinkSync(`build/${contractName}.fif`); + } + + // create one string with all source code from all merged files + let sourceToCompile = ""; + const importFiles = fg.sync(["contracts/lib/**/*.fc", "contracts/lib/**/*.func"]); + for (const importFile of importFiles) { + console.log(` - Adding import '${importFile}'`); + sourceToCompile += `${fs.readFileSync(importFile).toString()}\n`; + } + console.log(` - Adding the contract itself '${rootContract}'`); + sourceToCompile += `${fs.readFileSync(rootContract).toString()}\n`; + + // debug - uncomment the next line if you can't understand weird compilation errors + // fs.writeFileSync(`build/${contractName}.merged.fc`, sourceToCompile); + + // run the compiler + console.log(` - Trying to compile with 'func' compiler..`); + const buildErrors = child_process.execSync(`func -APSI -o build/${contractName}.fif 2>&1`, { input: sourceToCompile }).toString(); + if (buildErrors.length > 0) { + console.log(` - OH NO! Compilation Errors! The compiler output was:`); + console.log(`\n${buildErrors}`); + process.exit(1); + } else { + console.log(` - Compilation successful!`); + } + + // make sure build artifact was created + if (!fs.existsSync(`build/${contractName}.fif`)) { + console.log(` - For some reason 'build/${contractName}.fif' was not created!`); + process.exit(1); + } else { + console.log(` - Build artifact created 'build/${contractName}.fif'`); + } +} + +console.log(``); diff --git a/package-lock.json b/package-lock.json index f8b7e38..28d7eec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", "chai": "^4.3.4", + "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prettier": "^2.6.2", "ts-node": "^10.4.0", @@ -39,6 +40,41 @@ "node": ">=12" } }, + "node_modules/@nodelib/fs.scandir": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", + "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", + "dev": true, + "dependencies": { + "@nodelib/fs.stat": "2.0.5", + "run-parallel": "^1.1.9" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.stat": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", + "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.walk": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", + "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", + "dev": true, + "dependencies": { + "@nodelib/fs.scandir": "2.1.5", + "fastq": "^1.6.0" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/@tsconfig/node10": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", @@ -437,6 +473,31 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/fast-glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.11.tgz", + "integrity": "sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==", + "dev": true, + "dependencies": { + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.2", + "merge2": "^1.3.0", + "micromatch": "^4.0.4" + }, + "engines": { + "node": ">=8.6.0" + } + }, + "node_modules/fastq": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz", + "integrity": "sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==", + "dev": true, + "dependencies": { + "reusify": "^1.0.4" + } + }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -735,6 +796,28 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, + "node_modules/merge2": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", + "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "dev": true, + "engines": { + "node": ">= 8" + } + }, + "node_modules/micromatch": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", + "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "dev": true, + "dependencies": { + "braces": "^3.0.2", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } + }, "node_modules/minimatch": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-4.2.1.tgz", @@ -910,6 +993,26 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, + "node_modules/queue-microtask": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", + "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -940,6 +1043,39 @@ "node": ">=0.10.0" } }, + "node_modules/reusify": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", + "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", + "dev": true, + "engines": { + "iojs": ">=1.0.0", + "node": ">=0.10.0" + } + }, + "node_modules/run-parallel": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", + "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "queue-microtask": "^1.2.2" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -1247,6 +1383,32 @@ "@cspotcode/source-map-consumer": "0.8.0" } }, + "@nodelib/fs.scandir": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", + "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", + "dev": true, + "requires": { + "@nodelib/fs.stat": "2.0.5", + "run-parallel": "^1.1.9" + } + }, + "@nodelib/fs.stat": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", + "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", + "dev": true + }, + "@nodelib/fs.walk": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", + "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", + "dev": true, + "requires": { + "@nodelib/fs.scandir": "2.1.5", + "fastq": "^1.6.0" + } + }, "@tsconfig/node10": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", @@ -1552,6 +1714,28 @@ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", "dev": true }, + "fast-glob": { + "version": "3.2.11", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.11.tgz", + "integrity": "sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==", + "dev": true, + "requires": { + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.2", + "merge2": "^1.3.0", + "micromatch": "^4.0.4" + } + }, + "fastq": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.13.0.tgz", + "integrity": "sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==", + "dev": true, + "requires": { + "reusify": "^1.0.4" + } + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -1767,6 +1951,22 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==", "dev": true }, + "merge2": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", + "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "dev": true + }, + "micromatch": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", + "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "dev": true, + "requires": { + "braces": "^3.0.2", + "picomatch": "^2.3.1" + } + }, "minimatch": { "version": "4.2.1", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-4.2.1.tgz", @@ -1883,6 +2083,12 @@ "integrity": "sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==", "dev": true }, + "queue-microtask": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", + "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", + "dev": true + }, "randombytes": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", @@ -1907,6 +2113,21 @@ "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=", "dev": true }, + "reusify": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", + "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==", + "dev": true + }, + "run-parallel": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", + "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", + "dev": true, + "requires": { + "queue-microtask": "^1.2.2" + } + }, "safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", diff --git a/package.json b/package.json index 6ec998b..26a4127 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,15 @@ "license": "MIT", "author": "", "scripts": { - "prettier": "npx prettier --write '{test,contracts}/**/*.{ts,js,json}'", - "build": "cd build && ./build.sh", + "prettier": "npx prettier --write '{test,contracts,build}/**/*.{ts,js,json}'", + "build": "ts-node ./build/build.ts", "test": "mocha test/**/*.spec.ts" }, "devDependencies": { "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", "chai": "^4.3.4", + "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prettier": "^2.6.2", "ts-node": "^10.4.0", @@ -22,6 +23,9 @@ "printWidth": 180 }, "mocha": { - "require": ["chai", "ts-node/register"] + "require": [ + "chai", + "ts-node/register" + ] } -} \ No newline at end of file +}