install-dmd.ps1 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. param (
  2. [string]$Version = $null
  3. )
  4. Set-StrictMode -Version latest
  5. $ErrorActionPreference = "Stop"
  6. $ProgressPreference = "SilentlyContinue"
  7. # default installation directory
  8. $dmd_install = "C:\D"
  9. $dmd_version_file = "C:\cache\DMD_LATEST"
  10. if (!$Version) {
  11. #echo "Fetching latest DMD version..."
  12. $dmd_latest_url = "http://downloads.dlang.org/releases/LATEST"
  13. $retries = 10
  14. for ($i = 1; $i -le $retries; $i++) {
  15. try {
  16. [system.io.directory]::CreateDirectory((Split-Path -parent $dmd_version_file)) > $null
  17. Invoke-WebRequest -URI $dmd_latest_url -OutFile $dmd_version_file
  18. break
  19. } catch [net.WebException] {
  20. if ($i -eq $retries) {
  21. break
  22. }
  23. $backoff = (10 * $i) # backoff 10s, 20s, 30s...
  24. echo ('{0}: {1}' -f $dmd_latest_url, $_.Exception.Message)
  25. echo ('Retrying in {0}s...' -f $backoff)
  26. Start-Sleep -m ($backoff * 1000)
  27. } catch {
  28. throw
  29. }
  30. }
  31. if (Test-Path $dmd_version_file) {
  32. $dmd_version = Get-Content -Path $dmd_version_file
  33. } else {
  34. throw "Failed to resolve latest DMD version"
  35. }
  36. } else {
  37. $dmd_version = $Version
  38. }
  39. $dmd_url = "http://downloads.dlang.org/releases/2.x/$dmd_version/dmd.$dmd_version.windows.zip"
  40. $dmd_filename = [System.IO.Path]::GetFileName($dmd_url)
  41. $dmd_archive = Join-Path ($env:temp) $dmd_filename
  42. #echo "Downloading $dmd_filename..."
  43. $retries = 10
  44. for ($i = 1; $i -le $retries; $i++) {
  45. try {
  46. (New-Object net.webclient).DownloadFile($dmd_url, $dmd_archive)
  47. break
  48. } catch [net.WebException] {
  49. if ($i -eq $retries) {
  50. throw # fail on last retry
  51. }
  52. $backoff = (10 * $i) # backoff 10s, 20s, 30s...
  53. echo ('{0}: {1}' -f $dmd_url, $_.Exception.Message)
  54. echo ('Retrying in {0}s...' -f $backoff)
  55. Start-Sleep -m ($backoff * 1000)
  56. }
  57. }
  58. #echo "Extracting $dmd_filename..."
  59. Expand-Archive $dmd_archive -Force -DestinationPath $dmd_install
  60. # add to environment path
  61. #echo "Installing DMD..."
  62. $dmd_bin = Join-Path $dmd_install "dmd2\windows\bin"
  63. $Env:Path = $Env:Path + ";" + $dmd_bin
  64. #echo "Testing DMD..."
  65. & dmd.exe --version