External code paths now have to be prefixed with @ext/.

This commit is contained in:
Martin Sosic 2020-02-04 22:39:38 +01:00 committed by Martin Šošić
parent e9d0ea9781
commit 7b415caf6f
12 changed files with 63 additions and 17 deletions

View File

@ -21,7 +21,7 @@ main = do
absOutDirPath <- Path.parseAbsDir (ensurePathIsAbs absCwdPath outDirPath) absOutDirPath <- Path.parseAbsDir (ensurePathIsAbs absCwdPath outDirPath)
-- TODO(martin): Take compile options as arguments to the command, right now I hardcoded the value. -- TODO(martin): Take compile options as arguments to the command, right now I hardcoded the value.
let options = CompileOptions let options = CompileOptions
{ externalCodeDirPath = (Path.parent absWaspFilePath) </> [reldir|src|] { externalCodeDirPath = (Path.parent absWaspFilePath) </> [reldir|ext|]
} }
result <- compile absWaspFilePath absOutDirPath options result <- compile absWaspFilePath absOutDirPath options
either putStrLn (\_ -> print ("Success!" :: String)) result either putStrLn (\_ -> print ("Success!" :: String)) result

View File

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -1,6 +1,6 @@
// Goal of this file is to re-create a TODO app from http://todomvc.com // Goal of this file is to re-create a TODO app from http://todomvc.com
import Todo from "Todo" // Imports from external code dir (src/). import Todo from "@ext/Todo" // Imports from external code dir (ext/).
// -- Entities // -- Entities
entity Task { entity Task {

View File

@ -0,0 +1,21 @@
module Parser.ExternalCode
( extCodeFilePathString
) where
import Text.Parsec (unexpected)
import Text.Parsec.String (Parser)
import Path (reldir)
import qualified Path
import qualified Path.Aliases as Path
import qualified Parser.Common
-- Parses string literal that is file path to file in 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 Path.RelFile
extCodeFilePathString = do
path <- Parser.Common.relFilePathString
maybe (unexpected $ "string \"" ++ (show path) ++ "\": External code file path should start with \"@ext/\".")
return
(Path.stripProperPrefix [reldir|@ext|] path)

View File

@ -5,8 +5,8 @@ module Parser.JsImport
import Text.Parsec (manyTill, anyChar, try) import Text.Parsec (manyTill, anyChar, try)
import Text.Parsec.Char (space) import Text.Parsec.Char (space)
import Text.Parsec.String (Parser) import Text.Parsec.String (Parser)
import qualified Parser.Common
import qualified Parser.ExternalCode
import qualified Lexer as L import qualified Lexer as L
import qualified Wasp.JsImport import qualified Wasp.JsImport
@ -21,5 +21,5 @@ jsImport = do
what <- anyChar `manyTill` (try (space *> L.whiteSpace *> L.reserved L.reservedNameFrom)) what <- anyChar `manyTill` (try (space *> L.whiteSpace *> L.reserved L.reservedNameFrom))
-- TODO: For now we only support double quotes here, we should also support single quotes. -- TODO: For now we only support double quotes here, we should also support single quotes.
-- We would need to write this from scratch, with single quote escaping enabled. -- We would need to write this from scratch, with single quote escaping enabled.
from <- Parser.Common.relFilePathString from <- Parser.ExternalCode.extCodeFilePathString
return Wasp.JsImport.JsImport { Wasp.JsImport.jsImportWhat = what, Wasp.JsImport.jsImportFrom = from } return Wasp.JsImport.JsImport { Wasp.JsImport.jsImportWhat = what, Wasp.JsImport.jsImportFrom = from }

View File

@ -7,6 +7,7 @@ import Text.Parsec.String (Parser)
import qualified Data.Text as Text import qualified Data.Text as Text
import qualified Parser.Common import qualified Parser.Common
import qualified Parser.ExternalCode
import qualified Wasp.Style import qualified Wasp.Style
@ -14,7 +15,7 @@ style :: Parser Wasp.Style.Style
style = cssFile <|> cssCode style = cssFile <|> cssCode
cssFile :: Parser Wasp.Style.Style cssFile :: Parser Wasp.Style.Style
cssFile = Wasp.Style.ExtCodeCssFile <$> Parser.Common.relFilePathString cssFile = Wasp.Style.ExtCodeCssFile <$> Parser.ExternalCode.extCodeFilePathString
cssCode :: Parser Wasp.Style.Style cssCode :: Parser Wasp.Style.Style
cssCode = (Wasp.Style.CssCode . Text.pack) <$> Parser.Common.waspNamedClosure "css" cssCode = (Wasp.Style.CssCode . Text.pack) <$> Parser.Common.waspNamedClosure "css"

View File

@ -0,0 +1,20 @@
module Parser.ExternalCodeTest where
import Test.Tasty.Hspec
import Data.Either (isLeft)
import Path (relfile)
import Parser.ExternalCode (extCodeFilePathString)
import Parser.Common (runWaspParser)
spec_ParserExternalCode :: Spec
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 [relfile|foo/bar.txt|]
it "When path does not start with @ext/, returns Left" $ do
isLeft (runWaspParser extCodeFilePathString "\"@ext2/foo/bar.txt\"") `shouldBe` True

View File

@ -12,22 +12,26 @@ import qualified Wasp
spec_parseJsImport :: Spec spec_parseJsImport :: Spec
spec_parseJsImport = do spec_parseJsImport = do
it "Parses typical js import correctly" $ do it "Parses external code js import correctly" $ do
runWaspParser jsImport "import something from \"some/file.js\"" runWaspParser jsImport "import something from \"@ext/some/file.js\""
`shouldBe` Right (Wasp.JsImport "something" [relfile|some/file.js|]) `shouldBe` Right (Wasp.JsImport "something" [relfile|some/file.js|])
it "Parses correctly when there is whitespace up front" $ do it "Parses correctly when there is whitespace up front" $ do
runWaspParser jsImport " import something from \"some/file.js\"" runWaspParser jsImport " import something from \"@ext/some/file.js\""
`shouldBe` Right (Wasp.JsImport "something" [relfile|some/file.js|]) `shouldBe` Right (Wasp.JsImport "something" [relfile|some/file.js|])
it "Parses correctly when 'from' is part of WHAT part" $ do it "Parses correctly when 'from' is part of WHAT part" $ do
runWaspParser jsImport "import somethingfrom from \"some/file.js\"" runWaspParser jsImport "import somethingfrom from \"@ext/some/file.js\""
`shouldBe` Right (Wasp.JsImport "somethingfrom" [relfile|some/file.js|]) `shouldBe` Right (Wasp.JsImport "somethingfrom" [relfile|some/file.js|])
it "Throws error if there is no whitespace after import" $ do it "Throws error if there is no whitespace after import" $ do
isLeft (runWaspParser jsImport "importsomething from \"some/file.js\"") isLeft (runWaspParser jsImport "importsomething from \"@ext/some/file.js\"")
`shouldBe` True
it "Throws error if import is not from external code" $ do
isLeft (runWaspParser jsImport "import something from \"some/file.js\"")
`shouldBe` True `shouldBe` True
it "For now we don't support single quotes in FROM part (TODO: support them in the future!)" $ do it "For now we don't support single quotes in FROM part (TODO: support them in the future!)" $ do
isLeft (runWaspParser jsImport "import something from 'some/file.js'") isLeft (runWaspParser jsImport "import something from '@ext/some/file.js'")
`shouldBe` True `shouldBe` True

View File

@ -12,14 +12,14 @@ import qualified Wasp.Style
spec_parseStyle :: Spec spec_parseStyle :: Spec
spec_parseStyle = do spec_parseStyle = do
it "Parses relative file path correctly" $ do it "Parses external code file path correctly" $ do
runWaspParser style "\"some/file.js\"" runWaspParser style "\"@ext/some/file.js\""
`shouldBe` Right (Wasp.Style.ExtCodeCssFile [relfile|some/file.js|]) `shouldBe` Right (Wasp.Style.ExtCodeCssFile [relfile|some/file.js|])
it "Parses css closure correctly" $ do it "Parses css closure correctly" $ do
runWaspParser style "{=css Some css code css=}" runWaspParser style "{=css Some css code css=}"
`shouldBe` Right (Wasp.Style.CssCode "Some css code") `shouldBe` Right (Wasp.Style.CssCode "Some css code")
it "Throws error if path is not relative" $ do it "Throws error if path is not external code path." $ do
isLeft (runWaspParser style "\"/some/file.js\"") isLeft (runWaspParser style "\"some/file.js\"")
`shouldBe` True `shouldBe` True

View File

@ -1,6 +1,6 @@
// Test .wasp file. // Test .wasp file.
import something from "some/file" import something from "@ext/some/file"
// App definition. // App definition.
app test_app { app test_app {
@ -38,7 +38,7 @@ page TestPage {
content: {=jsx content: {=jsx
<div>This is a test page!</div> <div>This is a test page!</div>
jsx=}, jsx=},
style: "test.css" style: "@ext/test.css"
} }
// Entity definition. // Entity definition.