Skip to content

Commit 3ca8c36

Browse files
feat: build script fixes and ref
1 parent 84ea851 commit 3ca8c36

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

build/_build.ts

+49-9
Original file line numberDiff line numberDiff line change
@@ -26,72 +26,92 @@ async function main() {
2626

2727
// make sure func compiler is available
2828
const minSupportFunc = "0.2.0";
29+
2930
try {
3031
const funcVersion = child_process
3132
.execSync("func -V")
3233
.toString()
3334
.match(/semantic version: v([0-9.]+)/)?.[1];
34-
if (!semver.gte(semver.coerce(funcVersion) ?? "", minSupportFunc)) throw new Error("Nonexistent version or outdated");
35+
36+
if (!semver.gte(semver.coerce(funcVersion) ?? "", minSupportFunc)) {
37+
throw new Error("Nonexistent version or outdated");
38+
}
3539
} catch (e) {
3640
console.log(`\nFATAL ERROR: 'func' with version >= ${minSupportFunc} executable is not found, is it installed and in path?`);
3741
process.exit(1);
3842
}
3943

4044
// make sure fift cli is available
4145
let fiftVersion = "";
46+
4247
try {
4348
fiftVersion = child_process.execSync("fift -V").toString();
44-
} catch (e) {}
49+
} catch (e) {
50+
console.error(e);
51+
}
52+
4553
if (!fiftVersion.includes("Fift build information")) {
4654
console.log("\nFATAL ERROR: 'fift' executable is not found, is it installed and in path?");
4755
process.exit(1);
4856
}
4957

