Path: blob/trunk/javascript/selenium-webdriver/lib/test/build.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const fs = require('node:fs')20const path = require('node:path')21const { spawn } = require('node:child_process')22const PROJECT_ROOT = path.normalize(path.join(__dirname, '../../../..'))23const WORKSPACE_FILE = path.join(PROJECT_ROOT, 'WORKSPACE')2425function isDevMode() {26return fs.existsSync(WORKSPACE_FILE)27}2829function checkIsDevMode() {30if (!isDevMode()) {31throw Error('Cannot execute build; not running in dev mode')32}33}3435/**36* Targets that have been previously built.37* @type {!Object}38*/39let builtTargets = {}4041/**42* @param {!Array.<string>} targets The targets to build.43* @throws {Error} If not running in dev mode.44* @constructor45*/46const Build = function (targets) {47checkIsDevMode()48this.targets_ = targets49}5051/** @private {boolean} */52Build.prototype.cacheResults_ = false5354/**55* Configures this build to only execute if it has not previously been56* run during the life of the current process.57* @return {!Build} A self reference.58*/59Build.prototype.onlyOnce = function () {60this.cacheResults_ = true61return this62}6364/**65* Executes the build.66* @return {!Promise} A promise that will be resolved when67* the build has completed.68* @throws {Error} If no targets were specified.69*/70Build.prototype.go = function () {71let targets = this.targets_72if (!targets.length) {73throw Error('No targets specified')74}7576// Filter out cached results.77if (this.cacheResults_) {78targets = targets.filter(function (target) {79return !Object.prototype.hasOwnProperty.call(builtTargets, target)80})8182if (!targets.length) {83return Promise.resolve()84}85}8687console.log('\nBuilding', targets.join(' '), '...')8889let cmd,90args = targets91if (process.platform === 'win32') {92cmd = 'cmd.exe'93args.unshift('/c', path.join(PROJECT_ROOT, 'go.bat'))94} else {95cmd = path.join(PROJECT_ROOT, 'go')96}9798return new Promise((resolve, reject) => {99spawn(cmd, args, {100cwd: PROJECT_ROOT,101env: process.env,102stdio: ['ignore', process.stdout, process.stderr],103}).on('exit', function (code, signal) {104if (code === 0) {105targets.forEach(function (target) {106builtTargets[target] = 1107})108return resolve()109}110111let msg = 'Unable to build artifacts'112if (code) {113// May be null.114msg += '; code=' + code115}116if (signal) {117msg += '; signal=' + signal118}119120reject(Error(msg))121})122})123}124125// PUBLIC API126127exports.isDevMode = isDevMode128129/**130* Creates a build of the listed targets.131* @param {...string} var_args The targets to build.132* @return {!Build} The new build.133* @throws {Error} If not running in dev mode.134*/135exports.of = function (_) {136let targets = Array.prototype.slice.call(arguments, 0)137return new Build(targets)138}139140/**141* @return {string} Absolute path of the project's root directory.142* @throws {Error} If not running in dev mode.143*/144exports.projectRoot = function () {145return PROJECT_ROOT146}147148149