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

fix(builder): Add preset reference in source/package name #415

Merged
merged 5 commits into from
Sep 11, 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
6 changes: 3 additions & 3 deletions packages/builder/src/schemas.zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const argsUnion = z.union([argtype, argtype2, argtype3, argtype4]);
// <%= string interpolation %>, step.names or property.names, packages:versions
const interpolatedRegex = RegExp(/^<%=\s\w+.+[\w()[\]-]+\s%>$/, 'i');
const stepRegex = RegExp(/^[\w-]+\.[.\w-]+$/, 'i');
const packageRegex = RegExp(/^[\w-]+\.*:*[\w.-]+$/, 'i');
const packageRegex = RegExp(/^[\w-]+\.*:*[\w.-@]+$/, 'i');
const jsonAbiPathRegex = RegExp(/^(?!.*\.d?$).*\.json?$/, 'i');

// This regex matches artifact names which are just capitalized words like solidity contract names
Expand Down Expand Up @@ -147,7 +147,7 @@ export const importSchema = z
source: z.string().refine(
(val) => !!val.match(packageRegex) || !!val.match(stepRegex) || !!val.match(interpolatedRegex),
(val) => ({
message: `Source value: ${val} must match package format "package:version" or step format "import.Contract" or be an interpolated value`,
message: `Source value: ${val} must match package formats: "package:version" or "package:version@preset" or step format "import.Contract" or be an interpolated value`,
})
),
})
Expand Down Expand Up @@ -343,7 +343,7 @@ export const provisionSchema = z
source: z.string().refine(
(val) => !!val.match(packageRegex) || !!val.match(interpolatedRegex),
(val) => ({
message: `Source value: ${val} must match package format "package:version" or be an interpolated value`,
message: `Source value: ${val} must match package formats: "package:version" or "package:version@preset" or be an interpolated value`,
})
),
})
Expand Down
24 changes: 22 additions & 2 deletions packages/builder/src/steps/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ export default {
async getState(runtime: ChainBuilderRuntime, ctx: ChainBuilderContextWithHelpers, config: Config) {
const cfg = this.configInject(ctx, config);

const preset = config.preset ?? 'main';
const chainId = config.chainId ?? runtime.chainId;
/*
* Source preset reference
* Checks for preset reference in source name, anything after the '@' references a preset.
* If a preset reference is found in the package name by default it will take preference over a defined preset.
*/
if (cfg.source.includes('@')) {
cfg.preset = cfg.source.slice(cfg.source.indexOf('@') + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a user specsifies both a preset in the source and a preset in the config, its probably an error, one way or the other. shouldn't we warn them about this somehow?

cfg.source = cfg.source.substring(0, cfg.source.indexOf('@'));
}

const preset = cfg.preset;
const chainId = cfg.chainId ?? runtime.chainId;

debug('resolved pkg', cfg.source, `${chainId}-${preset}`);
const url = await runtime.registry.getUrl(cfg.source, `${chainId}-${preset}`);
Expand Down Expand Up @@ -62,6 +72,16 @@ export default {
const importLabel = packageState.currentLabel?.split('.')[1] || '';
debug('exec', config);

/*
* Source preset reference
* Checks for preset reference in source name, anything after the '@' references a preset.
* If a preset reference is found in the package name by default it will take preference over a defined preset.
*/
if (config.source.includes('@')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might make sense to turn the package name into a proper data structure at this point, rather than having this splitting/math happening all over the place

config.preset = config.source.slice(config.source.indexOf('@') + 1);
config.source = config.source.substring(0, config.source.indexOf('@'));
}

const packageRef = config.source.includes(':') ? config.source : `${config.source}:latest`;
const preset = config.preset ?? 'main';
const chainId = config.chainId ?? runtime.chainId;
Expand Down
24 changes: 22 additions & 2 deletions packages/builder/src/steps/provision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ export default {
const importLabel = packageState.currentLabel?.split('.')[1] || '';
const cfg = this.configInject(ctx, config, packageState);

const sourcePreset = config.sourcePreset ?? 'main';
const chainId = config.chainId ?? CANNON_CHAIN_ID;
/*
* Source preset reference
* Checks for preset reference in source name, anything after the '@' references a preset.
* If a preset reference is found in the package name by default it will take preference over a defined sourcePreset.
*/
if (cfg.source.includes('@')) {
cfg.sourcePreset = cfg.source.slice(cfg.source.indexOf('@') + 1);
cfg.source = cfg.source.substring(0, cfg.source.indexOf('@'));
}

const sourcePreset = cfg.sourcePreset;
const chainId = cfg.chainId ?? CANNON_CHAIN_ID;

if (ctx.imports[importLabel]?.url) {
const prevUrl = ctx.imports[importLabel].url!;
Expand Down Expand Up @@ -97,6 +107,16 @@ export default {
const importLabel = packageState.currentLabel.split('.')[1] || '';
debug('exec', config);

/*
* Source preset reference
* Checks for preset reference in source name, anything after the '@' references a preset.
* If a preset reference is found in the package name by default it will take preference over a defined sourcePreset.
*/
if (config.source.includes('@')) {
config.sourcePreset = config.source.slice(config.source.indexOf('@') + 1);
config.source = config.source.substring(0, config.source.indexOf('@'));
}

const sourcePreset = config.sourcePreset ?? 'main';
const targetPreset = config.targetPreset ?? 'main';
const chainId = config.chainId ?? CANNON_CHAIN_ID;
Expand Down