Skip to content

Drop dependency on shake for install.hs #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cabal-hls-install
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
cabal v2-run ./install.hs --project-file install/shake.project $@
cabal v2-run ./install.hs --project-file install/shake.project -- $@
2 changes: 1 addition & 1 deletion cabal-hls-install.cmd
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@cabal v2-run .\install.hs --project-file=install\shake.project %*
@cabal v2-run .\install.hs --project-file=install\shake.project -- %*
5 changes: 3 additions & 2 deletions install/hls-install.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ library
, Print
, Env
, Help
, Utils
build-depends: base >= 4.9 && < 5
, shake >= 0.16.4 && < 0.19
, directory
, process
, filepath
, extra
, optparse-applicative
, text
default-extensions: LambdaCase
, TupleSections
Expand Down
59 changes: 22 additions & 37 deletions install/src/Cabal.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{-# LANGUAGE CPP #-}
module Cabal where

import Development.Shake
import Development.Shake.FilePath
import Control.Monad
import System.Directory ( copyFile )
import System.Directory ( copyFile, doesFileExist )
import System.FilePath
import System.Process

import Version
import Print
import Env
import Utils
#if RUN_FROM_STACK
import Control.Exception ( throwIO )
#else
Expand All @@ -24,28 +25,30 @@ getInstallDir = throwIO $ userError "Stack and cabal should never be mixed"
getInstallDir = runIdentity . cfgInstallDir <$> readConfig
#endif

execCabal :: CmdResult r => [String] -> Action r
execCabal = command [] "cabal"
execCabal :: [String] -> IO String
execCabal args = readProcess "cabal" args ""

execCabal_ :: [String] -> Action ()
execCabal_ = execCabal
execCabal_ :: [String] -> IO ()
execCabal_ args = do
_ <- execCabal args
return ()

cabalBuildData :: [String] -> Action ()
cabalBuildData :: [String] -> IO ()
cabalBuildData args = do
execCabal_ $ ["v2-build", "hoogle"] ++ args
execCabal_ $ ["v2-exec", "hoogle", "generate"] ++ args

getGhcPathOfOrThrowError :: VersionNumber -> Action GhcPath
getGhcPathOfOrThrowError :: VersionNumber -> IO GhcPath
getGhcPathOfOrThrowError versionNumber =
getGhcPathOf versionNumber >>= \case
Nothing -> do
printInStars $ ghcVersionNotFoundFailMsg versionNumber
error (ghcVersionNotFoundFailMsg versionNumber)
Just p -> return p

cabalInstallHls :: VersionNumber -> [String] -> Action ()
cabalInstallHls :: VersionNumber -> [String] -> IO ()
cabalInstallHls versionNumber args = do
localBin <- liftIO $ getInstallDir
localBin <- getInstallDir
cabalVersion <- getCabalVersion args
ghcPath <- getGhcPathOfOrThrowError versionNumber

Expand Down Expand Up @@ -74,37 +77,37 @@ cabalInstallHls versionNumber args = do
let minorVerExe = "haskell-language-server-" ++ versionNumber <.> exe
majorVerExe = "haskell-language-server-" ++ dropExtension versionNumber <.> exe

liftIO $ do
copyFile (localBin </> "haskell-language-server" <.> exe) (localBin </> minorVerExe)
copyFile (localBin </> "haskell-language-server" <.> exe) (localBin </> majorVerExe)

copyFile (localBin </> "haskell-language-server" <.> exe) (localBin </> minorVerExe)
copyFile (localBin </> "haskell-language-server" <.> exe) (localBin </> majorVerExe)

printLine $ "Copied executables "
putStrLn $ "Copied executables "
++ ("haskell-language-server-wrapper" <.> exe) ++ ", "
++ ("haskell-language-server" <.> exe) ++ ", "
++ majorVerExe ++ " and "
++ minorVerExe
++ " to " ++ localBin

getProjectFile :: VersionNumber -> Action FilePath
getProjectFile :: VersionNumber -> IO FilePath
getProjectFile ver = do
existFile <- doesFileExist $ "cabal.project-" ++ ver
return $ if existFile
then "cabal.project-" ++ ver
else "cabal.project"

checkCabal_ :: [String] -> Action ()
checkCabal_ :: [String] -> IO ()
checkCabal_ args = checkCabal args >> return ()

-- | check `cabal` has the required version
checkCabal :: [String] -> Action String
checkCabal :: [String] -> IO String
checkCabal args = do
cabalVersion <- getCabalVersion args
unless (checkVersion requiredCabalVersion cabalVersion) $ do
printInStars $ cabalInstallIsOldFailMsg cabalVersion
error $ cabalInstallIsOldFailMsg cabalVersion
return cabalVersion

getCabalVersion :: [String] -> Action String
getCabalVersion :: [String] -> IO String
getCabalVersion args = trimmedStdout <$> (execCabal $ ["--numeric-version"] ++ args)

-- | Error message when the `cabal` binary is an older version
Expand All @@ -124,21 +127,3 @@ requiredCabalVersion | isWindowsSystem = requiredCabalVersionForWindows

requiredCabalVersionForWindows :: RequiredVersion
requiredCabalVersionForWindows = [3, 0, 0, 0]

getVerbosityArg :: Verbosity -> String
getVerbosityArg v = "-v" ++ cabalVerbosity
where cabalVerbosity = case v of
Silent -> "0"
#if MIN_VERSION_shake(0,18,4)
Error -> "0"
Warn -> "1"
Info -> "1"
Verbose -> "2"
#else
Quiet -> "0"
Normal -> "1"
Loud -> "2"
Chatty -> "2"
#endif
Diagnostic -> "3"

30 changes: 15 additions & 15 deletions install/src/Env.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Env where

import Development.Shake
import Control.Monad.IO.Class
import Control.Monad
import Development.Shake.FilePath
import System.FilePath
import System.Info ( os )
import System.Process
import Data.Maybe ( isJust
, mapMaybe
)
Expand All @@ -21,18 +21,18 @@ import Data.List ( sort
, nubBy
)
import Data.Ord ( comparing )
import Control.Monad.Extra ( mapMaybeM )

import qualified Data.Text as T

import Version
import Print
import Utils


type GhcPath = String

existsExecutable :: MonadIO m => String -> m Bool
existsExecutable executable = liftIO $ isJust <$> findExecutable executable
existsExecutable :: String -> IO Bool
existsExecutable executable = isJust <$> findExecutable executable


-- | Check if the current system is windows
Expand All @@ -41,7 +41,7 @@ isWindowsSystem = os `elem` ["mingw32", "win32"]

findInstalledGhcs :: IO [(VersionNumber, GhcPath)]
findInstalledGhcs = do
hlsVersions <- getHlsVersions :: IO [VersionNumber]
hlsVersions <- getHlsVersions
knownGhcs <- mapMaybeM
(\version -> getGhcPathOf version >>= \case
Nothing -> return Nothing
Expand All @@ -58,7 +58,7 @@ findInstalledGhcs = do
-- filter out stack provided GHCs (assuming that stack programs path is the default one in linux)
$ filter (not . isInfixOf ".stack" . snd) (knownGhcs ++ availableGhcs)

showInstalledGhcs :: MonadIO m => [(VersionNumber, GhcPath)] -> m ()
showInstalledGhcs :: [(VersionNumber, GhcPath)] -> IO ()
showInstalledGhcs ghcPaths = do
let msg = "Found the following GHC paths: \n"
++ unlines
Expand All @@ -67,7 +67,7 @@ showInstalledGhcs ghcPaths = do
)
printInStars msg

checkInstalledGhcs :: MonadIO m => [(VersionNumber, GhcPath)] -> m ()
checkInstalledGhcs :: [(VersionNumber, GhcPath)] -> IO ()
checkInstalledGhcs ghcPaths = when (null ghcPaths) $ do
let msg = "No ghc installations found in $PATH. \n"
++ "The script requires at least one ghc in $PATH \n"
Expand All @@ -80,18 +80,18 @@ checkInstalledGhcs ghcPaths = when (null ghcPaths) $ do
-- First, it is checked whether there is a GHC with the name `ghc-$VersionNumber`.
-- If this yields no result, it is checked, whether the numeric-version of the `ghc`
-- command fits to the desired version.
getGhcPathOf :: MonadIO m => VersionNumber -> m (Maybe GhcPath)
getGhcPathOf :: VersionNumber -> IO (Maybe GhcPath)
getGhcPathOf ghcVersion =
liftIO $ findExecutable ("ghc-" ++ ghcVersion <.> exe) >>= \case
Nothing -> lookup ghcVersion <$> getGhcPaths
path -> return path

-- | Get a list of GHCs that are available in $PATH
getGhcPaths :: MonadIO m => m [(VersionNumber, GhcPath)]
getGhcPaths = liftIO $ do
getGhcPaths :: IO [(VersionNumber, GhcPath)]
getGhcPaths = do
paths <- findExecutables "ghc"
forM paths $ \path -> do
Stdout version <- cmd path ["--numeric-version"]
version <- readProcess path ["--numeric-version"] ""
return (trim version, path)

-- | No suitable ghc version has been found. Show a message.
Expand All @@ -106,11 +106,11 @@ ghcVersionNotFoundFailMsg versionNumber =
-- | Defines all different hls versions that are buildable.
--
-- The current directory is scanned for `stack-*.yaml` files.
getHlsVersions :: MonadIO m => m [VersionNumber]
getHlsVersions ::IO [VersionNumber]
getHlsVersions = do
let stackYamlPrefix = T.pack "stack-"
let stackYamlSuffix = T.pack ".yaml"
files <- liftIO $ listDirectory "."
files <- listDirectory "."
let hlsVersions =
files
& map T.pack
Expand All @@ -125,5 +125,5 @@ getHlsVersions = do

-- | Most recent version of hls.
-- Shown in the more concise help message.
mostRecentHlsVersion :: MonadIO m => m VersionNumber
mostRecentHlsVersion :: IO VersionNumber
mostRecentHlsVersion = last <$> getHlsVersions
39 changes: 19 additions & 20 deletions install/src/Help.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-- |Module for Help messages and traget descriptions
module Help where

import Development.Shake
import Data.List ( intercalate )

import Env
Expand All @@ -19,23 +18,23 @@ buildCommand :: TargetDescription -> String
buildCommand | isRunFromCabal = cabalCommand
| otherwise = stackCommand

printUsage :: Action ()
printUsage :: IO ()
printUsage = do
printLine ""
printLine "Usage:"
printLineIndented (stackCommand templateTarget)
printLineIndented "or"
printLineIndented (cabalCommand templateTarget)
putStrLn ""
putStrLn "Usage:"
putStrLnIndented (stackCommand templateTarget)
putStrLnIndented "or"
putStrLnIndented (cabalCommand templateTarget)

-- | short help message is printed by default
shortHelpMessage :: Action ()
shortHelpMessage :: IO ()
shortHelpMessage = do
hlsVersions <- getHlsVersions
printUsage
printLine ""
printLine "Targets:"
mapM_ (printLineIndented . showHelpItem (spaces hlsVersions)) (targets hlsVersions)
printLine ""
putStrLn ""
putStrLn "Targets:"
mapM_ (putStrLnIndented . showHelpItem (spaces hlsVersions)) (targets hlsVersions)
putStrLn ""
where
spaces hlsVersions = space (targets hlsVersions)
targets hlsVersions =
Expand All @@ -60,16 +59,16 @@ getDefaultBuildSystemVersions BuildableVersions {..}
| isRunFromCabal = cabalVersions
| otherwise = error $ "unknown build system: " ++ buildSystem

helpMessage :: BuildableVersions -> Action ()
helpMessage :: BuildableVersions -> IO ()
helpMessage versions@BuildableVersions {..} = do
printUsage
printLine ""
printLine "Targets:"
mapM_ (printLineIndented . showHelpItem spaces) targets
printLine ""
printLine "Options:"
mapM_ (printLineIndented . showHelpItem spaces) options
printLine ""
putStrLn ""
putStrLn "Targets:"
mapM_ (putStrLnIndented . showHelpItem spaces) targets
putStrLn ""
putStrLn "Options:"
mapM_ (putStrLnIndented . showHelpItem spaces) options
putStrLn ""
where
spaces = space targets
-- All targets the shake file supports
Expand Down
Loading