diff --git a/.projen/tasks.json b/.projen/tasks.json index b052bb0..48771c9 100644 --- a/.projen/tasks.json +++ b/.projen/tasks.json @@ -33,8 +33,7 @@ "BUMPFILE": "dist/version.txt", "RELEASETAG": "dist/releasetag.txt", "RELEASE_TAG_PREFIX": "", - "BUMP_PACKAGE": "commit-and-tag-version@^12", - "NEXT_VERSION_COMMAND": "tsx ./projenrc/next-version.ts" + "BUMP_PACKAGE": "commit-and-tag-version@^12" }, "steps": [ { @@ -249,7 +248,8 @@ "name": "release", "description": "Prepare a release from \"main\" branch", "env": { - "RELEASE": "true" + "RELEASE": "true", + "MIN_MAJOR": "39" }, "steps": [ { @@ -300,8 +300,7 @@ "BUMPFILE": "dist/version.txt", "RELEASETAG": "dist/releasetag.txt", "RELEASE_TAG_PREFIX": "", - "BUMP_PACKAGE": "commit-and-tag-version@^12", - "NEXT_VERSION_COMMAND": "tsx ./projenrc/next-version.ts" + "BUMP_PACKAGE": "commit-and-tag-version@^12" }, "steps": [ { diff --git a/.projenrc.ts b/.projenrc.ts index 068cf75..a319b62 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -3,6 +3,8 @@ import { JsonPatch, cdk } from 'projen'; import { Stability } from 'projen/lib/cdk'; import { TrailingComma } from 'projen/lib/javascript'; +const SCHEMA_VERSION: typeof import('./schema/version.json') = require('./schema/version.json'); + export const project = new cdk.JsiiProject({ author: 'Amazon Web Services', authorAddress: '', @@ -55,7 +57,8 @@ export const project = new cdk.JsiiProject({ printWidth: 100, }, }, - nextVersionCommand: 'tsx ./projenrc/next-version.ts', + // This forces every release to be the major version from the data file + minMajorVersion: SCHEMA_VERSION.revision, eslintOptions: { prettier: true, dirs: ['lib'], diff --git a/lib/manifest.ts b/lib/manifest.ts index 54f2d6c..1101553 100644 --- a/lib/manifest.ts +++ b/lib/manifest.ts @@ -22,7 +22,7 @@ import INTEG_SCHEMA = require('../schema/integ.schema.json'); /** * Version is shared for both manifests */ -const SCHEMA_VERSION = require('../package.json').version; +import SCHEMA_VERSION = require('../schema/version.json'); /** * Options for the loadManifest operation @@ -138,7 +138,7 @@ export class Manifest { * Fetch the current schema version number. */ public static version(): string { - return SCHEMA_VERSION; + return `${SCHEMA_VERSION.revision}.0.0`; } /** diff --git a/projenrc/next-version.ts b/projenrc/next-version.ts deleted file mode 100644 index 19d535c..0000000 --- a/projenrc/next-version.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { schemasChanged } from './update-schema'; - -/** - * If any of the schema files changed, we need to bump the major version - */ -async function main() { - const latestTag = process.env.LATEST_TAG; - if (latestTag && schemasChanged(latestTag)) { - console.log('major'); - } -} - -main().catch((error) => { - console.error(error); - process.exitCode = 1; -}); diff --git a/projenrc/update.ts b/projenrc/update.ts index 1616f20..14265ee 100644 --- a/projenrc/update.ts +++ b/projenrc/update.ts @@ -1,10 +1,13 @@ import { SCHEMAS } from './schema-definition'; import { generateSchema } from './update-schema'; +import { maybeBumpVersion } from './versioning'; function update() { + const schemas: Record = {}; for (const s of SCHEMAS) { - generateSchema(s); + schemas[s] = generateSchema(s); } + maybeBumpVersion(schemas); } update(); diff --git a/projenrc/versioning.ts b/projenrc/versioning.ts new file mode 100644 index 0000000..d39d50d --- /dev/null +++ b/projenrc/versioning.ts @@ -0,0 +1,44 @@ +import * as crypto from 'crypto'; +import * as fs from 'fs'; +import * as path from 'path'; +import { SCHEMA_DIR } from './schema-definition'; + +export function maybeBumpVersion(schemas: Record) { + const serializedSchema = JSON.stringify(sortJson(schemas), null, 2); + + const versionFile = path.join(SCHEMA_DIR, 'version.json'); + let current: SchemaVersionFile = JSON.parse(fs.readFileSync(versionFile, 'utf8')); + const schemaHash = sha256(serializedSchema); + + if (current.schemaHash !== schemaHash) { + current = { schemaHash, revision: current.revision + 1 }; + console.log(`Schemas changed, bumping version to ${current.revision}`); + } + + fs.writeFileSync(versionFile, JSON.stringify(current, null, 2)); +} + +function sha256(x: string) { + const hash = crypto.createHash('sha256'); + hash.update(x); + return hash.digest('hex'); +} + +interface SchemaVersionFile { + revision: number; + schemaHash: string; +} + +function sortJson(x: A): A { + if (Array.isArray(x)) { + return x; + } + if (typeof x === 'object' && x !== null) { + const ret: Record = {}; + for (const key of Object.keys(x).sort()) { + ret[key] = sortJson((x as any)[key]); + } + return ret as any; + } + return x; +} diff --git a/schema/version.json b/schema/version.json new file mode 100644 index 0000000..d0e215b --- /dev/null +++ b/schema/version.json @@ -0,0 +1,4 @@ +{ + "schemaHash": "f0ccc3212331af8a1c75830878d218fd667e5957063831be3b5bfd803b25f0cc", + "revision": 39 +} \ No newline at end of file