Skip to content

Commit 34c9cf1

Browse files
tbidneBodigrim
authored andcommitted
Add --cabal-global-options
1 parent 1f88571 commit 34c9cf1

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

clc-stackage.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ library utils
4545
, deepseq >=1.4.6.0 && <1.6
4646
, directory ^>=1.3.5.0
4747
, file-io ^>=0.1.0.0
48-
, filepath >=1.4.100.0 && <1.6
48+
, filepath >=1.5.0.0 && <1.6
49+
, os-string ^>=2.0.0
4950
, pretty-terminal ^>=0.1.0.0
5051
, text >=1.2.3.2 && <2.2
5152
, time >=1.9.3 && <1.15

src/runner/CLC/Stackage/Runner/Args.hs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ data Args = MkArgs
4545
{ -- | If given, batches packages together so we build more than one.
4646
-- Defaults to batching everything together in the same group.
4747
batch :: Maybe Int,
48+
-- | Global options to pass to cabal e.g. --store-dir.
49+
cabalGlobalOpts :: [String],
4850
-- | Options to pass to cabal e.g. --semaphore.
4951
cabalOpts :: [String],
5052
-- | Optional path to cabal executable.
@@ -123,7 +125,7 @@ getArgs = OA.execParser parserInfoArgs
123125
],
124126
mkExample
125127
[ "# Run with custom cabal",
126-
"$ clc-stackage --cabal-path=path/to/cabal --cabal-options='--store-dir=path/to/store'"
128+
"$ clc-stackage --cabal-path=path/to/cabal --cabal-global-options='--store-dir=path/to/store'"
127129
],
128130
mkExample
129131
[ "# Run with custom snapshot",
@@ -139,6 +141,7 @@ parseCliArgs :: Parser Args
139141
parseCliArgs =
140142
( do
141143
batch <- parseBatch
144+
cabalGlobalOpts <- parseCabalGlobalOpts
142145
cabalOpts <- parseCabalOpts
143146
cabalPath <- parseCabalPath
144147
colorLogs <- parseColorLogs
@@ -153,6 +156,7 @@ parseCliArgs =
153156
pure $
154157
MkArgs
155158
{ batch,
159+
cabalGlobalOpts,
156160
cabalOpts,
157161
cabalPath,
158162
colorLogs,
@@ -186,6 +190,24 @@ parseBatch =
186190
]
187191
)
188192

193+
parseCabalGlobalOpts :: Parser [String]
194+
parseCabalGlobalOpts =
195+
OA.option
196+
readOpts
197+
( mconcat
198+
[ OA.long "cabal-global-options",
199+
OA.metavar "ARGS...",
200+
OA.value [],
201+
mkHelp $
202+
mconcat
203+
[ "Global arguments to pass to cabal e.g. '--store-dir=path/to/store'. ",
204+
"These precede the build command."
205+
]
206+
]
207+
)
208+
where
209+
readOpts = Str.words <$> OA.str
210+
189211
parseCabalOpts :: Parser [String]
190212
parseCabalOpts =
191213
OA.option
@@ -194,7 +216,7 @@ parseCabalOpts =
194216
[ OA.long "cabal-options",
195217
OA.metavar "ARGS...",
196218
OA.value [],
197-
mkHelp "Quoted arguments to pass to cabal e.g. '--semaphore --verbose=1'"
219+
mkHelp "Quoted arguments to pass to cabal e.g. '--semaphore --verbose=1'."
198220
]
199221
)
200222
where

