Skip to content

Commit

Permalink
add patch for 0.57+ tsolc version
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyace84 committed Aug 4, 2023
1 parent b83dab8 commit 87a0d29
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
32 changes: 16 additions & 16 deletions src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import fs from 'fs';
import { SolcInput, SolcOutput } from 'solidity-ast/solc';
import { Sources } from './types';
import { getContractName, getAstsFromSources } from './getters';
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';
} from "./ast-updater";
import { Config } from "../config";
import { Build } from "../site";
import path from "path";

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

const createRawOutput = (sources: Sources) => {
const output: SolcOutput = { sources: {} };
Expand All @@ -28,7 +28,7 @@ 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();
const fileContent = fs.readFileSync(key, "utf8").toString();
SolcInput.sources[key] = { content: fileContent };
}
return SolcInput;
Expand All @@ -37,7 +37,7 @@ const createInput = (solcOutput: SolcOutput) => {
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');
throw new Error("File does not exist");
}
const contractName = getContractName(absolutePath);

Expand All @@ -52,7 +52,7 @@ function fileExists(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (err: any) {
if (err.code === 'ENOENT') {
if (err.code === "ENOENT") {
return false;
} else {
throw err;
Expand All @@ -62,19 +62,19 @@ function fileExists(filePath: string) {

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

const sortedSources = sortASTByDependency(sourcesList, solcOutput);
updateDependices(fullSources, sortedSources, config);
const ph = path.join(config.root!, 'build/astBuild.json');
const ph = path.join(config.root!, "build/astBuild.json");
fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2));
solcOutput.sources = sortedSources;
const solcInput = createInput(solcOutput);
Expand Down
68 changes: 51 additions & 17 deletions src/ast-builder/compile-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,55 @@ import fs from "fs";
import { execSync } from "child_process";
import { Config } from "../config";
import { getAstsFromSources, getContractsList } from "./getters";
import path from "path";
import { basename, join, resolve } from "path";

const createDirectoryIfNotExists = (dir: string) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
};

const moveFiles = (sourceDir: string, destinationDir: string) => {
let files = fs.readdirSync(sourceDir);
files.forEach((file) => {
let oldPath = join(sourceDir, file);
let newPath = join(destinationDir, file);
fs.renameSync(oldPath, newPath);
});
};

const deleteDirectoryIfExists = (dir: string) => {
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true });
}
};

/**
* It takes the config object, gets a list of contracts, and then compiles the AST for each contract
* @param {Config} config - The configuration object that we created earlier.
*/
export const compileAst = async (config: Config) => {
export const compileAst = (config: Config) => {
const contracts = getContractsList(config.sourcesDir!, config.exclude!);

let astOutputPath = path.resolve(config.root!, config.astOutputDir!);
if (fs.existsSync(astOutputPath)) {
fs.rmSync(astOutputPath, { recursive: true, force: true });
}
let astOutputPath = resolve(config.root!, config.astOutputDir!);
deleteDirectoryIfExists(astOutputPath);

let ast_cache_path = `ast-cache`;
let ast_path = `$PWD/ast`;
let ast_path = resolve(process.cwd(), "ast");

if (!fs.existsSync(ast_cache_path)) {
execSync(`mkdir ${ast_cache_path}`);
}

if (!fs.existsSync(ast_path)) {
execSync(`mkdir ${ast_path}`);
}
createDirectoryIfNotExists(ast_cache_path);
createDirectoryIfNotExists(ast_path);

contracts.forEach((contract) => {
execSync(
`${config.compilerPath} --ast-compact-json $PWD/${config.sourcesDir}/${contract} --output-dir=$PWD/${ast_cache_path}`
`${config.compilerPath} --ast-compact-json ${config.sourcesDir}/${contract} --output-dir=${ast_cache_path}`
);
execSync(`mv $PWD/${ast_cache_path}/* ${astOutputPath}`);
moveFiles(ast_cache_path, astOutputPath);
});
execSync(`rm -rf $PWD/${ast_cache_path}`);

deleteDirectoryIfExists(ast_cache_path);
compileExternalAst(config);
renameAstFiles(astOutputPath);
};

/**
Expand All @@ -59,3 +75,21 @@ export const compileExternalAst = async (config: Config) => {
}
});
};

const renameAstFiles = (dir: string) => {
const files = fs.readdirSync(dir);

files.forEach((file) => {
if (file.endsWith(".sol_json.ast")) {
const oldPath = join(dir, file);
const newFileName = basename(file, ".sol_json.ast") + ".ast.json";
const newPath = join(dir, newFileName);
fs.renameSync(oldPath, newPath);
} else if (file.endsWith(".tsol_json.ast")) {
const oldPath = join(dir, file);
const newFileName = basename(file, ".tsol_json.ast") + ".ast.json";
const newPath = join(dir, newFileName);
fs.renameSync(oldPath, newPath);
}
});
};
2 changes: 1 addition & 1 deletion src/ast-builder/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const getAstsFromSources = (astDir: string, root: string) => {
const sources: Sources = {};
const fullSources: FullSources = {};
astSources.forEach(astSourceFullPath => {
const astContent: SourceUnit[] = require(astSourceFullPath) as SourceUnit[];
const astContent: SourceUnit[] = [require(astSourceFullPath) as SourceUnit];
const withNormalPathAsts = renameAbsolutePaths(root, astContent);
const astContractName = getContractName(astSourceFullPath)!;
const mainAst = getMainAst(
Expand Down

0 comments on commit 87a0d29

Please sign in to comment.