Created
August 20, 2019 03:51
-
-
Save slide/158d3210c11a407f984db095bc6ccc4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Init Script: | |
Set-ExecutionPolicy Unrestricted | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
$baseDir = 'c:\azurecsdir' | |
$JDKUrl = 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.3%2B7/OpenJDK11U-jre_x64_windows_hotspot_11.0.3_7.zip' | |
$destinationJDKZipPath = "$baseDir\adoptOpenJDK.zip" | |
$javaHome = "$baseDir\jdk-11.0.3+7-jre" | |
$GITUrl = 'https://github.com/git-for-windows/git/releases/download/v2.10.0.windows.1/MinGit-2.10.0-64-bit.zip' | |
$destinationGITZipPath = "$baseDir\MinGit.zip" | |
$GITPath = "$baseDir\git\cmd\" | |
# Function to get path of script file | |
function Get-ScriptPath | |
{ | |
return $MyInvocation.ScriptName; | |
} | |
function DownloadFile($url, $targetFile) | |
{ | |
$uri = New-Object "System.Uri" "$url" | |
$request = [System.Net.HttpWebRequest]::Create($uri) | |
$request.set_Timeout(15000) #15 second timeout | |
$response = $request.GetResponse() | |
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) | |
$responseStream = $response.GetResponseStream() | |
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create | |
$buffer = new-object byte[] 10KB | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $count | |
while ($count -gt 0) { | |
$targetStream.Write($buffer, 0, $count) | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $downloadedBytes + $count | |
Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100) | |
} | |
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'" | |
$targetStream.Flush() | |
$targetStream.Close() | |
$targetStream.Dispose() | |
$responseStream.Dispose() | |
} | |
# Checking if this is first time script is getting executed, if yes then downloading JDK, Git and VS Tools | |
If(-not((Test-Path $destinationJDKZipPath))) | |
{ | |
md -Path $baseDir -Force | |
DownloadFile $JDKUrl $destinationJDKZipPath | |
$shell_app = new-object -com shell.application | |
$zip_file = $shell_app.namespace($destinationJDKZipPath) | |
$javaInstallDir = $shell_app.namespace($baseDir) | |
$javaInstallDir.Copyhere($zip_file.items()) | |
DownloadFile $GITUrl $destinationGITZipPath | |
md -Path "$baseDir\git" -Force | |
$zip_file = $shell_app.namespace($destinationGITZipPath) | |
$gitInstallDir = $shell_app.namespace("$baseDir\git") | |
$gitInstallDir.Copyhere($zip_file.items()) | |
& "$GITPath\git" config --system core.autocrlf false | |
& "$GITPath\git" config --system core.longpaths true | |
# update the system path to include Git and Java | |
$oldPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path | |
$newPath = "$oldPath;$javaHome\bin;$GITPath" | |
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath | |
# setup JAVA_HOME environment variable | |
New-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name JAVA_HOME -Value $javaHome | |
} | |
Write-Host 'Done Init Script.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment