Skip to content

Commit

Permalink
Add ls path (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez authored Oct 19, 2023
1 parent 9543d4b commit 552160a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
13 changes: 11 additions & 2 deletions bin/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Spago.Command.Fetch as Fetch
import Spago.Command.Graph (GraphModulesArgs, GraphPackagesArgs)
import Spago.Command.Graph as Graph
import Spago.Command.Init as Init
import Spago.Command.Ls (LsDepsArgs, LsPackagesArgs)
import Spago.Command.Ls (LsPathsArgs, LsDepsArgs, LsPackagesArgs)
import Spago.Command.Ls as Ls
import Spago.Command.Publish as Publish
import Spago.Command.Registry (RegistryInfoArgs, RegistrySearchArgs, RegistryPackageSetsArgs)
Expand Down Expand Up @@ -178,6 +178,7 @@ data Command a
| Fetch FetchArgs
| Init InitArgs
| Install InstallArgs
| LsPaths LsPathsArgs
| LsDeps LsDepsArgs
| LsPackages LsPackagesArgs
| Publish PublishArgs
Expand Down Expand Up @@ -230,6 +231,7 @@ argParser =
( O.hsubparser $ Foldable.fold
[ commandParser "packages" (LsPackages <$> lsPackagesArgsParser) "List packages available in the local package set"
, commandParser "deps" (LsDeps <$> lsDepsArgsParser) "List dependencies of the project"
, commandParser "paths" (LsPaths <$> lsPathsArgsParser) "List the paths used by Spago"
]
)
(O.progDesc "List packages or dependencies")
Expand Down Expand Up @@ -433,6 +435,11 @@ graphPackagesArgsParser = Optparse.fromRecord
, topo: Flags.topo
}

lsPathsArgsParser :: Parser LsPathsArgs
lsPathsArgsParser = Optparse.fromRecord
{ json: Flags.json
}

lsPackagesArgsParser :: Parser LsPackagesArgs
lsPackagesArgsParser = Optparse.fromRecord
{ json: Flags.json
Expand Down Expand Up @@ -599,6 +606,8 @@ main =
runSpago buildEnv (Build.run options)
testEnv <- runSpago env (mkTestEnv args buildEnv)
runSpago testEnv Test.run
LsPaths args -> do
runSpago { logOptions } $ Ls.listPaths args
LsPackages args -> do
let fetchArgs = { packages: mempty, selectedPackage: Nothing, ensureRanges: false, testDeps: false }
{ env: env@{ workspace }, fetchOpts } <- mkFetchEnv offline fetchArgs
Expand Down Expand Up @@ -960,7 +969,7 @@ mkRegistryEnv offline = do

-- Now that we are up to date with the Registry we init/refresh the database
db <- liftEffect $ Db.connect
{ database: Db.databasePath
{ database: Paths.databasePath
, logger: \str -> Reader.runReaderT (logDebug $ "DB: " <> str) { logOptions }
}
Registry.updatePackageSetsDb db
Expand Down
40 changes: 38 additions & 2 deletions src/Spago/Command/Ls.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
module Spago.Command.Ls (listPackages, listPackageSet, LsEnv(..), LsDepsArgs, LsPackagesArgs) where
module Spago.Command.Ls
( listPaths
, listPackages
, listPackageSet
, LsEnv(..)
, LsPathsArgs
, LsDepsArgs
, LsPackagesArgs
) where

import Spago.Prelude

import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Common as CAC
import Data.Codec.Argonaut.Record as CAR
import Data.Foldable (elem)
import Data.Foldable (elem, traverse_)
import Data.Map (filterKeys)
import Data.Map as Map
import Data.Tuple.Nested (type (/\))
Expand All @@ -15,6 +24,7 @@ import Registry.Version as Version
import Spago.Command.Fetch as Fetch
import Spago.Config (Package(..), PackageSet(..), Workspace, WorkspacePackage)
import Spago.Config as Config
import Spago.Paths as Paths
import Type.Proxy (Proxy(..))

type LsPackagesArgs =
Expand All @@ -32,6 +42,10 @@ type LsDepsOpts =
, transitive :: Boolean
}

type LsPathsArgs =
{ json :: Boolean
}

type LsSetEnv =
{ dependencies :: Fetch.PackageTransitiveDeps
, logOptions :: LogOptions
Expand All @@ -45,6 +59,28 @@ type LsEnv =
, selected :: WorkspacePackage
}

listPaths :: LsPathsArgs -> Spago { logOptions :: LogOptions } Unit
listPaths { json } = do
logDebug "Running `listPaths`"
case json of
true ->
output $ OutputJson (CAC.map CA.string CA.string) $ Map.fromFoldable keyValuePairs
false ->
output $ OutputTable
{ titles: [ "Name", "Path" ]
, rows: (\(Tuple k v) -> [ k, v ]) <$> keyValuePairs
}
where
keyValuePairs =
[ Tuple "Global cache path" Paths.globalCachePath
, Tuple "Global registry path" Paths.registryPath
, Tuple "Global registry index path" Paths.registryIndexPath
, Tuple "Global package sets path" Paths.packageSetsPath
, Tuple "Global database path" Paths.databasePath
, Tuple "Local cache path" Paths.localCachePath
, Tuple "Local cache packages path" Paths.localCachePackagesPath
]

-- TODO: add LICENSE field

listPackageSet :: LsPackagesArgs -> Spago LsSetEnv Unit
Expand Down
10 changes: 1 addition & 9 deletions src/Spago/Db.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Spago.Db
, PackageSetEntry
, PackageVersion
, connect
, databasePath
, databaseVersion
, selectPackageSets
, selectLatestPackageSetByCompiler
, insertPackageSet
Expand Down Expand Up @@ -38,13 +36,6 @@ import Spago.Paths as Paths
--------------------------------------------------------------------------------
-- API

-- | We should bump this number every time we change the database schema in a breaking way
databaseVersion :: Int
databaseVersion = 1

databasePath :: FilePath
databasePath = Path.concat [ Paths.globalCachePath, "spago.v" <> show databaseVersion <> ".sqlite" ]

type ConnectOptions =
{ database :: FilePath
, logger :: String -> Effect Unit
Expand Down Expand Up @@ -93,6 +84,7 @@ selectPackageSetEntriesByPackage db packageName version = do
--------------------------------------------------------------------------------
-- Table types and conversions

-- Note: bump `Paths.databaseVersion` every time we change the database schema in a breaking way
data Db

type PackageSetJs =
Expand Down
12 changes: 5 additions & 7 deletions src/Spago/Paths.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ registryIndexPath = Path.concat [ globalCachePath, "registry-index" ]
packageSetsPath :: FilePath
packageSetsPath = Path.concat [ registryPath, "package-sets" ]

localCachePersistedWarningsPath :: FilePath
localCachePersistedWarningsPath = Path.concat [ localCachePath, "persisted-warnings" ]
-- | We should bump this number every time we change the database schema in a breaking way
databaseVersion :: Int
databaseVersion = 1

localCachesPersistedWarningsEntireWorkspace :: FilePath
localCachesPersistedWarningsEntireWorkspace = mkLocalCachesPersistentWarningsFile "entire-workspace"

mkLocalCachesPersistentWarningsFile :: String -> FilePath
mkLocalCachesPersistentWarningsFile fileName = Path.concat [ localCachePersistedWarningsPath, fileName <> ".stash" ]
databasePath :: FilePath
databasePath = Path.concat [ globalCachePath, "spago.v" <> show databaseVersion <> ".sqlite" ]

0 comments on commit 552160a

Please sign in to comment.