|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { exec } from "child_process"; |
| 4 | +import * as chalkUtils from "./chalkUtils.js"; |
| 5 | +import config from "../template/addonConfig.js"; |
| 6 | +import fromConsole from "./fromConsole.js"; |
| 7 | + |
| 8 | +// Paths |
| 9 | +const solutionDirectory = "../src_cpp/Project"; |
| 10 | +const buildDirectory = "../src_cpp/Build"; |
| 11 | + |
| 12 | +const msBuildPaths = [ |
| 13 | + // Visual Studio 2022 |
| 14 | + "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 15 | + "C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 16 | + "C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 17 | + "C:\\Program Files\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 18 | + |
| 19 | + // Visual Studio 2022 (x86) |
| 20 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 21 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Professional\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 22 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\Enterprise\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 23 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 24 | + |
| 25 | + // Visual Studio 2019 |
| 26 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 27 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 28 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 29 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 30 | + |
| 31 | + // Visual Studio 2017 |
| 32 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\MSBuild.exe", |
| 33 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Professional\\MSBuild\\15.0\\Bin\\MSBuild.exe", |
| 34 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\MSBuild.exe", |
| 35 | + "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe", |
| 36 | + |
| 37 | + // Visual Studio 2015 |
| 38 | + "C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\MSBuild.exe", |
| 39 | + |
| 40 | + // Visual Studio 2013 |
| 41 | + "C:\\Program Files (x86)\\MSBuild\\12.0\\Bin\\MSBuild.exe", |
| 42 | + |
| 43 | + // Visual Studio 2012 |
| 44 | + "C:\\Program Files (x86)\\MSBuild\\11.0\\Bin\\MSBuild.exe", |
| 45 | + |
| 46 | + // Visual Studio 2010 |
| 47 | + "C:\\Program Files (x86)\\MSBuild\\10.0\\Bin\\MSBuild.exe", |
| 48 | + |
| 49 | + // .NET Framework paths |
| 50 | + "C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe", // 32-bit |
| 51 | + "C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\MSBuild.exe", // 64-bit |
| 52 | +]; |
| 53 | + |
| 54 | +let msbuildPath = ""; |
| 55 | +const findMSBuild = (paths, callback) => { |
| 56 | + let foundPath = ""; |
| 57 | + |
| 58 | + paths.some((path) => { |
| 59 | + if (fs.existsSync(path)) { |
| 60 | + foundPath = path; |
| 61 | + return true; |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + msbuildPath = foundPath; |
| 66 | +}; |
| 67 | + |
| 68 | +// Build configurations |
| 69 | +const configurations = [ |
| 70 | + { configuration: "Release", platform: "x64" }, |
| 71 | + { configuration: "Release", platform: "x86" }, |
| 72 | +]; |
| 73 | + |
| 74 | +// Function to run MSBuild |
| 75 | +function buildSolution(configuration, platform) { |
| 76 | + return new Promise((resolve, reject) => { |
| 77 | + const command = `& "${msbuildPath}" "${path.join( |
| 78 | + solutionDirectory, |
| 79 | + "WrapperExtension.sln" |
| 80 | + )}" -t:Build /p:Configuration=${configuration} /p:Platform=${platform}`; |
| 81 | + |
| 82 | + console.log(command); |
| 83 | + |
| 84 | + exec(command, { shell: "powershell.exe" }, (err, stdout, stderr) => { |
| 85 | + if (err) { |
| 86 | + console.error(`Error building ${configuration}|${platform}: ${stderr}`); |
| 87 | + console.error(err); |
| 88 | + reject(err); |
| 89 | + } else { |
| 90 | + // console.log(stdout); |
| 91 | + console.log(`Built ${configuration}|${platform} successfully.`); |
| 92 | + resolve(stdout); |
| 93 | + } |
| 94 | + }); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +export default async function buildWrapperExtension() { |
| 99 | + let hadError = false; |
| 100 | + let hadOptionalError = false; |
| 101 | + let hadTip = false; |
| 102 | + |
| 103 | + if (config.files.extensionScript && config.files.extensionScript.enabled) { |
| 104 | + chalkUtils.step("Building wrapper extension..."); |
| 105 | + const srcCppExists = fs.existsSync("../src_cpp"); |
| 106 | + if (!srcCppExists) { |
| 107 | + chalkUtils.error( |
| 108 | + "Could not find src_cpp folder. Please make sure you have the src_cpp folder in the root of your project." |
| 109 | + ); |
| 110 | + hadError = true; |
| 111 | + } |
| 112 | + |
| 113 | + findMSBuild(msBuildPaths); |
| 114 | + |
| 115 | + // if it didn't find MSBuild, throw an error |
| 116 | + if (!msbuildPath) { |
| 117 | + chalkUtils.error( |
| 118 | + "Could not find MSBuild. Please install MSBuild and try again." |
| 119 | + ); |
| 120 | + hadError = true; |
| 121 | + } |
| 122 | + |
| 123 | + if (!hadError) { |
| 124 | + try { |
| 125 | + // Build each configuration |
| 126 | + for (const config of configurations) { |
| 127 | + await buildSolution(config.configuration, config.platform); |
| 128 | + } |
| 129 | + } catch (error) { |
| 130 | + chalkUtils.error("An error occurred:", error); |
| 131 | + hadError = true; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return { hadError, hadTip, hadOptionalError }; |
| 137 | +} |
| 138 | + |
| 139 | +if (fromConsole(import.meta.url)) { |
| 140 | + const dependsOn = []; |
| 141 | + build(dependsOn).then((hadError) => { |
| 142 | + if (hadError) return; |
| 143 | + buildWrapperExtension(); |
| 144 | + }); |
| 145 | +} |
0 commit comments