Skip to content

Commit

Permalink
reading third party contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Javadyakuza committed Aug 14, 2023
1 parent f1ce337 commit 49a159c
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 38 deletions.
32 changes: 23 additions & 9 deletions src/ast-builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "./ast-updater";
import { Config } from "../config";
import { Build } from "../site";
import path from "path";
import path, { resolve } from "path";

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

Expand All @@ -24,20 +24,33 @@ const createRawOutput = (sources: Sources) => {
return output;
};

const createInput = (solcOutput: SolcOutput) => {
const createInput = (solcOutput: SolcOutput, sourcesDir: string) => {
const sources = solcOutput.sources;
const SolcInput: SolcInput = { sources: {} };
for (const key of Object.keys(sources)) {
const fileContent = fs.readFileSync(key, "utf8").toString();
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;
};

export function isMainContract(absolutePath: string, astPath: string) {
export function isMainContract(
absolutePath: string,
astPath: string,
sourcesDir: string
) {
// checking if its in the project main contracts ot=r and third party contract
sourcesDir = absolutePath.startsWith("contracts")
? sourcesDir.replace("/contracts", "")
: sourcesDir.replace("/contracts", "/node_modules");
// Extract the contract name from absolute path
if (!fileExists(absolutePath) || !fileExists(astPath)) {
throw new Error("File does not exist");
if (!fileExists(resolve(sourcesDir, absolutePath)) || !fileExists(astPath)) {
throw new Error(`File does not exist :${resolve(sourcesDir, absolutePath)},
${absolutePath},
${astPath}`);
}
const contractName = getContractName(absolutePath);

Expand Down Expand Up @@ -67,7 +80,8 @@ export const makeBuild = async (
compileAst(config);
const { sources: astSources, fullSources } = getAstsFromSources(
config.astOutputDir!,
config.root!
config.root!,
config.sourcesDir!
)!;
const solcOutput = createRawOutput(astSources);
const sourcesList = Object.values(fullSources).map((source) => source.asts);
Expand All @@ -77,7 +91,7 @@ export const makeBuild = async (
const ph = path.join(config.root!, "build/astBuild.json");
fs.writeFileSync(ph, JSON.stringify(sortedSources, null, 2));
solcOutput.sources = sortedSources;
const solcInput = createInput(solcOutput);
const solcInput = createInput(solcOutput, config.sourcesDir!);
const build: Build = {
input: solcInput,
output: solcOutput,
Expand Down
23 changes: 18 additions & 5 deletions src/ast-builder/compile-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ export const compileAst = async (config: Config) => {

createDirectoryIfNotExists(astCachePath);
createDirectoryIfNotExists(astPath);

const nodeModules =
config.sourcesDir != "contracts"
? `--base-path ${config.sourcesDir?.replace(
"/contracts",
""
)} -i ${config.sourcesDir?.replace("/contracts", "/node_modules")}`
: "";
contracts.forEach((contract) => {
execSync(
`${config.compilerPath} --ast-compact-json ${config.sourcesDir}/${contract} --output-dir=${astCachePath}`
`${config.compilerPath} --ast-compact-json ${config.sourcesDir}/${contract} --output-dir=${astCachePath} ${nodeModules}`
);
moveFiles(astCachePath, astOutputPath);
});
Expand All @@ -62,21 +68,28 @@ export const compileAst = async (config: Config) => {
export const compileExternalAst = async (config: Config) => {
const { fullSources } = getAstsFromSources(
config.astOutputDir!,
config.root!
config.root!,
config.sourcesDir!
);

let astCachePath = resolve(config.root!, `ast-cache`);
let astPath = resolve(config.root!, "ast");

createDirectoryIfNotExists(astCachePath);
createDirectoryIfNotExists(astPath);

const nodeModules =
config.sourcesDir != "contracts"
? `--base-path ${config.sourcesDir?.replace(
"/contracts",
""
)} -i ${config.sourcesDir?.replace("contracts", "node_modules")}`
: "";
Object.values(fullSources).forEach((source) => {
for (const ast of source.asts) {
const absolutePath = ast.absolutePath;
if (!absolutePath.startsWith(config.sourcesDir!)) {
execSync(
`${config.compilerPath} --ast-compact-json $PWD/${absolutePath} --output-dir=${astCachePath}`
`${config.compilerPath} --ast-compact-json $PWD/${config.sourcesDir}/${absolutePath} --output-dir=${astCachePath} ${nodeModules}`
);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/ast-builder/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ export function getContractName(filePath: string): string {
export const getMainAst = (
asts: SourceUnit[],
contractName: string,
astFullPath: string
astFullPath: string,
sourcesDir: string
) => {
let mainContract = null;
for (const ast of asts) {
if (
ast.absolutePath.endsWith(`${contractName}.sol`) ||
ast.absolutePath.endsWith(`${contractName}.tsol`)
) {
if (isMainContract(ast.absolutePath, astFullPath)) {
if (isMainContract(ast.absolutePath, astFullPath, sourcesDir)) {
mainContract = ast;
}
}
Expand Down Expand Up @@ -112,7 +113,11 @@ export const getDependenciesCount = (sources: SourceUnit[][]) => {
* @returns An object with the absolute path of the contract as the key and the contract's AST as the
* value.
*/
export const getAstsFromSources = (astDir: string, root: string) => {
export const getAstsFromSources = (
astDir: string,
root: string,
sourcesDir: string
) => {
const astSources = fs
.readdirSync(path.resolve(root, astDir))
.filter((file) => file.endsWith(".ast.json"))
Expand All @@ -127,7 +132,8 @@ export const getAstsFromSources = (astDir: string, root: string) => {
const mainAst = getMainAst(
withNormalPathAsts,
astContractName,
astSourceFullPath
astSourceFullPath,
sourcesDir
);
if (mainAst) {
sources[mainAst.absolutePath] = mainAst;
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function main(
config.collapseNewlines
);
replaceAdocReferences(renderedSite, config.sourcesDir);
if (renderedSite.length <= 0) throw new Error(`Sites not generated`);
for (const { id, contents } of renderedSite) {
const outputFile = path.resolve(config.root, config.outputDir, id);
await fs.mkdir(path.dirname(outputFile), { recursive: true });
Expand Down
6 changes: 4 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Site, Page, DocItemWithContext, DOC_ITEM_CONTEXT } from "./site";
import { Templates } from "./templates";
import { itemType } from "./utils/item-type";
import fs from "fs";
import { join } from "path";
import { join, resolve } from "path";

export interface RenderedPage {
id: string;
Expand Down Expand Up @@ -67,7 +67,9 @@ function readmeHelper(
])
);
return new H.SafeString(
H.compile(fs.readFileSync(path, "utf8"))(renderedItems, opts)
H.compile(
fs.readFileSync(resolve(srcDir.replace("/contracts", ""), path), "utf8")
)(renderedItems, opts)
);
}

Expand Down
11 changes: 6 additions & 5 deletions src/site.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "path";
import path, { resolve } from "path";
import { ContractDefinition, SourceUnit } from "solidity-ast";
import { SolcOutput, SolcInput } from "solidity-ast/solc";
import {
Expand Down Expand Up @@ -98,10 +98,11 @@ export function buildSite(
const isNewFile = !seen.has(ast.absolutePath);
seen.add(ast.absolutePath);

const relativePath = path.relative(
siteConfig.sourcesDir,
ast.absolutePath
);
const sourcesDir = ast.absolutePath.startsWith("contracts")
? siteConfig.sourcesDir.replace("/contracts", "")
: siteConfig.sourcesDir.replace("/contracts", "/node_modules");

const relativePath = path.relative(sourcesDir, ast.absolutePath);
const file = Object.assign(ast, { relativePath });

for (const topLevelItem of file.nodes) {
Expand Down
42 changes: 31 additions & 11 deletions src/utils/gen-HTML.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import Processor from "asciidoctor";
import fs from "fs";
import { extname, resolve } from "path";
import { extname, join, resolve } from "path";
import { Config } from "../config";
const processor = Processor();

function searchFilesWithExtension(folderPath: string): string[] {
let results: string[] = [];

function searchRecursively(currentPath: string) {
const files = fs.readdirSync(currentPath);

files.forEach((file) => {
const filePath = resolve(currentPath, file);
const stat = fs.statSync(filePath);

if (stat.isDirectory()) {
searchRecursively(filePath);
} else if (extname(filePath) === ".adoc") {
results.push(filePath);
}
});
}

searchRecursively(folderPath);

return results;
}
export async function generateHTMLFiles(config: Config): Promise<void> {
try {
const distDirPath = resolve(config.root!, config.outputDir!, "dist");
Expand All @@ -20,18 +42,16 @@ export async function generateHTMLFiles(config: Config): Promise<void> {
await fs.promises.unlink(resolve(distDirPath, file));
}
}
const filesInPath = await fs.promises.readdir(outputDirPath);
const adocFiles = filesInPath.filter((file) => file.endsWith(".adoc"));
if (adocFiles.length === 0) {
const filesInPath = searchFilesWithExtension(outputDirPath);

if (filesInPath.length === 0) {
throw new Error("No adoc files found!!");
}
for (const file of filesInPath) {
if (extname(file) === ".adoc") {
await processor.convertFile(resolve(outputDirPath, file), {
to_dir: distDirPath,
});
}
}
filesInPath.forEach(async (file) => {
processor.convertFile(file, {
to_dir: distDirPath,
});
});
} catch (err: any) {
throw new Error(err.message);
}
Expand Down
6 changes: 4 additions & 2 deletions src/utils/is-child.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';
import path from "path";

export function isChild(file: string, parent: string) {
return path.normalize(file + path.sep).startsWith(path.normalize(parent + path.sep));
return path
.normalize(file + path.sep)
.startsWith(path.normalize(parent + path.sep));
}

0 comments on commit 49a159c

Please sign in to comment.