2
0

WinSetup.ps1 3.2 KB

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