wasp/waspc/src/Wasp/Lib.hs

82 lines
3.0 KiB
Haskell
Raw Normal View History

module Wasp.Lib
2021-04-28 18:36:00 +03:00
( compile,
Generator.setup,
Generator.start,
ProjectRootDir,
findWaspFile,
2021-04-28 18:36:00 +03:00
)
where
2021-04-28 18:36:00 +03:00
import Data.List (find, isSuffixOf)
import StrongPath (Abs, Dir, File', Path', relfile)
2021-04-28 18:36:00 +03:00
import qualified StrongPath as SP
import System.Directory (doesDirectoryExist, doesFileExist)
import Wasp.Common (DbMigrationsDir, WaspProjectDir, dbMigrationsDirInWaspProjectDir)
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
type CompileError = String
2021-04-28 18:36:00 +03:00
compile ::
Path' Abs (Dir WaspProjectDir) ->
Path' Abs (Dir ProjectRootDir) ->
2021-04-28 18:36:00 +03:00
CompileOptions ->
IO (Either CompileError ())
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
maybeMigrationsDir <- findMigrationsDir waspDir
2021-04-28 18:36:00 +03:00
( wasp
`Wasp.setDotEnvFile` maybeDotEnvFile
`Wasp.setMigrationsDir` maybeMigrationsDir
2021-04-28 18:36:00 +03:00
`enrichWaspASTBasedOnCompileOptions` options
)
>>= generateCode
2019-04-19 16:22:14 +03:00
where
generateCode wasp = Generator.writeWebAppCode wasp outDir options >> return (Right ())
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
)
findWaspFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
findWaspFile waspDir = do
files <- fst <$> Util.IO.listDirectory waspDir
return $ (waspDir SP.</>) <$> find isWaspFile files
where
2021-04-28 18:36:00 +03:00
isWaspFile path =
".wasp" `isSuffixOf` SP.toFilePath path
&& (length (SP.toFilePath path) > length (".wasp" :: String))
findDotEnvFile :: Path' Abs (Dir WaspProjectDir) -> IO (Maybe (Path' Abs File'))
findDotEnvFile waspDir = do
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
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