Commit 7d510317 authored by Armin Bernstetter's avatar Armin Bernstetter
Browse files

rudimentary implementation of calling headless chrome on command line

parent 15ab2bdf
......@@ -3,6 +3,7 @@ import Common
import Exception
import External
import Flags (hasPreextractedResources)
import Pdf
import Project
import Resources
import Shake
......@@ -97,7 +98,8 @@ main = do
--
phony "sketch-pad-index" $ do
indicesA >>= need
indicesA >>= writeSketchPadIndex ((directories ^. public) </> "sketch-pad.yaml")
indicesA >>=
writeSketchPadIndex ((directories ^. public) </> "sketch-pad.yaml")
--
phony "index" $ need ["support", index]
--
......@@ -119,7 +121,11 @@ main = do
need [src]
putNormal $ src ++ " -> " ++ out
runHttpServer serverPort directories Nothing
decktape [serverUrl </> makeRelative (directories ^. public) src, out]
-- decktape [serverUrl </> makeRelative (directories ^. public) src, out]
liftIO $
launchChrome
(serverUrl </> makeRelative (directories ^. public) src)
out
--
priority 2 $
"//*-handout.html" %> \out -> do
......
......@@ -3,9 +3,11 @@ module System.Decker.OS
( defaultProvisioning
, urlPath
, preextractedResourceFolder
, chrome
) where
import Common
import System.Directory
import System.Environment
import System.FilePath
......@@ -19,3 +21,32 @@ preextractedResourceFolder :: IO FilePath
preextractedResourceFolder = do
exep <- getExecutablePath
return $ joinPath [(takeDirectory exep), "..", "Resources", "resource"]
-- Look for chromium executable on $PATH
chromeExecutable :: IO String
chromeExecutable = do
chrium <- findExecutable "chromium"
chr <- findExecutable "chrome"
case (chrium, chr) of
(Just c, _) -> return c
(_, Just c) -> return c
(Nothing, Nothing) ->
error
"Neither chrome nor chromium are on $PATH. Please make sure Google Chrome is installed to use 'decker pdf'."
chrome :: IO String
chrome = do
localExists <- localChrome >>= \h -> doesFileExist h
globalExists <- doesFileExist chromeLocation
if globalExists
then return chromeCommand
else if localExists
then localChromeCommand
else chromeExecutable
where
localChrome = fmap (\h -> h ++ chromeLocation) getHomeDirectory
localChromeCommand = fmap (\h -> h ++ chromeCommand) getHomeDirectory
chromeLocation =
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
chromeCommand =
"/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome"
......@@ -3,6 +3,7 @@ module System.Decker.OS
( defaultProvisioning
, urlPath
, preextractedResourceFolder
, chrome
) where
import Common
......@@ -20,3 +21,6 @@ preextractedResourceFolder :: IO FilePath
preextractedResourceFolder = do
exep <- getExecutablePath
return $ joinPath [(takeDirectory exep), "..", "resource"]
chrome :: IO String
chrome = return "start chrome"
......@@ -5,7 +5,10 @@ module Pdf
import System.Decker.OS
import Control.Exception
import Data.List
import System.Directory
import System.Exit
import System.IO
import System.Process
......@@ -48,24 +51,36 @@ getDirs
print contents
-- This will be what is imported from the OS module
chrome :: FilePath
chrome = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
-- chrome :: FilePath
-- chrome = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome"
pdfPath = "test.pdf"
pdfOption :: FilePath -> [Char]
pdfOption path = "--print-to-pdf=" ++ path
launchChrome :: IO ()
launchChrome = do
(_, _, _, ph2) <-
modifySrc :: FilePath -> FilePath
modifySrc path = path ++ "?print-pdf#/"
-- if isSuffixOf ".html" path
-- then Just $ path ++ "?print-pdf#/"
-- else Nothing
chromeOptions :: FilePath -> FilePath -> [String]
chromeOptions src out =
["--headless", "--disable-gpu", pdfOption out, modifySrc src]
launchChrome :: FilePath -> FilePath -> IO ()
launchChrome src out = do
command <- chrome
let options = unwords (chromeOptions src out)
-- print options
(_, _, _, ph) <-
createProcess
(proc
chrome
[ "--headless"
, "--disable-gpu"
, pdfOption pdfPath
, "http://0.0.0.0:8888/example-deck.html?print-pdf#/"
])
waitForProcess ph2
print "test"
-- (proc chrome ["--headless", "--disable-gpu", pdfOption out, modifySrc src])
(shell $ command ++ " " ++ options)
code <- waitForProcess ph
case code of
ExitFailure _ ->
error
("Error: Google Chrome is most likely not installed." ++
" Make sure Google Chrome is installed to use 'decker pdf'.")
ExitSuccess -> putStrLn $ "completed:" ++ src ++ " -> " ++ out
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