-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: store schema version inside package (#96)
(Take 2 of #62). Currently, the schema version espoused by this package is the package version itself (i.e., the version of its own `package.json`). Move this version to a resource file that maintained inside the package and is bumped explicitly. In a projen (mono)repo, all packages have version 0.0.0. At development time, the Cloud Assembly Schema has to know its own version, for various versioning checks. It can’t get it from the package version (which is 0.0.0, so it needs to get it elsewhere, which will be schema.version.json). Closes #62.
- Loading branch information
Showing
7 changed files
with
62 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { SCHEMAS } from './schema-definition'; | ||
import { generateSchema } from './update-schema'; | ||
import { maybeBumpVersion } from './versioning'; | ||
|
||
function update() { | ||
const schemas: Record<string, any> = {}; | ||
for (const s of SCHEMAS) { | ||
generateSchema(s); | ||
schemas[s] = generateSchema(s); | ||
} | ||
maybeBumpVersion(schemas); | ||
} | ||
|
||
update(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, any>) { | ||
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<A>(x: A): A { | ||
if (Array.isArray(x)) { | ||
return x; | ||
} | ||
if (typeof x === 'object' && x !== null) { | ||
const ret: Record<string, any> = {}; | ||
for (const key of Object.keys(x).sort()) { | ||
ret[key] = sortJson((x as any)[key]); | ||
} | ||
return ret as any; | ||
} | ||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"schemaHash": "f0ccc3212331af8a1c75830878d218fd667e5957063831be3b5bfd803b25f0cc", | ||
"revision": 39 | ||
} |