Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compile all contracts if contract name not specified #121

Merged
merged 6 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions packages/cli/src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export class CompileContract extends Command {
static args = [
{
name: "contractName",
required: true,
description: "Name of the contract to compile",
required: false,
codespool marked this conversation as resolved.
Show resolved Hide resolved
default: "",
description: "Name of the contract to compile, compile all contracts if not specified.",
},
];

Expand All @@ -43,64 +44,70 @@ export class CompileContract extends Command {

const config = await getSwankyConfig();

const contractInfo = config.contracts[args.contractName];
if (!contractInfo) {
this.error(`Cannot find a contract named ${args.contractName} in swanky.config.json`);
let contractList = readdirSync(path.resolve("contracts"));
shunsukew marked this conversation as resolved.
Show resolved Hide resolved
if (args.contractName !== "") {
if (!config.contracts[args.contractName]) {
this.error(`Cannot find a contract named ${args.contractName} in swanky.config.json`);
}

if (!contractList.includes(args.contractName)) {
this.error(`Path to contract ${args.contractName} does not exist: ${path.resolve("contracts", args.contractName)}`);
}

// remove unselected contracts from contractList to compile
contractList = contractList.filter(contractName => contractName === args.contractName);
} else {
if (contractList.length === 0) {
this.error("Nothing to compile");
}
for (const contractName of contractList) {
shunsukew marked this conversation as resolved.
Show resolved Hide resolved
console.log(`${contractName} contract is found`)
}
}

const spinner = new Spinner();

const contractList = readdirSync(path.resolve("contracts"));
for (const contractName of contractList) {
const contractInfo = config.contracts[contractName];
shunsukew marked this conversation as resolved.
Show resolved Hide resolved
const contractPath = path.resolve("contracts", contractName);

await spinner.runCommand(
async () => {
return new Promise<void>((resolve, reject) => {
const compile = getBuildCommandFor(contractInfo.language, contractPath, flags.release);
compile.stdout.on("data", () => spinner.ora.clear());
compile.stdout.pipe(process.stdout);
if (flags.verbose) {
compile.stderr.on("data", () => spinner.ora.clear());
compile.stderr.pipe(process.stdout);
}
compile.on("exit", (code) => {
if (code === 0) resolve();
else reject();
});
});
},
`Compiling ${contractName} contract`,
`${contractName} Contract compiled successfully`,
);

const contractPath = path.resolve("contracts", args.contractName);
if (!contractList.includes(args.contractName)) {
this.error(`Path to contract ${args.contractName} does not exist: ${contractPath}`);
}
await spinner.runCommand(
async () => await generateTypesFor(contractInfo.language, contractInfo.name, contractPath),
`Generating ${contractName} contract ts types`,
`${contractName} contract's TS types Generated successfully`
);

await spinner.runCommand(
async () => {
return new Promise<void>((resolve, reject) => {
const compile = getBuildCommandFor(contractInfo.language, contractPath, flags.release);
compile.stdout.on("data", () => spinner.ora.clear());
compile.stdout.pipe(process.stdout);
if (flags.verbose) {
compile.stderr.on("data", () => spinner.ora.clear());
compile.stderr.pipe(process.stdout);
}
compile.on("exit", (code) => {
if (code === 0) resolve();
else reject();
});
});
},
"Compiling contract",
"Contract compiled successfully",
);

await spinner.runCommand(
async () => await generateTypesFor(contractInfo.language, contractInfo.name, contractPath),
"Generating contract ts types",
"TS types Generated successfully"
);

const buildData = (await spinner.runCommand(async () => {
return moveArtifacts(contractInfo.name);
}, "Moving artifacts")) as BuildData;
const buildData = (await spinner.runCommand(async () => {
return moveArtifacts(contractInfo.name);
}, "Moving artifacts")) as BuildData;

await spinner.runCommand(async () => {
contractInfo.build = buildData;
}

await spinner.runCommand(async () => {
await writeJSON(path.resolve("swanky.config.json"), config, {
spaces: 2,
});
}, "Writing config");
}
}

// https://github.com/Supercolony-net/typechain-polkadot#usage-of-typechain-compiler
interface TypechainCompilerConfig {
projectFiles: string[]; // Path to all project files, everystring in glob format
skipLinting : boolean; // Skip linting of project files
artifactsPath : string; // Path to artifacts folder, where artifacts will be stored it will save both .contract and .json (contract ABI)
typechainGeneratedPath : string; // Path to typechain generated folder
}
113 changes: 69 additions & 44 deletions packages/cli/src/commands/contract/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class CompileContract extends Command {
static args = [
{
name: "contractName",
required: true,
required: false,
default: "",
description: "Name of the contract to compile",
shunsukew marked this conversation as resolved.
Show resolved Hide resolved
},
];
Expand All @@ -33,54 +34,78 @@ export class CompileContract extends Command {

const config = await getSwankyConfig();

const contractInfo = config.contracts[args.contractName];
if (!contractInfo) {
this.error(`Cannot find a contract named ${args.contractName} in swanky.config.json`);
}

const contractList = readdirSync(path.resolve("contracts"));
let contractList = readdirSync(path.resolve("contracts"));
if (args.contractName !== "") {
if (!config.contracts[args.contractName]) {
this.error(`Cannot find a contract named ${args.contractName} in swanky.config.json`);
}

const contractPath = path.resolve("contracts", args.contractName);
if (!contractList.includes(args.contractName)) {
this.error(`Path to contract ${args.contractName} does not exist: ${contractPath}`);
}
if (!contractList.includes(args.contractName)) {
this.error(`Path to contract ${args.contractName} does not exist: ${path.resolve("contracts", args.contractName)}`);
}

if (!contractInfo.build) {
this.error(`Cannot find build data for ${args.contractName} contract in swanky.config.json`);
// remove unselected contracts from contractList to compile
contractList = contractList.filter(contractName => contractName === args.contractName);
} else {
if (contractList.length === 0) {
this.error("Nothing to test");
}
for (const contractName of contractList) {
console.log(`${contractName} contract is found`)
}
}
const buildData = contractInfo.build;

const reportDir = path.resolve(buildData.artifactsPath, "testReports", Date.now().toString());
await ensureDir(reportDir);
const projectDir = path.resolve();
const testDir = path.resolve("test");
console.log(projectDir)
console.log(testDir);
for (const contractName of contractList) {
const contractInfo = config.contracts[contractName];

const mocha = new Mocha({
timeout: 200000,
reporter: "mochawesome",
reporterOptions: {
reportDir,
charts: true,
reportTitle: `${args.contractName} test report`,
quiet: true,
json: false,
},
});

const tests = await globby(`${path.resolve("test", args.contractName)}/*.test.ts`);

mocha.addFile;
tests.forEach((test) => {
mocha.addFile(test);
});

global.contractTypesPath = path.resolve("test", args.contractName, "typedContract");

shell.cd(`./test/${args.contractName}`);
mocha.run((failures) => {
if (failures) {
this.error(`At least one of the tests failed. Check report for details: ${reportDir}`);
} else {
this.log(`All tests passing. Check the report for details: ${reportDir}`);
if (!contractInfo.build) {
this.error(`Cannot find build data for ${contractName} contract in swanky.config.json`);
}
const buildData = contractInfo.build;

const reportDir = path.resolve(projectDir, buildData.artifactsPath, "testReports", Date.now().toString());
await ensureDir(reportDir);

const mocha = new Mocha({
timeout: 200000,
reporter: "mochawesome",
reporterOptions: {
reportDir,
charts: true,
reportTitle: `${contractName} test report`,
quiet: true,
json: false,
},
});

const tests = await globby(`${path.resolve(testDir, contractName)}/*.test.ts`);

mocha.addFile;
tests.forEach((test) => {
mocha.addFile(test);
});

global.contractTypesPath = path.resolve(testDir, contractName, "typedContract");

shell.cd(`${testDir}/${contractName}`);
try {
await new Promise<void>((resolve, reject) => {
mocha.run((failures) => {
if (failures) {
reject(`At least one of the tests failed. Check report for details: ${reportDir}`);
} else {
this.log(`All tests passing. Check the report for details: ${reportDir}`);
resolve();
}
});
});
} catch (error) {
this.error(error as string);
}
});
}
}
}