on master help sign in
static: improve logging of static derivation rebuild in ob run
commitf56ce139049618a6c2a29ff97e97504091c97de4 authorAli Abrar <aliabrar@gmail.com> dateFri 18 Dec 2020, 12:09 (6 years ago) parent77cbb3a
2 files changed+37−30
lib/cliapp/src/Obelisk/CliApp/Logging.hs+5−1 lib/command/src/Obelisk/Command/Project.hs+32−29
lib/cliapp/src/Obelisk/CliApp/Logging.hs+5−1
@@ -44,7 +44,7 @@ import Data.Text (Text)
4444import qualified Data.Text as T
4545import qualified Data.Text.IO as T
4646import GHC.IO.Encoding.Types
47−import System.Console.ANSI (Color (Red, Yellow), ColorIntensity (Vivid),
47+import System.Console.ANSI (Color (..), ColorIntensity (Vivid),
4848 ConsoleIntensity (FaintIntensity), ConsoleLayer (Foreground),
4949 SGR (SetColor, SetConsoleIntensity), clearLine)
5050import System.Exit (ExitCode (..))
@@ -184,6 +184,8 @@ writeLog withNewLine noColor (WithSeverity severity s) = if T.null s then pure (
184184 | noColor && severity <= Warning = liftIO $ putFn $ T.pack (show severity) <> ": " <> s
185185 | not noColor && severity <= Error = TS.putStrWithSGR errorColors h withNewLine s
186186 | not noColor && severity <= Warning = TS.putStrWithSGR warningColors h withNewLine s
187+ | not noColor && severity == Notice = TS.putStrWithSGR noticeColors h withNewLine s
188+ | not noColor && severity == Informational = TS.putStrWithSGR infoColors h withNewLine s
187189 | not noColor && severity >= Debug = TS.putStrWithSGR debugColors h withNewLine s
188190 | otherwise = liftIO $ putFn s
189191
@@ -191,6 +193,8 @@ writeLog withNewLine noColor (WithSeverity severity s) = if T.null s then pure (
191193 h = if severity <= Error then stderr else stdout
192194 errorColors = [SetColor Foreground Vivid Red]
193195 warningColors = [SetColor Foreground Vivid Yellow]
196+ infoColors = [SetColor Foreground Vivid Green]
197+ noticeColors = [SetColor Foreground Vivid Blue]
194198 debugColors = [SetConsoleIntensity FaintIntensity]
195199
196200-- | Allow the user to immediately switch to verbose logging upon pressing a particular key.
lib/command/src/Obelisk/Command/Project.hs+32−29
@@ -60,7 +60,7 @@ import Text.ShellEscape (bash, bytes)
6060import GitHub.Data.GitData (Branch)
6161import GitHub.Data.Name (Name)
6262
63−import Obelisk.App (MonadObelisk)
63+import Obelisk.App (MonadObelisk, runObelisk, getObelisk)
6464import Obelisk.CliApp
6565import Obelisk.Command.Nix
6666import Obelisk.Command.Thunk
@@ -374,9 +374,7 @@ findProjectAssets root = do
374374 -- Check whether the impure static files are a derivation (and so must be built)
375375 if isDerivation == "1"
376376 then do
377− _ <- readProcessAndLogStderr Debug $ setCwd (Just root) $
378− proc nixBuildExePath staticFilesDrvSymlinkArgs
379−
377+ _ <- buildStaticFilesDerivationAndSymlink root
380378 pure (AssetSource_Derivation, T.pack $ root </> "static.out")
381379 else fmap (AssetSource_Files,) $ readProcessAndLogStderr Debug $ setCwd (Just root) $
382380 proc nixExePath ["eval", "-f", ".", "passthru.staticFilesImpure", "--raw"]
@@ -390,42 +388,47 @@ getHaskellManifestProjectPath root = fmap T.strip $ readProcessAndLogStderr Debu
390388 , "(let a = import ./. {}; in a.passthru.processedStatic.haskellManifest)"
391389 ]
392390
393−staticFilesDrvSymlinkArgs :: [String]
394−staticFilesDrvSymlinkArgs =
395− [ "-o", "static.out"
396− , "-E", "(import ./. {}).passthru.staticFilesImpure"
397− ]
398−
399391-- | Watch the project directory for file changes and check whether those file changes
400392-- cause changes in the static files nix derivation. If so, rebuild it.
401393watchStaticFilesDerivation
402− :: MonadObelisk m
394+ :: (MonadIO m, MonadObelisk m)
403395 => FilePath
404396 -> m ()
405397watchStaticFilesDerivation root = do
406− cfg <- getCliConfig
407− drv0 <- liftIO runShowDrv
398+ ob <- getObelisk
399+ drv0 <- showDerivation
408400 liftIO $ runHeadlessApp $ do
409401 pb <- getPostBuild
410402 checkForChanges <- watchDirectoryTree defaultConfig (root <$ pb) (const True)
411− drv <- performEvent $ liftIO runShowDrv <$ checkForChanges
403+ drv <- performEvent $ ffor checkForChanges $ \_ ->
404+ liftIO $ runObelisk ob showDerivation
412405 drvs <- foldDyn (\new (_, old, _) -> (old, new, old /= new)) (drv0, drv0, False) drv
413406 out <- throttleBatchWithLag
414− (\e -> performEvent $ ffor e $ \_ -> liftIO $ void runDrvBuild)
407+ (\e -> performEvent $ ffor e $ \_ -> liftIO $ runObelisk ob $
408+ buildStaticFilesDerivationAndSymlink root >> pure ())
415409 (void $ ffilter (^._3) $ updated drvs)
416− performEvent_ $ ffor out $ \_ ->
417− runCli cfg $
418− putLog Informational "Static assets rebuilt and symlinked to static.out"
410+ performEvent_ $ ffor out $ \_ -> runObelisk ob $
411+ putLog Notice "Static assets rebuilt and symlinked to static.out"
419412 pure never
420413 where
421− showDrv :: Proc.CreateProcess
422− showDrv = (Proc.proc nixExePath
423− [ "show-derivation"
424− , "-f", "."
425− , "passthru.staticFilesImpure"
426− ]) { Proc.cwd = Just root }
427− runShowDrv :: IO String
428− runShowDrv = Proc.readCreateProcess showDrv ""
429− runDrvBuild :: IO String
430− runDrvBuild = Proc.readCreateProcess
431− ((Proc.proc nixBuildExePath staticFilesDrvSymlinkArgs) { Proc.cwd = Just root }) ""
414+ showDerivation :: MonadObelisk m => m Text
415+ showDerivation = readProcessAndLogStderr Debug $
416+ setCwd (Just root) $ ProcessSpec
417+ { _processSpec_createProcess = Proc.proc nixExePath
418+ [ "show-derivation"
419+ , "-f", "."
420+ , "passthru.staticFilesImpure"
421+ ]
422+ , _processSpec_overrideEnv = Nothing
423+ }
424+
425+buildStaticFilesDerivationAndSymlink :: MonadObelisk m => FilePath -> m Text
426+buildStaticFilesDerivationAndSymlink root = readProcessAndLogStderr Debug $
427+ setCwd (Just root) $ ProcessSpec
428+ { _processSpec_createProcess = Proc.proc
429+ nixBuildExePath
430+ [ "-o", "static.out"
431+ , "-E", "(import ./. {}).passthru.staticFilesImpure"
432+ ]
433+ , _processSpec_overrideEnv = Nothing
434+ }