Skip to content
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

daml-assistant: Install bash completion scripts on Linux and Mac. #3946

Merged
merged 10 commits into from
Jan 6, 2020
Merged
Prev Previous commit
Next Next commit
Install bash completion script automatically
  • Loading branch information
associahedron committed Jan 3, 2020
commit 27e9963fb43f1bbc6e094a1b04d71c3944df2828
1 change: 1 addition & 0 deletions daml-assistant/exe/DA/Daml/Assistant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ autoInstall env@Env{..} = do
, iActivate = ActivateInstall False
, iForce = ForceInstall False
, iSetPath = SetPath True
, iBashCompletions = BashCompletions Auto
}
installEnv = InstallEnv
{ options = options
Expand Down
1 change: 1 addition & 0 deletions daml-assistant/src/DA/Daml/Assistant/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ installParser = InstallOptions
<*> iflag ForceInstall "force" (short 'f') "Overwrite existing installation"
<*> iflag QuietInstall "quiet" (short 'q') "Don't display installation messages"
<*> fmap SetPath (flagYesNoAuto "set-path" True "Adjust PATH automatically. This option only has an effect on Windows." idm)
<*> fmap BashCompletions (flagYesNoAuto' "bash-completions" "Install bash completions for DAML assistant. Default is yes for linux and mac, no for windows." idm)
where
iflag p name opts desc = fmap p (switch (long name <> help desc <> opts))

Expand Down
2 changes: 2 additions & 0 deletions daml-assistant/src/DA/Daml/Assistant/Install.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DA.Daml.Assistant.Types
import DA.Daml.Assistant.Util
import qualified DA.Daml.Assistant.Install.Github as Github
import DA.Daml.Assistant.Install.Path
import DA.Daml.Assistant.Install.BashCompletion
import DA.Daml.Project.Consts
import DA.Daml.Project.Config
import DA.Daml.Project.Util
Expand Down Expand Up @@ -190,6 +191,7 @@ activateDaml env@InstallEnv{..} targetPath = do
else createSymbolicLink damlBinarySourcePath damlBinaryTargetPath

updatePath options (\s -> unlessQuiet env (output s)) damlBinaryTargetDir
installBashCompletions options damlPath (\s -> unlessQuiet env (output s))

data WalkCallbacks = WalkCallbacks
{ walkOnFile :: FilePath -> IO ()
Expand Down
55 changes: 55 additions & 0 deletions daml-assistant/src/DA/Daml/Assistant/Install/BashCompletion.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

-- | Installation of bash completions. These are installed in
-- /usr/local/etc/bash_completion.d (on Mac) or /etc/bash_completion.d (on Linux).
-- These are not installed on Windows by default.
module DA.Daml.Assistant.Install.BashCompletion
( installBashCompletions
) where

import DA.Daml.Assistant.Types

import qualified Data.ByteString.Lazy as BSL
import Control.Monad.Extra (when, unlessM)
import System.Directory (doesDirectoryExist, doesFileExist)
import System.FilePath ((</>))
import System.Info.Extra (isMac, isWindows)
import System.Process.Typed (proc, readProcessStdout_)

installBashCompletions :: InstallOptions -> DamlPath -> (String -> IO ()) -> IO ()
installBashCompletions options damlPath output =
when (shouldInstallBashCompletions options) $
doInstallBashCompletions damlPath output

shouldInstallBashCompletions :: InstallOptions -> Bool
shouldInstallBashCompletions options =
case iBashCompletions options of
BashCompletions Yes -> True
BashCompletions No -> False
BashCompletions Auto -> not isWindows

doInstallBashCompletions :: DamlPath -> (String -> IO ()) -> IO ()
doInstallBashCompletions damlPath output = do
dirExists <- doesDirectoryExist bashCompletionDir
if dirExists
then do
unlessM (doesFileExist bashCompletionScript) $ do
completionScript <- getCompletionScript damlPath
BSL.writeFile bashCompletionScript completionScript
output ("Bash completions for DAML assistant installed in " <> bashCompletionDir)
else output ("Bash completions for DAML assistant not installed: " <> bashCompletionDir <> " does not exist")

getCompletionScript :: DamlPath -> IO BSL.ByteString
getCompletionScript damlPath = do
let assistant = assistantPath damlPath
readProcessStdout_ (proc assistant ["--bash-completion-script", assistant])

assistantPath :: DamlPath -> FilePath
assistantPath (DamlPath p) = p </> "bin" </> "daml"

bashCompletionDir :: FilePath
bashCompletionDir
| isMac = "/usr/local/etc/bash_completion.d"
This conversation was marked as resolved.
Show resolved Hide resolved
| otherwise = "/etc/bash_completion.d"
This conversation was marked as resolved.
Show resolved Hide resolved

bashCompletionScript :: FilePath
bashCompletionScript = bashCompletionDir </> "daml"
4 changes: 2 additions & 2 deletions daml-assistant/src/DA/Daml/Assistant/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ data InstallOptions = InstallOptions
, iForce :: ForceInstall -- ^ force reinstall if already installed
, iQuiet :: QuietInstall -- ^ don't print messages
, iSetPath :: SetPath -- ^ set the user's PATH (on Windows)
, iBashCompletions :: BashCompletions -- ^ install bash completions for the daml assistant
} deriving (Eq, Show)

-- | An install URL is a fully qualified HTTP[S] URL to an SDK release tarball. For example:
Expand All @@ -102,5 +103,4 @@ newtype QuietInstall = QuietInstall { unQuietInstall :: Bool } deriving (Eq, Sho
newtype ActivateInstall = ActivateInstall { unActivateInstall :: Bool } deriving (Eq, Show)
newtype SetPath = SetPath Bool deriving (Eq, Show)
newtype InstallAssistant = InstallAssistant { unwrapInstallAssistant :: YesNoAuto } deriving (Eq, Show)


newtype BashCompletions = BashCompletions { unwrapBashCompletions :: YesNoAuto } deriving (Eq, Show)
4 changes: 4 additions & 0 deletions daml-assistant/test/DA/Daml/Assistant/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ testInstall = Tasty.testGroup "DA.Daml.Assistant.Install"
, iQuiet = QuietInstall True
, iForce = ForceInstall False
, iSetPath = SetPath False
, iBashCompletions = BashCompletions No
}

setCurrentDirectory base
Expand Down Expand Up @@ -411,6 +412,7 @@ testInstallUnix = Tasty.testGroup "unix-specific tests"
, iQuiet = QuietInstall True
, iForce = ForceInstall False
, iSetPath = SetPath False
, iBashCompletions = BashCompletions No
}

setCurrentDirectory base
Expand Down Expand Up @@ -439,6 +441,7 @@ testInstallUnix = Tasty.testGroup "unix-specific tests"
, iQuiet = QuietInstall True
, iForce = ForceInstall False
, iSetPath = SetPath False
, iBashCompletions = BashCompletions No
}

setCurrentDirectory base
Expand Down Expand Up @@ -467,6 +470,7 @@ testInstallUnix = Tasty.testGroup "unix-specific tests"
, iQuiet = QuietInstall True
, iForce = ForceInstall False
, iSetPath = SetPath False
, iBashCompletions = BashCompletions No
}

setCurrentDirectory base
Expand Down