Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion waspc/cli/src/Wasp/Cli/Command/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Wasp.Cli.Command.Compile
withSetupGoals,
printCompilationResult,
printWarningsAndErrorsIfAny,
printWarningsIfAny,
analyze,
analyzeWithOptions,
analyzeWithDiagnosticsOnStderr,
Expand Down Expand Up @@ -42,12 +43,20 @@ import Wasp.Project (CompileError, CompileResult (..), CompileWarning, WaspProje
import qualified Wasp.Project
import qualified Wasp.Project.BuildType as BuildType
import Wasp.Project.Common (generatedAppDirInWaspProjectDir)
import Wasp.Project.Db.Dev (findDevDatabaseUrl)
import Wasp.Util.IO (doesDirectoryExist, removeDirectory)

-- | Meant for the standalone `wasp compile` command: commands that hold the
-- project lock themselves should call 'compile' instead.
compileCommand :: Command ([CompileWarning], AS.AppSpec)
compileCommand = ProjectLock.acquireExclusive compile
compileCommand = ProjectLock.acquireExclusive $ do

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might look a big heavy, but it's to preserve the current behaviour. We drop this change in a later PR because it mostly always fails (wasp compile won't auto start the database): #5427

(warnings, appSpec) <- compile
InWaspProject waspProjectDir <- require
let outDir = waspProjectDir </> generatedAppDirInWaspProjectDir
devDatabaseUrl <- liftIO $ findDevDatabaseUrl appSpec
dbWarnings <- liftIO $ Wasp.Project.warnIfDbNeedsMigration appSpec devDatabaseUrl outDir
liftIO $ printWarningsIfAny dbWarnings
return (warnings, appSpec)

-- | Same like 'compileWithOptions', but with default compile options.
compile :: Command ([CompileWarning], AS.AppSpec)
Expand Down
11 changes: 7 additions & 4 deletions waspc/cli/src/Wasp/Cli/Command/Start.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import StrongPath (Abs, Dir, Path', (</>))
import qualified StrongPath as SP
import Wasp.Cli.Command (Command, CommandError (..), require)
import Wasp.Cli.Command.Call (Arguments)
import Wasp.Cli.Command.Compile (compile, printWarningsAndErrorsIfAny)
import Wasp.Cli.Command.Compile (compile, printWarningsAndErrorsIfAny, printWarningsIfAny)
import Wasp.Cli.Command.Db (throwIfDbConnectionNotEstablished)
import Wasp.Cli.Command.Message (cliSendMessageC)
import Wasp.Cli.Command.News (fetchAndListMustSeeNewsIfDue)
Expand All @@ -29,10 +29,10 @@ import qualified Wasp.Generator.ServerGenerator.RunConfig as ServerGenerator
import qualified Wasp.Generator.ServerGenerator.Start as ServerGenerator.Start
import qualified Wasp.Generator.WebAppGenerator.RunConfig as WebAppGenerator
import qualified Wasp.Message as Msg
import Wasp.Project (CompileError, CompileWarning)
import Wasp.Project (CompileError, CompileWarning, warnIfDbNeedsMigration)
import Wasp.Project.Common (WaspProjectDir, findFileInWaspProjectDir, generatedAppDirInWaspProjectDir)
import qualified Wasp.Project.Env as Env
import Wasp.Project.RunConfig (ProjectRunConfig)
import Wasp.Project.RunConfig (ProjectRunConfig (databaseUrl))

-- | Does initial compile of wasp code and then runs the generated project.
-- It also listens for any file changes and recompiles and restarts generated project accordingly.
Expand All @@ -55,7 +55,7 @@ start = withArguments startArgsParser $ \args ->

cliSendMessageC $ Msg.Start "Starting compilation and setup phase. Hold tight..."

(warnings, appSpec) <- compile
(compileWarnings, appSpec) <- compile

ports <- findProjectPorts (args.clientPort, args.serverPort)
-- The ports and custom URLs stay the same for the whole session, while the
Expand All @@ -65,6 +65,9 @@ start = withArguments startArgsParser $ \args ->
assertImplicitEnvVarsDontOverrideWaspEnvVars waspProjectDir projectRunConfig

throwIfDbConnectionNotEstablished projectRunConfig
dbWarnings <- liftIO $ warnIfDbNeedsMigration appSpec projectRunConfig.databaseUrl outDir
liftIO $ printWarningsIfAny dbWarnings
let warnings = compileWarnings ++ dbWarnings

cliSendMessageC $ Msg.Start "Listening for file changes..."
cliSendMessageC $ Msg.Start "Starting up generated project..."
Expand Down
13 changes: 11 additions & 2 deletions waspc/cli/src/Wasp/Cli/Command/Watch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import Wasp.Cli.ProjectLock.Handle (ProjectLock, setWatcherStatus)
import qualified Wasp.Generator.Common as Wasp.Generator
import Wasp.Generator.ServerGenerator.Start (CompileChanges (..), CompileOutcome (..), ServerEvent (..))
import qualified Wasp.Message as Msg
import Wasp.Project (CompileError, CompileWarning, WaspProjectDir, compileResultWarningsAndErrors)
import Wasp.Project (CompileError, CompileWarning, WaspProjectDir, compileResultWarningsAndErrors, warnIfDbNeedsMigration)
import Wasp.Project.Common (srcDirInWaspProjectDir)
import Wasp.Project.Db.Dev (findDevDatabaseUrl)
import Wasp.Project.RunConfig (ProjectRunConfig)

-- TODO: Idea: Read .gitignore file, and ignore everything from it. This will then also cover the
Expand Down Expand Up @@ -149,7 +150,7 @@ watch waspProjectDir outDir projectLock ongoingCompilationResultMVar makeProject
recompile :: UTCTime -> [Path' (Rel WaspProjectDir) File'] -> IO ()
recompile compileStartTime changedProjectFiles = do
cliSendMessage $ Msg.Start "Recompiling on file change..."
compileResult <- compileIO waspProjectDir outDir
compileResult <- compileIO waspProjectDir outDir >>= addDbMigrationWarnings
let (warnings, errors) = compileResultWarningsAndErrors compileResult

printCompilationResult (warnings, errors)
Expand All @@ -172,6 +173,14 @@ watch waspProjectDir outDir projectLock ongoingCompilationResultMVar makeProject
updateOngoingCompilationResultMVar (warnings, errors)
onServerEvent $ Compiled compileOutcome

addDbMigrationWarnings :: CompileResult -> IO CompileResult
addDbMigrationWarnings compileResult = case _compileOutcome compileResult of
Right appSpec -> do
devDatabaseUrl <- findDevDatabaseUrl appSpec
dbWarnings <- warnIfDbNeedsMigration appSpec devDatabaseUrl outDir
return compileResult {_compileWarnings = _compileWarnings compileResult ++ dbWarnings}
Left _ -> return compileResult

-- TODO: This is a hardcoded approach to ignoring most of the common tmp files that editors
-- create next to the source code. Bad thing here is that users can't modify this,
-- so better approach would be probably to use information from .gitignore instead, or
Expand Down
20 changes: 2 additions & 18 deletions waspc/src/Wasp/Generator/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ module Wasp.Generator.Setup
)
where

