Skip to content

Commit 2a392ac

Browse files
committed
Merge remote-tracking branch 'origin/script-no-run'
2 parents 35d12cc + 0070264 commit 2a392ac

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Other enhancements:
1717
[#5288](https://github.com/commercialhaskell/stack/issues/5288)
1818
* `stack upgrade` makes less assumptions about archive format. See
1919
[#5288](https://github.com/commercialhaskell/stack/issues/5288)
20+
* Add a `--no-run` flag to the `script` command when compiling.
2021

2122
Bug fixes:
2223

src/Stack/Options/ScriptParser.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ data ScriptOpts = ScriptOpts
1313
, soCompile :: !ScriptExecute
1414
, soGhcOptions :: ![String]
1515
, soScriptExtraDeps :: ![PackageIdentifierRevision]
16+
, soShouldRun :: !ShouldRun
1617
}
1718
deriving Show
1819

@@ -22,6 +23,9 @@ data ScriptExecute
2223
| SEOptimize
2324
deriving Show
2425

26+
data ShouldRun = YesRun | NoRun
27+
deriving Show
28+
2529
scriptOptsParser :: Parser ScriptOpts
2630
scriptOptsParser = ScriptOpts
2731
<$> many (strOption
@@ -48,5 +52,6 @@ scriptOptsParser = ScriptOpts
4852
(long "extra-dep" <>
4953
metavar "PACKAGE-VERSION" <>
5054
help "Extra dependencies to be added to the snapshot"))
55+
<*> (flag' NoRun (long "no-run" <> help "Don't run, just compile.") <|> pure YesRun)
5156
where
5257
extraDepRead = eitherReader $ mapLeft show . parsePackageIdentifierRevision . fromString

src/Stack/Script.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ scriptCmd opts = do
8484
, globalStackYaml = SYLNoProject $ soScriptExtraDeps opts
8585
}
8686

87+
case soShouldRun opts of
88+
YesRun -> pure ()
89+
NoRun -> do
90+
unless (null $ soArgs opts) $ throwString "--no-run incompatible with arguments"
91+
case soCompile opts of
92+
SEInterpret -> throwString "--no-run requires either --compile or --optimize"
93+
SECompile -> pure ()
94+
SEOptimize -> pure ()
95+
8796
-- Optimization: if we're compiling, and the executable is newer
8897
-- than the source file, run it immediately.
8998
local (over globalOptsL modifyGO) $
@@ -93,11 +102,16 @@ scriptCmd opts = do
93102
SEOptimize -> shortCut file scriptDir
94103

95104
where
105+
runCompiled file = do
106+
let exeName = toExeName $ toFilePath file
107+
case soShouldRun opts of
108+
YesRun -> exec exeName (soArgs opts)
109+
NoRun -> logInfo $ "Compilation finished, executable available at " <> fromString exeName
96110
shortCut file scriptDir = handleIO (const $ longWay file scriptDir) $ do
97111
srcMod <- getModificationTime file
98112
exeMod <- Dir.getModificationTime $ toExeName $ toFilePath file
99113
if srcMod < exeMod
100-
then exec (toExeName $ toFilePath file) (soArgs opts)
114+
then runCompiled file
101115
else longWay file scriptDir
102116

103117
longWay file scriptDir =
@@ -168,7 +182,7 @@ scriptCmd opts = do
168182
compilerExeName
169183
(ghcArgs ++ [toFilePath file])
170184
(void . readProcessStdout_)
171-
exec (toExeName $ toFilePath file) (soArgs opts)
185+
runCompiled file
172186

173187
toPackageName = reverse . drop 1 . dropWhile (/= '-') . reverse
174188

0 commit comments

Comments
 (0)