Skip to content

Instantly share code, notes, and snippets.

@Monarch73
Last active February 27, 2022 11:18
Show Gist options
  • Save Monarch73/8b0aa6fcccce4e8de04f45a03628ba39 to your computer and use it in GitHub Desktop.
Save Monarch73/8b0aa6fcccce4e8de04f45a03628ba39 to your computer and use it in GitHub Desktop.
create VHD that contains dev enviroment for ionic and android dev env
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$cwd = (Get-Item .).FullName
$arguments = "& '" +$myinvocation.mycommand.definition + "'"
#Start-Process powershell -Verb runAs -ArgumentList $arguments
Start-Process powershell -ArgumentList ("-NoExit",("cd {0}; $arguments" -f $cwd)) -Verb RunAs
Break
}
$ProgressPreference = 'SilentlyContinue'
$env:Path = "C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;"
Function Add-PathVariable {
param (
[string]$addPath
)
if (Test-Path $addPath){
$regexAddPath = [regex]::Escape($addPath)
$arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch "^$regexAddPath\\?"}
$env:Path = ($arrPath + $addPath) -join ';'
} else {
Throw "'$addPath' is not a valid path."
}
}
$string2 = @"
create vdisk file="ionicdev.vhdx" TYPE=EXPANDABLE MAXIMUM=10000
select vdisk file="ionicdev.vhdx"
attach vdisk
create partition primary
format fs=NTFS label="FriendlyName" quick
assign letter=x
"@
#---------------- init script --------------------
$initScript = @'
Function Add-PathVariable {
param (
[string]$addPath
)
if (Test-Path $addPath){
$regexAddPath = [regex]::Escape($addPath)
$arrPath = $env:Path -split ';' | Where-Object {$_ -notMatch "^$regexAddPath\\?"}
$env:Path = ($arrPath + $addPath) -join ';'
} else {
Throw "'$addPath' is not a valid path."
}
}
$env:Path = "C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;"
$javahome = (Get-ChildItem x:\java\ -Name)
$env:ANDROID_HOME = 'x:\Android\android-sdk'
Add-PathVariable -addPath 'x:\Android\Android-sdk\tools\bin'
Add-PathVariable -addPath "x:\java\$javahome\bin"
$env:JAVA_HOME = "x:\java\$javahome"
Add-PathVariable -addPath 'X:\Android\android-sdk\platform-tools'
Add-PathVariable -addPath "x:\nvm"
$env:NVM_HOME = "x:\nvm"
$env:NVM_SYMLINK = "X:\nodejs"
$env:NVM_PATH = "X:\nvm"
Add-PathVariable -addPath "x:\nodejs"
$env:USERPROFILE = "x:\home"
$env:HOMEDRIVE = "x:"
$env:HOMEPATH = "\home"
$env:TEMP = "x:\Home\AppData\Local\Temp"
$env:TMP = "x:\Home\AppData\Local\Temp"
$env:LOCALAPPDATA = "x:\Home\AppData\Local"
$env:APPDATA = "x:\Home\AppData\Roaming"
Add-PathVariable -addPath "x:\tools\bin"
'@
#----------------------------------------------------------
New-Item -type file .\ionicdev.vhdx
$fullpath = Resolve-Path .\ionicdev.vhdx
Remove-Item .\ionicdev.vhdx
Write-Output $fullpath
($string2.Replace('ionicdev.vhdx', $fullpath)) | Set-Content diskpartscript_tmp.txt
diskpart /s diskpartscript_tmp.txt
Remove-Item diskpartscript_tmp.txt
New-Item -Type directory "x:\Home\AppData\Local\Temp" -Force
New-Item -Type directory "x:\Home\Local\Roaming" -Force
New-Item -Type directory "x:\Home\AppData\Roaming" -Force
$env:USERPROFILE = "x:\home"
$env:HOMEDRIVE = "x:"
$env:HOMEPATH = "\home"
$env:TEMP = "x:\Home\AppData\Local\Temp"
$env:TMP = "x:\Home\AppData\Local\Temp"
$env:LOCALAPPDATA = "x:\Home\AppData\Local"
$env:APPDATA = "x:\Home\AppData\Roaming"
Write-Output "Downloading JDK"
Invoke-WebRequest "https://github.com/AdoptOpenJDK/openjdk8-upstream-binaries/releases/download/jdk8u292-b10/OpenJDK8U-jdk_x64_windows_8u292b10.zip" -OutFile x:\openjdk.zip
Write-Output "Downloading Android SDK"
Invoke-WebRequest "https://dl.google.com/android/repository/sdk-tools-windows-4333796.zip" -OutFile x:\sdktools.zip
Expand-Archive x:\sdktools.zip -DestinationPath x:\Android\android-sdk\
Expand-Archive x:\openjdk.zip -DestinationPath x:\java
$javahome = (Get-ChildItem x:\java\ -Name)
Write-Output $javahome
New-Item $env:userprofile\.android\repositories.cfg -type file -Force
$env:ANDROID_HOME = 'x:\Android\android-sdk'
Add-PathVariable -addPath 'x:\Android\Android-sdk\tools\bin'
Add-PathVariable -addPath "x:\java\$javahome\bin"
$env:JAVA_HOME = "x:\java\$javahome"
Write-Output yes | sdkmanager tools platform-tools
sdkmanager "platforms;android-22"
Add-PathVariable -addPath 'X:\Android\android-sdk\platform-tools'
# nodsjs through nvm
Write-Output "Downloading NVM"
Invoke-WebRequest "https://github.com/coreybutler/nvm-windows/releases/download/1.1.7/nvm-noinstall.zip" -OutFile x:\nvm.zip
Expand-Archive x:\nvm.zip x:\nvm
Add-PathVariable -addPath "x:\nvm"
$env:NVM_HOME = "x:\nvm"
$env:NVM_SYMLINK = "X:\nodejs"
$env:NVM_PATH = "X:\nvm"
Write-Output 'root: x:\nvm' > x:\nvm\settings.txt
Write-Output 'path: x:\nodejs' >> x:\nvm\settings.txt
Write-Output 'arch: 64' >> x:\nvm\settings.txt
Write-Output 'proxy: none' >> x:\nvm\settings.txt
nvm root x:\nvm
nvm install 14.17.0
nvm use 14.17.0
Write-VolumeCache x
Start-Sleep 2
Add-PathVariable -addPath "x:\nodejs"
npm install -g @ionic/[email protected]
npm install -g @angular/[email protected]
# visual studio code
Write-Output "Downloading visual studio code"
Invoke-WebRequest "https://update.code.visualstudio.com/1.56.2/win32-archive/stable" -OutFile x:\vscode.zip
Expand-Archive x:\vscode.zip -DestinationPath x:\tools\
Add-PathVariable -addPath "x:\tools\bin"
code --install-extension angular-schule.angular-schule-extension-pack
code --install-extension ms-vscode.powershell
code --install-extension mpotthoff.vscode-android-webview-debug
#git
Write-Output "Downloading git"
Invoke-WebRequest "https://github.com/git-for-windows/git/releases/download/v2.31.1.windows.1/PortableGit-2.31.1-64-bit.7z.exe" -OutFile x:\git.7z.exe
x:\git.7z.exe -o"X:\tools\" -y
Write-Output $initScript | Set-Content x:\init.ps1
#Dismount-VHD $fullpath
#---------full defrag (future use) ------------------------
#defrag x: /h /x
#defrag x: /h /k /l
#defrag x: /h /x
#defrag x: /h /k
#dismount-vhd "$fullpath"
#optimize-vhd "$fullpath" -Mode Full
@Monarch73
Copy link
Author

Setting up a development environment for ionic/ android can be a pain in the ass.

This windows powershell script creates a virtual disk (VHDX) and download and installs all requirements needed to develop ionic apps for android inside this vhdx:

  • Visual Studio Code
  • nodejs
  • Java 8 JDK
  • Android SDK
  • git
  • ionic
  • angular

after installation, exit powershell, start a new powershell as administrator, cd onto x: and execute init.ps1-script, which will initialize all environment variables for this powershell session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment