Skip to content

Commit

Permalink
fix script for creating new versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jul 25, 2023
1 parent 31a2e32 commit 951bfd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
10 changes: 6 additions & 4 deletions definitions/3.0.0/multiFormatSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
{
"description": "All the schema formats tooling MUST support",
"enum": [
"application/schema+json;version=draft-07",
"application/schema+yaml;version=draft-07",

"application/vnd.aai.asyncapi;version=3.0.0",
"application/vnd.aai.asyncapi+json;version=3.0.0",
"application/vnd.aai.asyncapi+yaml;version=3.0.0",

"application/schema+json;version=draft-07",
"application/schema+yaml;version=draft-07"
"application/vnd.aai.asyncapi+yaml;version=3.0.0"
]
},
{
Expand All @@ -54,6 +54,7 @@
{
"if": {
"not": {
"description": "If no schemaFormat has been defined, default to schema or reference",
"required": [
"schemaFormat"
]
Expand All @@ -76,6 +77,7 @@
},
{
"if": {
"description": "If schemaFormat has been defined check if it's one of the AsyncAPI Schema Object formats",
"required": [
"schemaFormat"
],
Expand Down
52 changes: 32 additions & 20 deletions scripts/add-new-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,42 @@ function execute(command) {
* if minor or fix, it add new ones
*/
function addNewSchemaVersion(newVersion, newVersionDir, latestVersion) {
const objFile = path.resolve(newVersionDir, 'messageObject.json');
const obj = require(objFile);
let enums = [] = obj?.properties?.schemaFormat?.anyOf[1]?.enum;
const newSchemaFormats = [
`application/vnd.aai.asyncapi;version=${newVersion}`,
`application/vnd.aai.asyncapi+json;version=${newVersion}`,
`application/vnd.aai.asyncapi+yaml;version=${newVersion}`
];
//Did the major version (first char) change from last to new version?
const isMajorVersionChange = newVersion.charAt(0) !== latestVersion.charAt(0);
const objFile = path.resolve(newVersionDir, 'multiFormatSchema.json');
const obj = require(objFile);

//Add new version to schemaFormat
if(enums) {
// Adapt all the MUST supported schema formats
let mustSupportedSchemaFormats = [] = obj?.else?.properties?.schemaFormat?.anyOf[1]?.enum;

//Add new version to the list of available schemaFormat values
if(mustSupportedSchemaFormats) {
if(isMajorVersionChange) {
//Remove all old AsyncAPI schema formats
enums = enums.filter((format) => !format.includes('application/vnd.aai.asyncapi'));
//Remove all old AsyncAPI schema formats because we want a clean slate
mustSupportedSchemaFormats = mustSupportedSchemaFormats.filter((format) => !format.includes('application/vnd.aai.asyncapi'));
}
//Add new schema formats
enums.push(`application/vnd.aai.asyncapi;version=${newVersion}`);
enums.push(`application/vnd.aai.asyncapi+json;version=${newVersion}`);
enums.push(`application/vnd.aai.asyncapi+yaml;version=${newVersion}`);
obj.properties.schemaFormat.anyOf[1].enum = enums;
mustSupportedSchemaFormats.push(...newSchemaFormats);
obj.else.properties.schemaFormat.anyOf[1].enum = mustSupportedSchemaFormats;
} else {
throw new Error("Could not find object to add schemaFormat values to");
}

//Make sure new versions apply the right schema
let enumsForValidatingSchema = [] = obj?.else?.allOf[1]?.if?.properties?.schemaFormat?.enum;
if(enumsForValidatingSchema) {
//Add new schema formats
enumsForValidatingSchema.push(...newSchemaFormats);
obj.else.allOf[1].if.properties.schemaFormat.enum = enumsForValidatingSchema;
} else {
throw new Error("Could not find location for schemaFormats that applies the AsyncAPI Schema object to the schema property");
}

fs.writeFileSync(objFile, JSON.stringify(obj, null, 2));
}

Expand Down Expand Up @@ -79,22 +95,18 @@ async function addNewVersion(newVersion) {
const latestVersion = (await execute('ls -d ./definitions/* | sort -V -r | head -1 | xargs -n 1 basename')).trim();
await execute(`cp -R ./definitions/${latestVersion} ${newVersionDir}`);

<<<<<<< HEAD
const latestExampleVersion = (await execute('ls -d ./examples/* | sort -V -r | head -1 | xargs -n 1 basename')).trim();
await execute(`cp -R ./examples/${latestExampleVersion} ${newExampleVersionDir}`);

// Replace $ref and $id paths such as `/3.0.0/` with new version (http://asyncapi.com/definitions/3.0.0/specificationExtension.json)
await execute(`find ${newVersionDir} -name '*.json' -exec sed -i '' \"s+\/${latestVersion}\/+\/${newVersion}\/+g\" {} +`);

// Replace .asyncapi version from old to new version
// Replace old version in title with new version
adaptRootObject(newVersion, newVersionDir);

// Add new schemaFormat version entries
addNewSchemaVersion(newVersion, newVersionDir, latestVersion);
=======
const latestExampleVersion = (await execute('ls -d ./examples/* | sort -V -r | head -1 | xargs -n 1 basename')).trim();
await execute(`cp -R ./definitions/${latestVersion} ${newVersionDir}`);
await execute(`cp -R ./examples/${latestExampleVersion} ${newExampleVersionDir}`);
// Replace old version numbers with new
await execute(`find ${newVersionDir} -name '*.json' -exec sed -i '' "s+${latestVersion}+${newVersion}+g" {} +`);
>>>>>>> next-major-spec

console.log(`New version added to ${newVersionDir}`)
}
Expand Down

0 comments on commit 951bfd7

Please sign in to comment.