Skip to content

Commit

Permalink
chore: more configuration improvements (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealAmazonKendra authored Aug 13, 2024
1 parent cef237b commit a96063b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 14 additions & 11 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const project = new cdk.JsiiProject({
javaPackage: 'software.amazon.awscdk.cloudassembly.schema',
mavenArtifactId: 'cdk-cloud-assembly-schema',
mavenGroupId: 'software.amazon.awscdk',
mavenEndpoint: 'https://s01.oss.sonatype.org',
mavenEndpoint: 'https://aws.oss.sonatype.org',
},
publishToNuget: {
dotNetNamespace: 'Amazon.CDK.CloudAssembly.Schema',
Expand All @@ -46,8 +46,7 @@ export const project = new cdk.JsiiProject({
module: 'aws_cdk.cloud_assembly_schema',
},
publishToGo: {
// The version at the end of the module name may be a number or v_NEXT in package.json
moduleName: `github.com/aws/aws-cdk-go/awscdk/cloudassemblyschema/awscdkcloudassemblyschema/v${Version.goVersion()}`,
moduleName: `github.com/cdklabs/cloud-assembly-schema-go`,
},
prettier: true,
prettierOptions: {
Expand Down Expand Up @@ -78,27 +77,31 @@ export const project = new cdk.JsiiProject({
minMajorVersion: Version.bump(),
});

project.addScripts({
'update-schema': 'ts-node --prefer-ts-exts -e "require(\'./projenrc/update.ts\').update()"',
});
const updateSchema = 'ts-node --prefer-ts-exts -e "require(\'./projenrc/update.ts\').update()"';

project.preCompileTask.exec('yarn update-schema');
project.preCompileTask.exec(updateSchema);
project.addTask('pre-release', {
env: { RELEASE: 'true' },
steps: [
{
exec: 'yarn update-schema',
exec: updateSchema,
},
{
condition: `node -e "if (process.env.CI) process.exit(1)"`,
say: '✨ No changes created as a result of running pre-release or release should be committed. They will likely be reverted upon submission of the PR.',
},
{
exec: 'yarn default',
},
],
});

project.tasks
.tryFind('release')
?.updateStep(4, { exec: "git diff --ignore-space-at-eol ':!.projen/tasks.json'" });
project.tasks.tryFind('release')?.updateStep(4, {
exec: 'git restore package.json',
});

project.tasks.tryFind('release')?.exec('git restore .projen/tasks.json');
project.tasks.tryFind('release')?.exec('git diff --ignore-space-at-eol');

const packageJson = project.tryFindObjectFile('package.json');
packageJson?.patch(
Expand Down
5 changes: 2 additions & 3 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projenrc/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './update';
export * from './bump';
export * from './version-bump';
33 changes: 8 additions & 25 deletions projenrc/update-schema.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as tjs from 'typescript-json-schema';
import { SCHEMA_DIR, getGeneratedSchemaPaths, getSchemaDefinition } from './schema-definition';
import { exec, log } from './util';

export function bump() {
const tags = exec([
'git',
'ls-remote',
'--tags',
'git@github.com:cdklabs/cloud-assembly-schema.git',
]);

const oldVersion = tags.split('/v').pop()!.slice(0, -3);

const newVersion = schemasChanged() ? semver.inc(oldVersion, 'major')! : oldVersion;

log(`Updating schema version: ${oldVersion} -> ${newVersion}`);
return parseInt(newVersion);
}

export function schemasChanged(): boolean {
const changes = exec(['git', 'diff', '--name-only', 'origin/main']).split('\n');
const branch = exec(['git', 'rev-parse', '--abbrev-ref', 'HEAD']);
console.log(exec(['git', 'fetch', 'origin', '--prune']));
console.log(exec(['git', 'branch', '-a']));
const changes = exec(['git', 'diff', '--name-only', branch, 'remotes/origin/main']).split('\n');
return changes.filter((change) => getGeneratedSchemaPaths().includes(change)).length > 0;
}

/**
* Generates a schema from typescript types.
* @returns JSON schema
* @param schemaName the schema to generate
* @param shouldBump writes a new version of the schema and bumps the major version
*/
export function generateSchema(schemaName: string, saveToFile: boolean = true) {
export function generateSchema(schemaName: string) {
const spec = getSchemaDefinition(schemaName);
const out = saveToFile ? path.join(SCHEMA_DIR, `${schemaName}.schema.json`) : '';
const out = path.join(SCHEMA_DIR, `${schemaName}.schema.json`);

const settings: Partial<tjs.Args> = {
required: true,
Expand All @@ -54,10 +39,8 @@ export function generateSchema(schemaName: string, saveToFile: boolean = true) {
augmentDescription(schema);
addAnyMetadataEntry(schema);

if (out) {
log(`Generating schema to ${out}`);
fs.writeFileSync(out, JSON.stringify(schema, null, 4));
}
log(`Generating schema to ${out}`);
fs.writeFileSync(out, JSON.stringify(schema, null, 4));

return schema;
}
Expand Down
27 changes: 10 additions & 17 deletions projenrc/bump.ts → projenrc/version-bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import * as semver from 'semver';
import { schemasChanged } from './update-schema';
import { exec, log } from './util';

const PLACEHOLDER = '_NEXT';
export class Version {
public static bump() {
try {
const versionInfo = new Version();
if (versionInfo.changed) {
log(`Updating schema version: ${versionInfo.current} -> ${versionInfo.next}`);
return versionInfo.asInt();
if (versionInfo.changed && process.env.RELEASE) {
log(`✨ Updating schema version: ${versionInfo.current} -> ${versionInfo.next}`);
return versionInfo.nextAsInt();
} else if (versionInfo.changed) {
log(
`✨ This change will update the schema version: ${versionInfo.current} -> ${versionInfo.next}`
);
}
return undefined;
} catch (e) {
Expand All @@ -21,28 +24,18 @@ export class Version {
}
}

public static goVersion() {
return Version.bump() ?? PLACEHOLDER;
}

public readonly current: string;
public readonly next: string;
public readonly changed: boolean;

constructor() {
const tags = exec([
'git',
'ls-remote',
'--tags',
'git@github.com:cdklabs/cloud-assembly-schema.git',
]);

this.current = tags.split('/v').pop()!.slice(0, -3);
const tags = exec(['git', 'ls-remote', '--tags', 'origin']);
this.current = tags.split('/v').pop()!;
this.changed = schemasChanged();
this.next = this.changed ? semver.inc(this.current, 'major')! : this.current;
}

public asInt() {
public nextAsInt() {
return parseInt(this.next);
}
}

0 comments on commit a96063b

Please sign in to comment.