gulp-msbuild
Version:
msbuild plugin for gulp. Inspired by grunt-msbuild.
161 lines (160 loc) • 7.11 kB
JavaScript
"use strict";
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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MSBuildFinder = void 0;
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const constants_1 = require("./constants");
const ArchitectureService_1 = require("../utilities/ArchitectureService");
class MSBuildFinder {
constructor(options) {
var _a, _b;
this.buildIs64Bit = ArchitectureService_1.ArchitectureService.Architecture === 'x64';
this.version = (_a = options.toolsVersion) !== null && _a !== void 0 ? _a : 'auto';
this.windir = (_b = options.windir) !== null && _b !== void 0 ? _b : 'C:\Windows';
}
findVersion() {
if (this.version == 'auto')
return this.getLatestAvailableVersion();
const parsedNumber = parseFloat(this.version);
if (isNaN(parsedNumber))
throw new Error('version unknown');
// Pre-v12: MSBuild ships with the .NET Framework and does not require vswhere
if (parsedNumber < 12) {
const frameworkPath = this.concatPreV12(this.version);
if (fs.existsSync(frameworkPath)) {
return [frameworkPath, this.version];
}
return null;
}
const vswherePath = path.join(process.env['ProgramFiles(x86)'] || '', 'Microsoft Visual Studio', 'Installer', 'vswhere.exe');
if (!fs.existsSync(vswherePath)) {
console.error('vswhere.exe not found');
return null;
}
try {
const installationsBuffer = (0, child_process_1.execSync)(`"${vswherePath}" -products * -requires Microsoft.Component.MSBuild -format json`);
const installations = JSON.parse(installationsBuffer.toString());
const major = (Number)(this.version.split('.')[0]);
if (major >= 16) {
// For v16+, match by the major part of installationVersion rather than
// relying on the installation path containing a particular substring.
// This correctly handles VS 18 (and any future VS version) regardless
// of the year/name used in the installation directory.
const matchingInstallation = installations.find((installation) => {
const installMajor = parseInt((installation.installationVersion || '').split('.')[0] || '0');
return installMajor === major;
});
if (!matchingInstallation) {
return [null, this.version];
}
const msbuildPath = path.join(matchingInstallation.installationPath, 'MSBuild', 'Current', 'Bin', this.getx64_dir(), 'MSBuild.exe');
return [fs.existsSync(msbuildPath) ? msbuildPath : null, this.version];
}
// v12–v15: use path-based detection via vswhere installation paths
const installationPaths = installations.map((installation) => this.concatCorrectPath(installation.installationPath));
const installedPath = this.getInstalledVersion(installationPaths);
return [
installedPath,
this.version
];
}
catch (error) {
console.error(`Error executing vswhere: ${error.message}`);
return null;
}
}
concatCorrectPath(installationPath) {
const major = (Number)(this.version.split('.')[0]);
if (major >= 16) {
return this.concatV16AndAbove(installationPath, this.version);
}
else if (major >= 12 && major <= 15) {
return this.concatV15AndBelow(installationPath, this.version);
}
return this.concatPreV12(this.version);
}
concatPreV12(version) {
let toolVersion = constants_1.MSBUILD_VERSIONS[version];
const framework = this.buildIs64Bit ? 'Framework64' : 'Framework';
return path.join(this.windir, 'Microsoft.Net', framework, toolVersion, 'MSBuild.exe');
}
concatV15AndBelow(installationPath, version) {
return path.join(installationPath, 'MSBuild', version, 'Bin', this.getx64_dir(), 'MSBuild.exe');
}
concatV16AndAbove(installationPath, version) {
if (!installationPath.includes(constants_1.MSBUILD_VERSIONS[version]))
return '';
return path.join(installationPath, 'MSBuild', 'Current', 'Bin', this.getx64_dir(), 'MSBuild.exe');
}
getInstalledVersion(installationPaths) {
let installedPath = null;
for (let i = 0; i < installationPaths.length; i++) {
const installationPath = installationPaths[i];
if (installationPath == '')
continue;
if (fs.existsSync(installationPath)) {
installedPath = installationPath;
break;
}
}
return installedPath;
}
getLatestAvailableVersion() {
let installationPaths = [];
let versions = [];
for (const key in constants_1.MSBUILD_VERSIONS) {
this.version = key;
const installationPath = this.findVersion();
if (installationPath != null) {
if (installationPath[0] != null) {
installationPaths.push(installationPath[0]);
versions.push(key);
}
}
}
if (installationPaths.length === 0)
return null;
return [
installationPaths[installationPaths.length - 1],
versions[versions.length - 1]
];
}
getx64_dir() {
return this.buildIs64Bit ? 'amd64' : '';
}
}
exports.MSBuildFinder = MSBuildFinder;