Skip to content

Commit

Permalink
🧩 [rename]: output -> evaluate, copyFiles -> publish for consistency …
Browse files Browse the repository at this point in the history
…with commands.
  • Loading branch information
rolyp committed Dec 19, 2024
1 parent fe4d1d6 commit ed7a0ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
58 changes: 35 additions & 23 deletions src/Fluid.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ data Program = Program
data Command = Evaluate Program | Publish Folder Boolean

between :: forall a. Pattern -> Pattern -> Endo (String -> Either String a)
between p1 p2 = \f s -> do
case (stripPrefix p1) s >>= (stripSuffix p2) of
between p1 p2 f s =
case (stripPrefix p1) s >>= stripSuffix p2 of
Just rest -> f rest
Nothing -> Left ("Expected " <> show p1 <> "..." <> show p2 <> " but got ...")

Expand Down Expand Up @@ -76,42 +76,55 @@ program = ado
fileName <- strOption (long "file" <> short 'f' <> help "The file to parse")
in Program { imports, datasets, fileName }

publish :: Parser Command
publish = Publish <$> (Folder <$> strOption (long "website" <> short 'w' <> help "root directory of website under dist/" <> value "Misc"))
<*> (switch $ fold [ long "local", short 'l', help "Are you publishing from source (false), or an npm package (true)?" ])
commands :: { publish :: Parser Command, evaluate :: Parser Command }
commands =
{ publish: Publish <$> (Folder <$> strOption (long "website" <> short 'w' <> help "root directory of website under dist/" <> value "Misc"))
<*> switch (fold [ long "local", short 'l', help "Are you publishing from source (false), or an npm package (true)?" ])
, evaluate: Evaluate <$> program
}

commandParser :: Parser Command
commandParser = subparser
( command "evaluate" (info (Evaluate <$> program) (progDesc "Evaluate a file"))
<> command "publish" (info publish (progDesc "Publish a file"))
( command "evaluate" (info commands.evaluate (progDesc "Evaluate a file"))
<> command "publish" (info commands.publish (progDesc "Publish a file"))
)

dispatchCommand ∷ Command → Aff Unit
dispatchCommand = case _ of
Evaluate p -> void $ output p
Publish (Folder website) b -> do -- Publish -> BundleWebsite?
_ <- liftEffect $ copyFiles website b
log "Published"
dispatchCommand (Evaluate p) =
void $ evaluate p
dispatchCommand (Publish (Folder website) b) = do -- Publish -> BundleWebsite?
void $ liftEffect $ publish website b
log "Published"

copyOptions :: ExecOptions
copyOptions =
{ cwd: Nothing,
env: Nothing, timeout: Nothing, killSignal: Nothing, maxBuffer: Nothing, uid: Nothing, gid: Nothing, encoding: Nothing, shell: Nothing }
{ cwd: Nothing
, env: Nothing
, timeout: Nothing
, killSignal: Nothing
, maxBuffer: Nothing
, uid: Nothing
, gid: Nothing
, encoding: Nothing
, shell: Nothing
}

-- Rename to bundleWebsite?
copyFiles ∷ String -> Boolean -> Effect ChildProcess
copyFiles website b = do
let cmd = if b then "./node_modules/@explorable-viz/fluid/script/bundle-website.sh -w " <> website <> " -r true" else "./script/bundle-website.sh -w " <> website
exec cmd copyOptions \{ error, stdout } -> do
publish ∷ String -> Boolean -> Effect ChildProcess
publish website b =
exec cmd copyOptions \{ error, stdout } ->
case error of
(Just err) -> logShow err
Nothing -> do
out <- toString ASCII stdout
log out
where
cmd =
if b then "./node_modules/@explorable-viz/fluid/script/bundle-website.sh -w " <> website <> " -r true"
else "./script/bundle-website.sh -w " <> website

main :: Effect Unit
main = runAff_ callback do
dispatchCommand =<< (liftEffect $ execParser opts)
main = runAff_ callback (dispatchCommand =<< liftEffect (execParser opts))
where
opts = info (commandParser <**> helper) (fullDesc <> progDesc "Parse a file" <> header "parse - a simple parser")

Expand All @@ -120,9 +133,8 @@ callback = case _ of
Left err -> logShow err
Right _ -> log "Success"

-- Rename to Evaluate?
output :: Program -> Aff (Val Unit)
output (Program { imports, datasets, fileName }) = do
evaluate :: Program -> Aff (Val Unit)
evaluate (Program { imports, datasets, fileName }) = do
progCxt <- loadProgCxt imports datasets
{ e, gconfig } <- prepConfig (File fileName) progCxt
{ outα } <- graphEval gconfig e
Expand Down
6 changes: 3 additions & 3 deletions test/Fluid.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Control.Promise (fromAff)
import Data.Foldable (sequence_)
import Effect (Effect)
import Effect.Aff (Aff)
import Fluid (Program(..), output)
import Fluid (Program(..), evaluate)
import Lattice (erase)
import Pretty (prettyP)
import Test.Util (testCondition)
Expand All @@ -17,9 +17,9 @@ main = do

testFluid :: Aff Unit
testFluid = do
outα <- output $ Program
outα <- evaluate $ Program
{ imports: []
, datasets: []
, fileName: "length"
}
testCondition "length" ((prettyP $ erase outα) == "2") (prettyP outα)
testCondition "length" ((prettyP $ erase outα) == "2") (prettyP outα)

0 comments on commit ed7a0ea

Please sign in to comment.