Skip to content

Commit

Permalink
fix: Fixes on publish with preset (#675)
Browse files Browse the repository at this point in the history
Co-authored-by: Matías <[email protected]>
  • Loading branch information
FuzzB0t and mjlescano authored Dec 8, 2023
1 parent 9292832 commit f0df807
Show file tree
Hide file tree
Showing 15 changed files with 974 additions and 623 deletions.
1,344 changes: 831 additions & 513 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usecannon/builder",
"version": "2.10.5",
"version": "2.10.6-alpha.4",
"description": "Assembles cannonfile.toml manifests into cannon packages.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
72 changes: 45 additions & 27 deletions packages/builder/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import _ from 'lodash';
import { createInitialContext, getArtifacts } from './builder';
import { ChainDefinition } from './definition';
import { CannonStorage } from './runtime';
import { BundledOutput, ChainArtifacts, DeploymentInfo } from './types';
import { BundledOutput, ChainArtifacts, DeploymentInfo, StepState } from './types';

const debug = Debug('cannon:cli:publish');

interface PartialRefValues {
name: string;
version?: string;
preset?: string;
}

export type CopyPackageOpts = {
packageRef: string;
chainId: number;
Expand All @@ -24,7 +30,6 @@ export const PKG_REG_EXP = /^(?<name>@?[a-z0-9][A-Za-z0-9-]{1,29}[a-z0-9])(?::(?
* Used to format any reference to a cannon package and split it into it's core parts
*/
export class PackageReference {
private ref: string;
/**
* Anything before the colon or an @ (if no version is present) is the package name.
*/
Expand All @@ -43,11 +48,35 @@ export class PackageReference {
* Convenience parameter for returning packageRef with interpolated version and preset like name:version@preset
*/
get fullPackageRef() {
return `${this.name}:${this.version}@${this.preset}`;
const res = `${this.name}:${this.version}@${this.preset}`;
if (!PackageReference.isValid(res)) throw new Error(`Invalid package reference "${res}"`);
return res;
}

get packageRef() {
return `${this.name}:${this.version}`;
const res = `${this.name}:${this.version}`;
if (!PackageReference.isValid(res)) throw new Error(`Invalid package reference "${res}"`);
return res;
}

/**
* Parse package reference without normalizing it
*/
static parse(ref: string) {
const match = ref.match(PKG_REG_EXP);

if (!match || !match.groups?.name) {
throw new Error(
`Invalid package name "${ref}". Should be of the format <package-name>:<version> or <package-name>:<version>@<preset>`
);
}

const res: PartialRefValues = { name: match.groups.name };

if (match.groups.version) res.version = match.groups.version;
if (match.groups.preset) res.preset = match.groups.preset;

return res;
}

static isValid(ref: string) {
Expand All @@ -61,26 +90,13 @@ export class PackageReference {
}

constructor(ref: string) {
this.ref = ref;

const match = this.ref!.match(PKG_REG_EXP);

if (!match) {
throw new Error(
`Invalid package name "${this.ref}". Should be of the format <package-name>:<version> or <package-name>:<version>@<preset>`
);
}

const { name, version = 'latest', preset = 'main' } = match.groups!;
const parsed = PackageReference.parse(ref);
const { name, version = 'latest', preset = 'main' } = parsed;

this.name = name;
this.version = version;
this.preset = preset;
}

toString() {
return this.fullPackageRef;
}
}

/**
Expand Down Expand Up @@ -128,7 +144,7 @@ export async function forPackageTree<T extends { url?: string; artifacts?: Chain

function _deployImports(deployInfo: DeploymentInfo) {
if (!deployInfo.state) return [];
return Object.values(deployInfo.state).flatMap((state) => Object.values(state.artifacts.imports || {}));
return _.flatMap(_.values(deployInfo.state), (state: StepState) => Object.values(state.artifacts.imports || {}));
}

export async function getProvisionedPackages(packageRef: string, chainId: number, tags: string[], storage: CannonStorage) {
Expand All @@ -140,7 +156,7 @@ export async function getProvisionedPackages(packageRef: string, chainId: number

if (!deployInfo) {
throw new Error(
`could not find deployment artifact for ${fullPackageRef} while checking for provisioned packages. Please double check your settings, and rebuild your package.`
`could not find deployment artifact for ${fullPackageRef} with chain id "${chainId}" while checking for provisioned packages. Please double check your settings, and rebuild your package.`
);
}

Expand All @@ -157,7 +173,7 @@ export async function getProvisionedPackages(packageRef: string, chainId: number

return {
packagesNames: _.uniq([def.getVersion(preCtx) || 'latest', ...(context && context.tags ? context.tags : tags)]).map(
(t) => `${def.getName(preCtx)}:${t}@${context && context.preset ? context.preset : preset}`
(t: string) => `${def.getName(preCtx)}:${t}@${context && context.preset ? context.preset : preset || 'main'}`
),
chainId: chainId,
url: context?.url,
Expand All @@ -176,14 +192,16 @@ export async function publishPackage({
chainId,
fromStorage,
toStorage,
preset,
includeProvisioned = false,
}: CopyPackageOpts) {
debug(`copy package ${packageRef} (${fromStorage.registry.getLabel()} -> ${toStorage.registry.getLabel()})`);

// TODO: packageRef in this case can be a package name or an IPFS hash (@ipfs://Qm...) for the pin command, however, this functionality should have
// it's own function to handle the pinning of IPFS urls.
const fullPackageRef = !packageRef.startsWith('@') ? new PackageReference(packageRef).fullPackageRef : packageRef;
const packageReference = PackageReference.isValid(packageRef) ? new PackageReference(packageRef) : null;

const presetRef = packageReference ? packageReference.preset : 'main';
const fullPackageRef = packageReference ? packageReference.fullPackageRef : packageRef;

const alreadyCopiedIpfs = new Map<string, any>();

Expand Down Expand Up @@ -223,9 +241,9 @@ export async function publishPackage({

const returnVal = {
packagesNames: _.uniq([def.getVersion(preCtx) || 'latest', ...(context && context.tags ? context.tags : tags)]).map(
(t) => `${def.getName(preCtx)}:${t}@${context && context.preset ? context.preset : preset || 'main'}`
(t: string) => `${def.getName(preCtx)}:${t}@${context && context.preset ? context.preset : presetRef}`
),
chainId: chainId,
chainId,
url,
metaUrl: newMetaUrl || '',
};
Expand All @@ -239,7 +257,7 @@ export async function publishPackage({

if (!deployData) {
throw new Error(
`could not find deployment artifact for ${fullPackageRef}. Please double check your settings, and rebuild your package.`
`could not find deployment artifact for ${fullPackageRef} with chain id "${chainId}". Please double check your settings, and rebuild your package.`
);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/builder/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ export class InMemoryRegistry extends CannonRegistry {
return this.pkgs[packageRef] ? this.pkgs[packageRef][variant] : null;
}

async getMetaUrl(packageRef: string, chainId: number): Promise<string | null> {
const { preset, fullPackageRef } = new PackageReference(packageRef);
async getMetaUrl(packageOrServiceRef: string, chainId: number): Promise<string | null> {
const { preset, packageRef } = new PackageReference(packageOrServiceRef);

const variant = `${chainId}-${preset}`;
return this.metas[fullPackageRef] ? this.metas[fullPackageRef][variant] : null;
return this.metas[packageRef] ? this.metas[packageRef][variant] : null;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -300,10 +300,11 @@ export class OnChainRegistry extends CannonRegistry {
for (const registerPackage of packagesNames) {
const versions = packagesNames.filter((pkg) => pkg === registerPackage).map((p) => new PackageReference(p).version);

const ref = new PackageReference(registerPackage);
const { name, preset } = new PackageReference(registerPackage);
const variant = `${chainId}-${preset}`;

console.log(`Package: ${name}`);
console.log(`Package: ${ref.fullPackageRef}`);
console.log(`Tags: ${versions}`);
console.log(`Package URL: ${url}`);

Expand Down
2 changes: 1 addition & 1 deletion packages/builder/src/steps/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const importSpec = {
}

config.source = packageRef.fullPackageRef;
config.preset = _.template(config.preset)(ctx) || packageRef.preset;
config.preset = _.template(config.preset)(ctx);

return config;
},
Expand Down
6 changes: 3 additions & 3 deletions packages/builder/src/steps/provision.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('steps/provision.ts', () => {

expect(result).toStrictEqual({
source: 'abc:latest@main',
sourcePreset: 'main',
sourcePreset: '',
targetPreset: 'with-who',
});
});
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('steps/provision.ts', () => {
const result = await action.getState(
fakeRuntime,
fakeCtx,
{ source: 'hello:1.0.0', sourcePreset: 'foobar', chainId: 1234, targetPreset: 'voop', options: { bar: 'baz' } },
{ source: 'hello:1.0.0', sourcePreset: 'main', chainId: 1234, targetPreset: 'voop', options: { bar: 'baz' } },
{ name: 'who', version: '1.0.0', currentLabel: 'provision.whatever' }
);

Expand Down Expand Up @@ -153,7 +153,7 @@ describe('steps/provision.ts', () => {
const result = await action.exec(
fakeRuntime,
fakeCtx,
{ source: 'hello:1.0.0' },
{ source: 'hello:1.0.0@main' },
{ name: 'package', version: '1.0.0', currentLabel: 'import.something' }
);

Expand Down
16 changes: 11 additions & 5 deletions packages/builder/src/steps/provision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ const provisionSpec = {
configInject(ctx: ChainBuilderContextWithHelpers, config: Config, packageState: PackageState) {
config = _.cloneDeep(config);

const packageRef = new PackageReference(_.template(config.source)(ctx));
const ref = new PackageReference(_.template(config.source)(ctx));

config.source = ref.fullPackageRef;

if (config.sourcePreset) {
console.warn(
Expand All @@ -84,10 +86,11 @@ const provisionSpec = {
)
)
);

config.source = PackageReference.from(ref.name, ref.version, config.sourcePreset).fullPackageRef;
}

config.source = packageRef.fullPackageRef;
config.sourcePreset = _.template(config.sourcePreset)(ctx) || packageRef.preset;
config.sourcePreset = _.template(config.sourcePreset)(ctx);
config.targetPreset = _.template(config.targetPreset)(ctx) || `with-${packageState.name}`;

if (config.options) {
Expand Down Expand Up @@ -134,7 +137,8 @@ const provisionSpec = {
const importLabel = packageState.currentLabel.split('.')[1] || '';
debug('exec', config);

const source = config.source;
const sourceRef = new PackageReference(config.source);
const source = sourceRef.fullPackageRef;
const sourcePreset = config.sourcePreset;
const targetPreset = config.targetPreset ?? 'main';
const chainId = config.chainId ?? CANNON_CHAIN_ID;
Expand All @@ -143,7 +147,9 @@ const provisionSpec = {
const deployInfo = await runtime.readDeploy(source, chainId);
if (!deployInfo) {
throw new Error(
`deployment not found: ${source}. please make sure it exists for preset ${sourcePreset} and network ${chainId}.`
`deployment not found: ${source}. please make sure it exists for preset ${
sourcePreset || sourceRef.preset
} and network ${chainId}.`
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@usecannon/cli",
"version": "2.10.5",
"version": "2.10.6-alpha.4",
"description": "Utility for instantly loading cannon packages in standalone contexts",
"main": "dist/src/index.js",
"scripts": {
Expand Down Expand Up @@ -55,7 +55,7 @@
"dependencies": {
"@iarna/toml": "^3.0.0",
"@synthetixio/wei": "^2.74.1",
"@usecannon/builder": "^2.10.5",
"@usecannon/builder": "^2.10.6-alpha.4",
"chalk": "^4.1.2",
"commander": "^9.5.0",
"debug": "^4.3.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function build({
preset = presetArg;
}

const fullPackageRef = packageRef.toString();
const { fullPackageRef } = packageRef;

let pkgName = name;
let pkgVersion = version;
Expand Down Expand Up @@ -219,7 +219,7 @@ export async function build({
console.log();
});

runtime.on(Events.ResolveDeploy, (packageName, chainId, registry, d) =>
runtime.on(Events.ResolveDeploy, (packageName, preset, chainId, registry, d) =>
console.log(magenta(`${' '.repeat(d)} Resolving ${packageName} (Chain ID: ${chainId}) via ${registry}...`))
);
runtime.on(Events.DownloadDeploy, (hash, gateway, d) =>
Expand Down
Loading

0 comments on commit f0df807

Please sign in to comment.