Skip to content

Commit

Permalink
Update cabal script environment hack for Cabal 3.8
Browse files Browse the repository at this point in the history
Thanks to useful pointer from haskell/cabal#6999 (comment).

File watching actually doesn't work, as the same hash is reused for the same script (based on location?) even after edits. This is much cleaner anyway.
  • Loading branch information
georgefst committed Jan 5, 2023
1 parent 659a4ad commit 818aab1
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions CabScriptWatchEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,32 @@ It is the workaround mentioned [here](https://github.com/haskell/cabal/issues/69
-}
module CabScriptWatchEnv (main) where

import Control.Concurrent
import Control.Monad
import Data.ByteString.Char8 (
isPrefixOf,
isSuffixOf,
lines,
putStrLn,
unlines,
)
import Data.ByteString.RawFilePath (readFile, writeFile)
import Data.Foldable
import RawFilePath
import System.Exit
import System.FilePath.ByteString
import System.INotify
import System.Posix.ByteString
import Prelude hiding (lines, putStrLn, readFile, unlines, writeFile)

main :: IO ()
main = do
m <- newEmptyMVar
tmp <- getTemporaryDirectory
inot <- initINotify
void $ addWatch inot [Create] tmp \case
Created isDir p -> do
when (isDir && "cabal-repl." `isPrefixOf` p) $
void $ addWatch inot [MoveIn] (tmp </> p) \case
MovedIn _ p' _ -> do
when (".ghc.environment." `isPrefixOf` p' && not (".tmp" `isSuffixOf` p')) do
putStrLn $ "Found: " <> p'
putMVar m (tmp </> p </> p')
_ -> pure ()
_ -> pure ()
envFile <- takeMVar m
contents <- readFile envFile
writeFile (takeFileName envFile) $ unlines $ filter (not . ("package-db dist-newstyle" `isPrefixOf`)) $ lines contents
p <-
getArgs >>= \case
[p] -> pure p
_ -> putStrLn "Provide a script-build folder" >> exitFailure
envFiles <- filter (".ghc.environment." `isPrefixOf`) <$> listDirectory p
if null envFiles
then putStrLn "No environment files found" >> exitFailure
else putStrLn "Found environment files:"
for_ envFiles \envFile -> do
putStrLn envFile
contents <- readFile $ p </> envFile
writeFile (takeFileName envFile) $ unlines $ filter (not . ("package-db dist-newstyle" `isPrefixOf`)) $ lines contents

0 comments on commit 818aab1

Please sign in to comment.