import Control.Monad (forM_, when)
import Control.Monad (forM_)
import Control.Monad.Except (ExceptT, runExceptT, throwError)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Writer.Strict (WriterT, runWriterT, tell)
import Control.Monad.Writer.Strict (WriterT, runWriterT)
import Data.Either (fromLeft)
import Data.List (nub)
import Data.Maybe (maybeToList)
import StrongPath (Abs, Dir, Path')
import System.Exit (ExitCode (..))
import Wasp.AppSpec (AppSpec)
Expand All @@ -30,12 +29,10 @@ import Wasp.Generator.NpmInstall (installNpmDependenciesWithInstallRecord)
import qualified Wasp.Generator.SdkGenerator as SdkGenerator
import qualified Wasp.Job as J
import qualified Wasp.Message as Msg
import Wasp.Project.Db.Dev (devDatabaseEnvVars, findDevDatabaseUrl)

-- | Something a command needs from the generated app, after code generation.
data SetupGoal
= PrismaCliReady
| DbMigrationsChecked
| SdkReady
| UserCodeTypeChecked
deriving (Eq, Show, Enum, Bounded)
Expand All @@ -48,7 +45,6 @@ allSetupGoals = [minBound .. maxBound]
data SetupStep
= InstallNpmDeps
| FormatPrismaSchema
| WarnIfDbNeedsMigration
| GeneratePrismaClient
| BuildSdk
| TypeCheckUserCode
Expand All @@ -65,7 +61,6 @@ allSetupSteps = [minBound .. maxBound]
prerequisites :: SetupStep -> [SetupStep]
prerequisites InstallNpmDeps = []
prerequisites FormatPrismaSchema = [InstallNpmDeps]
prerequisites WarnIfDbNeedsMigration = [InstallNpmDeps, FormatPrismaSchema]
prerequisites GeneratePrismaClient = [InstallNpmDeps, FormatPrismaSchema]
-- Runs `tsc`, and the SDK imports the Prisma client.
prerequisites BuildSdk = [InstallNpmDeps, GeneratePrismaClient]
Expand All @@ -92,7 +87,6 @@ resolveSetupSteps = withPrerequisites . concatMap stepsForGoal

stepsForGoal :: SetupGoal -> [SetupStep]
stepsForGoal PrismaCliReady = [InstallNpmDeps, FormatPrismaSchema]
stepsForGoal DbMigrationsChecked = [WarnIfDbNeedsMigration]
stepsForGoal SdkReady = [BuildSdk]
stepsForGoal UserCodeTypeChecked = [TypeCheckUserCode]

Expand All @@ -107,9 +101,6 @@ runSetupStep :: SetupStep -> AppSpec -> Path' Abs (Dir GeneratedAppDir) -> Msg.S
runSetupStep step spec generatedAppDir sendMessage = case step of
InstallNpmDeps -> installDependencies spec generatedAppDir sendMessage
FormatPrismaSchema -> formatPrismaSchema generatedAppDir
-- A production build is deployed elsewhere, so there is no local database
-- to check for pending migrations.
WarnIfDbNeedsMigration -> when (AS.isDevelopment spec) $ warnIfDbNeedsMigration spec generatedAppDir

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes the migration check from the compile setup so compile can be "static".

GeneratePrismaClient -> generatePrismaClient spec generatedAppDir sendMessage
-- todo(filip): Should we consider building SDK as part of code generation?
-- todo(filip): Avoid building on each setup if we don't need to.
Expand All @@ -123,13 +114,6 @@ installDependencies spec generatedAppDir sendMessage = do
Left npmInstallError -> throwError [npmInstallError]
Right () -> liftIO $ sendMessage $ Msg.Success "Successfully completed npm install."

warnIfDbNeedsMigration :: AppSpec -> Path' Abs (Dir GeneratedAppDir) -> Setup ()
warnIfDbNeedsMigration spec generatedAppDir = do
warning <- liftIO $ do
envVars <- devDatabaseEnvVars <$> findDevDatabaseUrl spec
DbGenerator.warnIfDbNeedsMigration envVars spec generatedAppDir
tell $ maybeToList warning

formatPrismaSchema :: Path' Abs (Dir GeneratedAppDir) -> Setup ()
formatPrismaSchema generatedAppDir = do
result <- liftIO $ DbGenerator.formatPrismaSchema generatedAppDir
Expand Down
10 changes: 10 additions & 0 deletions waspc/src/Wasp/Project.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Wasp.Project
CompileError,
CompileWarning,
analyzeWaspProject,
warnIfDbNeedsMigration,
compileAndRenderDockerfile,
)
where
Expand All @@ -21,10 +22,12 @@ import StrongPath (Abs, Dir, Path')
import qualified Wasp.AppSpec as AS
import Wasp.CompileOptions (CompileOptions (sendMessage, setupGoals))
import qualified Wasp.Generator as Generator
import qualified Wasp.Generator.DbGenerator as DbGenerator
import qualified Wasp.Generator.DockerGenerator as DockerGenerator
import Wasp.Generator.WriteFileDrafts (ChangedGeneratedAppPath)
import Wasp.Project.Analyze (analyzeWaspProject)
import Wasp.Project.Common (CompileError, CompileWarning, WaspProjectDir)
import Wasp.Project.Db.Dev (devDatabaseEnvVars)
import qualified Wasp.Project.Env as Project.Env

data CompileResult = CompileResult
Expand Down Expand Up @@ -75,6 +78,13 @@ generateCode appSpec outDir options = do
_compileChangedGeneratedAppPaths = changedGeneratedAppPaths
}

-- | Warns if the dev database needs a migration.
-- Needs npm dependencies installed and the Prisma schema formatted.
warnIfDbNeedsMigration :: AS.AppSpec -> Maybe String -> Path' Abs (Dir Generator.GeneratedAppDir) -> IO [CompileWarning]
warnIfDbNeedsMigration appSpec devDatabaseUrl outDir =
map show . maybeToList
<$> DbGenerator.warnIfDbNeedsMigration (devDatabaseEnvVars devDatabaseUrl) appSpec outDir

compileAndRenderDockerfile :: Path' Abs (Dir WaspProjectDir) -> CompileOptions -> IO (Either [CompileError] Text)
compileAndRenderDockerfile waspDir compileOptions = do
(appSpecOrAnalyzerErrors, _analyzerWarnings) <- analyzeWaspProject waspDir compileOptions
Expand Down
Loading