@lionden/leo-compiler 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +23 -0
- package/dist/abi-hash.d.ts +15 -0
- package/dist/abi-hash.d.ts.map +1 -0
- package/dist/abi-hash.js +75 -0
- package/dist/abi-hash.js.map +1 -0
- package/dist/abi-parser.d.ts +38 -0
- package/dist/abi-parser.d.ts.map +1 -0
- package/dist/abi-parser.js +513 -0
- package/dist/abi-parser.js.map +1 -0
- package/dist/abi-types.d.ts +111 -0
- package/dist/abi-types.d.ts.map +1 -0
- package/dist/abi-types.js +5 -0
- package/dist/abi-types.js.map +1 -0
- package/dist/cache.d.ts +27 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +74 -0
- package/dist/cache.js.map +1 -0
- package/dist/codegen/codegen-error.d.ts +10 -0
- package/dist/codegen/codegen-error.d.ts.map +1 -0
- package/dist/codegen/codegen-error.js +14 -0
- package/dist/codegen/codegen-error.js.map +1 -0
- package/dist/codegen/contract-wrapper.d.ts +6 -0
- package/dist/codegen/contract-wrapper.d.ts.map +1 -0
- package/dist/codegen/contract-wrapper.js +2482 -0
- package/dist/codegen/contract-wrapper.js.map +1 -0
- package/dist/codegen/type-mapper.d.ts +46 -0
- package/dist/codegen/type-mapper.d.ts.map +1 -0
- package/dist/codegen/type-mapper.js +132 -0
- package/dist/codegen/type-mapper.js.map +1 -0
- package/dist/codegen/typescript-generator.d.ts +65 -0
- package/dist/codegen/typescript-generator.d.ts.map +1 -0
- package/dist/codegen/typescript-generator.js +2076 -0
- package/dist/codegen/typescript-generator.js.map +1 -0
- package/dist/compiler.d.ts +43 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +532 -0
- package/dist/compiler.js.map +1 -0
- package/dist/dependency-resolver.d.ts +33 -0
- package/dist/dependency-resolver.d.ts.map +1 -0
- package/dist/dependency-resolver.js +142 -0
- package/dist/dependency-resolver.js.map +1 -0
- package/dist/import-parser.d.ts +17 -0
- package/dist/import-parser.d.ts.map +1 -0
- package/dist/import-parser.js +46 -0
- package/dist/import-parser.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/package-materializer.d.ts +29 -0
- package/dist/package-materializer.d.ts.map +1 -0
- package/dist/package-materializer.js +160 -0
- package/dist/package-materializer.js.map +1 -0
- package/dist/source-discovery.d.ts +24 -0
- package/dist/source-discovery.d.ts.map +1 -0
- package/dist/source-discovery.js +131 -0
- package/dist/source-discovery.js.map +1 -0
- package/dist/source-scrubber.d.ts +22 -0
- package/dist/source-scrubber.d.ts.map +1 -0
- package/dist/source-scrubber.js +71 -0
- package/dist/source-scrubber.js.map +1 -0
- package/dist/types.d.ts +72 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +34 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { parseImports } from "./import-parser.js";
|
|
2
|
+
import { unitId } from "./types.js";
|
|
3
|
+
export class CircularDependencyError extends Error {
|
|
4
|
+
cycle;
|
|
5
|
+
constructor(cycle) {
|
|
6
|
+
super(`Circular dependency detected: ${cycle.join(" → ")}`);
|
|
7
|
+
this.cycle = cycle;
|
|
8
|
+
this.name = "CircularDependencyError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class UnitNameCollisionError extends Error {
|
|
12
|
+
key;
|
|
13
|
+
firstUnit;
|
|
14
|
+
secondUnit;
|
|
15
|
+
constructor(key, firstUnit, secondUnit) {
|
|
16
|
+
super(`Local unit name collision for "${key}": ${formatUnit(firstUnit)} conflicts with ${formatUnit(secondUnit)}. Local unit names and program IDs must be unique.`);
|
|
17
|
+
this.key = key;
|
|
18
|
+
this.firstUnit = firstUnit;
|
|
19
|
+
this.secondUnit = secondUnit;
|
|
20
|
+
this.name = "UnitNameCollisionError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* On-chain program ids that a local unit may never claim. `credits.aleo` is
|
|
25
|
+
* Aleo's one universal builtin: it must always resolve to the network, never a
|
|
26
|
+
* local unit. Any other on-chain import is user-supplied and is legitimately
|
|
27
|
+
* resolved as a network dependency, so it is not reserved here.
|
|
28
|
+
*/
|
|
29
|
+
const RESERVED_BUILTIN_IDS = new Set(["credits.aleo"]);
|
|
30
|
+
export class ReservedUnitNameError extends Error {
|
|
31
|
+
reservedId;
|
|
32
|
+
unit;
|
|
33
|
+
constructor(reservedId, unit) {
|
|
34
|
+
super(`Local unit ${formatUnit(unit)} claims the reserved on-chain builtin id "${reservedId}". ` +
|
|
35
|
+
`"${reservedId}" must resolve to the network, so a local unit may not shadow it. ` +
|
|
36
|
+
`Rename the local unit so its name (and any ".aleo" alias) does not collide with "${reservedId}".`);
|
|
37
|
+
this.reservedId = reservedId;
|
|
38
|
+
this.unit = unit;
|
|
39
|
+
this.name = "ReservedUnitNameError";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Build a dependency graph from discovered units by parsing imports from all
|
|
44
|
+
* .leo source files. Returns units in topological compile order.
|
|
45
|
+
*
|
|
46
|
+
* Local project units (programs and libraries) are resolved as local deps.
|
|
47
|
+
* All other imports (e.g. credits.aleo) are classified as network deps.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveDependencies(units) {
|
|
50
|
+
validateUniqueLookupKeys(units);
|
|
51
|
+
// Build lookup by all possible IDs a unit can be referenced as
|
|
52
|
+
const unitById = new Map();
|
|
53
|
+
for (const unit of units) {
|
|
54
|
+
unitById.set(unitId(unit), unit);
|
|
55
|
+
// Libraries can also be imported as "<name>.aleo" in Leo source
|
|
56
|
+
if (unit.kind === "library") {
|
|
57
|
+
unitById.set(`${unit.name}.aleo`, unit);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Parse imports for each unit, normalizing local dep names to canonical unitId
|
|
61
|
+
const importsMap = new Map();
|
|
62
|
+
const networkDeps = new Set();
|
|
63
|
+
const localAdj = new Map(); // unitId -> [local dep unitIds]
|
|
64
|
+
for (const unit of units) {
|
|
65
|
+
const rawImports = parseImports(unit.sourceDir, unit.allSources);
|
|
66
|
+
const id = unitId(unit);
|
|
67
|
+
const normalizedImports = [];
|
|
68
|
+
const localDeps = [];
|
|
69
|
+
for (const dep of rawImports) {
|
|
70
|
+
const resolved = unitById.get(dep);
|
|
71
|
+
if (resolved) {
|
|
72
|
+
// Normalize to canonical unitId (e.g. "math.aleo" → "math" for libraries)
|
|
73
|
+
const canonicalId = unitId(resolved);
|
|
74
|
+
if (canonicalId === id)
|
|
75
|
+
continue; // a unit never depends on itself
|
|
76
|
+
normalizedImports.push(canonicalId);
|
|
77
|
+
localDeps.push(canonicalId);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
// Network dependency (credits.aleo, etc.)
|
|
81
|
+
normalizedImports.push(dep);
|
|
82
|
+
networkDeps.add(dep);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
importsMap.set(id, normalizedImports);
|
|
86
|
+
localAdj.set(id, localDeps);
|
|
87
|
+
}
|
|
88
|
+
// Topological sort via DFS with cycle detection
|
|
89
|
+
const order = [];
|
|
90
|
+
const visited = new Set();
|
|
91
|
+
const visiting = new Set(); // currently in DFS stack
|
|
92
|
+
function visit(id, stack) {
|
|
93
|
+
if (visited.has(id))
|
|
94
|
+
return;
|
|
95
|
+
if (visiting.has(id)) {
|
|
96
|
+
const cycleStart = stack.indexOf(id);
|
|
97
|
+
throw new CircularDependencyError([...stack.slice(cycleStart), id]);
|
|
98
|
+
}
|
|
99
|
+
visiting.add(id);
|
|
100
|
+
stack.push(id);
|
|
101
|
+
for (const dep of localAdj.get(id) ?? []) {
|
|
102
|
+
visit(dep, stack);
|
|
103
|
+
}
|
|
104
|
+
stack.pop();
|
|
105
|
+
visiting.delete(id);
|
|
106
|
+
visited.add(id);
|
|
107
|
+
const unit = unitById.get(id);
|
|
108
|
+
if (unit)
|
|
109
|
+
order.push(unit);
|
|
110
|
+
}
|
|
111
|
+
for (const unit of units) {
|
|
112
|
+
visit(unitId(unit), []);
|
|
113
|
+
}
|
|
114
|
+
return { order, imports: importsMap, networkDeps };
|
|
115
|
+
}
|
|
116
|
+
function validateUniqueLookupKeys(units) {
|
|
117
|
+
const claimedBy = new Map();
|
|
118
|
+
for (const unit of units) {
|
|
119
|
+
for (const key of lookupKeys(unit)) {
|
|
120
|
+
if (RESERVED_BUILTIN_IDS.has(key)) {
|
|
121
|
+
throw new ReservedUnitNameError(key, unit);
|
|
122
|
+
}
|
|
123
|
+
const existing = claimedBy.get(key);
|
|
124
|
+
if (existing && existing !== unit) {
|
|
125
|
+
throw new UnitNameCollisionError(key, existing, unit);
|
|
126
|
+
}
|
|
127
|
+
claimedBy.set(key, unit);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function lookupKeys(unit) {
|
|
132
|
+
const keys = [unitId(unit)];
|
|
133
|
+
if (unit.kind === "library") {
|
|
134
|
+
keys.push(`${unit.name}.aleo`);
|
|
135
|
+
}
|
|
136
|
+
return keys;
|
|
137
|
+
}
|
|
138
|
+
function formatUnit(unit) {
|
|
139
|
+
const label = unit.kind === "program" ? `program ${unit.programId}` : `library ${unit.name}`;
|
|
140
|
+
return `${label} at ${unit.entryFile}`;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=dependency-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dependency-resolver.js","sourceRoot":"","sources":["../src/dependency-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IACpB;IAA5B,YAA4B,KAAe;QACzC,KAAK,CAAC,i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,GAAG,KAAK,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse Leo import statements and cross-program calls from .leo source files.
|
|
3
|
+
*
|
|
4
|
+
* Matches:
|
|
5
|
+
* - `import <name>.aleo;` — explicit import declarations
|
|
6
|
+
* - `<name>.aleo::function()` — cross-program calls (Leo v4 syntax)
|
|
7
|
+
* - `<name>.aleo/function()` — cross-program calls (Leo v3.5 syntax)
|
|
8
|
+
*
|
|
9
|
+
* Comments and string literals are stripped before matching, so a `.aleo`
|
|
10
|
+
* token that only appears in a comment or string is never treated as a
|
|
11
|
+
* dependency.
|
|
12
|
+
*
|
|
13
|
+
* Returns a deduplicated set of external program/library IDs that this
|
|
14
|
+
* set of source files depends on.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseImports(sourceDir: string, relativeFiles: string[]): string[];
|
|
17
|
+
//# sourceMappingURL=import-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-parser.d.ts","sourceRoot":"","sources":["../src/import-parser.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAiCjF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { stripCommentsAndStrings } from "./source-scrubber.js";
|
|
4
|
+
/**
|
|
5
|
+
* Parse Leo import statements and cross-program calls from .leo source files.
|
|
6
|
+
*
|
|
7
|
+
* Matches:
|
|
8
|
+
* - `import <name>.aleo;` — explicit import declarations
|
|
9
|
+
* - `<name>.aleo::function()` — cross-program calls (Leo v4 syntax)
|
|
10
|
+
* - `<name>.aleo/function()` — cross-program calls (Leo v3.5 syntax)
|
|
11
|
+
*
|
|
12
|
+
* Comments and string literals are stripped before matching, so a `.aleo`
|
|
13
|
+
* token that only appears in a comment or string is never treated as a
|
|
14
|
+
* dependency.
|
|
15
|
+
*
|
|
16
|
+
* Returns a deduplicated set of external program/library IDs that this
|
|
17
|
+
* set of source files depends on.
|
|
18
|
+
*/
|
|
19
|
+
export function parseImports(sourceDir, relativeFiles) {
|
|
20
|
+
const imports = new Set();
|
|
21
|
+
// import foo.aleo;
|
|
22
|
+
const importDeclRegex = /import\s+([\w]+\.aleo)\s*;/g;
|
|
23
|
+
// foo.aleo::bar( — v4
|
|
24
|
+
const crossCallRegex = /([\w]+\.aleo)::/g;
|
|
25
|
+
// foo.aleo/bar( — v3.5
|
|
26
|
+
const slashCallRegex = /([\w]+\.aleo)\//g;
|
|
27
|
+
for (const relFile of relativeFiles) {
|
|
28
|
+
const absPath = path.join(sourceDir, relFile);
|
|
29
|
+
const content = stripCommentsAndStrings(fs.readFileSync(absPath, "utf-8"));
|
|
30
|
+
let match;
|
|
31
|
+
importDeclRegex.lastIndex = 0;
|
|
32
|
+
while ((match = importDeclRegex.exec(content)) !== null) {
|
|
33
|
+
imports.add(match[1]);
|
|
34
|
+
}
|
|
35
|
+
crossCallRegex.lastIndex = 0;
|
|
36
|
+
while ((match = crossCallRegex.exec(content)) !== null) {
|
|
37
|
+
imports.add(match[1]);
|
|
38
|
+
}
|
|
39
|
+
slashCallRegex.lastIndex = 0;
|
|
40
|
+
while ((match = slashCallRegex.exec(content)) !== null) {
|
|
41
|
+
imports.add(match[1]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return [...imports];
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=import-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-parser.js","sourceRoot":"","sources":["../src/import-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC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,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;AACtB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { computeAbiHash } from "./abi-hash.js";
|
|
2
|
+
export { AbiParseError, parseAbi } from "./abi-parser.js";
|
|
3
|
+
export type { AbiInput, AbiJsonValue, AbiOutput, AleoType, ConstParameterABI, InterfaceRefABI, MappingABI, Mode, PlaintextType, PrimitiveType, ProgramABI, RecordABI, RecordFieldABI, RecordRef, StorageType, StorageVariableABI, StructABI, StructFieldABI, StructRef, TransitionABI, ViewABI, } from "./abi-types.js";
|
|
4
|
+
export { computeUnitHash, isCached, writeCache } from "./cache.js";
|
|
5
|
+
export { CodegenError } from "./codegen/codegen-error.js";
|
|
6
|
+
export { aleoTypeToTs, pathToTsName, plaintextToTs, primitiveToTs } from "./codegen/type-mapper.js";
|
|
7
|
+
export { assertTypechainModuleNamesUnique, type GenerateBindingsOptions, generateBaseContract, generateBindings, programIdToClassName, resolveContractClassName, } from "./codegen/typescript-generator.js";
|
|
8
|
+
export { CompilationError, type CompilePipelineResult, compilePipeline, defaultFetchNetworkDep, type FetchNetworkDep, networkDepCacheScope, } from "./compiler.js";
|
|
9
|
+
export { CircularDependencyError, type DependencyGraph, ReservedUnitNameError, resolveDependencies, UnitNameCollisionError, } from "./dependency-resolver.js";
|
|
10
|
+
export { parseImports } from "./import-parser.js";
|
|
11
|
+
export { effectiveUnitId, getCachedNetworkDep, linkNetworkDependency, materializePackage, } from "./package-materializer.js";
|
|
12
|
+
export { discoverUnits, extractProgramId, MissingProgramDeclarationError, ProgramFolderNameMismatchError, } from "./source-discovery.js";
|
|
13
|
+
export type { CompilationResult, CompilationUnitResult, CompileOptions, DiscoveredLibrary, DiscoveredProgram, DiscoveredUnit, LibraryCompilationResult, ProgramCompilationResult, RenameProgramOptions, } from "./types.js";
|
|
14
|
+
export { unitId } from "./types.js";
|
|
15
|
+
//# sourceMapping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AAEnC,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,8BAA8B,EAC9B,8BAA8B,GAC/B,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EACV,iBAAiB,EACjB,qBAAqB,EACrB,cAAc,EACd,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,wBAAwB,EACxB,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Types
|
|
2
|
+
export { computeAbiHash } from "./abi-hash.js";
|
|
3
|
+
// ABI parser
|
|
4
|
+
export { AbiParseError, parseAbi } from "./abi-parser.js";
|
|
5
|
+
// Cache
|
|
6
|
+
export { computeUnitHash, isCached, writeCache } from "./cache.js";
|
|
7
|
+
export { CodegenError } from "./codegen/codegen-error.js";
|
|
8
|
+
export { aleoTypeToTs, pathToTsName, plaintextToTs, primitiveToTs } from "./codegen/type-mapper.js";
|
|
9
|
+
// Codegen
|
|
10
|
+
export { assertTypechainModuleNamesUnique, generateBaseContract, generateBindings, programIdToClassName, resolveContractClassName, } from "./codegen/typescript-generator.js";
|
|
11
|
+
// Compiler pipeline
|
|
12
|
+
export { CompilationError, compilePipeline, defaultFetchNetworkDep, networkDepCacheScope, } from "./compiler.js";
|
|
13
|
+
// Dependency resolver
|
|
14
|
+
export { CircularDependencyError, ReservedUnitNameError, resolveDependencies, UnitNameCollisionError, } from "./dependency-resolver.js";
|
|
15
|
+
// Import parser
|
|
16
|
+
export { parseImports } from "./import-parser.js";
|
|
17
|
+
// Package materializer
|
|
18
|
+
export { effectiveUnitId, getCachedNetworkDep, linkNetworkDependency, materializePackage, } from "./package-materializer.js";
|
|
19
|
+
// Source discovery
|
|
20
|
+
export { discoverUnits, extractProgramId, MissingProgramDeclarationError, ProgramFolderNameMismatchError, } from "./source-discovery.js";
|
|
21
|
+
export { unitId } from "./types.js";
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,QAAQ;AAER,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,aAAa;AACb,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AA0B1D,QAAQ;AACR,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACpG,UAAU;AACV,OAAO,EACL,gCAAgC,EAEhC,oBAAoB,EACpB,gBAAgB,EAChB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,mCAAmC,CAAC;AAC3C,oBAAoB;AACpB,OAAO,EACL,gBAAgB,EAEhB,eAAe,EACf,sBAAsB,EAEtB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AACvB,sBAAsB;AACtB,OAAO,EACL,uBAAuB,EAEvB,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAClC,gBAAgB;AAChB,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,uBAAuB;AACvB,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,2BAA2B,CAAC;AACnC,mBAAmB;AACnB,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,8BAA8B,EAC9B,8BAA8B,GAC/B,MAAM,uBAAuB,CAAC;AAY/B,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { LionDenResolvedConfig } from "@lionden/config";
|
|
2
|
+
import type { DependencyGraph } from "./dependency-resolver.js";
|
|
3
|
+
import type { DiscoveredUnit, RenameProgramOptions } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Materialize a Leo CLI package for a discovered unit under
|
|
6
|
+
* `<artifactsDir>/.build/<id>/`.
|
|
7
|
+
*
|
|
8
|
+
* This is the "unflatten" stage: users write .leo files in programs/
|
|
9
|
+
* without program.json, imports/, etc. We create the full Leo CLI package
|
|
10
|
+
* structure that `leo build` expects.
|
|
11
|
+
*/
|
|
12
|
+
export declare function materializePackage(unit: DiscoveredUnit, config: LionDenResolvedConfig, graph: DependencyGraph, network?: string, renamePlan?: RenameProgramOptions): string;
|
|
13
|
+
export declare function effectiveUnitId(unit: DiscoveredUnit, renamePlan?: RenameProgramOptions): string;
|
|
14
|
+
/**
|
|
15
|
+
* Cache a network dependency's .aleo source into the cache directory
|
|
16
|
+
* and copy it into the package's imports/ directory.
|
|
17
|
+
*
|
|
18
|
+
* Cache is scoped by effective network+endpoint so that switching
|
|
19
|
+
* `defaultNetwork` in config does not serve stale source from a different
|
|
20
|
+
* network or endpoint.
|
|
21
|
+
*/
|
|
22
|
+
export declare function linkNetworkDependency(packageDir: string, depName: string, aleoSource: string, cacheDir: string, networkScope?: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Check if a network dependency is already cached.
|
|
25
|
+
*
|
|
26
|
+
* Cache is scoped by network+endpoint — see {@link linkNetworkDependency}.
|
|
27
|
+
*/
|
|
28
|
+
export declare function getCachedNetworkDep(cacheDir: string, depName: string, networkScope?: string): string | null;
|
|
29
|
+
//# sourceMappingURL=package-materializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package-materializer.d.ts","sourceRoot":"","sources":["../src/package-materializer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAEhE,OAAO,KAAK,EAAqB,cAAc,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAG1F;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,qBAAqB,EAC7B,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,oBAAoB,GAChC,MAAM,CAuDR;AA+CD,wBAAgB,eAAe,CAAC,IAAI,EAAE,cAAc,EAAE,UAAU,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAI/F;AA8CD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,YAAY,CAAC,EAAE,MAAM,GACpB,IAAI,CAWN;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,YAAY,CAAC,EAAE,MAAM,GACpB,MAAM,GAAG,IAAI,CAOf"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { stripCommentsAndStrings } from "./source-scrubber.js";
|
|
4
|
+
import { unitId } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Materialize a Leo CLI package for a discovered unit under
|
|
7
|
+
* `<artifactsDir>/.build/<id>/`.
|
|
8
|
+
*
|
|
9
|
+
* This is the "unflatten" stage: users write .leo files in programs/
|
|
10
|
+
* without program.json, imports/, etc. We create the full Leo CLI package
|
|
11
|
+
* structure that `leo build` expects.
|
|
12
|
+
*/
|
|
13
|
+
export function materializePackage(unit, config, graph, network, renamePlan) {
|
|
14
|
+
const effectiveId = effectiveUnitId(unit, renamePlan);
|
|
15
|
+
const packageDir = path.join(config.paths.artifacts, ".build", effectiveId);
|
|
16
|
+
const srcDir = path.join(packageDir, "src");
|
|
17
|
+
const importsDir = path.join(packageDir, "imports");
|
|
18
|
+
// Clean and recreate, but preserve the build/ directory (contains leo build
|
|
19
|
+
// output that the cache expects to still be there on subsequent runs).
|
|
20
|
+
const buildDir = path.join(packageDir, "build");
|
|
21
|
+
const buildExists = fs.existsSync(buildDir);
|
|
22
|
+
const tmpBuildDir = buildExists
|
|
23
|
+
? path.join(config.paths.artifacts, ".build", `.preserve-${effectiveId}-${process.pid}`)
|
|
24
|
+
: null;
|
|
25
|
+
if (tmpBuildDir) {
|
|
26
|
+
fs.renameSync(buildDir, tmpBuildDir);
|
|
27
|
+
}
|
|
28
|
+
fs.rmSync(packageDir, { recursive: true, force: true });
|
|
29
|
+
fs.mkdirSync(srcDir, { recursive: true });
|
|
30
|
+
fs.mkdirSync(importsDir, { recursive: true });
|
|
31
|
+
if (tmpBuildDir) {
|
|
32
|
+
fs.renameSync(tmpBuildDir, buildDir);
|
|
33
|
+
}
|
|
34
|
+
// Copy all .leo sources preserving directory structure
|
|
35
|
+
for (const relPath of unit.allSources) {
|
|
36
|
+
const src = path.join(unit.sourceDir, relPath);
|
|
37
|
+
const dest = path.join(srcDir, relPath);
|
|
38
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
39
|
+
if (unit.kind === "program" &&
|
|
40
|
+
renamePlan &&
|
|
41
|
+
unit.programId === renamePlan.sourceProgramId &&
|
|
42
|
+
relPath === "main.leo") {
|
|
43
|
+
fs.writeFileSync(dest, rewriteMainProgramDeclaration(fs.readFileSync(src, "utf-8"), renamePlan));
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
fs.copyFileSync(src, dest);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Generate program.json
|
|
50
|
+
const programJson = buildProgramJson(unit, config, graph, effectiveId);
|
|
51
|
+
fs.writeFileSync(path.join(packageDir, "program.json"), JSON.stringify(programJson, null, 2) + "\n");
|
|
52
|
+
// Generate .env
|
|
53
|
+
const env = buildDotEnv(config, network);
|
|
54
|
+
fs.writeFileSync(path.join(packageDir, ".env"), env);
|
|
55
|
+
return packageDir;
|
|
56
|
+
}
|
|
57
|
+
function buildProgramJson(unit, config, graph, effectiveId = unitId(unit)) {
|
|
58
|
+
const id = unitId(unit);
|
|
59
|
+
const imports = graph.imports.get(id) ?? [];
|
|
60
|
+
// Leo CLI expects the program field to use the .aleo suffix for both
|
|
61
|
+
// programs and libraries (matching import syntax, e.g. "math_utils.aleo").
|
|
62
|
+
const programName = unit.kind === "program" ? effectiveId : `${unit.name}.aleo`;
|
|
63
|
+
const dependencies = [];
|
|
64
|
+
for (const dep of imports) {
|
|
65
|
+
if (graph.networkDeps.has(dep)) {
|
|
66
|
+
dependencies.push({ name: dep, location: "network" });
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Local dependency — point to its materialized package.
|
|
70
|
+
// Leo CLI expects dependency names to match import statements
|
|
71
|
+
// (e.g. "math_utils.aleo"), but libraries have canonical IDs
|
|
72
|
+
// without the .aleo suffix, so we normalize here.
|
|
73
|
+
const depName = dep.endsWith(".aleo") ? dep : `${dep}.aleo`;
|
|
74
|
+
const depPackageDir = path.join(config.paths.artifacts, ".build", dep);
|
|
75
|
+
dependencies.push({ name: depName, location: "local", path: depPackageDir });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
program: programName,
|
|
80
|
+
version: "0.1.0",
|
|
81
|
+
description: "",
|
|
82
|
+
license: "MIT",
|
|
83
|
+
dependencies: dependencies.length > 0 ? dependencies : undefined,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export function effectiveUnitId(unit, renamePlan) {
|
|
87
|
+
return unit.kind === "program" && renamePlan && unit.programId === renamePlan.sourceProgramId
|
|
88
|
+
? renamePlan.targetProgramId
|
|
89
|
+
: unitId(unit);
|
|
90
|
+
}
|
|
91
|
+
function rewriteMainProgramDeclaration(source, rename) {
|
|
92
|
+
const scrubbed = stripCommentsAndStrings(source);
|
|
93
|
+
const declaration = /\bprogram\s+([\w]+\.aleo)\s*(?=[:{])/g;
|
|
94
|
+
const match = declaration.exec(scrubbed);
|
|
95
|
+
if (!match || match[1] !== rename.sourceProgramId) {
|
|
96
|
+
throw new Error(`Cannot rename "${rename.sourceProgramId}" to "${rename.targetProgramId}": main.leo does not declare the source program.`);
|
|
97
|
+
}
|
|
98
|
+
const idStart = match.index + match[0].indexOf(match[1]);
|
|
99
|
+
const idEnd = idStart + match[1].length;
|
|
100
|
+
return `${source.slice(0, idStart)}${rename.targetProgramId}${source.slice(idEnd)}`;
|
|
101
|
+
}
|
|
102
|
+
function buildDotEnv(config, network) {
|
|
103
|
+
const networkConfig = config.networks[network ?? config.defaultNetwork];
|
|
104
|
+
const lines = [];
|
|
105
|
+
// Every resolved network config carries a `network` field — use it directly.
|
|
106
|
+
lines.push(`NETWORK=${networkConfig?.network ?? "testnet"}`);
|
|
107
|
+
if (networkConfig?.type === "http" && networkConfig.privateKey) {
|
|
108
|
+
lines.push(`PRIVATE_KEY=${networkConfig.privateKey}`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// Default devnode private key (recommended by Leo CLI for local devnets)
|
|
112
|
+
lines.push("PRIVATE_KEY=APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH");
|
|
113
|
+
}
|
|
114
|
+
if (networkConfig?.type === "http") {
|
|
115
|
+
lines.push(`ENDPOINT=${networkConfig.endpoint}`);
|
|
116
|
+
}
|
|
117
|
+
else if (networkConfig?.type === "devnode") {
|
|
118
|
+
lines.push(`ENDPOINT=http://${networkConfig.socketAddr}`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
lines.push("ENDPOINT=http://127.0.0.1:3030");
|
|
122
|
+
}
|
|
123
|
+
if (networkConfig?.type === "devnode") {
|
|
124
|
+
lines.push("DEVNET=true");
|
|
125
|
+
}
|
|
126
|
+
return lines.join("\n") + "\n";
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Cache a network dependency's .aleo source into the cache directory
|
|
130
|
+
* and copy it into the package's imports/ directory.
|
|
131
|
+
*
|
|
132
|
+
* Cache is scoped by effective network+endpoint so that switching
|
|
133
|
+
* `defaultNetwork` in config does not serve stale source from a different
|
|
134
|
+
* network or endpoint.
|
|
135
|
+
*/
|
|
136
|
+
export function linkNetworkDependency(packageDir, depName, aleoSource, cacheDir, networkScope) {
|
|
137
|
+
// Cache the source (scoped by network+endpoint)
|
|
138
|
+
const scope = networkScope ?? "default";
|
|
139
|
+
const cachePath = path.join(cacheDir, "network-deps", scope, depName);
|
|
140
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
141
|
+
fs.writeFileSync(cachePath, aleoSource);
|
|
142
|
+
// Copy to imports/
|
|
143
|
+
const importsDir = path.join(packageDir, "imports");
|
|
144
|
+
fs.mkdirSync(importsDir, { recursive: true });
|
|
145
|
+
fs.writeFileSync(path.join(importsDir, depName), aleoSource);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Check if a network dependency is already cached.
|
|
149
|
+
*
|
|
150
|
+
* Cache is scoped by network+endpoint — see {@link linkNetworkDependency}.
|
|
151
|
+
*/
|
|
152
|
+
export function getCachedNetworkDep(cacheDir, depName, networkScope) {
|
|
153
|
+
const scope = networkScope ?? "default";
|
|
154
|
+
const cachePath = path.join(cacheDir, "network-deps", scope, depName);
|
|
155
|
+
if (fs.existsSync(cachePath)) {
|
|
156
|
+
return fs.readFileSync(cachePath, "utf-8");
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=package-materializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package-materializer.js","sourceRoot":"","sources":["../src/package-materializer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW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B,CACnC,UAAkB,EAClB,OAAe,EACf,UAAkB,EAClB,QAAgB,EAChB,YAAqB;IAErB,gDAAgD;IAChD,MAAM,KAAK,GAAG,YAAY,IAAI,SAAS,CAAC;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IACtE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3D,EAAE,CAAC,aAAa,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IAExC,mBAAmB;IACnB,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;IACpD,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC9C,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,EAAE,UAAU,CAAC,CAAC;AAC/D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAAgB,EAChB,OAAe,EACf,YAAqB;IAErB,MAAM,KAAK,GAAG,YAAY,IAAI,SAAS,CAAC;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IACtE,IAAI,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC7B,OAAO,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DiscoveredUnit } from "./types.js";
|
|
2
|
+
export declare class ProgramFolderNameMismatchError extends Error {
|
|
3
|
+
constructor(sourceDir: string, programId: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class MissingProgramDeclarationError extends Error {
|
|
6
|
+
constructor(entryFile: string);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Recursively scan `programsDir` for Leo program roots (main.leo) and
|
|
10
|
+
* library roots (lib.leo) at any depth. Returns all discovered compilation units.
|
|
11
|
+
*
|
|
12
|
+
* A directory is a program root if it contains main.leo, or a library root
|
|
13
|
+
* if it contains lib.leo. Once a root is found, its subtree is collected as
|
|
14
|
+
* sources (not scanned for further roots).
|
|
15
|
+
*/
|
|
16
|
+
export declare function discoverUnits(programsDir: string): DiscoveredUnit[];
|
|
17
|
+
/**
|
|
18
|
+
* Extract the program ID from a main.leo file by finding the
|
|
19
|
+
* `program <name>.aleo { ... }` or `program <name>.aleo : ...` declaration.
|
|
20
|
+
* Interface syntax after the colon is intentionally left to Leo validation so
|
|
21
|
+
* discovery does not break when new interface-reference forms are introduced.
|
|
22
|
+
*/
|
|
23
|
+
export declare function extractProgramId(mainLeoPath: string): string | null;
|
|
24
|
+
//# sourceMappingURL=source-discovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-discovery.d.ts","sourceRoot":"","sources":["../src/source-discovery.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAwC,cAAc,EAAE,MAAM,YAAY,CAAC;AAEvF,qBAAa,8BAA+B,SAAQ,KAAK;gBAC3C,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAWjD;AAED,qBAAa,8BAA+B,SAAQ,KAAK;gBAC3C,SAAS,EAAE,MAAM;CAI9B;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,cAAc,EAAE,CASnE;AAyBD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAInE"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { stripCommentsAndStrings } from "./source-scrubber.js";
|
|
4
|
+
export class ProgramFolderNameMismatchError extends Error {
|
|
5
|
+
constructor(sourceDir, programId) {
|
|
6
|
+
const actualDirName = path.basename(sourceDir);
|
|
7
|
+
const expectedDirName = programId.replace(/\.aleo$/, "");
|
|
8
|
+
super(`Leo program folder name mismatch: folder "${actualDirName}" declares program "${programId}". ` +
|
|
9
|
+
`Program folder names must match the declared program name. ` +
|
|
10
|
+
`Rename the folder to "${expectedDirName}" or change the "program ..."` +
|
|
11
|
+
` declaration in main.leo to "program ${actualDirName}.aleo".`);
|
|
12
|
+
this.name = "ProgramFolderNameMismatchError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class MissingProgramDeclarationError extends Error {
|
|
16
|
+
constructor(entryFile) {
|
|
17
|
+
super(`Invalid Leo program root: ${entryFile} is missing a program <name>.aleo declaration.`);
|
|
18
|
+
this.name = "MissingProgramDeclarationError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Recursively scan `programsDir` for Leo program roots (main.leo) and
|
|
23
|
+
* library roots (lib.leo) at any depth. Returns all discovered compilation units.
|
|
24
|
+
*
|
|
25
|
+
* A directory is a program root if it contains main.leo, or a library root
|
|
26
|
+
* if it contains lib.leo. Once a root is found, its subtree is collected as
|
|
27
|
+
* sources (not scanned for further roots).
|
|
28
|
+
*/
|
|
29
|
+
export function discoverUnits(programsDir) {
|
|
30
|
+
const units = [];
|
|
31
|
+
if (!fs.existsSync(programsDir)) {
|
|
32
|
+
return units;
|
|
33
|
+
}
|
|
34
|
+
scanDir(programsDir, units);
|
|
35
|
+
return units;
|
|
36
|
+
}
|
|
37
|
+
function scanDir(dir, units) {
|
|
38
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
39
|
+
for (const entry of entries) {
|
|
40
|
+
if (!entry.isDirectory())
|
|
41
|
+
continue;
|
|
42
|
+
const dirPath = path.resolve(dir, entry.name);
|
|
43
|
+
const mainLeo = path.join(dirPath, "main.leo");
|
|
44
|
+
const libLeo = path.join(dirPath, "lib.leo");
|
|
45
|
+
if (fs.existsSync(mainLeo)) {
|
|
46
|
+
// Program root — collect sources but don't recurse for more roots
|
|
47
|
+
units.push(discoverProgram(dirPath, mainLeo));
|
|
48
|
+
}
|
|
49
|
+
else if (fs.existsSync(libLeo)) {
|
|
50
|
+
// Library root — collect sources but don't recurse for more roots
|
|
51
|
+
units.push(discoverLibrary(dirPath, libLeo, entry.name));
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// Not a root — recurse deeper
|
|
55
|
+
scanDir(dirPath, units);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Extract the program ID from a main.leo file by finding the
|
|
61
|
+
* `program <name>.aleo { ... }` or `program <name>.aleo : ...` declaration.
|
|
62
|
+
* Interface syntax after the colon is intentionally left to Leo validation so
|
|
63
|
+
* discovery does not break when new interface-reference forms are introduced.
|
|
64
|
+
*/
|
|
65
|
+
export function extractProgramId(mainLeoPath) {
|
|
66
|
+
const content = stripCommentsAndStrings(fs.readFileSync(mainLeoPath, "utf-8"));
|
|
67
|
+
const match = content.match(/\bprogram\s+([\w]+\.aleo)\s*(?=[:{])/);
|
|
68
|
+
return match ? match[1] : null;
|
|
69
|
+
}
|
|
70
|
+
function discoverProgram(sourceDir, entryFile) {
|
|
71
|
+
const programId = extractProgramId(entryFile);
|
|
72
|
+
// A directory containing main.leo but no parseable `program <name>.aleo`
|
|
73
|
+
// declaration is a layout error, not a unit to skip. Like the folder-name
|
|
74
|
+
// check below, this fails fast at global discovery scope (see comment on
|
|
75
|
+
// validateProgramFolderName): one broken root aborts compile/deploy/upgrade
|
|
76
|
+
// for the whole tree rather than silently dropping the program and surfacing
|
|
77
|
+
// a confusing "program not found" downstream.
|
|
78
|
+
if (!programId) {
|
|
79
|
+
throw new MissingProgramDeclarationError(entryFile);
|
|
80
|
+
}
|
|
81
|
+
validateProgramFolderName(sourceDir, programId);
|
|
82
|
+
return {
|
|
83
|
+
kind: "program",
|
|
84
|
+
programId,
|
|
85
|
+
sourceDir,
|
|
86
|
+
entryFile,
|
|
87
|
+
allSources: collectLeoFiles(sourceDir),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Validation runs here, during discovery, so it is intentionally global: every
|
|
91
|
+
// program root under `programsDir` is checked before any `--program` filter or
|
|
92
|
+
// dependency-graph pruning is applied (see compilePipeline). A single misnamed
|
|
93
|
+
// folder therefore fails compile/deploy/upgrade for the whole tree, not just the
|
|
94
|
+
// targeted unit. That fail-fast scope is deliberate — a folder/declaration
|
|
95
|
+
// mismatch is a layout error the user should fix regardless of which program is
|
|
96
|
+
// being built.
|
|
97
|
+
function validateProgramFolderName(sourceDir, programId) {
|
|
98
|
+
const actualDirName = path.basename(sourceDir);
|
|
99
|
+
const expectedDirName = programId.replace(/\.aleo$/, "");
|
|
100
|
+
if (actualDirName !== expectedDirName) {
|
|
101
|
+
throw new ProgramFolderNameMismatchError(sourceDir, programId);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function discoverLibrary(sourceDir, entryFile, dirName) {
|
|
105
|
+
return {
|
|
106
|
+
kind: "library",
|
|
107
|
+
name: dirName,
|
|
108
|
+
sourceDir,
|
|
109
|
+
entryFile,
|
|
110
|
+
allSources: collectLeoFiles(sourceDir),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Recursively collect all .leo files under a directory,
|
|
115
|
+
* returning paths relative to the directory root.
|
|
116
|
+
*/
|
|
117
|
+
function collectLeoFiles(dir, base = "") {
|
|
118
|
+
const results = [];
|
|
119
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
120
|
+
for (const entry of entries) {
|
|
121
|
+
const rel = base ? path.join(base, entry.name) : entry.name;
|
|
122
|
+
if (entry.isDirectory()) {
|
|
123
|
+
results.push(...collectLeoFiles(path.join(dir, entry.name), rel));
|
|
124
|
+
}
|
|
125
|
+
else if (entry.name.endsWith(".leo")) {
|
|
126
|
+
results.push(rel);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return results;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=source-discovery.js.map
|