|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Creates a cloud-based development environment for Business Central AL development using SaaS Sandbox. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + This script sets up a cloud-based development environment by: |
| 7 | + - Creating a Business Central SaaS Sandbox environment |
| 8 | + - Compiling and publishing all apps and test apps to the development scope |
| 9 | + - Configuring launch.json for Visual Studio Code with Cloud Sandbox configuration |
| 10 | + - Optionally applying custom settings to override repository settings |
| 11 | +
|
| 12 | + The script will prompt you interactively for authentication using device code flow. |
| 13 | + For automated/unattended execution, you can configure AdminCenterApiCredentials as a GitHub secret |
| 14 | + or in Azure KeyVault. See https://aka.ms/algosettings for more information about AdminCenterApiCredentials. |
| 15 | +
|
| 16 | + This is an alternative to localDevEnv.ps1 for users who cannot run Docker containers locally. |
| 17 | +
|
| 18 | + RECOMMENDED USAGE: |
| 19 | + Instead of modifying this script directly (which will be overwritten during AL-Go updates), |
| 20 | + create a custom script that calls this one with your preferred parameters. For example, |
| 21 | + create a file named after yourself (e.g., 'john-devenv.ps1') that contains: |
| 22 | +
|
| 23 | + # My personal cloud development environment script |
| 24 | + $mySettings = '{"country":"us"}' |
| 25 | + . .\.AL-Go\cloudDevEnv.ps1 -environmentName "john-sandbox" -reuseExistingEnvironment $true -customSettings $mySettings |
| 26 | +
|
| 27 | + This approach allows you to: |
| 28 | + - Maintain your personal preferences without losing them during updates |
| 29 | + - Share your setup with team members |
| 30 | + - Version control your custom development configurations |
| 31 | + - Easily switch between different development scenarios |
| 32 | +
|
| 33 | +.PARAMETER environmentName |
| 34 | + The name of the cloud sandbox environment to create or reuse. |
| 35 | + If not specified, the script will prompt for input with a default of "{username}-sandbox". |
| 36 | +
|
| 37 | +.PARAMETER reuseExistingEnvironment |
| 38 | + Boolean parameter indicating whether to reuse an existing environment with the same name. |
| 39 | + If $true, the script will use the existing environment if it exists. |
| 40 | + If $false, the script will recreate the environment (deleting the old one if it exists). |
| 41 | + If not specified, the script will prompt the user to select the behavior. |
| 42 | +
|
| 43 | +.PARAMETER fromVSCode |
| 44 | + Switch parameter indicating the script is being run from Visual Studio Code. |
| 45 | + When specified, the script will pause at the end waiting for user input before closing. |
| 46 | +
|
| 47 | +.PARAMETER clean |
| 48 | + Switch parameter to create a clean development environment without compiling and publishing apps. |
| 49 | + Useful for setting up a fresh environment without deploying any applications. |
| 50 | +
|
| 51 | +.PARAMETER customSettings |
| 52 | + JSON string containing custom settings that override repository settings. |
| 53 | + These settings have the highest precedence and can be used to override country, |
| 54 | + or other configuration without modifying repository files. |
| 55 | +
|
| 56 | +.EXAMPLE |
| 57 | + .\cloudDevEnv.ps1 |
| 58 | + Runs the script interactively, prompting for all required parameters. |
| 59 | +
|
| 60 | +.EXAMPLE |
| 61 | + .\cloudDevEnv.ps1 -environmentName "my-sandbox" -reuseExistingEnvironment $true |
| 62 | + Creates or reuses a cloud sandbox named "my-sandbox". |
| 63 | +
|
| 64 | +.EXAMPLE |
| 65 | + .\cloudDevEnv.ps1 -clean |
| 66 | + Creates a clean cloud development environment without compiling and publishing apps. |
| 67 | +
|
| 68 | +.EXAMPLE |
| 69 | + .\cloudDevEnv.ps1 -customSettings '{"country":"dk"}' |
| 70 | + Creates a cloud development environment with custom settings for Denmark country. |
| 71 | +
|
| 72 | +.EXAMPLE |
| 73 | + # Programmatic setup with custom settings |
| 74 | + $envName = "test-sandbox" |
| 75 | + $settings = '{"country": "us"}' |
| 76 | +
|
| 77 | + . ./cloudDevEnv.ps1 -environmentName $envName -reuseExistingEnvironment $true -customSettings $settings |
| 78 | +
|
| 79 | + Creates or reuses a cloud development environment with custom country setting. |
| 80 | +
|
| 81 | +.NOTES |
| 82 | + - Authentication is handled interactively via device code flow (https://aka.ms/devicelogin) |
| 83 | + - For unattended execution, configure AdminCenterApiCredentials secret (see link below) |
| 84 | + - Does not require Docker to be installed |
| 85 | + - Script automatically downloads required AL-Go helper modules and actions |
| 86 | + - Modifies launch.json in VS Code workspace for Cloud Sandbox configuration |
| 87 | + - Custom settings parameter allows runtime override of repository settings |
| 88 | + - If NewBcContainer.ps1 override exists, cloud development may not be supported |
| 89 | +
|
| 90 | +.LINK |
| 91 | + https://aka.ms/algosettings - AL-Go Settings Documentation |
| 92 | + https://github.com/microsoft/AL-Go/blob/main/Scenarios/CreateOnlineDevEnv2.md - Online Dev Environment Setup |
| 93 | +#> |
| 94 | + |
| 95 | +Param( |
| 96 | + [string] $environmentName = "", |
| 97 | + [bool] $reuseExistingEnvironment, |
| 98 | + [switch] $fromVSCode, |
| 99 | + [switch] $clean, |
| 100 | + [string] $customSettings = "" |
| 101 | +) |
| 102 | + |
| 103 | +$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0 |
| 104 | + |
| 105 | +function DownloadHelperFile { |
| 106 | + param( |
| 107 | + [string] $url, |
| 108 | + [string] $folder, |
| 109 | + [switch] $notifyAuthenticatedAttempt |
| 110 | + ) |
| 111 | + |
| 112 | + $prevProgressPreference = $ProgressPreference; $ProgressPreference = 'SilentlyContinue' |
| 113 | + $name = [System.IO.Path]::GetFileName($url) |
| 114 | + Write-Host "Downloading $name from $url" |
| 115 | + $path = Join-Path $folder $name |
| 116 | + try { |
| 117 | + Invoke-WebRequest -UseBasicParsing -uri $url -OutFile $path |
| 118 | + } |
| 119 | + catch { |
| 120 | + if ($notifyAuthenticatedAttempt) { |
| 121 | + Write-Host -ForegroundColor Red "Failed to download $name, trying authenticated download" |
| 122 | + } |
| 123 | + Invoke-WebRequest -UseBasicParsing -uri $url -OutFile $path -Headers @{ "Authorization" = "token $(gh auth token)" } |
| 124 | + } |
| 125 | + $ProgressPreference = $prevProgressPreference |
| 126 | + return $path |
| 127 | +} |
| 128 | + |
| 129 | +try { |
| 130 | +Clear-Host |
| 131 | +Write-Host |
| 132 | +Write-Host -ForegroundColor Yellow @' |
| 133 | + _____ _ _ _____ ______ |
| 134 | + / ____| | | | | __ \ | ____| |
| 135 | + | | | | ___ _ _ __| | | | | | _____ __ |__ _ ____ __ |
| 136 | + | | | |/ _ \| | | |/ _` | | | | |/ _ \ \ / / __| | '_ \ \ / / |
| 137 | + | |____| | (_) | |_| | (_| | | |__| | __/\ V /| |____| | | \ V / |
| 138 | + \_____|_|\___/ \__,_|\__,_| |_____/ \___| \_/ |______|_| |_|\_/ |
| 139 | +
|
| 140 | +'@ |
| 141 | + |
| 142 | +$tmpFolder = Join-Path ([System.IO.Path]::GetTempPath()) "$([Guid]::NewGuid().ToString())" |
| 143 | +New-Item -Path $tmpFolder -ItemType Directory -Force | Out-Null |
| 144 | +$GitHubHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/Github-Helper.psm1' -folder $tmpFolder -notifyAuthenticatedAttempt |
| 145 | +$ReadSettingsModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/.Modules/ReadSettings.psm1' -folder $tmpFolder |
| 146 | +$debugLoggingModule = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/.Modules/DebugLogHelper.psm1' -folder $tmpFolder |
| 147 | +$ALGoHelperPath = DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/AL-Go-Helper.ps1' -folder $tmpFolder |
| 148 | +DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/.Modules/settings.schema.json' -folder $tmpFolder | Out-Null |
| 149 | +DownloadHelperFile -url 'https://raw.githubusercontent.com/microsoft/AL-Go-Actions/v9.0/Environment.Packages.proj' -folder $tmpFolder | Out-Null |
| 150 | + |
| 151 | +Import-Module $GitHubHelperPath |
| 152 | +Import-Module $ReadSettingsModule |
| 153 | +Import-Module $debugLoggingModule |
| 154 | +. $ALGoHelperPath -local |
| 155 | + |
| 156 | +$baseFolder = GetBaseFolder -folder $PSScriptRoot |
| 157 | +$project = GetProject -baseFolder $baseFolder -projectALGoFolder $PSScriptRoot |
| 158 | + |
| 159 | +Write-Host @' |
| 160 | +
|
| 161 | +This script will create a cloud based development environment (Business Central SaaS Sandbox) for your project. |
| 162 | +All apps and test apps will be compiled and published to the environment in the development scope. |
| 163 | +The script will also modify launch.json to have a "Cloud Sandbox (<name>)" configuration point to your environment. |
| 164 | +
|
| 165 | +'@ |
| 166 | + |
| 167 | +if (Test-Path (Join-Path $PSScriptRoot "NewBcContainer.ps1")) { |
| 168 | + Write-Host -ForegroundColor Red "WARNING: The project has a NewBcContainer override defined. Typically, this means that you cannot run a cloud development environment" |
| 169 | +} |
| 170 | + |
| 171 | +Write-Host |
| 172 | + |
| 173 | +if (-not $environmentName) { |
| 174 | + $environmentName = Enter-Value ` |
| 175 | + -title "Environment name" ` |
| 176 | + -question "Please enter the name of the environment to create" ` |
| 177 | + -default "$($env:USERNAME)-sandbox" ` |
| 178 | + -trimCharacters @('"',"'",' ') |
| 179 | +} |
| 180 | + |
| 181 | +if ($PSBoundParameters.Keys -notcontains 'reuseExistingEnvironment') { |
| 182 | + $reuseExistingEnvironment = (Select-Value ` |
| 183 | + -title "What if the environment already exists?" ` |
| 184 | + -options @{ "Yes" = "Reuse existing environment"; "No" = "Recreate environment" } ` |
| 185 | + -question "Select behavior" ` |
| 186 | + -default "No") -eq "Yes" |
| 187 | +} |
| 188 | + |
| 189 | +CreateDevEnv ` |
| 190 | + -kind cloud ` |
| 191 | + -caller local ` |
| 192 | + -environmentName $environmentName ` |
| 193 | + -reuseExistingEnvironment:$reuseExistingEnvironment ` |
| 194 | + -baseFolder $baseFolder ` |
| 195 | + -project $project ` |
| 196 | + -clean:$clean ` |
| 197 | + -customSettings $customSettings |
| 198 | +} |
| 199 | +catch { |
| 200 | + Write-Host -ForegroundColor Red "Error: $($_.Exception.Message)`nStacktrace: $($_.scriptStackTrace)" |
| 201 | +} |
| 202 | +finally { |
| 203 | + if ($fromVSCode) { |
| 204 | + Read-Host "Press ENTER to close this window" |
| 205 | + } |
| 206 | +} |
0 commit comments