src/runner/CLC/Stackage/Runner/Env.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import CLC.Stackage.Utils.Logging qualified as Logging
4646
import CLC.Stackage.Utils.Package (Package (MkPackage, name, version))
4747
import CLC.Stackage.Utils.Paths qualified as Paths
4848
import Control.Exception (throwIO)
49-
import Control.Monad (unless)
49+
import Control.Monad (join, unless)
5050
import Data.Bool (Bool (False, True), not)
5151
import Data.Foldable (Foldable (foldl'))
5252
import Data.IORef (newIORef, readIORef)
@@ -61,7 +61,7 @@ import System.Directory.OsPath qualified as Dir
6161
import System.Exit (ExitCode (ExitSuccess))
6262
import System.OsPath (osp)
6363
import System.OsPath qualified as OsP
64-
import Prelude (IO, Monad ((>>=)), mconcat, pure, show, ($), (++), (.), (<$>), (<>))
64+
import Prelude (IO, Monad ((>>=)), mconcat, pure, show, ($), (.), (<$>), (<>))
6565

6666
-- | Args used for building all packages.
6767
data RunnerEnv = MkRunnerEnv
@@ -103,21 +103,27 @@ setup hLoggerRaw modifyPackages = do
103103

104104
-- Set up build args for cabal, filling in missing defaults
105105
let buildArgs =
106-
"build"
107-
: keepGoingArg
108-
++ cliArgs.cabalOpts
106+
join
107+
[ cliArgs.cabalGlobalOpts,
108+
["build"],
109+
keepGoingArg,
110+
cliArgs.cabalOpts
111+
]
109112

110113
-- when packageFailFast is false, add keep-going so that we build as many
111114
-- packages in the group.
112115
keepGoingArg = ["--keep-going" | not cliArgs.packageFailFast]
113116

114-
let cabalPathRaw = fromMaybe [osp|cabal|] cliArgs.cabalPath
117+
cabalPathRaw <- case cliArgs.cabalPath of
118+
Nothing -> pure [osp|cabal|]
119+
Just p -> Paths.canonicalizePath p
120+
115121
cabalPath <-
116122
Dir.findExecutable cabalPathRaw >>= \case
117123
-- TODO: It would be nice to avoid the decode here and keep everything
118124
-- in OsPath, though that is blocked until process support OsPath.
119125
Just p -> OsP.decodeUtf p
120-
Nothing -> Ex.throwText "Cabal not found"
126+
Nothing -> Ex.throwText $ "Cabal not found: " <> T.pack (show cabalPathRaw)
121127

122128
successesRef <- newIORef Set.empty
123129
failuresRef <- newIORef Set.empty

src/utils/CLC/Stackage/Utils/Paths.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module CLC.Stackage.Utils.Paths
1111
generatedCabalProjectLocalPath,
1212

1313
-- * Utils
14+
canonicalizePath,
1415
OsPath.encodeUtf,
1516
decodeUtfLenient,
1617
unsafeDecodeUtf,
@@ -20,8 +21,10 @@ where
2021
import GHC.IO.Encoding.Failure (CodingFailureMode (TransliterateCodingFailure))
2122
import GHC.IO.Encoding.UTF16 qualified as UTF16
2223
import GHC.IO.Encoding.UTF8 qualified as UTF8
24+
import System.Directory.OsPath qualified as Dir
2325
import System.OsPath (OsPath, osp, (</>))
2426
import System.OsPath qualified as OsPath
27+
import System.OsString qualified as OsStr
2528

2629
-- | Leniently decodes OsPath to String.
2730
decodeUtfLenient :: OsPath -> String
@@ -37,6 +40,21 @@ unsafeDecodeUtf p = case OsPath.decodeUtf p of
3740
Just fp -> fp
3841
Nothing -> error $ "Error decoding ospath: " <> show p
3942

43+
-- | Calls canonicalizePath, after manually expanding tilde (~) to the home
44+
-- directory. The latter usually shouldn't be needed, as the shell normally
45+
-- performs such expansions before the string makes it to the program.
46+
-- But when it is part of an argument e.g.
47+
--
48+
-- --cabal-path=~/...
49+
--
50+
-- it is not expanded.
51+
canonicalizePath :: OsPath -> IO OsPath
52+
canonicalizePath p = case OsStr.stripPrefix [osp|~/|] p of
53+
Nothing -> Dir.canonicalizePath p
54+
Just rest -> do
55+
home <- Dir.getHomeDirectory
56+
Dir.canonicalizePath $ home </> rest
57+
4058
-- | Output directory.
4159
outputDir :: OsPath
4260
outputDir = [osp|output|]

0 commit comments

Comments
 (0)