Skip to content

Commit

Permalink
cabal-doctest: Get rid of separate cabal build step
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jul 23, 2024
1 parent 5c63555 commit 8ea6731
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cabal-doctest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- run: cabal path -v0 --installdir >> $GITHUB_PATH
if: matrix.os == 'macos-12'

- run: ghcup install ghc 9.6 --no-set
- run: ghcup install ghc 8.6.5 --no-set

- run: cabal --version
Expand All @@ -43,6 +44,7 @@ jobs:
- run: cabal install -f cabal-doctest
- run: cabal doctest

- run: cabal doctest -w ghc-9.6
- run: cabal doctest -w ghc-8.6.5

cabal-doctest-success:
Expand Down
2 changes: 2 additions & 0 deletions doctest.cabal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ library:
ghc-paths: ">= 0.1.0.9"
transformers:
containers:
temporary:

flags:
cabal-doctest:
Expand Down
39 changes: 27 additions & 12 deletions src/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module Cabal (externalCommand) where

import Imports

import Data.List
import Data.Version (makeVersion)
import System.IO
import System.IO.Temp
import System.Environment
import System.Exit (exitWith)
import System.Exit
import System.Directory
import System.FilePath
import System.Process
import System.Process hiding (system)

import qualified Info
import Cabal.Paths
Expand Down Expand Up @@ -40,7 +43,6 @@ run cabal args = do
, "--program-suffix", "-" <> Info.version
, "--install-method=copy"
, "--with-compiler", ghc
, "--with-hc-pkg", ghcPkg
]

doesFileExist script >>= \ case
Expand All @@ -49,16 +51,29 @@ run cabal args = do

callProcess doctest ["--version"]

callProcess cabal ("build" : "--only-dependencies" : discardReplOptions args)
let
repl extraArgs = system cabal ("repl"
: "--build-depends=QuickCheck"
: "--build-depends=template-haskell"
: ("--repl-options=-ghci-script=" <> script)
: args ++ extraArgs)

case ghcVersion < makeVersion [9,4] of
True -> do
callProcess cabal ("build" : "--only-dependencies" : discardReplOptions args)
repl ["--with-compiler", doctest, "--with-hc-pkg", ghcPkg]

False -> do
withSystemTempDirectory "cabal-doctest" $ \ dir -> do
repl ["--keep-temp-files", "--repl-multi-file", dir]
files <- filter (isSuffixOf "-inplace") <$> listDirectory dir
options <- concat <$> mapM (fmap lines . readFile . combine dir) files
system doctest ("--no-magic" : options)

rawSystem cabal ("repl"
: "--build-depends=QuickCheck"
: "--build-depends=template-haskell"
: ("--repl-options=-ghci-script=" <> script)
: args ++ [
"--with-compiler", doctest
, "--with-hc-pkg", ghcPkg
]) >>= exitWith
system :: FilePath -> [FilePath] -> IO ()
system name args = rawSystem name args >>= \ case
ExitSuccess -> pass
err -> exitWith err

writeFileAtomically :: FilePath -> String -> IO ()
writeFileAtomically name contents = do
Expand Down
17 changes: 12 additions & 5 deletions src/Cabal/Paths.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE StrictData #-}
module Cabal.Paths (
Paths(..)
, paths
Expand All @@ -19,7 +20,8 @@ import System.Process
import Text.ParserCombinators.ReadP

data Paths = Paths {
ghc :: FilePath
ghcVersion :: Version
, ghc :: FilePath
, ghcPkg :: FilePath
, cache :: FilePath
} deriving (Eq, Show)
Expand All @@ -45,11 +47,15 @@ paths cabal args = do

ghc <- getPath "'ghc'" "compiler-path"

ghcVersion <- strip <$> readProcess ghc ["--numeric-version"] ""
ghcVersionString <- strip <$> readProcess ghc ["--numeric-version"] ""

ghcVersion <- case parseVersion ghcVersionString of
Nothing -> die $ "Cannot determine GHC version from '" <> ghcVersionString <> "'."
Just version -> return version

let
ghcPkg :: FilePath
ghcPkg = takeDirectory ghc </> "ghc-pkg-" <> ghcVersion
ghcPkg = takeDirectory ghc </> "ghc-pkg-" <> ghcVersionString
#ifdef mingw32_HOST_OS
<.> "exe"
#endif
Expand All @@ -61,12 +67,13 @@ paths cabal args = do
abi <- strip <$> readProcess ghcPkg ["--no-user-package-db", "field", "base", "abi", "--simple-output"] ""

cache_home <- getPath "Cabal's cache directory" "cache-home"
let cache = cache_home </> "doctest" </> "ghc-" <> ghcVersion <> "-" <> abi
let cache = cache_home </> "doctest" </> "ghc-" <> ghcVersionString <> "-" <> abi

createDirectoryIfMissing True cache

return Paths {
ghc
ghcVersion
, ghc
, ghcPkg
, cache
}
Expand Down

0 comments on commit 8ea6731

Please sign in to comment.