rewrite 0.2 → 0.4
raw patch · 2 files changed
+24/−46 lines, 2 filesdep +bytestringdep +process-extrasdep −processdep −temporary
Dependencies added: bytestring, process-extras
Dependencies removed: process, temporary
Files
- rewrite.cabal +8/−7
- rewrite.hs +16/−39
rewrite.cabal view
@@ -1,15 +1,12 @@--- Initial rewrite.cabal generated by cabal init. For further --- documentation, see http://haskell.org/cabal/users-guide/- name: rewrite-version: 0.2+version: 0.4 synopsis: open file and rewrite it with new contents description: In the Unix shell there is no easy way to use a filter program to change the contents of a file in-place. For example, if you want to sort a file in place, this will not work: .- sort < myfile > myfile+ sort \< myfile \> myfile . All that will get you is an empty myfile. This package gives you the rewrite program, so this will work:@@ -26,12 +23,16 @@ build-type: Simple cabal-version: >=1.8 +source-repository head+ type: git+ location: git://github.com/massysett/rewrite.git+ executable rewrite main-is: rewrite.hs build-depends: base ==4.6.* , multiarg ==0.16.*- , temporary ==1.1.*- , process ==1.1.* , directory ==1.2.*+ , process-extras ==0.2.*+ , bytestring ==0.10.* ghc-options: -Wall
rewrite.hs view
@@ -3,10 +3,10 @@ import Data.Either (partitionEithers) import qualified System.Console.MultiArg as MA import qualified System.IO as IO-import System.IO.Temp (withSystemTempDirectory) import qualified System.Exit as Exit+import System.Process.ByteString (readProcessWithExitCode)+import qualified Data.ByteString as BS import qualified System.Directory as D-import qualified System.Process as P help :: String -> String help pn = unlines@@ -67,46 +67,23 @@ -> [ProgramOpt] - -> FilePath- -- ^ Temporary directory- -> IO ()-runProgram mayBak inFile pn opts tempPath =- let outPath = tempPath ++ "/output"- in IO.withFile outPath IO.WriteMode $ \outHandle ->- IO.withFile inFile IO.ReadMode $ \inHandle -> do- writeReadme tempPath- let cp = P.CreateProcess- { P.cmdspec = P.RawCommand pn opts- , P.cwd = Nothing- , P.env = Nothing- , P.std_in = P.UseHandle inHandle- , P.std_out = P.UseHandle outHandle- , P.std_err = P.Inherit- , P.close_fds = False- , P.create_group = False }- (_, _, _, procHndle) <- P.createProcess cp- code <- P.waitForProcess procHndle- _ <- case code of- Exit.ExitSuccess -> return ()- Exit.ExitFailure bad ->- errExit $ "program " ++ pn ++ " exited with code "- ++ show bad- _ <- case mayBak of- Nothing -> return ()- Just bak -> doBackup inFile bak- D.copyFile outPath inFile+runProgram mayBak inFile pn opts = do+ input <- BS.readFile inFile+ (code, out, err) <- readProcessWithExitCode pn opts input+ BS.hPutStr IO.stderr err+ _ <- case code of+ Exit.ExitSuccess -> return ()+ Exit.ExitFailure bad ->+ errExit $ "program " ++ pn ++ " exited with code "+ ++ show bad+ _ <- case mayBak of+ Nothing -> return ()+ Just bak -> doBackup inFile bak+ BS.writeFile inFile out -writeReadme- :: FilePath- -- ^ Temporary directory- -> IO ()--writeReadme fp = writeFile (fp ++ "/README")- "This directory created by the rewrite program."- main :: IO () main = do (mayBak, inf, pn, opts) <- parseArgs- withSystemTempDirectory "rewrite" $ runProgram mayBak inf pn opts+ runProgram mayBak inf pn opts