2017-07-31 08:14:19 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Message = require('../Message');
|
|
|
|
|
|
|
|
const PUPPETEER_VERSION = require('../../../package.json').version;
|
|
|
|
|
|
|
|
module.exports = function(sources) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const messages = [];
|
2017-07-31 08:14:19 +00:00
|
|
|
let commands = [];
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const source of sources) {
|
2017-07-31 08:14:19 +00:00
|
|
|
const text = source.text();
|
|
|
|
const commandStartRegex = /<!--\s*gen:([a-z]+)(?:\s*\(\s*([^)]*)\s*\))?\s*-->/ig;
|
|
|
|
const commandEndRegex = /<!--\s*gen:stop\s*-->/ig;
|
|
|
|
let start;
|
|
|
|
|
|
|
|
while (start = commandStartRegex.exec(text)) { // eslint-disable-line no-cond-assign
|
|
|
|
commandEndRegex.lastIndex = commandStartRegex.lastIndex;
|
|
|
|
const end = commandEndRegex.exec(text);
|
|
|
|
if (!end) {
|
|
|
|
messages.push(Message.error(`Failed to find 'gen:stop' for comamnd ${start[0]}`));
|
|
|
|
break;
|
|
|
|
}
|
2017-07-31 10:23:29 +00:00
|
|
|
const name = start[1];
|
|
|
|
const arg = start[2];
|
|
|
|
const from = commandStartRegex.lastIndex;
|
|
|
|
const to = end.index;
|
2017-07-31 08:14:19 +00:00
|
|
|
commandStartRegex.lastIndex = commandEndRegex.lastIndex;
|
2017-07-31 10:23:29 +00:00
|
|
|
commands.push({name, arg, from, to, source});
|
2017-07-31 08:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-31 10:23:29 +00:00
|
|
|
|
|
|
|
commands = validateCommands(commands, messages);
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const changedSources = new Set();
|
2017-07-31 10:23:29 +00:00
|
|
|
// Iterate commands in reverse order so that edits don't conflict.
|
|
|
|
commands.sort((a, b) => b.from - a.from);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const command of commands) {
|
2017-07-31 08:14:19 +00:00
|
|
|
let newText = command.source.text();
|
2017-08-01 04:14:50 +00:00
|
|
|
if (command.name === 'version')
|
2017-07-31 08:14:19 +00:00
|
|
|
newText = replaceInText(newText, command.from, command.to, PUPPETEER_VERSION);
|
|
|
|
if (command.source.setText(newText))
|
|
|
|
changedSources.add(command.source);
|
|
|
|
}
|
2017-08-22 21:18:07 +00:00
|
|
|
for (const source of changedSources)
|
2017-07-31 08:14:19 +00:00
|
|
|
messages.push(Message.warning(`GEN: updated ${source.projectPath()}`));
|
|
|
|
return messages;
|
|
|
|
};
|
|
|
|
|
2017-07-31 10:23:29 +00:00
|
|
|
/**
|
|
|
|
* @param {!Array<!Object>} commands
|
|
|
|
* @param {!Array<!Message>} outMessages
|
|
|
|
* @return {!Array<!Object>}
|
|
|
|
*/
|
|
|
|
function validateCommands(commands, outMessages) {
|
|
|
|
// Filter sane commands
|
2017-08-21 23:39:04 +00:00
|
|
|
const goodCommands = commands.filter(command => {
|
2017-07-31 10:23:29 +00:00
|
|
|
if (command.name === 'version')
|
|
|
|
return check(command, !command.arg, `"gen:version" should not have argument`);
|
|
|
|
check(command, false, `Unknown command: "gen:${command.name}"`);
|
|
|
|
});
|
|
|
|
|
|
|
|
return goodCommands;
|
|
|
|
|
|
|
|
function check(command, condition, message) {
|
|
|
|
if (condition)
|
|
|
|
return true;
|
|
|
|
outMessages.push(Message.error(`${command.source.projectPath()}: ${message}`));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 04:14:50 +00:00
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @param {number} from
|
|
|
|
* @param {number} to
|
|
|
|
* @param {string} newText
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-07-31 08:14:19 +00:00
|
|
|
function replaceInText(text, from, to, newText) {
|
|
|
|
return text.substring(0, from) + newText + text.substring(to);
|
|
|
|
}
|