module Unison.Codebase.Editor.HandleInput.ProjectRename
( handleProjectRename,
)
where
import U.Codebase.Sqlite.Project (Project (..))
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Prelude
import Unison.Project (ProjectAndBranch (..), ProjectName)
handleProjectRename :: ProjectName -> Cli ()
handleProjectRename :: ProjectName -> Cli ()
handleProjectRename ProjectName
newName = do
ProjectAndBranch Project
project ProjectBranch
_branch <- Cli (ProjectAndBranch Project ProjectBranch)
Cli.getCurrentProjectAndBranch
let oldName :: ProjectName
oldName = Project
project.name
Bool -> Cli () -> Cli ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ProjectName
oldName ProjectName -> ProjectName -> Bool
forall a. Eq a => a -> a -> Bool
/= ProjectName
newName) do
((forall void. Output -> Transaction void) -> Transaction ())
-> Cli ()
forall a.
((forall void. Output -> Transaction void) -> Transaction a)
-> Cli a
Cli.runTransactionWithRollback \forall void. Output -> Transaction void
rollback -> do
ProjectName -> Transaction (Maybe Project)
Queries.loadProjectByName ProjectName
newName Transaction (Maybe Project)
-> (Maybe Project -> Transaction ()) -> Transaction ()
forall a b. Transaction a -> (a -> Transaction b) -> Transaction b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just Project
_ -> Output -> Transaction ()
forall void. Output -> Transaction void
rollback (ProjectName -> Output
Output.ProjectNameAlreadyExists ProjectName
newName)
Maybe Project
Nothing -> ProjectId -> ProjectName -> Transaction ()
Queries.renameProject Project
project.projectId ProjectName
newName
Output -> Cli ()
Cli.respond (ProjectName -> ProjectName -> Output
Output.RenamedProject ProjectName
oldName ProjectName
newName)