diff --git a/build/scons-format.js b/build/scons-format.js deleted file mode 100644 index 2bf01e5f84c..00000000000 --- a/build/scons-format.js +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env node -'use strict'; - -const async = require('async'); -const clangFormat = require('clang-format'); -const promisify = require('util').promisify; -const glob = promisify(require('glob')); -const path = require('path'); -const program = require('commander'); -const EXEC_LIMIT = 10; - -const ROOT = path.join(__dirname, '..'); - -const iosSrc = [ - 'iphone/Classes/*.h', - 'iphone/Classes/*.m', - 'iphone/Classes/Layout/*.h', - 'iphone/Classes/Layout/*.m', - 'iphone/TitaniumKit/TitaniumKit/*.h', - 'iphone/TitaniumKit/TitaniumKit/Sources/**/*.h', - 'iphone/TitaniumKit/TitaniumKit/Sources/**/*.m' -]; - -const androidSrc = [ - 'android/build/src/**/*.java', - 'android/kroll-apt/src/**/*.java', - 'android/modules/*/src/**/*.java', - 'android/runtime/*/src/**/*.java', - 'android/titanium/src/**/*.java' -]; - -program.option('--android', 'Run format on Android') - .option('--fix', 'Fix files automatically', false) - .option('-e, --exec-limit [limit]', 'Max number of processes to run in parallel', EXEC_LIMIT) - .option('--ios', 'Run format on iOS') - .parse(process.argv); - -async function main(program) { - - const files = []; - const errors = []; - - if (program.android) { - for (const globs of androidSrc) { - files.push(... await glob(globs, { cwd: ROOT })); - } - } - - if (program.ios) { - for (const globs of iosSrc) { - files.push(... await glob(globs, { cwd: ROOT })); - } - } - - return new Promise((resolve, reject) => { - async.mapLimit(files, program.execLimit, function (filepath, cb) { - filepath = path.resolve(ROOT, filepath); - let stdout = ''; - - const proc = clangFormat.spawnClangFormat([ '-style=file', '-output-replacements-xml', filepath ], function () {}, 'pipe'); - proc.stdout.on('data', function (data) { - stdout += data.toString(); - }); - proc.on('close', function (exit) { - if (exit) { - console.warn('Exit code: ' + exit); - return cb(); - } - - const modified = stdout.replace(/\r?\n/g, ''); - if (modified !== '') { - // Record failure, because formatting is bad. - // TODO Get the correctly formatted source? Give more details on the bad sections? - errors.push(new Error('Formatting incorrect on "' + filepath + '", proposed changes: ' + stdout)); - } - cb(); - }); - }, function () { - if (errors.length > 0) { - const error = new Error(errors.join('\n')); - error.code = 'ELINTFAILURE'; - return reject(error); - } - return resolve(); - }); - }); - -} - -main(program) - .then(() => process.exit(0)) - .catch(err => { - // If there's lint errors, just log the message to the console, otherwise it's a script error and we want the stack - if (err.code === 'ELINTFAILURE') { - console.log(err.message); - } else { - console.log(err); - } - process.exit(1); - }); diff --git a/build/scons.js b/build/scons.js index ca69bed93d0..e2318a57e53 100755 --- a/build/scons.js +++ b/build/scons.js @@ -18,5 +18,4 @@ commander .command('xcode-test', 'Hacks the XCode project for iOS to copy in the unit test suite so it can be run under XCode\'s debugger') .command('check-ios-toplevel', 'Ensures we don\'t check in prefilled values for version/hash/timestamp') .command('xcode-project-build ', 'Runs the portion of the xcode project setup') - .command('format', 'check format of source files') .parse(process.argv); diff --git a/package-lock.json b/package-lock.json index 0cfadef3041..8a9c7f9fd26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1601,6 +1601,40 @@ "any-observable": "^0.3.0" } }, + "@seadub/clang-format-lint": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/@seadub/clang-format-lint/-/clang-format-lint-0.0.2.tgz", + "integrity": "sha512-0P/NUsIpUsP5MJ9WpEmBMlCQY2zFJDVkstrl+jb8EsWNixeshfzQYxlAeabbEbT4Buixuelr/HKLphyZB1rDYA==", + "dev": true, + "requires": { + "commander": "^3.0.2", + "glob": "^7.1.4", + "p-limit": "^2.2.1" + }, + "dependencies": { + "commander": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/commander/-/commander-3.0.2.tgz", + "integrity": "sha512-Gar0ASD4BDyKC4hl4DwHqDrmvjoxWKZigVnAbn5H1owvm4CxCPdb0HQDehwNYMJpla5+M2tPmPARzhtYuwpHow==", + "dev": true + }, + "p-limit": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", + "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", + "dev": true, + "requires": { + "p-try": "^2.0.0" + } + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", + "dev": true + } + } + }, "@seadub/danger-plugin-dependencies": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/@seadub/danger-plugin-dependencies/-/danger-plugin-dependencies-0.1.0.tgz", diff --git a/package.json b/package.json index d5dc29daf43..1d1721f9493 100644 --- a/package.json +++ b/package.json @@ -28,15 +28,15 @@ "clean:ios": "npm run clean -- ios", "commit": "git-cz", "format": "npm-run-all --parallel format:**", - "format:android": "./build/scons format --android --fix", - "format:ios": "./build/scons format --ios --fix", + "format:android": "npm run lint:android -- --fix", + "format:ios": "npm run lint:ios -- --fix", "format:js": "npm run lint:js -- --fix", "ios": "./build/scons cleanbuild ios", "ios-sanity-check": "./build/scons check-ios-toplevel", "lint": "npm-run-all --parallel lint:**", - "lint:android": "./build/scons format --android", + "lint:android": "npx clang-format-lint $npm_package_config_format_android", "lint:docs": "tdoc-validate ./apidoc", - "lint:ios": "./build/scons format --ios", + "lint:ios": "npx clang-format-lint $npm_package_config_format_ios", "lint:js": "eslint .", "test": "npm run ios-sanity-check && npm run lint", "test:android": "npm run clean:android && npm run build:android && ./build/scons package -s --no-docs android && ./build/scons install -s && ti sdk select $npm_package_version && ./build/scons test -s android", @@ -71,6 +71,10 @@ "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" + }, + "format": { + "android": "android/!(dev|templates)/**/*.java", + "ios": "iphone/Classes/*.{m,h} iphone/Classes/Layout/*.{m,h} iphone/TitaniumKit/TitaniumKit/*.h iphone/TitaniumKit/TitaniumKit/Sources/**/*.{m,h}" } }, "dependencies": { @@ -109,6 +113,7 @@ "devDependencies": { "@commitlint/cli": "^8.2.0", "@commitlint/config-conventional": "^8.2.0", + "@seadub/clang-format-lint": "0.0.2", "@seadub/danger-plugin-dependencies": "0.1.0", "@seadub/danger-plugin-eslint": "^1.0.1", "@seadub/danger-plugin-junit": "0.1.2",