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

Cabal build support #375

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Enhancements

* Added support for automated builds using cabal, if a `cabal.project`
file is present. This mirrors the existing stack support.

* Added custom cursor shapes for resizing and moving windows.

### Bug Fixes
Expand Down
27 changes: 22 additions & 5 deletions src/XMonad/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -571,21 +571,23 @@ srcFileName, libFileName :: Directories -> FilePath
srcFileName Directories{ cfgDir } = cfgDir </> "xmonad.hs"
libFileName Directories{ cfgDir } = cfgDir </> "lib"

buildScriptFileName, stackYamlFileName :: Directories -> FilePath
buildScriptFileName Directories{ cfgDir } = cfgDir </> "build"
stackYamlFileName Directories{ cfgDir } = cfgDir </> "stack.yaml"
buildScriptFileName, stackYamlFileName, cabalProjectFileName :: Directories -> FilePath
buildScriptFileName Directories{ cfgDir } = cfgDir </> "build"
stackYamlFileName Directories{ cfgDir } = cfgDir </> "stack.yaml"
cabalProjectFileName Directories{ cfgDir } = cfgDir </> "cabal.project"

-- | Compilation method for xmonad configuration.
data Compile = CompileGhc | CompileStackGhc FilePath | CompileScript FilePath
data Compile = CompileGhc | CompileCabalGhc FilePath | CompileStackGhc FilePath | CompileScript FilePath
deriving (Show)

-- | Detect compilation method by looking for known file names in xmonad
-- configuration directory.
detectCompile :: Directories -> IO Compile
detectCompile dirs = tryScript <|> tryStack <|> useGhc
detectCompile dirs = tryScript <|> tryStack <|> tryCabal <|> useGhc
where
buildScript = buildScriptFileName dirs
stackYaml = stackYamlFileName dirs
cabalProject = cabalProjectFileName dirs

tryScript = do
guard =<< doesFileExist buildScript
Expand All @@ -605,6 +607,12 @@ detectCompile dirs = tryScript <|> tryStack <|> useGhc
trace $ "XMonad will use stack ghc --stack-yaml " <> show canonStackYaml <> " to recompile."
pure $ CompileStackGhc canonStackYaml

tryCabal = do
guard =<< doesFileExist cabalProject
canonCabalProject <- canonicalizePath cabalProject
trace $ "XMonad will use cabal exec ghc --project-file=" <> show canonCabalProject <> " to recompile."
pure $ CompileCabalGhc canonCabalProject

useGhc = do
trace $ "XMonad will use ghc to recompile, because neither "
<> show buildScript <> " nor " <> show stackYaml <> " exists."
Expand All @@ -629,6 +637,12 @@ shouldCompile dirs CompileGhc = do
cs <- prep <$> E.catch (getDirectoryContents t) (\(SomeException _) -> return [])
ds <- filterM doesDirectoryExist cs
concat . ((cs \\ ds):) <$> mapM allFiles ds
shouldCompile dirs CompileCabalGhc{} = do
cabalProjectT <- getModTime (cabalProjectFileName dirs)
binT <- getModTime (binFileName dirs)
if binT < cabalProjectT
then True <$ trace "XMonad recompiling because some files have changed."
else shouldCompile dirs CompileGhc
shouldCompile dirs CompileStackGhc{} = do
stackYamlT <- getModTime (stackYamlFileName dirs)
binT <- getModTime (binFileName dirs)
Expand All @@ -650,6 +664,9 @@ compile dirs method =
case method of
CompileGhc ->
run "ghc" ghcArgs
CompileCabalGhc cabalProject ->
run "cabal" ["build", "all", "-v0", "--project-file=" ++ cabalProject] .&&.
run "cabal" ("exec" : "ghc" : ("--project-file=" ++ cabalProject) : "--" : ghcArgs)
CompileStackGhc stackYaml ->
run "stack" ["build", "--silent", "--stack-yaml", stackYaml] .&&.
run "stack" ("ghc" : "--stack-yaml" : stackYaml : "--" : ghcArgs)
Expand Down