WinSetup.ps1 2.9 KB

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