Files
meteor/scripts/generate-dev-bundle.ps1
2024-10-21 17:00:04 -04:00

437 lines
14 KiB
PowerShell

$ErrorActionPreference = "Stop"
$DebugPreference = "Continue"
Import-Module -Force "$PSScriptRoot\windows\dev-bundle-lib.psm1"
$PLATFORM = Get-MeteorPlatform
$PYTHON_VERSION = "3.9.5" # For node-gyp
& cmd /c 'du 2>&1'
$dirCheckout = (Get-Item $PSScriptRoot).parent.FullName
$shCommon = Join-Path $PSScriptRoot 'build-dev-bundle-common.sh'
$tempSrcNode = Join-Path $(Join-Path $dirCheckout 'temp_build_src') 'node.7z'
# This will be the temporary directory we build the dev bundle in.
$DIR = Join-Path $dirCheckout 'gdbXXX'
# extract the bundle version from the meteor bash script
$BUNDLE_VERSION = Read-VariableFromShellScript "${dirCheckout}\meteor" 'BUNDLE_VERSION'
# extract the major package versions from the build-dev-bundle-common script.
$MONGO_VERSION_64BIT = Read-VariableFromShellScript $shCommon 'MONGO_VERSION_64BIT'
$NPM_VERSION = Read-VariableFromShellScript $shCommon 'NPM_VERSION'
$NODE_VERSION = Read-VariableFromShellScript $shCommon 'NODE_VERSION'
# 7-zip path.
$system7zip = "C:\Program Files\7-zip\7z.exe"
# Required for downloading MongoDB via HTTPS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Since we reuse the same temp directory, cleanup from previous failed runs.
Remove-DirectoryRecursively $DIR
# Some commonly used paths in this script.
$dirBin = Join-Path $DIR 'bin'
$dirLib = Join-Path $DIR 'lib'
$dirServerLib = Join-Path $DIR 'server-lib'
$dirTemp = Join-Path $DIR 'temp'
# Use a cache just for this build.
$dirNpmCache = Join-Path $dirTemp 'npm-cache'
# Build our directory framework.
New-Item -ItemType Directory -Force -Path $DIR | Out-Null
New-Item -ItemType Directory -Force -Path $dirTemp | Out-Null
New-Item -ItemType Directory -Force -Path $dirBin | Out-Null
New-Item -ItemType Directory -Force -Path $dirLib | Out-Null
New-Item -ItemType Directory -Force -Path $dirServerLib | Out-Null
$webclient = New-Object System.Net.WebClient
$shell = New-Object -com shell.application
Function Invoke-Install7ZipApplication {
Write-Host "Downloading 7-zip..." -ForegroundColor Magenta
$7zMsiPath = Join-Path $dirTemp '7z.msi'
# 32-bit, right now. But this does not go in the bundle.
$webclient.DownloadFile("https://www.7-zip.org/a/7z1604.msi", $7zMsiPath)
Write-Host "Installing 7-zip system-wide..." -ForegroundColor Magenta
& "msiexec.exe" /i $7zMsiPath /quiet /qn /norestart | Out-Null
# Cleanup.
Remove-Item $7zMsiPath
}
Function Add-7ZipTool {
Write-Host "Downloading 7-zip 'extra'..." -ForegroundColor Magenta
$extraArchive = Join-Path $dirTemp 'extra.7z'
$webclient.DownloadFile("https://www.7-zip.org/a/7z1604-extra.7z", $extraArchive)
$pathToExtract = 'x64/7za.exe'
Write-Host 'Placing 7za.exe from extra.7z in \bin...' -ForegroundColor Magenta
& "$system7zip" e $extraArchive -o"$dirTemp" $pathToExtract | Out-Null
Move-Item $(Join-Path $dirTemp '7za.exe') $(Join-Path $dirBin "7z.exe")
# Cleanup
Remove-Item $extraArchive
}
Function Add-Python {
# On Windows we provide a reliable version of python.exe for use by
# node-gyp (the tool that rebuilds binary node modules).
# This self-hosted 7z is created by archiving the result of running the
# Python MSI installer (from python.org), targeted at a temp directory, and
# only including: "Python" and "Utility Scripts". Then, 7z the temp directory.
$pythonUrl = "https://s3.amazonaws.com/com.meteor.static/windows-python/",
"$PLATFORM/python-${PYTHON_VERSION}.7z" -Join ''
$pythonArchive = Join-Path $dirTemp 'python.7z'
$webclient.DownloadFile($pythonUrl, $pythonArchive)
Expand-7zToDirectory $pythonArchive $DIR
$pythonDir = Join-Path $DIR 'python'
$pythonExe = Join-Path $pythonDir 'python.exe'
# Make sure the version is right, when python is called.
if (!(cmd /c python.exe --version '2>&1' -Eq "Python ${PYTHON_VERSION}")) {
throw "Python was not the version we expected it to be ($PYTHON_VERSION)"
}
Remove-Item $pythonArchive
"$pythonExe"
}
Function Add-NodeAndNpm {
# Ensure NVM is installed
if (!(Get-Command nvm -ErrorAction SilentlyContinue)) {
Write-Host "NVM is not installed. Installing NVM..." -ForegroundColor Magenta
# Create a new temporary folder with explicit permissions
$tempFolderName = "MeteorNVMInstall_" + [System.Guid]::NewGuid().ToString()
$tempFolder = Join-Path $env:TEMP $tempFolderName
try {
New-Item -ItemType Directory -Path $tempFolder | Out-Null
$acl = Get-Acl $tempFolder
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($env:USERNAME, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.SetAccessRule($accessRule)
Set-Acl $tempFolder $acl
# NVM for Windows installation
$nvmUrl = "https://github.com/coreybutler/nvm-windows/releases/download/1.1.10/nvm-setup.exe"
$nvmInstaller = Join-Path $tempFolder "nvm-setup.exe"
# Download NVM installer
$webclient.DownloadFile($nvmUrl, $nvmInstaller)
# Run NVM installer silently and capture output
$installOutput = & $nvmInstaller /SILENT /NORESTART 2>&1
# Remove the installer
Remove-Item $nvmInstaller
# Refresh environment variables
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# Verify NVM installation
if (!(Get-Command nvm -ErrorAction SilentlyContinue)) {
throw "NVM command not found after installation."
}
Write-Host "NVM installed successfully." -ForegroundColor Green
}
catch {
Write-Host "Failed to install NVM. Error details:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
if ($installOutput) {
Write-Host "Installer output:" -ForegroundColor Red
Write-Host $installOutput -ForegroundColor Red
}
throw "NVM installation failed. Please install it manually and try again."
}
finally {
# Clean up the temporary folder
if (Test-Path $tempFolder) {
Remove-Item -Recurse -Force $tempFolder
}
}
}
Write-Host "Installing Node.js ${NODE_VERSION} using NVM..." -ForegroundColor Magenta
nvm install $NODE_VERSION
if ($LASTEXITCODE -ne 0) {
throw "Failed to install Node.js ${NODE_VERSION} using NVM."
}
Write-Host "Setting Node.js ${NODE_VERSION} as the current version..." -ForegroundColor Magenta
nvm use $NODE_VERSION
if ($LASTEXITCODE -ne 0) {
throw "Failed to set Node.js ${NODE_VERSION} as the current version."
}
$nodeExePath = (Get-Command node).Source
$npmExePath = (Get-Command npm).Source
# Install the specific npm version we want
Write-Host "Installing npm@${NPM_VERSION}..." -ForegroundColor Magenta
& $npmExePath install -g npm@$NPM_VERSION
if ($LASTEXITCODE -ne 0) {
throw "Couldn't install npm@${NPM_VERSION}."
}
# Refresh npm path after installation
$npmExePath = (Get-Command npm).Source
# Copy Node.js and npm to the bundle directory
Copy-Item $nodeExePath $dirBin
Copy-Item $npmExePath $dirBin
$finalNodeExe = Join-Path $dirBin 'node.exe'
$finalNpmCmd = Join-Path $dirBin 'npm.cmd'
# Uses process.execPath to infer dev_bundle\bin, npm location, &c.
& "$finalNodeExe" "${dirCheckout}\scripts\windows\link-npm-bin-commands.js"
# We use our own npm.cmd.
Copy-Item "${dirCheckout}\scripts\npm.cmd" $finalNpmCmd
return New-Object -Type PSObject -Prop $(@{
node = $finalNodeExe
npm = $finalNpmCmd
})
}
Function Add-Mongo {
# Mongo >= 3.4 no longer supports 32-bit (x86) architectures, so we package
# the latest 3.2 version of Mongo for those builds and >= 3.4 for x64.
$mongo_filenames = @{
windows_x64 = "mongodb-windows-x86_64-${MONGO_VERSION_64BIT}"
}
# the folder inside the zip still uses win32
$mongo_zip_filenames = @{
windows_x64 = "mongodb-win32-x86_64-windows-${MONGO_VERSION_64BIT}"
}
$previousCwd = $PWD
cd "$DIR"
mkdir "$DIR\mongodb"
mkdir "$DIR\mongodb\bin"
$mongo_name = $mongo_filenames.Item($PLATFORM)
$mongo_zip_name = $mongo_zip_filenames.Item($PLATFORM)
$mongo_link = "https://fastdl.mongodb.org/windows/${mongo_name}.zip"
$mongo_zip = "$DIR\mongodb\mongo.zip"
Write-Host "Downloading Mongo from ${mongo_link}..." -ForegroundColor Magenta
$webclient.DownloadFile($mongo_link, $mongo_zip)
Write-Host "Extracting Mongo ${mongo_zip}..." -ForegroundColor Magenta
$zip = $shell.NameSpace($mongo_zip)
foreach($item in $zip.items()) {
$shell.Namespace("$DIR\mongodb").copyhere($item, 0x14) # 0x10 - overwrite, 0x4 - no dialog
}
Write-Host "Putting MongoDB mongod.exe in mongodb\bin" -ForegroundColor Magenta
cp "$DIR\mongodb\$mongo_zip_name\bin\mongod.exe" $DIR\mongodb\bin
Write-Host "Putting MongoDB mongos.exe in mongodb\bin" -ForegroundColor Magenta
cp "$DIR\mongodb\$mongo_zip_name\bin\mongos.exe" $DIR\mongodb\bin
Write-Host "Removing the old Mongo zip..." -ForegroundColor Magenta
rm -Recurse -Force $mongo_zip
Write-Host "Removing the old Mongo directory..." -ForegroundColor Magenta
rm -Recurse -Force "$DIR\mongodb\$mongo_zip_name"
cd "$previousCwd"
}
Function Add-NpmModulesFromJsBundleFile {
Param (
[Parameter(Mandatory=$True, Position=0)]
[string]$SourceJs,
[Parameter(Mandatory=$True, Position=1)]
[string]$Destination,
[Parameter(Mandatory=$True)]
$Commands,
[bool]$Shrinkwrap = $False
)
$previousCwd = $PWD
If (!(Test-Path $SourceJs)) {
throw "Couldn't find the source: $SourceJs"
}
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
cd "$Destination"
Write-Host "Writing 'package.json' from ${SourceJs} to ${Destination}" `
-ForegroundColor Magenta
& "$($Commands.node)" $SourceJs |
Out-File -FilePath $(Join-Path $Destination 'package.json') -Encoding ascii
# No bin-links because historically, they weren't used anyway.
& "$($Commands.npm)" install
if ($LASTEXITCODE -ne 0) {
throw "Couldn't install npm packages."
}
# As of npm@5, this just renames `package-lock.json` to `npm-shrinkwrap.json`.
if ($Shrinkwrap -eq $True) {
& "$($Commands.npm)" shrinkwrap
if ($LASTEXITCODE -ne 0) {
throw "Couldn't make shrinkwrap."
}
}
cd node_modules
cd "$previousCwd"
}
# Install the global 7zip application, if necessary.
if (!(Test-Path "$system7zip")) {
Write-Host "Installing 7-zip since not found at ${system7zip}" `
-ForegroundColor Magenta
Invoke-Install7ZipApplication
}
# Download and install 7zip command-line tool into \bin
Add-7ZipTool
# Download and install Mongo binaries into \bin
Add-Mongo
# Add Python to \bin, and use it for Node Gyp.
$env:PYTHON = Add-Python
# Set additional options for node-gyp
$env:GYP_MSVS_VERSION = "2015"
$env:npm_config_nodedir = "$DIR"
$env:npm_config_cache = "$dirNpmCache"
# Allow running $dirBin commands like node and npm.
$env:PATH = "$env:PATH;$dirBin"
# Install Node.js and npm and get their paths to use from here on.
$toolCmds = Add-NodeAndNpm
"Location of node.exe:"
& Get-Command node | Select-Object -ExpandProperty Definition
"Node process.versions:"
& node -p 'process.versions'
"Location of npm.cmd:"
& Get-Command npm | Select-Object -ExpandProperty Definition
"Npm 'version':"
& npm version
npm config set loglevel error
#
# Install the npms for the 'server'.
#
$npmServerArgs = @{
sourceJs = "${dirCheckout}\scripts\dev-bundle-server-package.js"
destination = $dirServerLib
commands = $toolCmds
shrinkwrap = $True
}
Add-NpmModulesFromJsBundleFile @npmServerArgs
# These are used by the Meteor tool bundler and written to the Meteor build.
# For information, see the 'ServerTarget' class in tools/isobuild/bundler.js,
# and look for 'serverPkgJson' and 'npm-shrinkwrap.json'
mkdir -Force "${DIR}\etc"
Move-Item $(Join-Path $dirServerLib 'package.json') "${DIR}\etc\"
Move-Item $(Join-Path $dirServerLib 'npm-shrinkwrap.json') "${DIR}\etc\"
#
# Install the npms for the 'tool'.
#
$npmToolArgs = @{
sourceJs = "${dirCheckout}\scripts\dev-bundle-tool-package.js"
destination = $dirLib
commands = $toolCmds
}
Add-NpmModulesFromJsBundleFile @npmToolArgs
# Leaving these probably doesn't hurt, but are removed for consistency w/ Unix.
Remove-Item $(Join-Path $dirLib 'package.json')
Remove-Item $(Join-Path $dirLib 'package-lock.json')
Write-Host "Done writing node_modules build(s)..." -ForegroundColor Magenta
Write-Host "Removing temp scratch $dirTemp" -ForegroundColor Magenta
Remove-DirectoryRecursively $dirTemp
# mark the version
Write-Host "Writing out the bundle version..." -ForegroundColor Magenta
echo "${BUNDLE_VERSION}" | Out-File $(Join-Path $DIR '.bundle_version.txt') -Encoding ascii
$devBundleName = "dev_bundle_${PLATFORM}_${BUNDLE_VERSION}"
$dirBundlePreArchive = Join-Path $dirCheckout $devBundleName
$devBundleTmpTar = Join-Path $dirCheckout "dev_bundle.tar"
$devBundleTarGz = Join-Path $dirCheckout "${devBundleName}.tar.gz"
# Cleanup from previous builds, if there are things in our way.
Remove-DirectoryRecursively $dirBundlePreArchive
If (Test-Path $devBundleTmpTar) {
Remove-Item -Force $devBundleTmpTar
}
If (Test-Path $devBundleTarGz) {
Remove-Item -Force $devBundleTarGz
}
# Get out of this directory, before we rename it.
cd "$DIR\.."
# rename the folder with the devbundle
Write-Host "Renaming to $dirBundlePreArchive" -ForegroundColor Magenta
Rename-Item "$DIR" $dirBundlePreArchive
Write-Host "Compressing $dirBundlePreArchive to $devBundleTmpTar"
& "$system7zip" a -ttar $devBundleTmpTar $dirBundlePreArchive
if ($LASTEXITCODE -ne 0) {
throw "Failure while building $devBundleTmpTar"
}
if ((Get-Item $devBundleTmpTar).length -lt 50mb) {
throw "Dev bundle .tar is <50mb. If this is correct, update this message!"
}
Write-Host "Compressing $devBundleTmpTar into $devBundleTarGz" `
-ForegroundColor Magenta
& "$system7zip" a -tgzip $devBundleTarGz $devBundleTmpTar
if ($LASTEXITCODE -ne 0) {
throw "Failure while building $devBundleTarGz"
}
if ((Get-Item $devBundleTarGz).length -lt 30mb) {
throw "Dev bundle .tar.gz is <30mb. If this is correct, update this message!"
}
Write-Host "Removing $devBundleTmpTar" -ForegroundColor Magenta
Remove-Item -Force $devBundleTmpTar
Write-Host "Removing the '$devBundleName' temp directory." `
-ForegroundColor Magenta
Remove-DirectoryRecursively $dirBundlePreArchive
Write-Host "Done building Dev Bundle!" -ForegroundColor Green
Exit 0