Skip to content

Commit

Permalink
successfully finds and uses workspace in parent directory
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbecich committed Jul 21, 2024
1 parent 00d3951 commit c69cd06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
38 changes: 23 additions & 15 deletions src/Spago/Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Data.Enum as Enum
import Data.Graph as Graph
import Data.HTTP.Method as Method
import Data.Int as Int
import Data.List (List(..), (:))
import Data.List (List(..))
import Data.Map as Map
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
Expand All @@ -48,7 +48,6 @@ import Data.Set.NonEmpty (NonEmptySet)
import Data.Set.NonEmpty as NonEmptySet
import Data.String (CodePoint, Pattern(..))
import Data.String as String
import Data.Traversable (sequence)
import Dodo as Log
import Effect.Aff as Aff
import Effect.Uncurried (EffectFn2, EffectFn3, runEffectFn2, runEffectFn3)
Expand Down Expand Up @@ -201,27 +200,30 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
checkForWorkspace :: forall a. FilePath
-> Spago (LogEnv a) (Maybe PrelimWorkspace)
checkForWorkspace config = do
result <- readConfig config
logInfo $ "Checking for workspace: " <> config
result <- map (map (\y -> y.yaml)) $ readConfig config
case result of
Left _ -> pure Nothing
Right { yaml: { workspace: Nothing } } -> pure Nothing
Right { yaml: { workspace: Just ws } } -> pure (Just ws)
Right { workspace: Nothing } -> pure Nothing
Right { workspace: Just ws } -> pure (Just ws)

searchHigherPaths :: forall a. List FilePath -> Spago (LogEnv a) (Maybe PrelimWorkspace)
searchHigherPaths :: forall a. List FilePath -> Spago (LogEnv a) (Maybe (Tuple FilePath PrelimWorkspace))
searchHigherPaths Nil = pure Nothing
searchHigherPaths (path : otherPaths) = do
mYaml :: Maybe String <- map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
-- TODO stop searching if .git is found, this is the root
logInfo $ "Searching " <> path
mYaml :: Maybe String <- map (map (\yml -> path <> yml)) $ map Array.head $ liftAff $ Glob.gitignoringGlob path [ "./spago.yaml" ]
case mYaml of
Nothing -> searchHigherPaths otherPaths
Just foundSpagoYaml -> do
mWorkspace :: Maybe PrelimWorkspace <- checkForWorkspace foundSpagoYaml
case mWorkspace of
Nothing -> searchHigherPaths otherPaths
workspace -> pure workspace
Just ws -> pure (pure (Tuple foundSpagoYaml ws))

mHigherConfigPath <- searchHigherPaths higherPaths
for_ mHigherConfigPath $ \higherConfigPath -> do
logDebug $ [ toDoc "Found workspace at higher path:" ]
mHigherWorkspace <- searchHigherPaths higherPaths
for_ mHigherWorkspace $ \(Tuple filepath _) ->
logInfo $ "Found workspace definition in " <> filepath

-- First try to read the config in the root. It _has_ to contain a workspace
-- configuration, or we fail early.
Expand All @@ -234,10 +236,16 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
, Log.break
, toDoc "The configuration file help can be found here https://github.com/purescript/spago#the-configuration-file"
]
Right { yaml: { workspace: Nothing } } -> die
[ "Your spago.yaml doesn't contain a workspace section."
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
]
Right config@{ yaml: { workspace: Nothing, package }, doc } -> case mHigherWorkspace of
Nothing ->
die
[ "No workspace definition found in this spago.yaml or any spago.yaml in parent directory."
, "See the relevant documentation here: https://github.com/purescript/spago#the-workspace"
]
Just (Tuple _ higherWorkspace) -> do
-- TODO migrate workspace at higher directory?
doMigrateConfig "spago.yaml" config
pure { workspace: higherWorkspace, package, workspaceDoc: doc }
Right config@{ yaml: { workspace: Just workspace, package }, doc } -> do
doMigrateConfig "spago.yaml" config
pure { workspace, package, workspaceDoc: doc }
Expand Down
4 changes: 2 additions & 2 deletions src/Spago/Paths.purs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ toLocalCachePackagesPath rootDir = Path.concat [ toLocalCachePath rootDir, "p" ]

-- search maximum 4 levels up the tree to find the Git project, if it exists
toGitSearchPath :: FilePath -> Array FilePath
toGitSearchPath rootDir = reverse $ makeSearchPaths rootDir 2 where
toGitSearchPath rootDir = reverse $ makeSearchPaths rootDir 4 where
makeSearchPath :: FilePath -> Int -> FilePath
makeSearchPath wd i = joinWith "" $ cons wd $ cons "/" $ replicate i "../"

makeSearchPaths :: FilePath -> Int -> Array FilePath
makeSearchPaths wd 0 = pure wd
makeSearchPaths wd 0 = pure (wd <> "/")
makeSearchPaths wd i | i > 0 = cons (makeSearchPath wd i) (makeSearchPaths wd (i - 1))
makeSearchPaths _ _ = mempty

Expand Down

0 comments on commit c69cd06

Please sign in to comment.