on master help sign in
Merge pull request #294 from obsidiansystems/pure-assets-lib

Prevent impure path from polluting builds

commitd5db37ccaaa818b5d1cb1cc10ed4b03ac9b40259 authorLuigy Leon <luichi.19@gmail.com> dateThu 1 Nov 2018, 16:59 (8 years ago) parent3ec4f82ce5e38b
5 files changed+26−19
default.nix+3−2 lib/asset/manifest/src-bin/generate.hs+1−2 lib/asset/manifest/src/Obelisk/Asset/Promoted.hs+1−9 lib/command/src/Obelisk/Command/Project.hs+1−1 lib/command/src/Obelisk/Command/Run.hs+20−5
default.nix+3−2
@@ -152,7 +152,7 @@ in rec {
152152 set -euo pipefail
153153 touch "$out"
154154 mkdir -p "$symlinked"
155− obelisk-asset-manifest-generate "$src" "${builtins.toString src}" "$haskellManifest" ${packageName} ${moduleName} "$symlinked"
155+ obelisk-asset-manifest-generate "$src" "$haskellManifest" ${packageName} ${moduleName} "$symlinked"
156156 '';
157157
158158 compressedJs = frontend: optimizationLevel: pkgs.runCommand "compressedJs" { buildInputs = [ pkgs.closurecompiler ]; } ''
@@ -273,6 +273,7 @@ in rec {
273273 backendName = "backend";
274274 commonName = "common";
275275 staticName = "obelisk-generated-static";
276+ staticFilesImpure = if lib.isDerivation staticFiles then staticFiles else toString staticFiles;
276277 processedStatic = processAssets { src = staticFiles; };
277278 # The packages whose names and roles are defined by this package
278279 predefinedPackages = lib.filterAttrs (_: x: x != null) {
@@ -324,7 +325,7 @@ in rec {
324325 nixpkgs.obeliskExecutableConfig.platforms.ios.inject injectableConfig processedStatic.symlinked;
325326 } // ios;
326327 };
327− passthru = { inherit android ios packages overrides tools shellToolOverrides withHoogle staticFiles __closureCompilerOptimizationLevel; };
328+ passthru = { inherit android ios packages overrides tools shellToolOverrides withHoogle staticFiles staticFilesImpure __closureCompilerOptimizationLevel; };
328329 };
329330 in mkProject (projectDefinition args));
330331 serverOn = sys: config: version: serverExe
lib/asset/manifest/src-bin/generate.hs+1−2
@@ -7,11 +7,10 @@ import System.Environment
77main :: IO ()
88main = do
99 --TODO: Usage
10− [root, rootLocalPath, haskellTarget, packageName, moduleName, fileTarget] <- getArgs
10+ [root, haskellTarget, packageName, moduleName, fileTarget] <- getArgs
1111 paths <- gatherHashedPaths root
1212 writeStaticProject paths haskellTarget $ StaticConfig
1313 { _staticConfig_packageName = T.pack packageName
1414 , _staticConfig_moduleName = T.pack moduleName
15− , _staticConfig_rootPath = rootLocalPath
1615 }
1716 copyAndSymlink paths root fileTarget
lib/asset/manifest/src/Obelisk/Asset/Promoted.hs+1−9
@@ -28,7 +28,6 @@ import qualified Data.Map as Map
2828data StaticConfig = StaticConfig
2929 { _staticConfig_packageName :: Text --TODO: Better type
3030 , _staticConfig_moduleName :: Text --TODO: Better type
31− , _staticConfig_rootPath :: FilePath
3231 }
3332
3433writeStaticProject :: Map FilePath FilePath -> FilePath -> StaticConfig -> IO ()
@@ -41,14 +40,7 @@ writeStaticProject paths target cfg = do
4140 Just (name, parents) -> (name, target </> "src" </> T.unpack (T.intercalate "/" $ reverse parents))
4241 createDirectoryIfMissing True moduleDirPath
4342 modContents <- staticModuleFile modName paths
44− T.writeFile (moduleDirPath </> T.unpack modName' <.> "hs") $ appendRootPath (_staticConfig_rootPath cfg) modContents
45−
46−appendRootPath :: FilePath -> Text -> Text
47−appendRootPath fp txt = T.unlines
48− [ txt
49− , "staticRootPath :: FilePath"
50− , "staticRootPath = \"" <> T.pack fp <> "\""
51− ]
43+ T.writeFile (moduleDirPath </> T.unpack modName' <.> "hs") modContents
5244
5345staticCabalFile :: StaticConfig -> Text
5446staticCabalFile cfg = T.unlines
lib/command/src/Obelisk/Command/Project.hs+1−1
@@ -145,7 +145,7 @@ findProjectRoot target = do
145145 (result, _) <- liftIO $ runStateT (walkToProjectRoot target targetStat myUid) []
146146 return result
147147
148−withProjectRoot :: MonadObelisk m => FilePath -> (FilePath -> m ()) -> m ()
148+withProjectRoot :: MonadObelisk m => FilePath -> (FilePath -> m a) -> m a
149149withProjectRoot target f = findProjectRoot target >>= \case
150150 Nothing -> failWith "Must be used inside of an Obelisk project"
151151 Just root -> f root
lib/command/src/Obelisk/Command/Run.hs+20−5
@@ -26,16 +26,19 @@ import Hpack.Config
2626import Hpack.Render
2727import Hpack.Yaml
2828import Language.Haskell.Extension
29−import Network.Socket
29+import Network.Socket hiding (Debug)
3030import System.Directory
3131import System.FilePath
32+import System.Process (proc)
3233import System.IO.Temp (withSystemTempDirectory)
3334import Data.ByteString (ByteString)
3435
3536import Obelisk.App (MonadObelisk, ObeliskT)
36−import Obelisk.CliApp (CliT (..), HasCliConfig, Severity (..), callCommand, failWith, getCliConfig, putLog,
37− runCli)
38−import Obelisk.Command.Project (inProjectShell)
37+import Obelisk.CliApp
38+ ( CliT (..), HasCliConfig, Severity (..)
39+ , callCommand, failWith, getCliConfig, putLog
40+ , readProcessAndLogStderr, runCli)
41+import Obelisk.Command.Project (inProjectShell, withProjectRoot)
3942
4043data CabalPackageInfo = CabalPackageInfo
4144 { _cabalPackageInfo_packageRoot :: FilePath
@@ -51,7 +54,19 @@ run = do
5154 pkgs <- getLocalPkgs
5255 withGhciScript pkgs $ \dotGhciPath -> do
5356 freePort <- getFreePort
54− runGhcid dotGhciPath $ Just $ unwords ["run", show freePort, "(runServeAsset Obelisk.Generated.Static.staticRootPath)", "Backend.backend", "Frontend.frontend"]
57+ assets <- withProjectRoot "." $ \root ->
58+ -- `--raw` is not available with old nix-instantiate. It drops quotation
59+ -- marks and trailing newline, so is very convenient for shelling out.
60+ readProcessAndLogStderr Debug $
61+ proc "nix" ["eval", "-f", root, "passthru.staticFilesImpure", "--raw"]
62+ putLog Debug $ "Assets impurely loaded from: " <> assets
63+ runGhcid dotGhciPath $ Just $ unwords
64+ [ "run"
65+ , show freePort
66+ , "(runServeAsset " ++ show assets ++ ")"
67+ , "Backend.backend"
68+ , "Frontend.frontend"
69+ ]
5570
5671runRepl :: MonadObelisk m => m ()
5772runRepl = do