From 5143d301a20d0fd2e8b3d6e969c0ed2748708483 Mon Sep 17 00:00:00 2001 From: André Oliveira Date: Mon, 25 Apr 2022 20:36:03 +0100 Subject: Creates the build script for Windows based on Ferdium-App --- scripts/build-windows.ps1 | 175 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 scripts/build-windows.ps1 (limited to 'scripts/build-windows.ps1') diff --git a/scripts/build-windows.ps1 b/scripts/build-windows.ps1 new file mode 100644 index 0000000..e932da9 --- /dev/null +++ b/scripts/build-windows.ps1 @@ -0,0 +1,175 @@ +# INTRO: +# This file is used to build ferdium-server on windows. +# It also handles any corrupted node modules with the 'CLEAN' env var (set it to 'true' for cleaning) +# It will install the system dependencies except for node and python (which are still verified) +# I sometimes symlink my 'recipes' folder so that any changes that I need to do in it can also be committed and pushed independently +# This file can live anywhere in your PATH + +$USERHOME = "${env:HOMEDRIVE}${env:HOMEPATH}" + +$env:CI = $true + +# ----------------------------------------------------------------------------- +# Utility functions + +Function fail_with_docs {Param ($1) + Write-Host "*************** FAILING ***************" + Write-Host "$1" + Write-Host "" + Write-Host "Please read the developer documentation in CONTRIBUTING.md" + exit 1 +} + +Function Test-CommandExists { Param ($command, $1) + $oldPreference = $ErrorActionPreference + $ErrorActionPreference = "stop" + + try { + if(Get-Command $command){RETURN} + } Catch { + fail_with_docs $1 + } + Finally {$ErrorActionPreference=$oldPreference} +} + +# ----------------------------------------------------------------------------- +# Checking the developer environment +# Check for installed programmes +Test-CommandExists node "Node is not installed" +Test-CommandExists npm "npm is not installed" +Test-CommandExists python "Python is not installed" +# NEEDS proper way to CHECK MSVS Tools + +# Check node version +$EXPECTED_NODE_VERSION = (cat .nvmrc) +$ACTUAL_NODE_VERSION = (node -v) +if ("v$EXPECTED_NODE_VERSION" -ne $ACTUAL_NODE_VERSION) { + fail_with_docs "You are not running the expected version of node! + expected: [v$EXPECTED_NODE_VERSION] + actual : [$ACTUAL_NODE_VERSION]" +} + +# Check if the 'recipes' folder is present either as a git submodule or a symbolic link +if (-not (Test-Path -Path recipes\package.json -PathType Leaf)) { + fail_with_docs "'recipes' folder is missing or submodule has not been checked out" +} + +# This log statement is only to remind me which 'recipes' folder I am using (symlink or git submodule) +# TODO: Implement this + +# ----------------------------------------------------------------------------- +# If you are moving to a new version of node or any other system dependency, then cleaning is recommended +# so that there's no irregular results due to cached modules +if ($env:CLEAN -eq "true") +{ + $NPM_PATH = "$USERHOME\AppData\Roaming\npm\node_modules" + $NPM_CACHE1_PATH = "$USERHOME\AppData\Local\npm-cache" + $NPM_CACHE2_PATH = "$USERHOME\AppData\Roaming\npm-cache" + $NODE_GYP = "$USERHOME\AppData\Local\node-gyp" + + Write-Host "Cleaning!" + + if ( (Test-Path -Path ".\pnpm-lock.yaml") -and (Get-Command -ErrorAction Ignore -Type Application pnpm) ) + { + $PNPM_STORE = "$USERHOME\.pnpm-store" + $PNPM_STATE = "$USERHOME\.pnpm-state" + + pnpm store prune + + Remove-Item -Path $PNPM_STORE -Recurse -ErrorAction SilentlyContinue + Remove-Item -Path $PNPM_STATE -Recurse -ErrorAction SilentlyContinue + } + + npm cache clean --force + Remove-Item -Path $NPM_PATH -Recurse -ErrorAction SilentlyContinue + Remove-Item -Path $NPM_CACHE1_PATH -Recurse -ErrorAction SilentlyContinue + Remove-Item -Path $NPM_CACHE2_PATH -Recurse -ErrorAction SilentlyContinue + Remove-Item -Path $NODE_GYP -Recurse -ErrorAction SilentlyContinue + + git -C recipes clean -fxd # Clean recipes folder/submodule + git clean -fxd # Note: This will blast away the 'recipes' folder if you have symlinked it +} + +# ----------------------------------------------------------------------------- +# Ensure that the system dependencies are at the correct version - fail if not +# Check python version +$EXPECTED_PYTHON_VERSION = "3.10.4" +$ACTUAL_PYTHON_VERSION = (python --version).trim("Python ") +if ([System.Version]$ACTUAL_PYTHON_VERSION -ne [System.Version]$EXPECTED_PYTHON_VERSION) { + fail_with_docs "You are not running the expected version of Python! + expected: [$EXPECTED_PYTHON_VERSION] + actual : [$ACTUAL_PYTHON_VERSION]" +} + +# TODO: Needs proper way to check MSVS Tools +# Check MSVS Tools through MSVS_VERSION +$EXPECTED_MSVST_VERSION = "2015" +$ACTUAL_MSVST_VERSION = (npm config get msvs_version) +if ([double]$ACTUAL_MSVST_VERSION -ne [double]$EXPECTED_MSVST_VERSION) { + fail_with_docs "You are not running the expected version of MSVS Tools! + expected: [$EXPECTED_MSVST_VERSION] + actual : [$ACTUAL_MSVST_VERSION]" +} + +# ----------------------------------------------------------------------------- +# Ensure that the system dependencies are at the correct version - recover if not +# Check npm version +$EXPECTED_NPM_VERSION = (Get-Content package.json | ConvertFrom-Json).engines.npm +$ACTUAL_NPM_VERSION = (npm -v) +if ($EXPECTED_NPM_VERSION -ne $ACTUAL_NPM_VERSION) { + Write-Host "You are not running the expected version of npm! + expected: [$EXPECTED_NPM_VERSION] + actual : [$ACTUAL_NPM_VERSION]" + Write-Host "Changing version of npm to [$EXPECTED_NPM_VERSION]" + npm i -gf npm@$EXPECTED_NPM_VERSION +} + +# Check pnpm version +$EXPECTED_PNPM_VERSION = (Get-Content recipes\package.json | ConvertFrom-Json).engines.pnpm +$ACTUAL_PNPM_VERSION = Get-Command pnpm --version -ErrorAction SilentlyContinue # in case the pnpm executable itself is not present +if ($ACTUAL_PNPM_VERSION -ne $EXPECTED_PNPM_VERSION) { + npm i -gf pnpm@$EXPECTED_PNPM_VERSION +} + +# ----------------------------------------------------------------------------- +# This is useful if we move from 'npm' to 'pnpm' for the main repo as well +if ((Test-Path -Path ".\pnpm-lock.yaml") -and (Get-Command -ErrorAction Ignore -Type Application pnpm)) +{ + $BASE_CMD="pnpm" + $env:EXEC_CMD="pnpm dlx" +} +else +{ + $BASE_CMD="npm" + $env:EXEC_CMD="npx" +} + +$ENV_FILE = ".env" +if (-not (Test-Path -Path $ENV_FILE)) { + Copy-Item .env.example -Destination $ENV_FILE + $APP_KEY = ("!@#$%^&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".tochararray() | sort {Get-Random})[0..32] -join '' + # SAVE APP_KEY TO .env FILE + (Get-Content $ENV_FILE -Raw) -replace 'APP_KEY', "APP_KEY=$APP_KEY" | Set-Content $ENV_FILE +} + +if (-not (Test-Path -Path "data")) { + mkdir data +} + +# ----------------------------------------------------------------------------- +Write-Host "*************** Building recipes ***************" +# Note: 'recipes' is already using only pnpm - can switch to $BASE_CMD AFTER both repos are using pnpm +Push-Location recipes +pnpm i +pnpm package +Pop-Location + +# Now the meat..... +& $BASE_CMD i +& node ace migration:refresh + +# ----------------------------------------------------------------------------- +Write-Host "*************** Starting app ***************" +& $BASE_CMD start --dev + +Write-Host "*************** App successfully stopped! ***************" -- cgit v1.2.3-54-g00ecf