Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | 'use strict'; |
| 5 | |
| 6 | const fs = require('fs'); |
| 7 | const path = require('path'); |
| 8 | |
| 9 | const FRONTEND_PATH = path.resolve(__dirname, '..', 'front_end'); |
| 10 | |
| 11 | const manifestModules = []; |
| 12 | for (var config of ['inspector.json', 'devtools_app.json', 'js_app.json', 'node_app.json', 'shell.json', 'worker_app.json']) |
| 13 | manifestModules.push(...require(path.resolve(FRONTEND_PATH, config)).modules); |
| 14 | |
| 15 | const utils = require('./utils'); |
| 16 | |
| 17 | const gnPath = path.resolve(__dirname, '..', 'BUILD.gn'); |
| 18 | const gnFile = fs.readFileSync(gnPath, 'utf-8'); |
| 19 | const gnLines = gnFile.split('\n'); |
| 20 | |
| 21 | function main() { |
| 22 | let errors = []; |
| 23 | errors = errors.concat(checkNonAutostartNonRemoteModules()); |
| 24 | errors = errors.concat(checkAllDevToolsFiles()); |
| 25 | if (errors.length) { |
| 26 | console.log('DevTools BUILD.gn checker detected errors!'); |
| 27 | console.log(`There's an issue with: ${gnPath}`); |
| 28 | console.log(errors.join('\n')); |
| 29 | process.exit(1); |
| 30 | } |
| 31 | console.log('DevTools BUILD.gn checker passed'); |
| 32 | } |
| 33 | |
| 34 | main(); |
| 35 | |
| 36 | /** |
| 37 | * Ensures that generated module files are in the right list in BUILD.gn. |
| 38 | * This is primarily to avoid remote modules from accidentally getting |
| 39 | * bundled with the main Chrome binary. |
| 40 | */ |
| 41 | function checkNonAutostartNonRemoteModules() { |
| 42 | const errors = []; |
| 43 | const gnVariable = 'generated_non_autostart_non_remote_modules'; |
| 44 | const lines = selectGNLines(`${gnVariable} = [`, ']'); |
| 45 | if (!lines.length) { |
| 46 | return [ |
| 47 | 'Could not identify non-autostart non-remote modules in gn file', |
| 48 | 'Please look at: ' + __filename, |
| 49 | ]; |
| 50 | } |
| 51 | const text = lines.join('\n'); |
| 52 | const modules = manifestModules.filter(m => m.type !== 'autostart' && m.type !== 'remote').map(m => m.name); |
| 53 | |
| 54 | const missingModules = modules.filter(m => !utils.includes(text, `${m}/${m}_module.js`)); |
| 55 | if (missingModules.length) |
| 56 | errors.push(`Check that you've included [${missingModules.join(', ')}] modules in: ` + gnVariable); |
| 57 | |
| 58 | // e.g. "$resources_out_dir/audits/audits_module.js" => "audits" |
| 59 | const mapLineToModuleName = line => line.split('/')[2].split('_module')[0]; |
| 60 | |
| 61 | const extraneousModules = lines.map(mapLineToModuleName).filter(module => !utils.includes(modules, module)); |
| 62 | if (extraneousModules.length) |
| 63 | errors.push(`Found extraneous modules [${extraneousModules.join(', ')}] in: ` + gnVariable); |
| 64 | |
| 65 | return errors; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Ensures that all source files (according to the various module.json files) are |
| 70 | * listed in BUILD.gn. |
| 71 | */ |
| 72 | function checkAllDevToolsFiles() { |
| 73 | const errors = []; |
Amanda Baker | 06da915 | 2019-03-29 17:04:12 +0000 | [diff] [blame^] | 74 | const excludedFiles = ['InspectorBackendCommands.js', 'SupportedCSSProperties.js', 'ARIAProperties.js', 'axe.js']; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 75 | const gnVariable = 'all_devtools_files'; |
Jeff Fisher | a1d0db6 | 2019-02-20 18:56:14 +0000 | [diff] [blame] | 76 | const lines = selectGNLines(`${gnVariable} = [`, ']').map(path.normalize); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 77 | if (!lines.length) { |
| 78 | return [ |
| 79 | 'Could not identify all_devtools_files list in gn file', |
| 80 | 'Please look at: ' + __filename, |
| 81 | ]; |
| 82 | } |
| 83 | const gnFiles = new Set(lines); |
| 84 | var moduleFiles = []; |
| 85 | fs.readdirSync(FRONTEND_PATH).forEach(function(moduleName) { |
| 86 | const moduleJSONPath = path.join(FRONTEND_PATH, moduleName, 'module.json'); |
| 87 | if (utils.isFile(moduleJSONPath)) { |
| 88 | const moduleJSON = require(moduleJSONPath); |
| 89 | const scripts = moduleJSON.scripts || []; |
| 90 | const resources = moduleJSON.resources || []; |
| 91 | const files = scripts.concat(resources) |
| 92 | .map(relativePathFromBuildGN) |
| 93 | .filter(file => excludedFiles.every(excludedFile => !file.includes(excludedFile))); |
| 94 | moduleFiles = moduleFiles.concat(files); |
| 95 | |
| 96 | function relativePathFromBuildGN(filename) { |
| 97 | const relativePath = path.normalize(`front_end/${moduleName}/${filename}`); |
| 98 | return `"${relativePath}",`; |
| 99 | } |
| 100 | } |
| 101 | }); |
| 102 | for (const file of moduleFiles) { |
| 103 | if (!gnFiles.has(file)) |
| 104 | errors.push(`Missing file in BUILD.gn for ${gnVariable}: ` + file); |
| 105 | } |
| 106 | return errors; |
| 107 | } |
| 108 | |
| 109 | function selectGNLines(startLine, endLine) { |
| 110 | let lines = gnLines.map(line => line.trim()); |
| 111 | let startIndex = lines.indexOf(startLine); |
| 112 | if (startIndex === -1) |
| 113 | return []; |
| 114 | let endIndex = lines.indexOf(endLine, startIndex); |
| 115 | if (endIndex === -1) |
| 116 | return []; |
| 117 | return lines.slice(startIndex + 1, endIndex); |
| 118 | } |