5058
// go over all the root contracts in the contracts directory
5159
const rootContracts = glob.sync(["contracts/*.fc", "contracts/*.func"]);
60+
5261
for (const rootContract of rootContracts) {
5362
// compile a new root contract
5463
console.log(`\n* Found root contract '${rootContract}' - let's compile it:`);
5564
const contractName = path.parse(rootContract).name;
5665

5766
// delete existing build artifacts
5867
const fiftArtifact = `build/${contractName}.fif`;
68+
5969
if (fs.existsSync(fiftArtifact)) {
6070
console.log(` - Deleting old build artifact '${fiftArtifact}'`);
6171
fs.unlinkSync(fiftArtifact);
6272
}
73+
6374
const mergedFuncArtifact = `build/${contractName}.merged.fc`;
75+
6476
if (fs.existsSync(mergedFuncArtifact)) {
6577
console.log(` - Deleting old build artifact '${mergedFuncArtifact}'`);
6678
fs.unlinkSync(mergedFuncArtifact);
6779
}
80+
6881
const fiftCellArtifact = `build/${contractName}.cell.fif`;
82+
6983
if (fs.existsSync(fiftCellArtifact)) {
7084
console.log(` - Deleting old build artifact '${fiftCellArtifact}'`);
7185
fs.unlinkSync(fiftCellArtifact);
7286
}
87+
7388
const cellArtifact = `build/${contractName}.cell`;
89+
7490
if (fs.existsSync(cellArtifact)) {
7591
console.log(` - Deleting old build artifact '${cellArtifact}'`);
7692
fs.unlinkSync(cellArtifact);
7793
}
94+
7895
const hexArtifact = `build/${contractName}.compiled.json`;
96+
7997
if (fs.existsSync(hexArtifact)) {
8098
console.log(` - Deleting old build artifact '${hexArtifact}'`);
8199
fs.unlinkSync(hexArtifact);
82100
}
83101

84102
// check if we have a tlb file
85103
const tlbFile = `contracts/${contractName}.tlb`;
104+
86105
if (fs.existsSync(tlbFile)) {
87106
console.log(` - TL-B file '${tlbFile}' found, calculating crc32 on all ops..`);
88107
const tlbContent = fs.readFileSync(tlbFile).toString();
89-
const tlbOpMessages = tlbContent.match(/^(\w+).*=\s*InternalMsgBody$/gm) ?? [];
108+
const tlbOpMessages = tlbContent.match(/^(\w+)[\w\s:^]*=\s*InternalMsgBody;?$/gm) ?? [];
109+
90110
for (const tlbOpMessage of tlbOpMessages) {
91111
const crc = crc32(tlbOpMessage);
92112
const asQuery = `0x${(crc & 0x7fffffff).toString(16)}`;
93113
const asResponse = `0x${((crc | 0x80000000) >>> 0).toString(16)}`;
94-
console.log(` op '${tlbOpMessage.split(" ")[0]}': '${asQuery}' as query (&0x7fffffff), '${asResponse}' as response (|0x80000000)`);
114+
console.log(` op '${tlbOpMessage.split(/[ \n]/)[0]}': '${asQuery}' as query (&0x7fffffff), '${asResponse}' as response (|0x80000000)`);
95115
}
96116
} else {
97117
console.log(` - Warning: TL-B file for contract '${tlbFile}' not found, are your op consts according to standard?`);
@@ -100,11 +120,15 @@ async function main() {
100120
// run the func compiler to create a fif file
101121
console.log(` - Trying to compile '${rootContract}' with 'func' compiler..`);
102122
let buildErrors: string;
123+
103124
try {
104125
buildErrors = child_process.execSync(`func -APS -o build/${contractName}.fif ${rootContract} 2>&1 1>node_modules/.tmpfunc`).toString();
105126
} catch (e) {
127+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
128+
// @ts-ignore
106129
buildErrors = e.stdout.toString();
107130
}
131+
108132
if (buildErrors.length > 0) {
109133
console.log(" - OH NO! Compilation Errors! The compiler output was:");
110134
console.log(`\n${buildErrors}`);
@@ -122,7 +146,7 @@ async function main() {
122146
}
123147

124148
// create a temp cell.fif that will generate the cell
125-
let fiftCellSource = '"Asm.fif" include\n';
149+
let fiftCellSource = "\"Asm.fif\" include\n";
126150
fiftCellSource += `${fs.readFileSync(fiftArtifact).toString()}\n`;
127151
fiftCellSource += `boc>B "${cellArtifact}" B>file`;
128152
fs.writeFileSync(fiftCellArtifact, fiftCellSource);
@@ -146,10 +170,14 @@ async function main() {
146170
console.log(` - Build artifact created '${cellArtifact}'`);
147171
}
148172

173+
const codeHash = Cell.fromBoc(fs.readFileSync(cellArtifact))[0].hash();
174+
149175
fs.writeFileSync(
150176
hexArtifact,
151177
JSON.stringify({
152178
hex: Cell.fromBoc(fs.readFileSync(cellArtifact))[0].toBoc().toString("hex"),
179+
hash: codeHash.toString("hex"),
180+
hashBase64: codeHash.toString("base64"),
153181
})
154182
);
155183

@@ -168,16 +196,28 @@ async function main() {
168196
console.log("");
169197
}
170198

171-
main();
199+
main().then(() => console.log("Success"));
172200

173201
// helpers
174202

175203
function crc32(r: string) {
176-
for (var a, o = [], c = 0; c < 256; c++) {
204+
const o = [];
205+
206+
for (let a, c = 0; c < 256; c++) {
177207
a = c;
178-
for (let f = 0; f < 8; f++) a = 1 & a ? 3988292384 ^ (a >>> 1) : a >>> 1;
208+
209+
for (let f = 0; f < 8; f++) {
210+
a = 1 & a ? 3988292384 ^ (a >>> 1) : a >>> 1;
211+
}
212+
179213
o[c] = a;
180214
}
181-
for (var n = -1, t = 0; t < r.length; t++) n = (n >>> 8) ^ o[255 & (n ^ r.charCodeAt(t))];
215+
216+
let n = -1;
217+
218+
for (let t = 0; t < r.length; t++) {
219+
n = (n >>> 8) ^ o[255 & (n ^ r.charCodeAt(t))];
220+
}
221+
182222
return (-1 ^ n) >>> 0;
183223
}

0 commit comments

Comments
 (0)