Skip to content

Commit be12826

Browse files
authored
Merge pull request #52 from jneira/improve-install
Port hie changes in install script of pull haskell/haskell-ide-engine#1665
2 parents 75b6610 + fafd8a6 commit be12826

File tree

7 files changed

+151
-126
lines changed

7 files changed

+151
-126
lines changed

install/src/Cabal.hs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
{-# LANGUAGE CPP #-}
2-
32
module Cabal where
43

54
import Development.Shake
6-
import Development.Shake.Command
75
import Development.Shake.FilePath
86
import Control.Monad
9-
import Data.Maybe ( isNothing
10-
, isJust
11-
)
12-
import Control.Monad.Extra ( whenMaybe )
13-
import System.Directory ( findExecutable
14-
, copyFile
15-
)
7+
import System.Directory ( copyFile )
168

179
import Version
1810
import Print
1911
import Env
20-
import Data.Functor.Identity
2112
#if RUN_FROM_STACK
2213
import Control.Exception ( throwIO )
2314
#else
2415
import Cabal.Config
16+
import Data.Functor.Identity
2517
#endif
2618

2719
getInstallDir :: IO FilePath
@@ -38,10 +30,10 @@ execCabal = command [] "cabal"
3830
execCabal_ :: [String] -> Action ()
3931
execCabal_ = execCabal
4032

41-
cabalBuildData :: Action ()
42-
cabalBuildData = do
43-
execCabal_ ["v2-build", "hoogle"]
44-
execCabal_ ["v2-exec", "hoogle", "generate"]
33+
cabalBuildData :: [String] -> Action ()
34+
cabalBuildData args = do
35+
execCabal_ $ ["v2-build", "hoogle"] ++ args
36+
execCabal_ $ ["v2-exec", "hoogle", "generate"] ++ args
4537

4638
getGhcPathOfOrThrowError :: VersionNumber -> Action GhcPath
4739
getGhcPathOfOrThrowError versionNumber =
@@ -51,17 +43,20 @@ getGhcPathOfOrThrowError versionNumber =
5143
error (ghcVersionNotFoundFailMsg versionNumber)
5244
Just p -> return p
5345

54-
cabalInstallHie :: VersionNumber -> Action ()
55-
cabalInstallHie versionNumber = do
46+
cabalInstallHie :: VersionNumber -> [String] -> Action ()
47+
cabalInstallHie versionNumber args = do
5648
localBin <- liftIO $ getInstallDir
57-
cabalVersion <- getCabalVersion
49+
cabalVersion <- getCabalVersion args
5850
ghcPath <- getGhcPathOfOrThrowError versionNumber
5951

6052
let isCabal3 = checkVersion [3,0,0,0] cabalVersion
6153
installDirOpt | isCabal3 = "--installdir"
6254
| otherwise = "--symlink-bindir"
6355
installMethod | isWindowsSystem && isCabal3 = ["--install-method=copy"]
6456
| otherwise = []
57+
58+
projectFile <- getProjectFile versionNumber
59+
6560
execCabal_ $
6661
[ "v2-install"
6762
, "exe:haskell-language-server"
@@ -71,8 +66,10 @@ cabalInstallHie versionNumber = do
7166
, installDirOpt, localBin
7267
, "--max-backjumps=5000"
7368
, "--overwrite-policy=always"
69+
, "--project-file=" ++ projectFile
7470
]
7571
++ installMethod
72+
++ args
7673

7774
let minorVerExe = "haskell-language-server-" ++ versionNumber <.> exe
7875
majorVerExe = "haskell-language-server-" ++ dropExtension versionNumber <.> exe
@@ -88,20 +85,27 @@ cabalInstallHie versionNumber = do
8885
++ minorVerExe
8986
++ " to " ++ localBin
9087

91-
checkCabal_ :: Action ()
92-
checkCabal_ = checkCabal >> return ()
88+
getProjectFile :: VersionNumber -> Action FilePath
89+
getProjectFile ver = do
90+
existFile <- doesFileExist $ "cabal.project-" ++ ver
91+
return $ if existFile
92+
then "cabal.project-" ++ ver
93+
else "cabal.project"
94+
95+
checkCabal_ :: [String] -> Action ()
96+
checkCabal_ args = checkCabal args >> return ()
9397

9498
-- | check `cabal` has the required version
95-
checkCabal :: Action String
96-
checkCabal = do
97-
cabalVersion <- getCabalVersion
99+
checkCabal :: [String] -> Action String
100+
checkCabal args = do
101+
cabalVersion <- getCabalVersion args
98102
unless (checkVersion requiredCabalVersion cabalVersion) $ do
99103
printInStars $ cabalInstallIsOldFailMsg cabalVersion
100104
error $ cabalInstallIsOldFailMsg cabalVersion
101105
return cabalVersion
102106

103-
getCabalVersion :: Action String
104-
getCabalVersion = trimmedStdout <$> execCabal ["--numeric-version"]
107+
getCabalVersion :: [String] -> Action String
108+
getCabalVersion args = trimmedStdout <$> (execCabal $ ["--numeric-version"] ++ args)
105109

106110
-- | Error message when the `cabal` binary is an older version
107111
cabalInstallIsOldFailMsg :: String -> String
@@ -120,3 +124,21 @@ requiredCabalVersion | isWindowsSystem = requiredCabalVersionForWindows
120124

121125
requiredCabalVersionForWindows :: RequiredVersion
122126
requiredCabalVersionForWindows = [3, 0, 0, 0]
127+
128+
getVerbosityArg :: Verbosity -> String
129+
getVerbosityArg v = "-v" ++ cabalVerbosity
130+
where cabalVerbosity = case v of
131+
Silent -> "0"
132+
#if MIN_VERSION_shake(0,18,4)
133+
Error -> "0"
134+
Warn -> "1"
135+
Info -> "1"
136+
Verbose -> "2"
137+
#else
138+
Quiet -> "0"
139+
Normal -> "1"
140+
Loud -> "2"
141+
Chatty -> "2"
142+
#endif
143+
Diagnostic -> "3"
144+

install/src/Env.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
module Env where
22

33
import Development.Shake
4-
import Development.Shake.Command
54
import Control.Monad.IO.Class
65
import Control.Monad
76
import Development.Shake.FilePath
8-
import System.Info ( os
9-
, arch
10-
)
7+
import System.Info ( os )
118
import Data.Maybe ( isJust
12-
, isNothing
139
, mapMaybe
1410
)
1511
import System.Directory ( findExecutable

install/src/Help.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
module Help where
33

44
import Development.Shake
5-
import Data.List ( intersperse
6-
, intercalate
7-
)
5+
import Data.List ( intercalate )
86

97
import Env
108
import Print
119
import Version
1210
import BuildSystem
13-
import Cabal
1411

1512
stackCommand :: TargetDescription -> String
16-
stackCommand target = "stack install.hs " ++ fst target
13+
stackCommand target = "stack install.hs " ++ fst target ++ " [options]"
1714

1815
cabalCommand :: TargetDescription -> String
19-
cabalCommand target = "cabal v2-run install.hs --project-file install/shake.project " ++ fst target
16+
cabalCommand target = "cabal v2-run install.hs --project-file install/shake.project -- " ++ fst target ++ " [options]"
2017

2118
buildCommand :: TargetDescription -> String
2219
buildCommand | isRunFromCabal = cabalCommand
@@ -37,7 +34,7 @@ shortHelpMessage = do
3734
printUsage
3835
printLine ""
3936
printLine "Targets:"
40-
mapM_ (printLineIndented . showTarget (spaces hieVersions)) (targets hieVersions)
37+
mapM_ (printLineIndented . showHelpItem (spaces hieVersions)) (targets hieVersions)
4138
printLine ""
4239
where
4340
spaces hieVersions = space (targets hieVersions)
@@ -68,7 +65,10 @@ helpMessage versions@BuildableVersions {..} = do
6865
printUsage
6966
printLine ""
7067
printLine "Targets:"
71-
mapM_ (printLineIndented . showTarget spaces) targets
68+
mapM_ (printLineIndented . showHelpItem spaces) targets
69+
printLine ""
70+
printLine "Options:"
71+
mapM_ (printLineIndented . showHelpItem spaces) options
7272
printLine ""
7373
where
7474
spaces = space targets
@@ -81,6 +81,10 @@ helpMessage versions@BuildableVersions {..} = do
8181
, if isRunFromCabal then [cabalGhcsTarget] else [stackDevTarget]
8282
, [macosIcuTarget]
8383
]
84+
options = [ ("-s, --silent", "Don't print anything.")
85+
, ("-q, --quiet", "Print less (pass repeatedly for even less).")
86+
, ("-V, --verbose", "Print more (pass repeatedly for even more).")
87+
]
8488

8589
-- All targets with their respective help message.
8690
generalTargets = [helpTarget]
@@ -97,10 +101,10 @@ templateTarget = ("<target>", "")
97101

98102
hieTarget :: String -> TargetDescription
99103
hieTarget version =
100-
("haskell-language-server-" ++ version, "Install haskell-language-server for GHC version " ++ version)
104+
("hls-" ++ version, "Install haskell-language-server for GHC version " ++ version)
101105

102106
buildTarget :: TargetDescription
103-
buildTarget = ("haskell-language-server", "Install haskell-language-server with the latest available GHC and the data files")
107+
buildTarget = ("hls", "Install haskell-language-server with the latest available GHC and the data files")
104108

105109
buildLatestTarget :: TargetDescription
106110
buildLatestTarget = ("latest", "Install haskell-language-server with the latest available GHC")

install/src/HieInstall.hs

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
11
module HieInstall where
22

33
import Development.Shake
4-
import Development.Shake.Command
5-
import Development.Shake.FilePath
64
import Control.Monad
7-
import Control.Monad.IO.Class
8-
import Control.Monad.Extra ( unlessM
9-
, mapMaybeM
10-
)
11-
import Data.Maybe ( isJust )
12-
import System.Directory ( listDirectory )
135
import System.Environment ( unsetEnv )
14-
import System.Info ( os
15-
, arch
16-
)
17-
18-
import Data.Maybe ( isNothing
19-
, mapMaybe
20-
)
21-
import Data.List ( dropWhileEnd
22-
, intersperse
23-
, intercalate
24-
, sort
25-
, sortOn
26-
)
27-
import qualified Data.Text as T
28-
import Data.Char ( isSpace )
29-
import Data.Version ( parseVersion
30-
, makeVersion
31-
, showVersion
32-
)
33-
import Data.Function ( (&) )
34-
import Text.ParserCombinators.ReadP ( readP_to_S )
356

367
import BuildSystem
378
import Stack
389
import Cabal
3910
import Version
40-
import Print
4111
import Env
4212
import Help
4313

@@ -60,36 +30,54 @@ defaultMain = do
6030
let latestVersion = last versions
6131

6232
shakeArgs shakeOptions { shakeFiles = "_build" } $ do
33+
34+
shakeOptionsRules <- getShakeOptionsRules
35+
36+
let verbosityArg = if isRunFromStack then Stack.getVerbosityArg else Cabal.getVerbosityArg
37+
38+
let args = [verbosityArg (shakeVerbosity shakeOptionsRules)]
39+
40+
phony "show-options" $ do
41+
putNormal $ "Options:"
42+
putNormal $ " Verbosity level: " ++ show (shakeVerbosity shakeOptionsRules)
43+
6344
want ["short-help"]
6445
-- general purpose targets
6546
phony "submodules" updateSubmodules
6647
phony "short-help" shortHelpMessage
6748
phony "help" (helpMessage toolsVersions)
6849

69-
phony "check" (if isRunFromStack then checkStack else checkCabal_)
50+
phony "check" (if isRunFromStack then checkStack args else checkCabal_ args)
7051

7152
phony "data" $ do
53+
need ["show-options"]
7254
need ["submodules"]
7355
need ["check"]
74-
if isRunFromStack then stackBuildData else cabalBuildData
56+
if isRunFromStack then stackBuildData args else cabalBuildData args
7557

7658
forM_
7759
versions
78-
(\version -> phony ("haskell-language-server-" ++ version) $ do
60+
(\version -> phony ("hls-" ++ version) $ do
61+
need ["show-options"]
7962
need ["submodules"]
8063
need ["check"]
81-
if isRunFromStack then do
82-
stackInstallHieWithErrMsg (Just version)
64+
if isRunFromStack then
65+
stackInstallHieWithErrMsg (Just version) args
8366
else
84-
cabalInstallHie version
67+
cabalInstallHie version args
8568
)
8669

87-
phony "latest" (need ["haskell-language-server-" ++ latestVersion])
88-
phony "haskell-language-server" (need ["data", "latest"])
70+
unless (null versions) $ do
71+
phony "latest" (need ["hls-" ++ latestVersion])
72+
phony "hls" (need ["data", "latest"])
8973

9074
-- stack specific targets
91-
when isRunFromStack $
92-
phony "dev" $ stackInstallHieWithErrMsg Nothing
75+
-- Default `stack.yaml` uses ghc-8.8.2 and we can't build hie in windows
76+
-- TODO: Enable for windows when it uses ghc-8.8.3
77+
when (isRunFromStack && not isWindowsSystem) $
78+
phony "dev" $ do
79+
need ["show-options"]
80+
stackInstallHieWithErrMsg Nothing args
9381

9482
-- cabal specific targets
9583
when isRunFromCabal $ do
@@ -98,20 +86,23 @@ defaultMain = do
9886
phony "ghcs" $ showInstalledGhcs ghcPaths
9987

10088
-- macos specific targets
101-
phony "icu-macos-fix"
102-
(need ["icu-macos-fix-install"] >> need ["icu-macos-fix-build"])
89+
phony "icu-macos-fix" $ do
90+
need ["show-options"]
91+
need ["icu-macos-fix-install"]
92+
need ["icu-macos-fix-build"]
93+
10394
phony "icu-macos-fix-install" (command_ [] "brew" ["install", "icu4c"])
104-
phony "icu-macos-fix-build" $ mapM_ buildIcuMacosFix versions
95+
phony "icu-macos-fix-build" $ mapM_ (flip buildIcuMacosFix $ args) versions
10596

10697

107-
buildIcuMacosFix :: VersionNumber -> Action ()
108-
buildIcuMacosFix version = execStackWithGhc_
109-
version
98+
buildIcuMacosFix :: VersionNumber -> [String] -> Action ()
99+
buildIcuMacosFix version args = execStackWithGhc_
100+
version $
110101
[ "build"
111102
, "text-icu"
112103
, "--extra-lib-dirs=/usr/local/opt/icu4c/lib"
113104
, "--extra-include-dirs=/usr/local/opt/icu4c/include"
114-
]
105+
] ++ args
115106

116107
-- | update the submodules that the project is in the state as required by the `stack.yaml` files
117108
updateSubmodules :: Action ()

install/src/Print.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module Print where
22

33
import Development.Shake
4-
import Development.Shake.Command
54
import Control.Monad.IO.Class
65
import Data.List ( dropWhileEnd
7-
, dropWhile
86
)
97
import Data.Char ( isSpace )
108

@@ -37,11 +35,11 @@ type TargetDescription = (String, String)
3735

3836
-- | Number of spaces the target name including whitespace should have.
3937
-- At least twenty, maybe more if target names are long. At most the length of the longest target plus five.
40-
space :: [TargetDescription] -> Int
41-
space phonyTargets = maximum (20 : map ((+ 5) . length . fst) phonyTargets)
38+
space :: [(String,String)] -> Int
39+
space helpItems = maximum (20 : map ((+ 5) . length . fst) helpItems)
4240

4341
-- | Show a target.
4442
-- Concatenates the target with its help message and inserts whitespace between them.
45-
showTarget :: Int -> TargetDescription -> String
46-
showTarget spaces (target, msg) =
47-
target ++ replicate (spaces - length target) ' ' ++ msg
43+
showHelpItem :: Int -> (String,String) -> String
44+
showHelpItem spaces (helpItemKey, msg) =
45+
helpItemKey ++ replicate (spaces - length helpItemKey) ' ' ++ msg

0 commit comments

Comments
 (0)