build.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const spawn = require('child_process').spawn,
  19. fs = require('fs'),
  20. path = require('path');
  21. const isDevMode = require('../devmode');
  22. var projectRoot = path.normalize(path.join(__dirname, '../../../../..'));
  23. function checkIsDevMode() {
  24. if (!isDevMode) {
  25. throw Error('Cannot execute build; not running in dev mode');
  26. }
  27. }
  28. /**
  29. * Targets that have been previously built.
  30. * @type {!Object}
  31. */
  32. var builtTargets = {};
  33. /**
  34. * @param {!Array.<string>} targets The targets to build.
  35. * @throws {Error} If not running in dev mode.
  36. * @constructor
  37. */
  38. var Build = function(targets) {
  39. checkIsDevMode();
  40. this.targets_ = targets;
  41. };
  42. /** @private {boolean} */
  43. Build.prototype.cacheResults_ = false;
  44. /**
  45. * Configures this build to only execute if it has not previously been
  46. * run during the life of the current process.
  47. * @return {!Build} A self reference.
  48. */
  49. Build.prototype.onlyOnce = function() {
  50. this.cacheResults_ = true;
  51. return this;
  52. };
  53. /**
  54. * Executes the build.
  55. * @return {!Promise} A promise that will be resolved when
  56. * the build has completed.
  57. * @throws {Error} If no targets were specified.
  58. */
  59. Build.prototype.go = function() {
  60. var targets = this.targets_;
  61. if (!targets.length) {
  62. throw Error('No targets specified');
  63. }
  64. // Filter out cached results.
  65. if (this.cacheResults_) {
  66. targets = targets.filter(function(target) {
  67. return !builtTargets.hasOwnProperty(target);
  68. });
  69. if (!targets.length) {
  70. return Promise.resolve();
  71. }
  72. }
  73. console.log('\nBuilding', targets.join(' '), '...');
  74. var cmd, args = targets;
  75. if (process.platform === 'win32') {
  76. cmd = 'cmd.exe';
  77. args.unshift('/c', path.join(projectRoot, 'go.bat'));
  78. } else {
  79. cmd = path.join(projectRoot, 'go');
  80. }
  81. return new Promise((resolve, reject) => {
  82. spawn(cmd, args, {
  83. cwd: projectRoot,
  84. env: process.env,
  85. stdio: ['ignore', process.stdout, process.stderr]
  86. }).on('exit', function(code, signal) {
  87. if (code === 0) {
  88. targets.forEach(function(target) {
  89. builtTargets[target] = 1;
  90. });
  91. return resolve();
  92. }
  93. var msg = 'Unable to build artifacts';
  94. if (code) { // May be null.
  95. msg += '; code=' + code;
  96. }
  97. if (signal) {
  98. msg += '; signal=' + signal;
  99. }
  100. reject(Error(msg));
  101. });
  102. });
  103. };
  104. // PUBLIC API
  105. /**
  106. * Creates a build of the listed targets.
  107. * @param {...string} var_args The targets to build.
  108. * @return {!Build} The new build.
  109. * @throws {Error} If not running in dev mode.
  110. */
  111. exports.of = function(var_args) {
  112. var targets = Array.prototype.slice.call(arguments, 0);
  113. return new Build(targets);
  114. };
  115. /**
  116. * @return {string} Absolute path of the project's root directory.
  117. * @throws {Error} If not running in dev mode.
  118. */
  119. exports.projectRoot = function() {
  120. checkIsDevMode();
  121. return projectRoot;
  122. };