{-# LANGUAGE DataKinds #-}
module Aura.Pkgbuild.Editing ( hotEdit ) where
import Aura.Languages
import Aura.Settings
import Aura.Types
import Aura.Utils
import Data.Generics.Product (field)
import Lens.Micro ((.~))
import RIO
import RIO.Directory (setCurrentDirectory)
import System.Path (toFilePath)
import System.Path.IO (getCurrentDirectory, getTemporaryDirectory)
import System.Process.Typed (proc, runProcess)
edit :: (FilePath -> IO a) -> Buildable -> IO Buildable
edit f p = do
writeFileBinary filename $ p ^. field @"pkgbuild" . field @"pkgbuild"
void $ f filename
newPB <- readFileBinary filename
pure (p & field @"pkgbuild" .~ Pkgbuild newPB)
where filename = "PKGBUILD"
hotEdit :: Settings -> Buildable -> IO Buildable
hotEdit ss b
| not $ switch ss HotEdit = pure b
| otherwise = do
ans <- liftIO $ optionalPrompt ss (hotEdit_1 $ b ^. field @"name")
bool (pure b) f ans
where f = do
here <- getCurrentDirectory
tmp <- getTemporaryDirectory
setCurrentDirectory $ toFilePath tmp
b' <- edit (runProcess . proc (editorOf ss) . (:[])) b
setCurrentDirectory $ toFilePath here
pure b'