Skip to content

Commit

Permalink
feat(blueprint): Support --blueprint-project
Browse files Browse the repository at this point in the history
Needed for github-actions
  • Loading branch information
jubnzv committed Oct 9, 2024
1 parent 85b90ec commit 959fd1e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
24 changes: 16 additions & 8 deletions src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ export type TactProjectInfo = {
options?: ConfigProject["options"];
};

/**
* Gets filepath to the compiler config of the given project, e.g. ./wrappers/Project1.compile.ts
*/
async function getCompileConfigPath(projectName: string): Promise<string> {
const compilablesDirectory = await getCompilablesDirectory();
return path.join(compilablesDirectory, projectName + COMPILE_END);
}

/**
* Extracts the `CompilerConfig` from the given project name.
*
* XXX: Imported from blueprint, since the original function is private:
* https://github.com/ton-org/blueprint/issues/151
*/
async function getCompilerConfigForContract(
name: string,
projectName: string,
): Promise<CompilerConfig> {
const compilablesDirectory = await getCompilablesDirectory();
const mod = await import(path.join(compilablesDirectory, name + COMPILE_END));
const configPath = await getCompileConfigPath(projectName);
const mod = await import(configPath);
if (typeof mod.compile !== "object") {
throw new Error(`Object 'compile' is missing`);
}
Expand All @@ -43,10 +51,8 @@ async function getCompilerConfigForContract(
* Extracts an information from the TypeScript wrapper file generated by Blueprint.
*/
export async function extractProjectInfo(
blueprintCompilePath: string,
projectName: string,
): Promise<TactProjectInfo> | never {
const filePath = path.resolve(__dirname, blueprintCompilePath);
const projectName = path.basename(filePath).replace(".compile.ts", "");
const compilerConfig = await getCompilerConfigForContract(projectName);
switch (compilerConfig.lang) {
case "func":
Expand All @@ -60,8 +66,10 @@ export async function extractProjectInfo(
options: compilerConfig.options,
};
default:
// XXX: It might be *anything* according to the Blueprint API
throw new Error(`Please specify \`lang\` property in ${filePath}`);
// XXX: `compilerConfig.lang` might be *anything* according to the Blueprint API
throw new Error(
`Please specify \`lang\` property in ${getCompileConfigPath(projectName)}`,
);
}
}

Expand Down
41 changes: 30 additions & 11 deletions src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function selectProject(
].join("\n"),
);
}
return await extractProjectInfo(result.path);
return await extractProjectInfo(result.name);
}

export class MistiExecutor {
Expand All @@ -50,8 +50,24 @@ export class MistiExecutor {
ui: UIProvider,
): Promise<MistiExecutor> | never {
let argsStr = argsToStringList(args).slice(1);

// Find and remove --blueprint-project argument
// That's a blueprint-misti argument, not a Misti argument
let blueprintProjectName: string | undefined;
const projectIndex = argsStr.indexOf("--blueprint-project");
if (projectIndex !== -1) {
if (projectIndex + 1 < argsStr.length) {
blueprintProjectName = argsStr[projectIndex + 1];
argsStr.splice(projectIndex, 2); // Remove --blueprint-project and its value
} else {
throw new Error("--blueprint-project argument is missing a value");
}
}

const command = createMistiCommand();

// XXX: This hack is required because Misti v0.4 requires a path to the
// project/contract to be specified in order to parse arguments.
let tactPathIsDefined = true;
const originalArgsStr = [...argsStr];
try {
Expand All @@ -74,16 +90,19 @@ export class MistiExecutor {
const tactPath = command.args[0];
const projectName = path.basename(tactPath).split(".")[0];
return new MistiExecutor(projectName, argsStr, ui);
}

// Interactively select the project
const project = await selectProject(ui, args);
try {
const tactPath = this.generateTactConfig(project, ".");
argsStr.push(tactPath);
return new MistiExecutor(project.projectName, argsStr, ui);
} catch {
throw new Error(`Cannot create a Tact config in current directory`);
} else {
const project = blueprintProjectName
? // The user has specified the project name using --blueprint-project
await extractProjectInfo(blueprintProjectName)
: // Interactively select the project
await selectProject(ui, args);
try {
const tactPath = this.generateTactConfig(project, ".");
argsStr.push(tactPath);
return new MistiExecutor(project.projectName, argsStr, ui);
} catch {
throw new Error(`Cannot create a Tact config in current directory`);
}
}
}

Expand Down

0 comments on commit 959fd1e

Please sign in to comment.