Commit 0e36027e authored by Jan Koniarik's avatar Jan Koniarik
Browse files

basic lazy generation of files

parent 0974a8c6
Pipeline #70532 failed with stage
in 48 seconds
......@@ -9,7 +9,7 @@ import Control.Monad.State.Lazy
import Data.ByteString as BS
( readFile )
import Data.ByteString.Lazy as BSL
( writeFile )
( writeFile, unpack )
import qualified Data.Csv as CSV
import Data.Fixed ( mod' )
import qualified Data.HashMap.Lazy as HM
......@@ -676,7 +676,7 @@ generateBOM walker pconf = do
write_f (filebase, p) = do
let filename = "out/bom/" ++ filebase ++ ".csv"
createDirectoryIfMissing True $ takeDirectory filename
BSL.writeFile filename (CSV.encode $ parts_to_CSV p)
writeFileIfChanged filename (show $ BSL.unpack $ CSV.encode $ parts_to_CSV p)
write_sum :: [ExtrPart] -> IO ()
write_sum parts = do
......
......@@ -58,7 +58,7 @@ library
other-extensions: DuplicateRecordFields, FlexibleInstances, InstanceSigs, TypeSynonymInstances
-- Other library packages from which modules are imported.
build-depends: base >=4.11, hashable >=1.2, process >=1.6, hxt >=9.3, linear >= 1.20, dimensional >= 1.1, numtype-dk >= 0.5, makefile > 1.0, text > 1.0, directory, system-filepath, mtl, Unique, language-openscad, csv, filepath
build-depends: base >=4.11, hashable >=1.2, process >=1.6, hxt >=9.3, linear >= 1.20, dimensional >= 1.1, numtype-dk >= 0.5, makefile > 1.0, text > 1.0, directory, system-filepath, mtl, Unique, language-openscad, csv, filepath, strict
-- Directories containing source files.
hs-source-dirs: src
......
......@@ -23,6 +23,8 @@ import Shared
import System.Directory
import System.IO
import System.Process
import qualified Data.Text.Lazy as TL
class SCADExpr a where
......@@ -195,10 +197,10 @@ stlRenderCommands scad_source stl_file =
renderSCADFiles :: [(Filename, Filename)] -> IO ()
renderSCADFiles pairs = do
createDirectoryIfMissing False ("/tmp/schpin_robot_lib/")
writeMakefile "/tmp/schpin_robot_lib/Makefile" mkfile
writeFileIfChanged "/tmp/schpin_robot_lib/Makefile" $ TL.unpack $ encodeMakefile mk_file
callCommand "cd /tmp/schpin_robot_lib/ && make -j 4 all"
where
mkfile = Makefile { entries = Rule (Target $ Text.pack "all") ( (\x -> Dependency $ Text.pack x) <$> stls ) [] : (render_rule <$> pairs) }
mk_file = Makefile { entries = Rule (Target $ Text.pack "all") ( (\x -> Dependency $ Text.pack x) <$> stls ) [] : (render_rule <$> pairs) }
render_rule :: (Filename, Filename) -> Entry
render_rule (scad, stl) = Rule (Target $ Text.pack stl) [Dependency $ Text.pack scad] (stlRenderCommands scad stl)
......@@ -209,5 +211,5 @@ renderSCADFiles pairs = do
renderSCAD :: Filename -> [TopLevel] -> IO ()
renderSCAD file d = do
createDirectoryIfMissing True $ encodeString $ directory $ decodeString file
withFile file WriteMode $ \handle -> mapM_ (hPutStrLn handle) (generateSCAD d)
writeFileIfChanged file (intercalate "\n" $ generateSCAD d)
......@@ -19,6 +19,7 @@ import Numeric.Units.Dimensional ( Dimension(Dim) )
import Numeric.Units.Dimensional.Prelude ( (*~)
, (/~)
)
import qualified System.IO.Strict as IOS
import qualified Numeric.Units.Dimensional.Prelude
as U
......@@ -97,3 +98,12 @@ quatToRPY (Quaternion w (V3 x y z)) = V3 roll pitch yaw
t3 = 2 * (w * z + x * y)
t4 = 1 - 2 * (y * y + z * z)
yaw = atan2 t3 t4
writeFileIfChanged :: Filename -> String -> IO ()
writeFileIfChanged fname content = do
present_content <- IOS.readFile fname
if present_content == content then
return ()
else
writeFile fname content
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment