From e115ece29ae16fa0636a3faef27c5742352320ad Mon Sep 17 00:00:00 2001 From: Martin Sosic Date: Wed, 23 Sep 2020 14:34:24 +0200 Subject: [PATCH] External code paths now again use StrongPath. --- .../ServerGenerator/OperationsGenerator.hs | 2 +- .../WebAppGenerator/RouterGenerator.hs | 28 +++++++-------- waspc/src/Parser/ExternalCode.hs | 15 ++++---- waspc/src/Wasp/JsImport.hs | 12 ++++--- waspc/src/Wasp/Style.hs | 14 ++++---- waspc/test/Parser/ActionTest.hs | 13 +++---- waspc/test/Parser/ExternalCodeTest.hs | 13 +++---- waspc/test/Parser/JsImportTest.hs | 21 ++++++----- waspc/test/Parser/PageTest.hs | 15 ++++---- waspc/test/Parser/ParserTest.hs | 36 +++++++++---------- waspc/test/Parser/QueryTest.hs | 16 +++++---- waspc/test/Parser/StyleTest.hs | 13 +++---- 12 files changed, 107 insertions(+), 91 deletions(-) diff --git a/waspc/src/Generator/ServerGenerator/OperationsGenerator.hs b/waspc/src/Generator/ServerGenerator/OperationsGenerator.hs index 487d0795b..dbbf63705 100644 --- a/waspc/src/Generator/ServerGenerator/OperationsGenerator.hs +++ b/waspc/src/Generator/ServerGenerator/OperationsGenerator.hs @@ -60,7 +60,7 @@ getImportDetailsForOperationJsFn getImportDetailsForOperationJsFn operation relPathToExtCodeDir = (importIdentifier, importStmt) where importStmt = "import " ++ importWhat ++ " from '" ++ importFrom ++ "'" - importFrom = relPathToExtCodeDir ++ P.toFilePath (Wasp.JsImport._from jsImport) + importFrom = relPathToExtCodeDir ++ SP.toFilePath (Wasp.JsImport._from jsImport) (importIdentifier, importWhat) = case (Wasp.JsImport._defaultImport jsImport, Wasp.JsImport._namedImports jsImport) of (Just defaultImport, []) -> (defaultImport, defaultImport) diff --git a/waspc/src/Generator/WebAppGenerator/RouterGenerator.hs b/waspc/src/Generator/WebAppGenerator/RouterGenerator.hs index 655895784..a6c1773ff 100644 --- a/waspc/src/Generator/WebAppGenerator/RouterGenerator.hs +++ b/waspc/src/Generator/WebAppGenerator/RouterGenerator.hs @@ -2,23 +2,23 @@ module Generator.WebAppGenerator.RouterGenerator ( generateRouter ) where -import Data.Aeson (ToJSON(..), (.=), object) -import qualified Path as P +import Data.Aeson (ToJSON (..), object, (.=)) +import qualified Path as P -import StrongPath (()) - -import Wasp (Wasp) -import qualified Wasp -import qualified Wasp.Route -import qualified Wasp.Page -import qualified Wasp.JsImport - -import Generator.FileDraft (FileDraft) -import Generator.WebAppGenerator.Common (asTmplFile, asWebAppSrcFile) +import Generator.FileDraft (FileDraft) +import Generator.WebAppGenerator.Common (asTmplFile, asWebAppSrcFile) import qualified Generator.WebAppGenerator.Common as C +import StrongPath (()) +import qualified StrongPath as SP +import Wasp (Wasp) +import qualified Wasp +import qualified Wasp.JsImport +import qualified Wasp.Page +import qualified Wasp.Route + data RouterTemplateData = RouterTemplateData - { _routes :: ![Wasp.Route.Route] + { _routes :: ![Wasp.Route.Route] , _pagesToImport :: ![PageTemplateData] } @@ -57,7 +57,7 @@ createRouterTemplateData wasp = RouterTemplateData createPageTemplateData :: Wasp.Page.Page -> PageTemplateData createPageTemplateData page = PageTemplateData - { _importFrom = relPathToExtSrcDir ++ P.toFilePath (Wasp.JsImport._from pageComponent) + { _importFrom = relPathToExtSrcDir ++ SP.toFilePath (Wasp.JsImport._from pageComponent) , _importWhat = case Wasp.JsImport._namedImports pageComponent of -- If no named imports, we go with the default import. [] -> pageName diff --git a/waspc/src/Parser/ExternalCode.hs b/waspc/src/Parser/ExternalCode.hs index a21919cbb..85fa6279b 100644 --- a/waspc/src/Parser/ExternalCode.hs +++ b/waspc/src/Parser/ExternalCode.hs @@ -2,19 +2,22 @@ module Parser.ExternalCode ( extCodeFilePathString ) where -import Text.Parsec (unexpected) -import Text.Parsec.String (Parser) -import qualified Path.Posix as PPosix +import qualified Path.Posix as PPosix +import Text.Parsec (unexpected) +import Text.Parsec.String (Parser) +import ExternalCode (SourceExternalCodeDir) import qualified Parser.Common +import StrongPath (File, Path', Posix, Rel) +import qualified StrongPath as SP -- Parses string literal that is file path to file in source external code dir. -- Returns file path relative to the external code dir. -- Example of input: "@ext/some/file.txt". Output would be: "some/file.txt". -extCodeFilePathString :: Parser (PPosix.Path PPosix.Rel PPosix.File) +extCodeFilePathString :: Parser (Path' Posix (Rel SourceExternalCodeDir) File) extCodeFilePathString = do path <- Parser.Common.relPosixFilePathString - maybe (unexpected $ "string \"" ++ (show path) ++ "\": External code file path should start with \"@ext/\".") - return + maybe (unexpected $ "string \"" ++ show path ++ "\": External code file path should start with \"@ext/\".") + (return . SP.fromPathRelFileP) (PPosix.stripProperPrefix [PPosix.reldir|@ext|] path) diff --git a/waspc/src/Wasp/JsImport.hs b/waspc/src/Wasp/JsImport.hs index 04e58a05e..90b600a99 100644 --- a/waspc/src/Wasp/JsImport.hs +++ b/waspc/src/Wasp/JsImport.hs @@ -2,21 +2,23 @@ module Wasp.JsImport ( JsImport(..) ) where -import Data.Aeson (ToJSON (..), object, (.=)) -import qualified Path.Posix as PPosix +import Data.Aeson (ToJSON (..), object, (.=)) + +import ExternalCode (SourceExternalCodeDir) +import StrongPath (File, Path', Posix, Rel) +import qualified StrongPath as SP -- | Represents javascript import -> "import from ". data JsImport = JsImport { _defaultImport :: !(Maybe String) , _namedImports :: ![String] - -- Relative to source external code dir. - , _from :: !(PPosix.Path PPosix.Rel PPosix.File) + , _from :: Path' Posix (Rel SourceExternalCodeDir) File } deriving (Show, Eq) instance ToJSON JsImport where toJSON jsImport = object [ "defaultImport" .= _defaultImport jsImport , "namedImports" .= _namedImports jsImport - , "from" .= PPosix.toFilePath (_from jsImport) + , "from" .= SP.toFilePath (_from jsImport) ] diff --git a/waspc/src/Wasp/Style.hs b/waspc/src/Wasp/Style.hs index 91bbc4a0d..89ac5211f 100644 --- a/waspc/src/Wasp/Style.hs +++ b/waspc/src/Wasp/Style.hs @@ -2,15 +2,17 @@ module Wasp.Style ( Style(..) ) where -import Data.Aeson (ToJSON(..)) -import Data.Text (Text) -import qualified Path.Posix as PPosix +import Data.Aeson (ToJSON (..)) +import Data.Text (Text) + +import ExternalCode (SourceExternalCodeDir) +import StrongPath (File, Path', Posix, Rel, toFilePath) -data Style = ExtCodeCssFile !(PPosix.Path PPosix.Rel PPosix.File) +data Style = ExtCodeCssFile !(Path' Posix (Rel SourceExternalCodeDir) File) | CssCode !Text deriving (Show, Eq) instance ToJSON Style where - toJSON (ExtCodeCssFile path) = toJSON $ PPosix.toFilePath path - toJSON (CssCode code) = toJSON code + toJSON (ExtCodeCssFile path) = toJSON $ toFilePath path + toJSON (CssCode code) = toJSON code diff --git a/waspc/test/Parser/ActionTest.hs b/waspc/test/Parser/ActionTest.hs index 106913994..621061223 100644 --- a/waspc/test/Parser/ActionTest.hs +++ b/waspc/test/Parser/ActionTest.hs @@ -1,12 +1,13 @@ module Parser.ActionTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) -import qualified Path.Posix as PPosix +import Data.Either (isLeft) +import qualified Path.Posix as PPosix -import Parser.Common (runWaspParser) -import Parser.Action (action) +import Parser.Action (action) +import Parser.Common (runWaspParser) +import qualified StrongPath as SP import qualified Wasp.Action import qualified Wasp.JsImport @@ -23,7 +24,7 @@ spec_parseAction = it "When given a valid action declaration, returns correct AST" $ do let testActionName = "myAction" testActionJsFunctionName = "myJsAction" - testActionJsFunctionFrom = [PPosix.relfile|some/path|] + testActionJsFunctionFrom = SP.fromPathRelFileP [PPosix.relfile|some/path|] let testAction = Wasp.Action.Action { Wasp.Action._name = testActionName , Wasp.Action._jsFunction = Wasp.JsImport.JsImport diff --git a/waspc/test/Parser/ExternalCodeTest.hs b/waspc/test/Parser/ExternalCodeTest.hs index 468c2c303..22baad0d4 100644 --- a/waspc/test/Parser/ExternalCodeTest.hs +++ b/waspc/test/Parser/ExternalCodeTest.hs @@ -1,12 +1,13 @@ module Parser.ExternalCodeTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) -import qualified Path.Posix as PPosix +import Data.Either (isLeft) +import qualified Path.Posix as PPosix -import Parser.ExternalCode (extCodeFilePathString) -import Parser.Common (runWaspParser) +import Parser.Common (runWaspParser) +import Parser.ExternalCode (extCodeFilePathString) +import qualified StrongPath as SP spec_ParserExternalCode :: Spec @@ -14,7 +15,7 @@ spec_ParserExternalCode = do describe "Parsing external code file path string" $ do it "Correctly parses external code path in double quotes" $ do runWaspParser extCodeFilePathString "\"@ext/foo/bar.txt\"" - `shouldBe` Right [PPosix.relfile|foo/bar.txt|] + `shouldBe` Right (SP.fromPathRelFileP [PPosix.relfile|foo/bar.txt|]) it "When path does not start with @ext/, returns Left" $ do isLeft (runWaspParser extCodeFilePathString "\"@ext2/foo/bar.txt\"") `shouldBe` True diff --git a/waspc/test/Parser/JsImportTest.hs b/waspc/test/Parser/JsImportTest.hs index 51b7603d7..4ce0d23c4 100644 --- a/waspc/test/Parser/JsImportTest.hs +++ b/waspc/test/Parser/JsImportTest.hs @@ -1,32 +1,35 @@ module Parser.JsImportTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) -import qualified Path.Posix as PPosix +import Data.Either (isLeft) +import Path.Posix (relfile) -import Parser.Common (runWaspParser) -import Parser.JsImport (jsImport) +import Parser.Common (runWaspParser) +import Parser.JsImport (jsImport) +import qualified StrongPath as SP import qualified Wasp spec_parseJsImport :: Spec spec_parseJsImport = do + let someFilePath = SP.fromPathRelFileP [relfile|some/file.js|] + it "Parses external code js import with default import correctly" $ do runWaspParser jsImport "import something from \"@ext/some/file.js\"" - `shouldBe` Right (Wasp.JsImport (Just "something") [] [PPosix.relfile|some/file.js|]) + `shouldBe` Right (Wasp.JsImport (Just "something") [] someFilePath) it "Parses correctly when there is whitespace up front" $ do runWaspParser jsImport " import something from \"@ext/some/file.js\"" - `shouldBe` Right (Wasp.JsImport (Just "something") [] [PPosix.relfile|some/file.js|]) + `shouldBe` Right (Wasp.JsImport (Just "something") [] someFilePath) it "Parses correctly when 'from' is part of WHAT part" $ do runWaspParser jsImport "import somethingfrom from \"@ext/some/file.js\"" - `shouldBe` Right (Wasp.JsImport (Just "somethingfrom") [] [PPosix.relfile|some/file.js|]) + `shouldBe` Right (Wasp.JsImport (Just "somethingfrom") [] someFilePath) it "Parses correctly when 'what' is a single named export" $ do runWaspParser jsImport "import { something } from \"@ext/some/file.js\"" - `shouldBe` Right (Wasp.JsImport Nothing ["something"] [PPosix.relfile|some/file.js|]) + `shouldBe` Right (Wasp.JsImport Nothing ["something"] someFilePath) it "For now we don't support multiple named exports in WHAT part" $ do isLeft (runWaspParser jsImport "import { foo, bar } from \"@ext/some/file.js\"") diff --git a/waspc/test/Parser/PageTest.hs b/waspc/test/Parser/PageTest.hs index 10f2f975a..4427c97a5 100644 --- a/waspc/test/Parser/PageTest.hs +++ b/waspc/test/Parser/PageTest.hs @@ -1,16 +1,17 @@ module Parser.PageTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) +import Data.Either (isLeft) +import qualified Path.Posix as PPosix -import qualified Path.Posix as PPosix - -import Parser.Common (runWaspParser) -import Parser.Page (page) +import Parser.Common (runWaspParser) +import Parser.Page (page) +import qualified StrongPath as SP import qualified Wasp.JsImport import qualified Wasp.Page + spec_parsePage :: Spec spec_parsePage = describe "Parsing page declaration" $ do @@ -22,7 +23,7 @@ spec_parsePage = let expectedPageComponentImport = Wasp.JsImport.JsImport { Wasp.JsImport._defaultImport = Just "Main" , Wasp.JsImport._namedImports = [] - , Wasp.JsImport._from = [PPosix.relfile|pages/Main|] + , Wasp.JsImport._from = (SP.fromPathRelFileP [PPosix.relfile|pages/Main|]) } parsePage ( diff --git a/waspc/test/Parser/ParserTest.hs b/waspc/test/Parser/ParserTest.hs index f1d67ccb4..0b8c3cabb 100644 --- a/waspc/test/Parser/ParserTest.hs +++ b/waspc/test/Parser/ParserTest.hs @@ -1,22 +1,22 @@ module Parser.ParserTest where -import Test.Tasty.Hspec -import Data.Either -import qualified Path.Posix as PPosix +import Data.Either +import qualified Path.Posix as PPosix +import Test.Tasty.Hspec -import Parser -import Wasp +import Parser +import qualified StrongPath as SP +import Wasp +import qualified Wasp.EntityPSL +import qualified Wasp.JsCode +import qualified Wasp.JsImport +import qualified Wasp.Page +import qualified Wasp.Query +import qualified Wasp.Route as R -- TODO(matija): old Entity stuff, to be removed. -import qualified Wasp.EntityForm as EF -import qualified Wasp.EntityList as EL - -import qualified Wasp.Route as R -import qualified Wasp.Page -import qualified Wasp.JsCode -import qualified Wasp.Query -import qualified Wasp.JsImport -import qualified Wasp.EntityPSL +import qualified Wasp.EntityForm as EF +import qualified Wasp.EntityList as EL spec_parseWasp :: Spec @@ -43,7 +43,7 @@ spec_parseWasp = , Wasp.Page._component = Wasp.JsImport.JsImport { Wasp.JsImport._defaultImport = Just "Landing" , Wasp.JsImport._namedImports = [] - , Wasp.JsImport._from = [PPosix.relfile|pages/Landing|] + , Wasp.JsImport._from = SP.fromPathRelFileP [PPosix.relfile|pages/Landing|] } } , WaspElementRoute $ R.Route @@ -55,7 +55,7 @@ spec_parseWasp = , Wasp.Page._component = Wasp.JsImport.JsImport { Wasp.JsImport._defaultImport = Just "Test" , Wasp.JsImport._namedImports = [] - , Wasp.JsImport._from = [PPosix.relfile|pages/Test|] + , Wasp.JsImport._from = SP.fromPathRelFileP [PPosix.relfile|pages/Test|] } } , WaspElementEntityPSL $ Wasp.EntityPSL.EntityPSL @@ -124,9 +124,9 @@ spec_parseWasp = , Wasp.Query._jsFunction = Wasp.JsImport.JsImport { Wasp.JsImport._defaultImport = Nothing , Wasp.JsImport._namedImports = [ "myJsQuery" ] - , Wasp.JsImport._from = [PPosix.relfile|some/path|] + , Wasp.JsImport._from = SP.fromPathRelFileP [PPosix.relfile|some/path|] } } ] - `setJsImports` [ JsImport (Just "something") [] [PPosix.relfile|some/file|] ] + `setJsImports` [ JsImport (Just "something") [] (SP.fromPathRelFileP [PPosix.relfile|some/file|]) ] ) diff --git a/waspc/test/Parser/QueryTest.hs b/waspc/test/Parser/QueryTest.hs index f6d65f8dc..a26e4bdcc 100644 --- a/waspc/test/Parser/QueryTest.hs +++ b/waspc/test/Parser/QueryTest.hs @@ -1,14 +1,16 @@ module Parser.QueryTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) -import qualified Path.Posix as PPosix +import Data.Either (isLeft) +import qualified Path.Posix as PPosix -import Parser.Common (runWaspParser) -import Parser.Query (query) -import qualified Wasp.Query +import Parser.Common (runWaspParser) +import Parser.Query (query) +import qualified StrongPath as SP import qualified Wasp.JsImport +import qualified Wasp.Query + spec_parseQuery :: Spec spec_parseQuery = @@ -18,7 +20,7 @@ spec_parseQuery = it "When given a valid query declaration, returns correct AST" $ do let testQueryName = "myQuery" testQueryJsFunctionName = "myJsQuery" - testQueryJsFunctionFrom = [PPosix.relfile|some/path|] + testQueryJsFunctionFrom = SP.fromPathRelFileP [PPosix.relfile|some/path|] let testQuery = Wasp.Query.Query { Wasp.Query._name = testQueryName , Wasp.Query._jsFunction = Wasp.JsImport.JsImport diff --git a/waspc/test/Parser/StyleTest.hs b/waspc/test/Parser/StyleTest.hs index 9c4b6f8e9..d74c8de29 100644 --- a/waspc/test/Parser/StyleTest.hs +++ b/waspc/test/Parser/StyleTest.hs @@ -1,12 +1,13 @@ module Parser.StyleTest where -import Test.Tasty.Hspec +import Test.Tasty.Hspec -import Data.Either (isLeft) -import qualified Path.Posix as PPosix +import Data.Either (isLeft) +import qualified Path.Posix as PPosix -import Parser.Common (runWaspParser) -import Parser.Style (style) +import Parser.Common (runWaspParser) +import Parser.Style (style) +import qualified StrongPath as SP import qualified Wasp.Style @@ -14,7 +15,7 @@ spec_parseStyle :: Spec spec_parseStyle = do it "Parses external code file path correctly" $ do runWaspParser style "\"@ext/some/file.css\"" - `shouldBe` Right (Wasp.Style.ExtCodeCssFile [PPosix.relfile|some/file.css|]) + `shouldBe` Right (Wasp.Style.ExtCodeCssFile (SP.fromPathRelFileP [PPosix.relfile|some/file.css|])) it "Parses css closure correctly" $ do runWaspParser style "{=css Some css code css=}"