gulp-msbuild
Version:
msbuild plugin for gulp. Inspired by grunt-msbuild.
151 lines (150 loc) • 6.61 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MSBuildRunner = void 0;
const child_process_1 = require("child_process");
const msbuild_command_builder_1 = require("./msbuild-command-builder");
const fs = __importStar(require("fs"));
const glob_1 = require("glob");
const gutil_1 = __importDefault(require("./gutil"));
const colorette_1 = require("colorette");
class MSBuildRunner {
static startMsBuildTask(options, file, stream, callback) {
console.log('');
const commandBuilder = new msbuild_command_builder_1.MSBuildCommandBuilder();
const command = commandBuilder.construct(file, options);
if (options.logCommand) {
console.log((0, colorette_1.cyan)("Using MSBuild command:"), command.executable, command.args.join(" "));
}
const spawnOptions = {
stdio: [
'ignore',
options.stdout ? 'inherit' : 'ignore',
options.stderr ? 'inherit' : 'ignore'
]
};
let closed = false;
const child = (0, child_process_1.spawn)(command.executable, command.args, spawnOptions);
child.on("error", (error) => {
if (error) {
console.log(error);
}
// The "exit" event also can fire after the error event. We need to guard
// when the process has already been closed:
// https://nodejs.org/api/child_process.html#child_process_event_error
if (closed) {
return;
}
closed = true;
if (error) {
console.log((0, colorette_1.red)('MSBuild failed!'));
if (options.errorOnFail) {
return callback(error);
}
}
return callback();
});
child.on("exit", (code, signal) => __awaiter(this, void 0, void 0, function* () {
// The "exit" event also can fire after the error event. We need to guard
// when the process has already been closed:
// https://nodejs.org/api/child_process.html#child_process_event_error
if (closed) {
return;
}
closed = true;
if (code === 0) {
console.log((0, colorette_1.cyan)('MSBuild complete!'));
console.log('');
if (options.emitPublishedFiles) {
const publishDirectory = options.publishDirectory;
yield (0, glob_1.glob)('**/*', { cwd: publishDirectory, nodir: true, absolute: true })
.then((files) => {
for (let i = 0; i < files.length; i++) {
const filePath = files[i];
if (fs.statSync(filePath).isFile()) {
stream.push(new gutil_1.default.File({
cwd: publishDirectory,
base: publishDirectory,
path: filePath,
contents: new Buffer(fs.readFileSync(filePath))
}));
}
}
return callback();
})
.catch((err) => {
if (err) {
const msg = 'Error globbing published files at ' + publishDirectory;
console.log((0, colorette_1.red)(msg));
return callback(err);
}
});
}
else {
return callback();
}
}
else {
let msg;
if (code) {
// Exited normally, but failed.
msg = 'MSBuild failed with code ' + code + '!';
}
else {
// Killed by parent process.
msg = 'MSBuild killed with signal ' + signal + '!';
}
console.log((0, colorette_1.red)(msg));
console.log('');
if (options.errorOnFail) {
return callback(new Error(msg));
}
}
}));
}
}
exports.MSBuildRunner = MSBuildRunner;