From 2c63b79361f5249b420222199a3c0a2072432233 Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sat, 15 Jun 2019 14:43:38 +0200 Subject: [PATCH 1/6] Replace defaultSourcePaths with configSourcePaths --- src/Spago/Build.hs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index 0835ae3bb..80ea9b47b 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -45,12 +45,6 @@ data BuildOptions = BuildOptions , passthroughArgs :: [Purs.ExtraArg] } -defaultSourcePaths :: [Purs.SourcePath] -defaultSourcePaths = - [ Purs.SourcePath "src/**/*.purs" - , Purs.SourcePath "test/**/*.purs" - ] - prepareBundleDefaults :: Maybe Purs.ModuleName -> Maybe Purs.TargetPath @@ -69,7 +63,7 @@ build BuildOptions{..} maybePostBuild = do config@Config.Config{ packageSet = PackageSet.PackageSet{..}, ..} <- Config.ensureConfig deps <- Packages.getProjectDeps config Fetch.fetchPackages maybeLimit cacheConfig deps packagesMinPursVersion - let projectGlobs = defaultSourcePaths <> sourcePaths + let projectGlobs = configSourcePaths <> sourcePaths allGlobs = Packages.getGlobs deps <> projectGlobs buildAction = do Purs.compile allGlobs passthroughArgs @@ -87,7 +81,7 @@ repl sourcePaths passthroughArgs = do echoDebug "Running `spago repl`" config <- Config.ensureConfig deps <- Packages.getProjectDeps config - let globs = Packages.getGlobs deps <> defaultSourcePaths <> sourcePaths + let globs = Packages.getGlobs deps <> Config.configSourcePaths config <> sourcePaths Purs.repl globs passthroughArgs -- | Test the project: compile and run "Test.Main" @@ -170,4 +164,4 @@ docs sourcePaths = do config <- Config.ensureConfig deps <- Packages.getProjectDeps config echo "Generating documentation for the project. This might take a while.." - Purs.docs $ defaultSourcePaths <> Packages.getGlobs deps <> sourcePaths + Purs.docs $ Config.configSourcePaths config <> Packages.getGlobs deps <> sourcePaths From 15727a5aface92b7926d58ddb3391511dc2c8e74 Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sat, 15 Jun 2019 14:44:15 +0200 Subject: [PATCH 2/6] Add configSourcePaths --- src/Spago/Config.hs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Spago/Config.hs b/src/Spago/Config.hs index b22e945a1..f19804bfc 100644 --- a/src/Spago/Config.hs +++ b/src/Spago/Config.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE ViewPatterns #-} module Spago.Config ( makeConfig , ensureConfig @@ -21,6 +22,7 @@ import qualified Spago.Dhall as Dhall import qualified Spago.Messages as Messages import qualified Spago.PackageSet as PackageSet import qualified Spago.PscPackage as PscPackage +import qualified Spago.Purs as Purs import qualified Spago.Templates as Templates import Spago.PackageSet (Package, PackageName, PackageSet) @@ -36,9 +38,10 @@ path = pathFromText pathText -- | Spago configuration file type data Config = Config - { name :: Text - , dependencies :: [PackageName] - , packageSet :: PackageSet + { name :: Text + , dependencies :: [PackageName] + , packageSet :: PackageSet + , configSourcePaths :: [Purs.SourcePath] } deriving (Show, Generic) type Expr = Dhall.DhallExpr Dhall.Import @@ -46,18 +49,21 @@ type Expr = Dhall.DhallExpr Dhall.Import -- | Tries to read in a Spago Config parseConfig :: Spago m => m Config parseConfig = do + withConfigAST $ pure . addSourcePaths expr <- liftIO $ Dhall.inputExpr $ "./" <> pathText case expr of Dhall.RecordLit ks -> do maybeConfig <- pure $ do let packageTyp = Dhall.genericAuto :: Dhall.Type Package packageNamesTyp = Dhall.list (Dhall.auto :: Dhall.Type PackageName) + sourcesType = Dhall.list (Dhall.auto :: Dhall.Type Purs.SourcePath) name <- Dhall.requireTypedKey ks "name" Dhall.strictText dependencies <- Dhall.requireTypedKey ks "dependencies" packageNamesTyp packages <- Dhall.requireKey ks "packages" $ \case Dhall.RecordLit pkgs -> (Map.mapKeys PackageSet.PackageName . Dhall.Map.toMap) <$> traverse (Dhall.coerceToType packageTyp) pkgs something -> Left $ Dhall.PackagesIsNotRecord something + configSourcePaths <- Dhall.requireTypedKey ks "sources" sourcesType let metadataPackageName = PackageSet.PackageName "metadata" (metadataMap, packagesDB) = Map.partitionWithKey (\k _v -> k == metadataPackageName) packages @@ -150,6 +156,15 @@ addRawDeps config newPackages r@(Dhall.RecordLit kvs) seens = Seq.scanl (flip Set.insert) Set.empty xs addRawDeps _ _ other = pure other +addSourcePaths :: Expr -> Expr +addSourcePaths (Dhall.RecordLit kvs) + | isConfigV1 kvs = Dhall.RecordLit + $ Dhall.Map.insert "sources" (Dhall.ListLit Nothing $ fmap Dhall.toTextLit $ Seq.fromList ["src/**/*.purs", "test/**/*.purs"]) kvs + where + isConfigV1 (Set.fromList . Dhall.Map.keys -> configKeySet) = + let configV1Keys = Set.fromList ["name", "dependencies", "packages"] + in configKeySet == configV1Keys +addSourcePaths expr = expr -- | Takes a function that manipulates the Dhall AST of the Config, and tries to run it -- on the current config. If it succeeds, it writes back to file the result returned. From 74543d7b611cde0a7643826ce0a58853dfd9ece7 Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sat, 15 Jun 2019 14:44:32 +0200 Subject: [PATCH 3/6] Derive Show and Dhall.Interpret in SourcePath --- src/Spago/Purs.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Spago/Purs.hs b/src/Spago/Purs.hs index 8ffdab4aa..0408f2ca8 100644 --- a/src/Spago/Purs.hs +++ b/src/Spago/Purs.hs @@ -4,6 +4,7 @@ import Spago.Prelude import qualified Data.Text as Text import Data.Versions as Version +import qualified Spago.Dhall as Dhall import qualified Spago.Messages as Messages @@ -11,6 +12,7 @@ import qualified Spago.Messages as Messages newtype ModuleName = ModuleName { unModuleName :: Text } newtype TargetPath = TargetPath { unTargetPath :: Text } newtype SourcePath = SourcePath { unSourcePath :: Text } + deriving newtype (Show, Dhall.Interpret) newtype ExtraArg = ExtraArg { unExtraArg :: Text } data WithMain = WithMain | WithoutMain From de6d4fada59b417a1b63d06d9c4bfc7b7e2d6705 Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sun, 16 Jun 2019 11:30:08 +0200 Subject: [PATCH 4/6] Remove echo fom writeRawExpr --- src/Spago/Dhall.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Spago/Dhall.hs b/src/Spago/Dhall.hs index b013194de..59f9b6256 100644 --- a/src/Spago/Dhall.hs +++ b/src/Spago/Dhall.hs @@ -12,9 +12,9 @@ import qualified Data.Text.Prettyprint.Doc.Render.Text as PrettyText import Dhall import Dhall.Core as Dhall hiding (Type, pretty) import qualified Dhall.Format +import qualified Dhall.Import import qualified Dhall.Map import qualified Dhall.Parser as Parser -import qualified Dhall.Import import qualified Dhall.Pretty import Dhall.TypeCheck (X, typeOf) @@ -56,7 +56,6 @@ writeRawExpr pathText (header, expr) = do -- if it doesn't we don't write to file. resolvedExpr <- Dhall.Import.load expr throws (Dhall.TypeCheck.typeOf resolvedExpr) - echo $ "Done. Updating the \"" <> pathText <> "\" file.." writeTextFile (pathFromText pathText) $ prettyWithHeader header expr <> "\n" format pathText From 1f2e48dfa3dcfadab98f9c6560b97adca28fbd10 Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sun, 16 Jun 2019 11:31:09 +0200 Subject: [PATCH 5/6] Add sources --- templates/spago.dhall | 2 ++ test/fixtures/spago-install-failure.dhall | 2 ++ test/fixtures/spago-install-success.dhall | 2 ++ test/fixtures/spago-psc-failure.dhall | 2 ++ test/fixtures/spago-psc-success.dhall | 2 ++ 5 files changed, 10 insertions(+) diff --git a/templates/spago.dhall b/templates/spago.dhall index 361544cb1..ae582f037 100644 --- a/templates/spago.dhall +++ b/templates/spago.dhall @@ -8,4 +8,6 @@ You can edit this file as you like. [ "effect", "console", "psci-support" ] , packages = ./packages.dhall +, sources = + [ "src/**/*.purs", "test/**/*.purs" ] } diff --git a/test/fixtures/spago-install-failure.dhall b/test/fixtures/spago-install-failure.dhall index 1c820e6b9..536142bbc 100644 --- a/test/fixtures/spago-install-failure.dhall +++ b/test/fixtures/spago-install-failure.dhall @@ -8,4 +8,6 @@ You can edit this file as you like. [ "console", "effect", "prelude", "psci-support" ] , packages = ./packages.dhall +, sources = + [ "src/**/*.purs", "test/**/*.purs" ] } diff --git a/test/fixtures/spago-install-success.dhall b/test/fixtures/spago-install-success.dhall index 60af2188b..c2e4a9a71 100644 --- a/test/fixtures/spago-install-success.dhall +++ b/test/fixtures/spago-install-success.dhall @@ -8,4 +8,6 @@ You can edit this file as you like. [ "console", "effect", "foreign", "prelude", "psci-support", "simple-json" ] , packages = ./packages.dhall +, sources = + [ "src/**/*.purs", "test/**/*.purs" ] } diff --git a/test/fixtures/spago-psc-failure.dhall b/test/fixtures/spago-psc-failure.dhall index 771409017..cc7e3ea42 100644 --- a/test/fixtures/spago-psc-failure.dhall +++ b/test/fixtures/spago-psc-failure.dhall @@ -8,4 +8,6 @@ You can edit this file as you like. [ "effect", "console", "psci-support" ] , packages = ./packages.dhall +, sources = + [ "src/**/*.purs", "test/**/*.purs" ] } diff --git a/test/fixtures/spago-psc-success.dhall b/test/fixtures/spago-psc-success.dhall index 1c820e6b9..536142bbc 100644 --- a/test/fixtures/spago-psc-success.dhall +++ b/test/fixtures/spago-psc-success.dhall @@ -8,4 +8,6 @@ You can edit this file as you like. [ "console", "effect", "prelude", "psci-support" ] , packages = ./packages.dhall +, sources = + [ "src/**/*.purs", "test/**/*.purs" ] } From 95d6c7c042860ab4357ab2e1d9018f8a04137d4a Mon Sep 17 00:00:00 2001 From: Valtteri Pajunen Date: Sun, 16 Jun 2019 12:07:34 +0200 Subject: [PATCH 6/6] Add test case for adding 'sources' key to configV1 --- test/SpagoSpec.hs | 13 +++++++++++-- test/Utils.hs | 5 +++-- test/fixtures/spago-configV1.dhall | 11 +++++++++++ test/fixtures/spago-configV2.dhall | 13 +++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/spago-configV1.dhall create mode 100644 test/fixtures/spago-configV2.dhall diff --git a/test/SpagoSpec.hs b/test/SpagoSpec.hs index ada25f098..9c83e5e00 100644 --- a/test/SpagoSpec.hs +++ b/test/SpagoSpec.hs @@ -6,8 +6,9 @@ import qualified System.IO.Temp as Temp import Test.Hspec (Spec, around_, describe, it, shouldBe) import Turtle (cp, decodeString, mkdir, mktree, mv, readTextFile, testdir, writeTextFile) -import Utils (checkFixture, runFor, shouldBeFailure, shouldBeFailureOutput, - shouldBeSuccess, shouldBeSuccessOutput, spago, withCwd) +import Utils (checkFixture, readFixture, runFor, shouldBeFailure, + shouldBeFailureOutput, shouldBeSuccess, shouldBeSuccessOutput, + spago, withCwd) setup :: IO () -> IO () @@ -125,6 +126,14 @@ spec = around_ setup $ do mv "src/Main.purs" "another_source_path/Main.purs" spago ["build", "--path", "another_source_path/*.purs"] >>= shouldBeSuccess + it "Spago should add sources to config when key is missing" $ do + + spago ["init"] >>= shouldBeSuccess + -- Replace initial config with the old config format (without 'sources') + writeTextFile "spago.dhall" =<< readFixture "spago-configV1.dhall" + spago ["build"] >>= shouldBeSuccess + mv "spago.dhall" "spago-configV2.dhall" + checkFixture "spago-configV2.dhall" describe "spago test" $ do diff --git a/test/Utils.hs b/test/Utils.hs index af725e42a..7cf68a06d 100644 --- a/test/Utils.hs +++ b/test/Utils.hs @@ -1,5 +1,6 @@ module Utils ( checkFixture + , readFixture , rmtree , runFor , shouldBeFailure @@ -16,8 +17,8 @@ import Prelude hiding (FilePath) import System.Directory (removePathForcibly) import qualified System.Process as Process import Test.Hspec (shouldBe) -import Turtle (ExitCode (..), FilePath, Text, cd, empty, - encodeString, procStrictWithErr, pwd, readTextFile) +import Turtle (ExitCode (..), FilePath, Text, cd, empty, encodeString, + procStrictWithErr, pwd, readTextFile) withCwd :: FilePath -> IO () -> IO () withCwd dir cmd = do diff --git a/test/fixtures/spago-configV1.dhall b/test/fixtures/spago-configV1.dhall new file mode 100644 index 000000000..60af2188b --- /dev/null +++ b/test/fixtures/spago-configV1.dhall @@ -0,0 +1,11 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. +-} +{ name = + "aaa" +, dependencies = + [ "console", "effect", "foreign", "prelude", "psci-support", "simple-json" ] +, packages = + ./packages.dhall +} diff --git a/test/fixtures/spago-configV2.dhall b/test/fixtures/spago-configV2.dhall new file mode 100644 index 000000000..4a54efb09 --- /dev/null +++ b/test/fixtures/spago-configV2.dhall @@ -0,0 +1,13 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. +-} +{ sources = + [ "src/**/*.purs", "test/**/*.purs" ] +, name = + "aaa" +, dependencies = + [ "console", "effect", "foreign", "prelude", "psci-support", "simple-json" ] +, packages = + ./packages.dhall +}