Skip to content

Commit

Permalink
search parent dirs for workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbecich committed Jul 7, 2024
1 parent f130b33 commit 2eb0594
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Spago/Config.purs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
doMigrateConfig "spago.yaml" config
pure { workspace, package, workspaceDoc: doc }

logDebug "Gathering all the spago configs in the tree..."
otherConfigPaths <- liftAff $ Glob.gitignoringGlob Paths.cwd [ "**/spago.yaml" ]
unless (Array.null otherConfigPaths) do
logDebug $ [ toDoc "Found packages at these paths:", Log.indent $ Log.lines (map toDoc otherConfigPaths) ]
logDebug "Gathering all the spago configs lower in the tree..."
otherLowerConfigPaths <- liftAff $ Glob.gitignoringGlob Paths.cwd [ "**/spago.yaml" ]
unless (Array.null otherLowerConfigPaths) do
logDebug $ [ toDoc "Found packages at these lower paths:", Log.indent $ Log.lines (map toDoc otherLowerConfigPaths) ]

-- We read all of them in, and only read the package section, if any.
let
Expand All @@ -220,7 +220,7 @@ readWorkspace { maybeSelectedPackage, pureBuild, migrateConfig } = do
Right config -> do
Right { config, hasTests, configPath: path, packagePath: Path.dirname path }

{ right: otherPackages, left: failedPackages } <- partitionMap identity <$> traverse readWorkspaceConfig otherConfigPaths
{ right: otherPackages, left: failedPackages } <- partitionMap identity <$> traverse readWorkspaceConfig otherLowerConfigPaths
unless (Array.null failedPackages) do
logWarn $ [ toDoc "Failed to read some configs:" ] <> failedPackages

Expand Down
13 changes: 13 additions & 0 deletions src/Spago/Paths.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Effect.Unsafe (unsafePerformEffect)
import Node.Path (FilePath)
import Node.Path as Path
import Node.Process as Process
import Data.Array (cons, replicate)
import Data.String (joinWith)

type NodePaths =
{ config :: FilePath
Expand Down Expand Up @@ -38,6 +40,17 @@ toLocalCachePath rootDir = Path.concat [ rootDir, ".spago" ]
toLocalCachePackagesPath :: FilePath -> FilePath
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 = 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 i | i > 0 = cons (makeSearchPath wd i) (makeSearchPaths wd (i - 1))
makeSearchPaths _ _ = mempty

registryPath FilePath
registryPath = Path.concat [ globalCachePath, "registry" ]

Expand Down
20 changes: 20 additions & 0 deletions test/Spago/Paths.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Test.Spago.Paths where

import Test.Prelude

import Test.Spec (Spec)
import Test.Spec as Spec
import Test.Spec.Assertions as Assert

import Spago.Paths (toGitSearchPath)

spec :: Spec Unit
spec = Spec.around withTempDir do
Spec.describe "paths" do
Spec.it "generate four paths to parent directories" \ _ -> do
toGitSearchPath "~/a/b/c/d/e" `Assert.shouldEqual`
[ "~/a/b/c/d/e"
, "~/a/b/c/d/e/.."
, "~/a/b/c/d/e/../.."
, "~/a/b/c/d/e/../../../"
]

0 comments on commit 2eb0594

Please sign in to comment.