Skip to content

Commit

Permalink
Merge branch 'master' into integrate-with-BDA
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyace84 authored Aug 30, 2023
2 parents 49a159c + 7288a38 commit 1368617
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./ast-updater";
import { Config } from "../config";
import { Build } from "../site";

import path, { resolve } from "path";

import { compileAst } from "./compile-ast";
Expand All @@ -27,11 +28,13 @@ const createRawOutput = (sources: Sources) => {
const createInput = (solcOutput: SolcOutput, sourcesDir: string) => {
const sources = solcOutput.sources;
const SolcInput: SolcInput = { sources: {} };

for (let key of Object.keys(sources)) {
const tempKey = key.startsWith("contracts")
? resolve(sourcesDir.replace("/contracts", "/"), key)
: resolve(sourcesDir.replace("/contracts", "/node_modules"), key);
const fileContent = fs.readFileSync(tempKey, "utf8").toString();

SolcInput.sources[key] = { content: fileContent };
}
return SolcInput;
Expand Down Expand Up @@ -97,4 +100,4 @@ export const makeBuild = async (
output: solcOutput,
};
return build;
};
};
46 changes: 45 additions & 1 deletion src/ast-builder/compile-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import { execSync } from "child_process";
import { Config } from "../config";
import { getAstsFromSources, getContractsList } from "./getters";

import { basename, join, extname, resolve } from "path";

const createDirectoryIfNotExists = (dir: string) => {
Expand Down Expand Up @@ -29,7 +30,7 @@ const deleteDirectoryIfExists = (dir: string) => {
* 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 = resolve(config.root!, config.astOutputDir!);
Expand Down Expand Up @@ -68,6 +69,7 @@ export const compileAst = async (config: Config) => {
export const compileExternalAst = async (config: Config) => {
const { fullSources } = getAstsFromSources(
config.astOutputDir!,

config.root!,
config.sourcesDir!
);
Expand Down Expand Up @@ -140,3 +142,45 @@ const wrapAstInArray = (dir: string) => {
}
});
};

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);
}
});
};

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

files.forEach((file) => {
if (extname(file) === ".json") {
const filePath = join(dir, file);
const content = fs.readFileSync(filePath, "utf8");
let jsonContent;

try {
jsonContent = JSON.parse(content);
} catch (error) {
console.error(`Error parsing file ${file}:`, error);
return;
}

if (!Array.isArray(jsonContent)) {
const wrappedContent = [jsonContent];
fs.writeFileSync(filePath, JSON.stringify(wrappedContent, null, 2));
}
}
});
};

0 comments on commit 1368617

Please sign in to comment.