on master help sign in
Merge pull request #652 from obsidiansystems/eac@fix-nested-ob-filter

command: Properly filter out nested obelisk projects

commitcb654e4184b1b0fcaa5de1b995c938423a3ca82b authorElliot Cameron <3noch@users.noreply.github.com> dateTue 11 Feb 2020, 08:58 (7 years ago) parenta11eceff3a56d1
3 files changed+13−29
lib/command/obelisk-command.cabal−1 lib/command/src/Obelisk/Command/Run.hs+13−16 lib/selftest/src/Obelisk/SelfTest.hs−12
lib/command/obelisk-command.cabal−1
@@ -20,7 +20,6 @@ library
2020 , either
2121 , errors
2222 , exceptions
23− , extra
2423 , filepath
2524 , git
2625 , github
lib/command/src/Obelisk/Command/Run.hs+13−16
@@ -15,7 +15,6 @@ import Control.Monad.Reader (MonadIO)
1515import Data.Coerce (coerce)
1616import Data.Either
1717import Data.Foldable (for_, toList)
18−import Data.List.Extra (dropPrefix)
1918import qualified Data.List.NonEmpty as NE
2019import Data.Map (Map)
2120import qualified Data.Map as Map
@@ -48,8 +47,7 @@ import System.IO.Temp (withSystemTempDirectory)
4847
4948import Obelisk.App (MonadObelisk)
5049import Obelisk.CliApp (Severity (..) , failWith, putLog, proc, readCreateProcessWithExitCode, readProcessAndLogStderr)
51−import Obelisk.Command.Project (obeliskDirName, toObeliskDir, withProjectRoot, nixShellWithPkgs, toNixPath)
52−import Obelisk.Command.Thunk (attrCacheFileName)
50+import Obelisk.Command.Project (withProjectRoot, nixShellWithPkgs, toNixPath)
5351import Obelisk.Command.Utils (findExePath, ghcidExePath, nixBuildExePath, nixExePath)
5452
5553data CabalPackageInfo = CabalPackageInfo
@@ -125,20 +123,19 @@ runWatch = withProjectRoot "." $ \root -> do
125123-- to the Nix @project@ function.
126124getLocalPkgs :: MonadObelisk m => FilePath -> m [FilePath]
127125getLocalPkgs root = do
128− (_exitCode, out, err) <- readCreateProcessWithExitCode $
129− proc findExePath ["-L", root, "(", "-name", "*.cabal", "-o", "-name", Hpack.packageConfig, ")", "-a", "-type", "f"]
130− putLog Debug $ T.strip $ T.pack err
126+ obeliskPaths <- runFind ["-L", root, "-name", ".obelisk", "-type", "d"]
131127
132− let
133− -- We ignore any path that has ".obelisk" in it, but keep the root ".obelisk" paths
134− packagePaths = filter (not . isIgnored) $ map T.unpack $ T.lines $ T.strip $ T.pack out
135− isIgnored path =
136− obeliskDirName `elem` dropPrefix (splitPath $ toObeliskDir root) pathSegments
137− && attrCacheFileName `notElem` pathSegments
138− where
139− pathSegments = splitPath path
140−
141− pure packagePaths
128+ -- We do not want to find packages that are embedded inside other obelisk projects, unless that
129+ -- obelisk project is our own.
130+ let exclusions = filter (/= root) $ map takeDirectory obeliskPaths
131+ runFind $
132+ ["-L", root, "(", "-name", "*.cabal", "-o", "-name", Hpack.packageConfig, ")", "-a", "-type", "f"]
133+ <> concat [["-not", "-path", p </> "*"] | p <- exclusions]
134+ where
135+ runFind args = do
136+ (_exitCode, out, err) <- readCreateProcessWithExitCode $ proc findExePath args
137+ putLog Debug $ T.strip $ T.pack err
138+ pure $ map T.unpack $ T.lines $ T.strip $ T.pack out
142139
143140data GuessPackageFileError = GuessPackageFileError_Ambiguous [FilePath] | GuessPackageFileError_NotFound
144141 deriving (Eq, Ord, Show)
lib/selftest/src/Obelisk/SelfTest.hs−12
@@ -27,7 +27,6 @@ import Shelly hiding (cp)
2727import System.Directory (getCurrentDirectory, withCurrentDirectory, getDirectoryContents)
2828import System.Environment
2929import System.Exit (ExitCode (..))
30−import System.FilePath (replaceBaseName, takeBaseName)
3130import System.Which (staticWhich)
3231import qualified System.Info
3332import System.IO (Handle, hClose, hIsEOF, hGetContents)
@@ -217,9 +216,6 @@ main = do
217216 describe "ob run" $ {- NOT parallel $ -} do
218217 it "works in root directory" $ inTmpObInit $ \_ -> testObRunInDir' Nothing httpManager
219218 it "works in sub directory" $ inTmpObInit $ \_ -> testObRunInDir' (Just "frontend") httpManager
220− it "works with differently named cabal files" $ inTmpObInit $ \_ -> do
221− changeCabalPackageName "backend/backend.cabal" "new-backend"
222− testObRunInDir' Nothing httpManager
223219
224220 describe "obelisk project" $ parallel $ do
225221 it "can build obelisk command" $ inTmpObInit $ \_ -> nixBuild ["-A", "command" , toTextIgnore obeliskImplDirty]
@@ -426,14 +422,6 @@ obRunCheck httpManager _stdout frontendUri = do
426422 req frontendUri >>= \r -> when (HTTP.responseStatus r /= HTTP.ok200) $ errorExit $
427423 "Request to frontend server failed: " <> T.pack (show r)
428424
429−-- | Rename a cabal file and do a really dumb, brittle search/replace in its content to update the name.
430−changeCabalPackageName :: FilePath -> Text -> Sh ()
431−changeCabalPackageName cabalFile newName = do
432− contents <- readfile cabalFile
433− writefile (replaceBaseName cabalFile (T.unpack newName)) $
434− T.replace (" " <> T.pack (takeBaseName cabalFile)) (" " <> newName) contents -- WARNING: Super brittle
435− rm cabalFile
436−
437425getFreePorts :: Int -> IO [Socket.PortNumber]
438426getFreePorts 0 = pure []
439427getFreePorts n = Socket.withSocketsDo $ do