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
Prev Previous commit
Next Next commit
Handle non-standard installations more robustly.
  • Loading branch information
associahedron committed Jan 6, 2020
commit d6498f2b7e5cfa7135aa419eee2ff50013c4dbd5
20 changes: 16 additions & 4 deletions daml-assistant/src/DA/Daml/Assistant/Install/BashCompletion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ module DA.Daml.Assistant.Install.BashCompletion
import DA.Daml.Assistant.Types

import qualified Data.ByteString.Lazy as BSL
import Control.Exception.Safe (catchIO)
import Control.Exception.Safe (tryIO, catchIO, displayException)
import Control.Monad.Extra (unless, andM, whenM)
import System.Directory (getHomeDirectory, doesFileExist)
import System.Directory (getHomeDirectory, getAppUserDataDirectory, doesFileExist, removePathForcibly)
import System.FilePath ((</>))
import System.Info.Extra (isWindows)
import System.Process.Typed (proc, readProcessStdout_)
Expand All @@ -35,6 +35,7 @@ shouldInstallBashCompletions options damlPath =
BashCompletions Auto -> andM
[ pure (not isWindows)
, not <$> doesFileExist (completionScriptPath damlPath)
, isDefaultDamlPath damlPath
]

-- | Generate the bash completion script, and add a hook.
Expand All @@ -43,8 +44,12 @@ doInstallBashCompletions damlPath output = do
let scriptPath = completionScriptPath damlPath
script <- getCompletionScript damlPath
BSL.writeFile scriptPath script
addCompletionHook scriptPath
output "Bash completions installed for DAML assistant."
unitE <- tryIO $ addCompletionHook scriptPath
case unitE of
Left e -> do
output ("Bash completions not installed: " <> displayException e)
catchIO (removePathForcibly scriptPath) (const $ pure ())
Right () -> output "Bash completions installed for DAML assistant."

-- | Read the bash completion script from optparse-applicative's
-- built-in @--bash-completion-script@ routine. Please read
Expand All @@ -67,6 +72,13 @@ addCompletionHook scriptPath = do
unless (newHook `elem` hooks) $ do
writeHooks hookPath (hooks ++ [newHook])

-- | Check the daml path is default. We don't want to install completions
-- for non-standard paths by default.
isDefaultDamlPath :: DamlPath -> IO Bool
isDefaultDamlPath damlPath = do
rawDamlPath <- getAppUserDataDirectory "daml"
pure $ damlPath == DamlPath rawDamlPath

newtype HookPath = HookPath FilePath
newtype Hook = Hook { unHook :: String } deriving Eq

Expand Down