Refactor Reanimate.Povray and OS-specifc POVRay

Further to pull request #225, I realised subsequently that the common code in the two `POVRay.mkPovrayImage'` functions could be returned to module `Reanimate.Povray`, and all that needed to be Windows- or Unix-like-specific was a `runPOVRay` function.

This pull request does that refactoring.
This commit is contained in:
Mike Pilgrem 2021-10-16 18:56:58 +01:00 committed by David Himmelstrup
commit d1096abb63
3 changed files with 34 additions and 43 deletions

View file

@ -14,15 +14,22 @@ module Reanimate.Povray
, povrayExtreme'
) where
import Data.Hashable (Hashable (hash))
import Data.Text (Text)
import qualified Data.Text as T (concat, pack)
import qualified Data.Text.IO as T (writeFile)
import Graphics.SvgTree (Tree)
import POVRay (mkPovrayImage')
import POVRay (runPOVRay)
import Reanimate.Cache (cacheFile, encodeInt)
import Reanimate.Constants (screenHeight, screenWidth)
import Reanimate.Parameters (pNoExternals)
import Reanimate.Raster (mkImage)
import Reanimate.Svg.Constructors (mkText)
import System.FilePath (replaceExtension, (<.>))
import System.IO.Unsafe (unsafePerformIO)
povrayRaw :: [String] -> Text -> Tree
povrayRaw args script =
unsafePerformIO $ mkPovrayImage args script
@ -83,3 +90,14 @@ mkPovrayImage :: [String] -> Text -> IO Tree
mkPovrayImage _ script | pNoExternals = pure $ mkText script
mkPovrayImage args script =
mkImage screenWidth screenHeight <$> mkPovrayImage' args script
mkPovrayImage' :: [String] -> Text -> IO FilePath
mkPovrayImage' _ _ | pNoExternals = pure "/povray/has/been/disabled"
mkPovrayImage' args script = cacheFile template $ \target -> do
let pov_file = replaceExtension target "pov"
otherArgs = ["-D", "+UA", "+I" ++ pov_file, "+O" ++ target]
T.writeFile pov_file script
runPOVRay $ args ++ otherArgs
where
template = encodeInt (hash key) <.> "png"
key = T.concat (script:map T.pack args)

View file

@ -1,27 +1,16 @@
module POVRay
( povrayApp
, mkPovrayImage'
, runPOVRay
) where
import Data.Hashable (Hashable (hash))
import Data.Text (Text)
import qualified Data.Text as T (concat, pack)
import qualified Data.Text.IO as T (writeFile)
import Reanimate.Cache (cacheFile, encodeInt)
import Reanimate.Misc (requireExecutable, runCmd)
import Reanimate.Parameters (pNoExternals)
import System.FilePath (replaceExtension, (<.>))
import Reanimate.Misc (requireExecutable, runCmd)
-- | Name of the POV-Ray executable
povrayApp :: String
povrayApp = "povray"
mkPovrayImage' :: [String] -> Text -> IO FilePath
mkPovrayImage' _ _ | pNoExternals = pure "/povray/has/been/disabled"
mkPovrayImage' args script = cacheFile template $ \target -> do
-- | Run the POV-Ray executable with arguments
runPOVRay :: [String] -> IO ()
runPOVRay args = do
exec <- requireExecutable povrayApp
let pov_file = replaceExtension target "pov"
T.writeFile pov_file script
runCmd exec (args ++ ["-D", "+UA", "+I" ++ pov_file, "+o" ++ target])
where
template = encodeInt (hash key) <.> "png"
key = T.concat (script:map T.pack args)
runCmd exec args

View file

@ -1,45 +1,29 @@
module POVRay
( povrayApp
, mkPovrayImage'
, runPOVRay
) where
import Data.Hashable (Hashable (hash))
import Data.Text (Text)
import qualified Data.Text as T (concat, pack)
import qualified Data.Text.IO as T (writeFile)
import Reanimate.Cache (cacheFile, encodeInt)
import Reanimate.Misc (requireExecutable, runCmd)
import Reanimate.Parameters (pNoExternals)
import System.FilePath (replaceExtension, (<.>))
import System.IO (hClose, hPutStrLn)
import System.IO.Temp (withSystemTempFile)
-- | Name of the POV-Ray executable
povrayApp :: String
povrayApp = "pvengine64" -- Assumes 64 bit Windows
mkPovrayImage' :: [String] -> Text -> IO FilePath
mkPovrayImage' _ _ | pNoExternals = pure "/povray/has/been/disabled"
mkPovrayImage' args script = cacheFile template $ \target -> do
-- | Run the POV-Ray executable with arguments
runPOVRay :: [String] -> IO ()
runPOVRay args = do
exec <- requireExecutable povrayApp
let pov_file = replaceExtension target "pov"
exec' = '"' : exec ++ "\"" -- Wrap exec in "" because the path is likely
let exec' = '"' : exec ++ "\"" -- Wrap exec in "" because the path is likely
-- to includes spaces
otherArgs = [ "-D"
, "+UA"
, "+I" ++ pov_file
, "+O" ++ target
, "/EXIT" -- Note [/EXIT special command-line option]
]
command = concatMap (' ':) (exec' : args ++ otherArgs)
T.writeFile pov_file script
args' = "/EXIT" : args -- Note [/EXIT special command-line option]
command = concatMap (' ':) (exec' : args')
-- Note [Use of a batch file]
withSystemTempFile "pvcommand.bat" $ \path h -> do
hPutStrLn h command
hClose h
runCmd path []
where
template = encodeInt (hash key) <.> "png"
key = T.concat (script:map T.pack args)
{-
Note [/EXIT special command-line option]