on master help sign in
Move findProjectAssets to Obelisk.Command.Project

This makes it easier to add ‘Ob run’-like commands by factoring out important bits.

commiteea6d5b9ce79348dd87b85de012da8e0135c995f authorMatthew Bauer <mjbauer95@gmail.com> dateMon 10 Feb 2020, 15:06 (7 years ago) parent5d8b781
2 files changed+29−25
lib/command/src/Obelisk/Command/Project.hs+25−1 lib/command/src/Obelisk/Command/Run.hs+4−24
lib/command/src/Obelisk/Command/Project.hs+25−1
@@ -5,6 +5,7 @@ module Obelisk.Command.Project
55 ( InitSource (..)
66 , findProjectObeliskCommand
77 , findProjectRoot
8+ , findProjectAssets
89 , initProject
910 , nixShellRunConfig
1011 , nixShellRunProc
@@ -50,7 +51,7 @@ import Obelisk.App (MonadObelisk)
5051import Obelisk.CliApp
5152import Obelisk.Command.Nix
5253import Obelisk.Command.Thunk
53−import Obelisk.Command.Utils (nixExePath)
54+import Obelisk.Command.Utils (nixExePath, nixBuildExePath)
5455
5556--TODO: Make this module resilient to random exceptions
5657
@@ -307,3 +308,26 @@ projectShell root isPure shellName command = do
307308 & nixShellConfig_common . nixCmdConfig_target . target_path ?~ "default.nix"
308309 & nixShellConfig_common . nixCmdConfig_target . target_attr ?~ ("shells." <> shellName)
309310 void $ liftIO $ waitForProcess ph
311+
312+findProjectAssets :: MonadObelisk m => FilePath -> m Text
313+findProjectAssets root = do
314+ let importableRoot = toNixPath root
315+ isDerivation <- readProcessAndLogStderr Debug $
316+ proc nixExePath
317+ [ "eval"
318+ , "-f"
319+ , root
320+ , "(let a = import " <> importableRoot <> " {}; in toString (a.reflex.nixpkgs.lib.isDerivation a.passthru.staticFilesImpure))"
321+ , "--raw"
322+ -- `--raw` is not available with old nix-instantiate. It drops quotation
323+ -- marks and trailing newline, so is very convenient for shelling out.
324+ ]
325+ -- Check whether the impure static files are a derivation (and so must be built)
326+ if isDerivation == "1"
327+ then fmap T.strip $ readProcessAndLogStderr Debug $ -- Strip whitespace here because nix-build has no --raw option
328+ proc nixBuildExePath
329+ [ "--no-out-link"
330+ , "-E", "(import " <> importableRoot <> "{}).passthru.staticFilesImpure"
331+ ]
332+ else readProcessAndLogStderr Debug $
333+ proc nixExePath ["eval", "-f", root, "passthru.staticFilesImpure", "--raw"]
lib/command/src/Obelisk/Command/Run.hs+4−24
@@ -47,10 +47,10 @@ import qualified System.Info
4747import System.IO.Temp (withSystemTempDirectory)
4848
4949import Obelisk.App (MonadObelisk)
50−import Obelisk.CliApp (Severity (..) , failWith, putLog, proc, readCreateProcessWithExitCode, readProcessAndLogStderr)
51−import Obelisk.Command.Project (obeliskDirName, toObeliskDir, withProjectRoot, nixShellWithPkgs, toNixPath)
50+import Obelisk.CliApp (Severity (..) , failWith, putLog, proc, readCreateProcessWithExitCode)
51+import Obelisk.Command.Project (obeliskDirName, toObeliskDir, withProjectRoot, nixShellWithPkgs, findProjectAssets)
5252import Obelisk.Command.Thunk (attrCacheFileName)
53−import Obelisk.Command.Utils (findExePath, ghcidExePath, nixBuildExePath, nixExePath)
53+import Obelisk.Command.Utils (findExePath, ghcidExePath)
5454
5555data CabalPackageInfo = CabalPackageInfo
5656 { _cabalPackageInfo_packageFile :: FilePath
@@ -76,27 +76,7 @@ run = withProjectRoot "." $ \root -> do
7676 pkgs <- fmap toList . parsePackagesOrFail =<< getLocalPkgs root
7777 withGhciScript pkgs root $ \dotGhciPath -> do
7878 freePort <- getFreePort
79− assets <- do
80− let importableRoot = toNixPath root
81− isDerivation <- readProcessAndLogStderr Debug $
82− proc nixExePath
83− [ "eval"
84− , "-f"
85− , root
86− , "(let a = import " <> importableRoot <> " {}; in toString (a.reflex.nixpkgs.lib.isDerivation a.passthru.staticFilesImpure))"
87− , "--raw"
88− -- `--raw` is not available with old nix-instantiate. It drops quotation
89− -- marks and trailing newline, so is very convenient for shelling out.
90− ]
91− -- Check whether the impure static files are a derivation (and so must be built)
92− if isDerivation == "1"
93− then fmap T.strip $ readProcessAndLogStderr Debug $ -- Strip whitespace here because nix-build has no --raw option
94− proc nixBuildExePath
95− [ "--no-out-link"
96− , "-E", "(import " <> importableRoot <> "{}).passthru.staticFilesImpure"
97− ]
98− else readProcessAndLogStderr Debug $
99− proc nixExePath ["eval", "-f", root, "passthru.staticFilesImpure", "--raw"]
79+ assets <- findProjectAssets root
10080 putLog Debug $ "Assets impurely loaded from: " <> assets
10181 runGhcid root True dotGhciPath pkgs $ Just $ unwords
10282 [ "Obelisk.Run.run"