WinSetup.ps1 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Usage:
  2. # - install Git
  3. # - install Neko
  4. # - checkout haxe git
  5. # - run from command "powershell -noexit -ExecutionPolicy Bypass -File .\WinSetup.ps1"
  6. function Cmd-Path($file) {
  7. try { Split-Path -Parent (Get-Command "$file.exe" -ErrorAction Stop).Source } catch { "" }
  8. }
  9. # resolve Opam binary and repo
  10. # you can choose when opam is installed by setting OPAM_INSTALL_DIR (and OPAMROOT - optional)
  11. $Opam = Cmd-Path "opam"
  12. $OpamRepo = $env:OPAMROOT
  13. $Git = Cmd-Path "git"
  14. if( !$Opam ) { $Opam = $env:OPAM_INSTALL_DIR }
  15. if( !$Opam ) { $Opam = (Get-Item .).FullName + "\opam" }
  16. if( !$OpamRepo ) { $OpamRepo = "$Opam\repo" }
  17. $CygRoot = "$OpamRepo\.cygwin\root"
  18. $WinSysPath = "$env:SystemRoot\System32"
  19. $Neko = Cmd-Path "neko"
  20. $RegPath = "HKCU:\Environment"
  21. $MbedVer = "2.16.3"
  22. $MbedTLS = "https://github.com/Simn/mingw64-mbedtls/releases/download/$MbedVer/mingw64-x86_64-mbedtls-$MbedVer-1.tar.xz"
  23. function Install-Init {
  24. if( !$Git ) {
  25. echo "**ERROR** git.exe could not be found in PATH"
  26. Exit
  27. }
  28. if( !$Neko ) {
  29. echo "**ERROR** Neko.exe could not be found in PATH"
  30. Exit
  31. }
  32. # reset PATH to prevent conflicting cygwin or existing install
  33. Set-Item -Path env:PATH -Value "$CygRoot\usr\x86_64-w64-mingw32\bin;$CygRoot\bin;$Opam;$Neko;$Git;$WinSysPath"
  34. # set OPAM root dir
  35. Set-Item -Path env:OPAMROOT -Value "$OpamRepo"
  36. }
  37. function Install-Opam {
  38. # download opam binary
  39. Invoke-Expression "& { $(Invoke-RestMethod https://opam.ocaml.org/install.ps1)} -OpamBinDir $Opam"
  40. # init opam, assume that we have windows GIT installed
  41. Invoke-Expression "opam init --cygwin-internal-install --no-git-location --shell=powershell --shell-setup"
  42. }
  43. function Install-Haxe-Deps {
  44. Invoke-Expression "opam install . --deps-only --confirm-level=yes"
  45. # install mbedtls mingw package
  46. $tmpFile = "./mbed.tgz"
  47. Invoke-Expression "curl $MbedTLS -o $tmpFile"
  48. Invoke-Expression "tar -C / -xvf $tmpFile"
  49. Remove-Item "$tmpFile"
  50. # install lsp server
  51. Invoke-Expression "opam install ocaml-lsp-server --confirm-level=yes"
  52. }
  53. function Add-Path($NewPath) {
  54. $CurrentPath = (Get-ItemProperty -Path $RegPath -Name Path).Path
  55. if ($CurrentPath -notlike "*$NewPath*") {
  56. $CurrentPath = "$NewPath;$CurrentPath"
  57. Set-ItemProperty -Path $RegPath -Name Path -Value $CurrentPath
  58. }
  59. }
  60. function Setup-Paths {
  61. Add-Path "$OpamRepo\default\bin"
  62. Add-Path "$CygRoot\bin"
  63. Add-Path "$CygRoot\usr\x86_64-w64-mingw32\bin"
  64. Add-Path "$CygRoot\usr\x86_64-w64-mingw32\sys-root\mingw\bin"
  65. Set-ItemProperty -Path $RegPath -Name OPAMROOT -Value $OpamRepo
  66. # refresh for all processes (no need to restart)
  67. $signature = @"
  68. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  69. public static extern int SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, int fuFlags, int uTimeout, out IntPtr lpdwResult);
  70. "@
  71. $SendMessageTimeout = Add-Type -MemberDefinition $signature -Name "Win32SendMessageTimeout" -Namespace Win32Functions -PassThru
  72. $SendMessageTimeout::SendMessageTimeout([IntPtr]0xFFFF, 0x1A, [IntPtr]::Zero, "Environment", 2, 5000, [ref][IntPtr]::Zero)
  73. }
  74. Install-Init
  75. Install-Opam
  76. Install-Haxe-Deps
  77. Setup-Paths