Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge generated tasks build configs to the base tasks automatically #19392

Merged
merged 11 commits into from
Jan 10, 2024
97 changes: 95 additions & 2 deletions make-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ var buildNodeTask = function (taskPath, outDir) {
fail('The package.json should not contain dev dependencies other than typescript. Move the dev dependencies into a package.json file under the Tests sub-folder. Offending package.json: ' + packageJsonPath);
}

run('npm ci');
run('npm install');
}

if (test('-f', rp(path.join('Tests', 'package.json')))) {
cd(rp('Tests'));
run('npm ci');
run('npm install');
qianz2 marked this conversation as resolved.
Show resolved Hide resolved
cd(taskPath);
}

Expand Down Expand Up @@ -1850,6 +1850,99 @@ var processGeneratedTasks = function(baseConfigToolPath, taskList, makeOptions,
}
exports.processGeneratedTasks = processGeneratedTasks;

/**
* Function to merge all tasks under a build config into base tasks.
* @param {String} buildConfig that selected to merge
*/
var mergeBuildConfigIntoBaseTasks = function(buildConfig) {
var makeOptionsPath = path.join(__dirname, 'make-options.json');
var makeOptions = fileToJson(makeOptionsPath);
if (!makeOptions) fail("makeOptions is not defined");
const AllTasksToMerge = makeOptions[buildConfig];
const TasksfailedToMerge = [];

const match = buildConfig.match(/^([a-zA-Z]+[0-9]+)_\d+(_\d+)?$/);
var surfixBuildConfig = match ? match[1] : buildConfig;

if (AllTasksToMerge && Array.isArray(AllTasksToMerge)) {
AllTasksToMerge.forEach(taskName => {
var generatedTaskPath = path.join(__dirname, '_generated', `${taskName}_${surfixBuildConfig}`);
var generatedDefaultTaskPath = path.join(__dirname, '_generated', taskName)
var versionmapFilePath = path.join(__dirname, '_generated', `${taskName}.versionmap.txt`);
var baseTaskPath = path.join(__dirname, 'Tasks', taskName);
var buildConfigTaskPath = path.join(baseTaskPath, '_buildConfigs', surfixBuildConfig);

if (!fs.existsSync(generatedTaskPath) || !fs.statSync(generatedTaskPath).isDirectory() || !fs.existsSync(versionmapFilePath)) {
console.log(`Invalid generated task path ${generatedTaskPath} or invalid ${taskName}.versionmap.txt file ${versionmapFilePath}\n`);
TasksfailedToMerge.push(taskName);
} else {
banner(`Merging ${generatedTaskPath} into base task...`);

// Copy generated task to base task, delete generated files
cp('-rf', generatedTaskPath + "/*", baseTaskPath);
console.log(`Copied ${generatedTaskPath} to ${baseTaskPath}`);
rm("-rf", buildConfigTaskPath);
console.log(`Deleted ${buildConfigTaskPath} folder`);
rm("-rf", generatedTaskPath);
console.log(`Deleted ${generatedTaskPath} folder`);
rm("-rf", generatedDefaultTaskPath);
console.log(`Deleted ${generatedDefaultTaskPath} folder`);

// Update versionmap.txt file
var versionmapFile = fs.readFileSync(versionmapFilePath, { encoding: 'utf-8' });
const originalLineEnding = versionmapFile.includes('\r\n') ? '\r\n' : '\n';
qianz2 marked this conversation as resolved.
Show resolved Hide resolved
const lines = versionmapFile.split(originalLineEnding);
var buildConfigVersion = null;
for (let i = lines.length - 1; i >= 0; i--) {
if (!lines[i]) continue;
const [name, version] = lines[i].split('|');
if (name.startsWith(surfixBuildConfig)) {
buildConfigVersion = version;
lines.splice(i, 1);
i++;
} else if (name === "Default" && buildConfigVersion != null) {
lines[i] = `Default|${buildConfigVersion}`;
}
}
const updatedContent = lines.join(originalLineEnding);
fs.writeFileSync(versionmapFilePath, updatedContent, { encoding: 'utf-8' });
console.log(`Updated ${versionmapFilePath} file`);

// Remove _buildConfigMapping section in task.json and task-loc.json
var taskJsonPath = path.join(baseTaskPath, 'task.json');
var taskJson = JSON.parse(fs.readFileSync(taskJsonPath));
var taskLocJsonPath = path.join(baseTaskPath, 'task.loc.json');
var taskLocJson = JSON.parse(fs.readFileSync(taskLocJsonPath));
if (taskJson && taskJson["_buildConfigMapping"]) {
delete taskJson["_buildConfigMapping"];
}
if (taskLocJson && taskLocJson["_buildConfigMapping"]) {
delete taskLocJson["_buildConfigMapping"];
}
fs.writeFileSync(taskJsonPath, JSON.stringify(taskJson, null, 2));
fs.writeFileSync(taskLocJsonPath, JSON.stringify(taskLocJson, null, 2));
console.log(`Updated task.json and task-loc.json files under ${baseTaskPath}`);
console.log(`${generatedTaskPath} was merged into ${baseTaskPath}\n`);
}
});
} else {
fail(`Invalid configuration for ${buildConfig}.`);
}

// Update make-options.json
if (TasksfailedToMerge.length > 0) {
console.log('The following tasks failed to merge into base tasks: ' + TasksfailedToMerge);
makeOptions[buildConfig] = makeOptions[buildConfig].filter(item =>
TasksfailedToMerge.includes(item)
);
} else {
delete makeOptions[buildConfig];
}
fs.writeFileSync(makeOptionsPath, JSON.stringify(makeOptions, null, 4));
console.log("Updated make-options.json file");
}
exports.mergeBuildConfigIntoBaseTasks = mergeBuildConfigIntoBaseTasks;

/**
* Wrapper for buildTask function which compares diff between source and generated tasks
* @param {Function} originalFunction - Original buildTask function
Expand Down
15 changes: 14 additions & 1 deletion make.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ CLI.bump = function() {
taskJson.version.Patch = taskJson.version.Patch + 1;
taskLocJson.version.Patch = taskLocJson.version.Patch + 1;

fs.writeFileSync(taskJsonPath, JSON.stringify(taskJson, null, 4));
fs.writeFileSync(taskJsonPath, JSON.stringify(taskJson, null, 2));
qianz2 marked this conversation as resolved.
Show resolved Hide resolved
fs.writeFileSync(taskLocJsonPath, JSON.stringify(taskLocJson, null, 2));

// Check that task.loc and task.loc.json versions match
Expand Down Expand Up @@ -900,6 +900,19 @@ function verifyAllAgentPluginTasksAreInSkipList() {
}
}

// Merge all tasks in a build config to base tasks
// e.g node make.js mergeBuildConfig --config Node20_225
// This will 'merge' all tasks under build config Node20_225 into base tasks.
// 1. Copy generated task to base task, delete generated files in _generated/Task_Node20 and Tasks/taskname/_buildConfig/Node20.
// 2. Update versionmap.txt file.
// 3. Remove _buildConfigMapping section in task.json and task-loc.json
// 4. Update the buildConfig section in make-option.json.
CLI.mergeBuildConfig = function(/** @type {{ config: string }} */ argv) {
var config = argv.config
banner(`Merging all tasks under ${config} build config into base tasks...`);
util.mergeBuildConfigIntoBaseTasks(config);
}

// Generate sprintly zip
// This methods generate a zip file that contains the tip of all task major versions for the last sprint
// Use:
Expand Down