build-scripts/validate.ts (66 lines of code) (raw):
/*!
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT
*/
import * as fs from 'fs'
import * as path from 'path'
import { records } from './resources/taskInputs.gen'
const tasksPath = path.join(__dirname, '../src/tasks') // Path to task entries
function validateTaskInputNotChanged() {
const newRecords: Record<string, string[]> = {}
const taskFiles = fs
.readdirSync(tasksPath, { withFileTypes: true })
.filter(e => e.isDirectory())
.map(e => path.join(tasksPath, e.name, 'task.json'))
const errors: string[] = []
for (const taskFile of taskFiles) {
const taskJson = JSON.parse(fs.readFileSync(taskFile, 'utf8'))
const taskName: string = taskJson.name
const actualTaskInputs: Set<string> = new Set(taskJson.inputs.map((i: any) => i.name))
for (const t of (records as Record<string, string[]>)[taskName]) {
if (!actualTaskInputs.has(t)) {
errors.push(` - ${taskName}/task.json: '${t}'`)
}
}
newRecords[taskName] = [...actualTaskInputs]
}
if (errors.length > 0) {
return (
'WARNING: Task inputs have changed. This is a non-backwards-compatible change and may break users with pipelines that define the removed inputs.\n' +
'Please ensure this change is intentional. You should consider a migration strategy, such as adding a new parameter only and reading both, with the new parameter overriding the old one. You can also add *Deprecated* to the display title or make it invisible.\n' +
`Missing expected task inputs:\n\n${errors.join('\n')}\n\n` +
'If this is intentional and migration steps are in place, please remove the mentioned variables in tests/resources/taskInputs.gen.json'
)
}
// Update existing task input records with newly added values
fs.writeFileSync(
path.join(__dirname, 'resources/taskInputs.gen.ts'),
`/**
* This file keeps a record of task input names for each task. We validate the current task inputs
* against this to guard against accidentally changing or removing the input tasks.
* Example of this risk: https://github.com/aws/aws-toolkit-azure-devops/issues/572
*
* How to use this file:
* - If you are adding a new input variable, no action is needed - it will be added automatically.
* - This file is autogenerated by validate.ts. It should only be modified if you are intentionally
* removing/renaming an input variable from a task, so that the validation script will pass.
* Consider the migration strategies mentioned below, since this sort of change will break users.
*
* Migration Strategies:
* - keep the old parameter but add the new parameter. You can read the new parameter first, then the old one.
* - mark the display name of the old parameter *deprecated*, or hide it from visibily (needs verification)
*/
export const records = ${JSON.stringify(newRecords, undefined, 4)
.replace(/"([^"]+)":/g, '$1:') // remove quotes around property names and convert " to '
.replace(/"/g, "'")}
`
)
return undefined
}
function main() {
const validations = [validateTaskInputNotChanged]
for (const v of validations) {
const result = v()
if (result) {
console.error(result)
process.exit(1)
}
}
console.log('All validations passed!')
}
main()