@@ -1,9 +1,6 @@
11{-# LANGUAGE FlexibleContexts #-}
22{-# LANGUAGE LambdaCase #-}
33{-# LANGUAGE OverloadedStrings #-}
4−{-# LANGUAGE ScopedTypeVariables #-}
5−{-# LANGUAGE TupleSections #-}
6−
74module Obelisk.Command.Project
85 ( InitSource (..)
96 , findProjectObeliskCommand
@@ -19,18 +16,12 @@ module Obelisk.Command.Project
1916 , toObeliskDir
2017 , withProjectRoot
2118 , bashEscape
22− , getHaskellManifestProjectPath
23− , AssetSource(..)
24− , describeImpureAssetSource
25− , watchStaticFilesDerivation
2619 ) where
2720
2821import Control.Concurrent.MVar (MVar, newMVar, withMVarMasked)
29−import Control.Lens ((.~), (?~), (<&>), (^.), _2, _3)
22+import Control.Lens ((.~), (?~), (<&>))
3023import Control.Monad
31−import Control.Monad.Except
3224import Control.Monad.IO.Class (liftIO)
33−import Control.Monad.Log
3425import Control.Monad.State
3526import qualified Data.Aeson as Json
3627import qualified Data.ByteString.UTF8 as BSU
@@ -39,32 +30,24 @@ import qualified Data.ByteString.Lazy as BSL
3930import Data.Default (def)
4031import Data.Function ((&), on)
4132import Data.Map (Map)
42−import Data.Maybe (isJust)
4333import Data.Text (Text)
4434import qualified Data.Text as T
4535import Data.Text.Encoding (decodeUtf8, encodeUtf8)
4636import Data.Traversable (for)
47−import Reflex
48−import Reflex.FSNotify
49−import Reflex.Host.Headless
5037import System.Directory
5138import System.Environment (lookupEnv)
52−import System.Exit (ExitCode(..))
5339import System.FilePath
54−import System.FSNotify (defaultConfig, eventPath, WatchConfig(..))
55−import qualified System.Info as SysInfo
5640import System.IO.Temp
5741import System.IO.Unsafe (unsafePerformIO)
5842import System.PosixCompat.Files
5943import System.PosixCompat.Types
6044import System.PosixCompat.User
61−import qualified System.Process as Proc
6245import Text.ShellEscape (bash, bytes)
6346
6447import GitHub.Data.GitData (Branch)
6548import GitHub.Data.Name (Name)
6649
67−import Obelisk.App (MonadObelisk, runObelisk, getObelisk)
50+import Obelisk.App (MonadObelisk)
6851import Obelisk.CliApp
6952import Obelisk.Command.Nix
7053import Obelisk.Command.Thunk
@@ -351,21 +334,7 @@ nixShellWithHoogle root isPure shell' command = do
351334 \})).project.shells.${shell}"
352335 & nixShellConfig_common . nixCmdConfig_args .~ [ strArg "shell" shell' ]
353336
354−-- | Describes the provenance of static assets (i.e., are they the result of a derivation
355−-- that was built, or just a folder full of files.
356−data AssetSource = AssetSource_Derivation
357− | AssetSource_Files
358− deriving (Eq)
359−
360−-- | Some log messages to make it easier to tell where static files are coming from
361−describeImpureAssetSource :: AssetSource -> Text -> Text
362−describeImpureAssetSource src path = case src of
363− AssetSource_Files -> "Assets impurely loaded from: " <> path
364− AssetSource_Derivation -> "Assets derivation built and impurely loaded from: " <> path
365−
366−-- | Determine where the static files of a project are and whether they're plain files or a derivation.
367−-- If they are a derivation, that derivation will be built.
368−findProjectAssets :: MonadObelisk m => FilePath -> m (AssetSource, Text)
337+findProjectAssets :: MonadObelisk m => FilePath -> m Text
369338findProjectAssets root = do
370339 isDerivation <- readProcessAndLogStderr Debug $ setCwd (Just root) $
371340 proc nixExePath
@@ -377,91 +346,10 @@ findProjectAssets root = do
377346 ]
378347 -- Check whether the impure static files are a derivation (and so must be built)
379348 if isDerivation == "1"
380− then do
381− _ <- buildStaticFilesDerivationAndSymlink
382− (readProcessAndLogStderr Debug)
383− root
384− pure (AssetSource_Derivation, T.pack $ root </> "static.out")
385− else fmap (AssetSource_Files,) $ readProcessAndLogStderr Debug $ setCwd (Just root) $
386− proc nixExePath ["eval", "-f", ".", "passthru.staticFilesImpure", "--raw"]
387−
388−-- | Get the nix store path to the generated static asset manifest module (e.g., "obelisk-generated-static")
389−getHaskellManifestProjectPath :: MonadObelisk m => FilePath -> m Text
390−getHaskellManifestProjectPath root = fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $
391− proc nixBuildExePath
392− [ "--no-out-link"
393− , "-E"
394− , "(let a = import ./. {}; in a.passthru.processedStatic.haskellManifest)"
395− ]
396−
397−-- | Watch the project directory for file changes and check whether those file changes
398−-- cause changes in the static files nix derivation. If so, rebuild it.
399−watchStaticFilesDerivation
400− :: (MonadIO m, MonadObelisk m)
401− => FilePath
402− -> m ()
403−watchStaticFilesDerivation root = do
404− ob <- getObelisk
405− drv0 <- showDerivation
406− liftIO $ runHeadlessApp $ do
407− pb <- getPostBuild
408− checkForChanges <- batchOccurrences 0.25 =<< watchDirectoryTree
409− -- On macOS, use the polling backend due to https://github.com/luite/hfsevents/issues/13
410− (defaultConfig { confUsePolling = SysInfo.os == "darwin", confPollInterval = 250000 })
411− (root <$ pb)
412− ((/="static.out") . takeFileName . eventPath)
413− drv <- performEvent $ ffor checkForChanges $ \_ ->
414− liftIO $ runObelisk ob showDerivation
415− drvs <- foldDyn (\new (_, old, _) -> (old, new, old /= new)) (drv0, drv0, False) drv
416− void $ throttleBatchWithLag
417− (\e -> performEvent $ ffor e $ \_ -> liftIO $ runObelisk ob $ do
418− putLog Notice "Static assets being built..."
419− buildStaticCatchErrors >>= \case
420− Nothing -> pure ()
421− Just _ -> putLog Notice "Static assets built and symlinked to static.out"
422− )
423− ((() <$) . ffilter (\x -> isJust (x ^._2) && x ^._3) $ updated drvs)
424− pure never
425− where
426− handleBuildFailure
427− :: MonadObelisk m
428− => (ExitCode, String, String)
429− -> m (Maybe Text)
430− handleBuildFailure (ex, out, err) = case ex of
431− ExitSuccess -> pure $ Just $ T.pack out
432− _ -> do
433− putLog Error $
434− ("Static assets build failed: " <>) $
435− T.unlines $ reverse $ take 10 $ reverse $ T.lines $ T.pack err
436− pure Nothing
437− showDerivation :: MonadObelisk m => m (Maybe Text)
438− showDerivation =
439− handleBuildFailure <=< readCreateProcessWithExitCode $
440− setCwd (Just root) $ ProcessSpec
441− { _processSpec_createProcess = Proc.proc nixExePath
442− [ "show-derivation"
443− , "-f", "."
444− , "passthru.staticFilesImpure"
445− ]
446− , _processSpec_overrideEnv = Nothing
447− }
448− buildStaticCatchErrors :: MonadObelisk m => m (Maybe Text)
449− buildStaticCatchErrors = handleBuildFailure =<<
450− buildStaticFilesDerivationAndSymlink
451− readCreateProcessWithExitCode
452− root
453−
454−buildStaticFilesDerivationAndSymlink
455− :: MonadObelisk m
456− => (ProcessSpec -> m a)
457− -> FilePath
458− -> m a
459−buildStaticFilesDerivationAndSymlink f root = f $
460− setCwd (Just root) $ ProcessSpec
461− { _processSpec_createProcess = Proc.proc
462− nixBuildExePath
463− [ "-o", "static.out"
349+ then fmap T.strip $ readProcessAndLogStderr Debug $ setCwd (Just root) $ -- Strip whitespace here because nix-build has no --raw option
350+ proc nixBuildExePath
351+ [ "--no-out-link"
464352 , "-E", "(import ./. {}).passthru.staticFilesImpure"
465353 ]
466− , _processSpec_overrideEnv = Nothing
467− }
354+ else readProcessAndLogStderr Debug $ setCwd (Just root) $
355+ proc nixExePath ["eval", "-f", ".", "passthru.staticFilesImpure", "--raw"]