module Aura.Pkgbuild.Editing ( hotEdit ) where
import Aura.IO
import Aura.Languages
import Aura.Settings
import Aura.Types
import RIO
import RIO.Directory
import System.Process.Typed (proc, runProcess)
edit :: (FilePath -> IO a) -> Buildable -> IO Buildable
edit f p = do
writeFileBinary filename . pkgbuild $ bPkgbuild p
void $ f filename
newPB <- readFileBinary filename
pure (p { bPkgbuild = Pkgbuild newPB})
where
filename :: FilePath
filename = "PKGBUILD"
hotEdit :: Settings -> Buildable -> IO Buildable
hotEdit ss b
| not $ switch ss HotEdit = pure b
| otherwise = do
ans <- liftIO $ optionalPrompt ss (hotEdit_1 $ bName b)
bool (pure b) f ans
where
f :: IO Buildable
f = do
here <- getCurrentDirectory
tmp <- getTemporaryDirectory
setCurrentDirectory tmp
b' <- edit (runProcess . proc (editorOf ss) . (:[])) b
setCurrentDirectory here
pure b'