2021-11-11 15:26:20 +03:00
|
|
|
module Wasp.Lib
|
2021-04-28 18:36:00 +03:00
|
|
|
( compile,
|
|
|
|
Generator.setup,
|
|
|
|
Generator.start,
|
|
|
|
ProjectRootDir,
|
2021-08-23 19:13:04 +03:00
|
|
|
findWaspFile,
|
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)
|
|
|
|
import Wasp.Common (DbMigrationsDir, WaspProjectDir, dbMigrationsDirInWaspProjectDir)
|
2021-11-11 15:26:20 +03:00
|
|
|
import Wasp.CompileOptions (CompileOptions)
|
|
|
|
import qualified Wasp.CompileOptions as CompileOptions
|
|
|
|
import qualified Wasp.ExternalCode as ExternalCode
|
|
|
|
import qualified Wasp.Generator as Generator
|
|
|
|
import Wasp.Generator.Common (ProjectRootDir)
|
|
|
|
import qualified Wasp.Parser as Parser
|
|
|
|
import qualified Wasp.Util.IO as Util.IO
|
|
|
|
import Wasp.Wasp (Wasp)
|
|
|
|
import qualified Wasp.Wasp as Wasp
|
2019-05-04 00:24:57 +03:00
|
|
|
|
2019-03-24 14:42:36 +03:00
|
|
|
type CompileError = 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 ->
|
|
|
|
IO (Either CompileError ())
|
2021-04-16 13:43:57 +03:00
|
|
|
compile waspDir outDir options = do
|
2021-04-28 18:36:00 +03:00
|
|
|
maybeWaspFile <- findWaspFile waspDir
|
|
|
|
case maybeWaspFile of
|
|
|
|
Nothing -> return $ Left "Couldn't find a single *.wasp file."
|
|
|
|
Just waspFile -> do
|
|
|
|
waspStr <- readFile (SP.toFilePath waspFile)
|
2019-04-19 16:22:14 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
case Parser.parseWasp waspStr of
|
|
|
|
Left err -> return $ Left (show err)
|
|
|
|
Right wasp -> do
|
|
|
|
maybeDotEnvFile <- findDotEnvFile waspDir
|
2021-12-13 20:06:42 +03:00
|
|
|
maybeMigrationsDir <- findMigrationsDir waspDir
|
2021-04-28 18:36:00 +03:00
|
|
|
( wasp
|
|
|
|
`Wasp.setDotEnvFile` maybeDotEnvFile
|
2021-12-13 20:06:42 +03:00
|
|
|
`Wasp.setMigrationsDir` maybeMigrationsDir
|
2021-04-28 18:36:00 +03:00
|
|
|
`enrichWaspASTBasedOnCompileOptions` options
|
|
|
|
)
|
|
|
|
>>= generateCode
|
2019-04-19 16:22:14 +03:00
|
|
|
where
|
2020-01-21 16:55:49 +03:00
|
|
|
generateCode wasp = Generator.writeWebAppCode wasp outDir options >> return (Right ())
|
2021-04-16 13:43:57 +03:00
|
|
|
|
2021-02-01 19:37:42 +03:00
|
|
|
enrichWaspASTBasedOnCompileOptions :: Wasp -> CompileOptions -> IO Wasp
|
|
|
|
enrichWaspASTBasedOnCompileOptions wasp options = do
|
2021-04-28 18:36:00 +03:00
|
|
|
externalCodeFiles <- ExternalCode.readFiles (CompileOptions.externalCodeDirPath options)
|
|
|
|
return
|
|
|
|
( wasp
|
|
|
|
`Wasp.setExternalCodeFiles` externalCodeFiles
|
|
|
|
`Wasp.setIsBuild` 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
|
|
|
|
|
|
|
findMigrationsDir :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs (Dir DbMigrationsDir)))
|
|
|
|
findMigrationsDir waspDir = do
|
|
|
|
let migrationsAbsPath = waspDir SP.</> dbMigrationsDirInWaspProjectDir
|
|
|
|
migrationsExists <- doesDirectoryExist $ SP.fromAbsDir migrationsAbsPath
|
|
|
|
return $ if migrationsExists then Just migrationsAbsPath else Nothing
|