Skip to content

Commit

Permalink
builder solidity output
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyace84 committed Jan 21, 2023
1 parent a63e362 commit 8664f97
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import fs from 'fs';
import { SolcInput, SolcOutput } from 'solidity-ast/solc';
import { Sources } from './types';
import { getContractName, getAstsFromSources } from './getters';
import {
replaceSrc,
sortASTByDependency,
updateDependices,
} from './ast-updater';
import { Config } from '../config';
import { Build } from '../site';
import path from 'path';

import { compileAst } from './compile-ast';

const createRawOutput = (sources: Sources) => {
const output: SolcOutput = { sources: {} };
let id = 0;
for (const key of Object.keys(sources)) {
const source = sources[key]!;
replaceSrc(source, id);
output.sources[key] = { ast: source, id: id++ };
}
return output;
};

const createInput = (solcOutput: SolcOutput) => {
const sources = solcOutput.sources;
const SolcInput: SolcInput = { sources: {} };
for (const key of Object.keys(sources)) {
const fileContent = fs.readFileSync(key, 'utf8').toString();
SolcInput.sources[key] = { content: fileContent };
}
return SolcInput;
};

export function isMainContract(absolutePath: string, astPath: string) {
// Extract the contract name from absolute path
if (!fileExists(absolutePath) || !fileExists(astPath)) {
throw new Error('File does not exist');
}
const contractName = getContractName(absolutePath);

// Extract the contract name from ast path
const astContractName = getContractName(astPath);

// Compare contract names
return contractName === astContractName;
}

function fileExists(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (err: any) {
if (err.code === 'ENOENT') {
return false;
} else {
throw err;
}
}
}

export const makeBuild = async (
config: Config,
contractList: string[] = [],
) => {
compileAst(config);
const { sources: astSources, fullSources } = getAstsFromSources(
config.astOutputDir!,
config.root!,
)!;
const solcOutput = createRawOutput(astSources);
const sourcesList = Object.values(fullSources).map(source => source.asts);

const sortedSources = sortASTByDependency(sourcesList, solcOutput);
updateDependices(fullSources, sortedSources, config);
const ph = path.join(config.root!, config.sourcesDir!, 'sortedSources.json');
fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2));
solcOutput.sources = sortedSources;
const solcInput = createInput(solcOutput);
const build: Build = {
input: solcInput,
output: solcOutput,
};
return build;
};

0 comments on commit 8664f97

Please sign in to comment.