2021-11-11 15:26:20 +03:00
|
|
|
module Wasp.Lib
|
2021-04-28 18:36:00 +03:00
|
|
|
( compile,
|
|
|
|
Generator.start,
|
|
|
|
ProjectRootDir,
|
2021-08-23 19:13:04 +03:00
|
|
|
findWaspFile,
|
2022-05-03 21:34:25 +03:00
|
|
|
analyzeWaspProject,
|
2021-04-28 18:36:00 +03:00
|
|
|
)
|
|
|
|
where
|
2019-03-29 02:37:41 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
import Data.List (find, isSuffixOf)
|
2021-07-03 12:00:01 +03:00
|
|
|
import StrongPath (Abs, Dir, File', Path', relfile)
|
2021-04-28 18:36:00 +03:00
|
|
|
import qualified StrongPath as SP
|
2021-12-13 20:06:42 +03:00
|
|
|
import System.Directory (doesDirectoryExist, doesFileExist)
|
2022-01-20 13:45:14 +03:00
|
|
|
import qualified Wasp.Analyzer as Analyzer
|
|
|
|
import Wasp.Analyzer.AnalyzeError (getErrorMessageAndCtx)
|
|
|
|
import qualified Wasp.AppSpec as AS
|
2022-03-17 14:36:36 +03:00
|
|
|
import qualified Wasp.AppSpec.Valid as ASV
|
2021-12-13 20:06:42 +03:00
|
|
|
import Wasp.Common (DbMigrationsDir, WaspProjectDir, dbMigrationsDirInWaspProjectDir)
|
2022-06-02 21:04:57 +03:00
|
|
|
import Wasp.CompileOptions (CompileOptions (generatorWarningsFilter), sendMessage)
|
2021-11-11 15:26:20 +03:00
|
|
|
import qualified Wasp.CompileOptions as CompileOptions
|
2022-01-20 13:45:14 +03:00
|
|
|
import Wasp.Error (showCompilerErrorForTerminal)
|
2021-11-11 15:26:20 +03:00
|
|
|
import qualified Wasp.ExternalCode as ExternalCode
|
|
|
|
import qualified Wasp.Generator as Generator
|
|
|
|
import Wasp.Generator.Common (ProjectRootDir)
|
|
|
|
import qualified Wasp.Util.IO as Util.IO
|
2019-05-04 00:24:57 +03:00
|
|
|
|
2019-03-24 14:42:36 +03:00
|
|
|
type CompileError = String
|
|
|
|
|
2022-01-23 17:21:31 +03:00
|
|
|
type CompileWarning = String
|
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
compile ::
|
2021-07-03 12:00:01 +03:00
|
|
|
Path' Abs (Dir WaspProjectDir) ->
|
|
|
|
Path' Abs (Dir ProjectRootDir) ->
|
2021-04-28 18:36:00 +03:00
|
|
|
CompileOptions ->
|
2022-01-23 17:21:31 +03:00
|
|
|
IO ([CompileWarning], [CompileError])
|
2021-04-16 13:43:57 +03:00
|
|
|
compile waspDir outDir options = do
|
2022-05-03 21:34:25 +03:00
|
|
|
appSpecOrCompileErrors <- analyzeWaspProject waspDir options
|
|
|
|
case appSpecOrCompileErrors of
|
|
|
|
Left compileErrors -> return ([], compileErrors)
|
|
|
|
Right appSpec ->
|
|
|
|
case ASV.validateAppSpec appSpec of
|
|
|
|
[] -> do
|
|
|
|
(generatorWarnings, generatorErrors) <- Generator.writeWebAppCode appSpec outDir (sendMessage options)
|
2022-06-02 21:04:57 +03:00
|
|
|
return (map show $ generatorWarningsFilter options generatorWarnings, map show generatorErrors)
|
2022-05-03 21:34:25 +03:00
|
|
|
validationErrors -> do
|
|
|
|
return ([], map show validationErrors)
|
|
|
|
|
|
|
|
analyzeWaspProject ::
|
|
|
|
Path' Abs (Dir WaspProjectDir) ->
|
|
|
|
CompileOptions ->
|
|
|
|
IO (Either [CompileError] AS.AppSpec)
|
|
|
|
analyzeWaspProject waspDir options = do
|
2022-01-20 13:45:14 +03:00
|
|
|
maybeWaspFilePath <- findWaspFile waspDir
|
|
|
|
case maybeWaspFilePath of
|
2022-05-03 21:34:25 +03:00
|
|
|
Nothing -> return $ Left ["Couldn't find a single *.wasp file."]
|
2022-01-20 13:45:14 +03:00
|
|
|
Just waspFilePath -> do
|
|
|
|
waspFileContent <- readFile (SP.fromAbsFile waspFilePath)
|
|
|
|
case Analyzer.analyze waspFileContent of
|
|
|
|
Left analyzeError ->
|
2022-05-03 21:34:25 +03:00
|
|
|
return $
|
|
|
|
Left
|
2022-01-23 17:21:31 +03:00
|
|
|
[ showCompilerErrorForTerminal
|
|
|
|
(waspFilePath, waspFileContent)
|
|
|
|
(getErrorMessageAndCtx analyzeError)
|
|
|
|
]
|
2022-01-20 13:45:14 +03:00
|
|
|
Right decls -> do
|
|
|
|
externalCodeFiles <-
|
|
|
|
ExternalCode.readFiles (CompileOptions.externalCodeDirPath options)
|
2021-04-28 18:36:00 +03:00
|
|
|
maybeDotEnvFile <- findDotEnvFile waspDir
|
2021-12-13 20:06:42 +03:00
|
|
|
maybeMigrationsDir <- findMigrationsDir waspDir
|
2022-05-03 21:34:25 +03:00
|
|
|
return $
|
|
|
|
Right
|
|
|
|
AS.AppSpec
|
|
|
|
{ AS.decls = decls,
|
|
|
|
AS.externalCodeFiles = externalCodeFiles,
|
|
|
|
AS.externalCodeDirPath = CompileOptions.externalCodeDirPath options,
|
|
|
|
AS.migrationsDir = maybeMigrationsDir,
|
|
|
|
AS.dotEnvFile = maybeDotEnvFile,
|
|
|
|
AS.isBuild = CompileOptions.isBuild options
|
|
|
|
}
|
2021-04-16 13:43:57 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
findWaspFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
|
2021-04-16 13:43:57 +03:00
|
|
|
findWaspFile waspDir = do
|
2021-10-28 15:07:26 +03:00
|
|
|
files <- fst <$> Util.IO.listDirectory waspDir
|
2021-07-03 12:00:01 +03:00
|
|
|
return $ (waspDir SP.</>) <$> find isWaspFile files
|
2021-04-16 13:43:57 +03:00
|
|
|
where
|
2021-04-28 18:36:00 +03:00
|
|
|
isWaspFile path =
|
2021-07-03 12:00:01 +03:00
|
|
|
".wasp" `isSuffixOf` SP.toFilePath path
|
|
|
|
&& (length (SP.toFilePath path) > length (".wasp" :: String))
|
2021-04-16 13:43:57 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
findDotEnvFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
|
2021-04-16 13:43:57 +03:00
|
|
|
findDotEnvFile waspDir = do
|
2021-07-03 12:00:01 +03:00
|
|
|
let dotEnvAbsPath = waspDir SP.</> [relfile|.env|]
|
2021-04-28 18:36:00 +03:00
|
|
|
dotEnvExists <- doesFileExist (SP.toFilePath dotEnvAbsPath)
|
|
|
|
return $ if dotEnvExists then Just dotEnvAbsPath else Nothing
|
2021-12-13 20:06:42 +03:00
|
|
|
|
2022-01-20 13:45:14 +03:00
|
|
|
findMigrationsDir ::
|
|
|
|
Path' Abs (Dir WaspProjectDir) ->
|
|
|
|
IO (Maybe (Path' Abs (Dir DbMigrationsDir)))
|
2021-12-13 20:06:42 +03:00
|
|
|
findMigrationsDir waspDir = do
|
|
|
|
let migrationsAbsPath = waspDir SP.</> dbMigrationsDirInWaspProjectDir
|
|
|
|
migrationsExists <- doesDirectoryExist $ SP.fromAbsDir migrationsAbsPath
|
|
|
|
return $ if migrationsExists then Just migrationsAbsPath else Nothing
|