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 ...@@ -3,6 +3,7 @@ import Common
import Exception import Exception
import External import External
import Flags (hasPreextractedResources) import Flags (hasPreextractedResources)
import Pdf
import Project import Project
import Resources import Resources
import Shake import Shake
...@@ -97,7 +98,8 @@ main = do ...@@ -97,7 +98,8 @@ main = do
-- --
phony "sketch-pad-index" $ do phony "sketch-pad-index" $ do
indicesA >>= need indicesA >>= need
indicesA >>= writeSketchPadIndex ((directories ^. public) </> "sketch-pad.yaml") indicesA >>=
writeSketchPadIndex ((directories ^. public) </> "sketch-pad.yaml")
-- --
phony "index" $ need ["support", index] phony "index" $ need ["support", index]
-- --
...@@ -119,7 +121,11 @@ main = do ...@@ -119,7 +121,11 @@ main = do
need [src] need [src]
putNormal $ src ++ " -> " ++ out putNormal $ src ++ " -> " ++ out
runHttpServer serverPort directories Nothing 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 $ priority 2 $
"//*-handout.html" %> \out -> do "//*-handout.html" %> \out -> do
......
...@@ -3,9 +3,11 @@ module System.Decker.OS ...@@ -3,9 +3,11 @@ module System.Decker.OS
( defaultProvisioning ( defaultProvisioning
, urlPath , urlPath
, preextractedResourceFolder , preextractedResourceFolder
, chrome
) where ) where
import Common import Common
import System.Directory
import System.Environment import System.Environment
import System.FilePath import System.FilePath
...@@ -19,3 +21,32 @@ preextractedResourceFolder :: IO FilePath ...@@ -19,3 +21,32 @@ preextractedResourceFolder :: IO FilePath
preextractedResourceFolder = do preextractedResourceFolder = do
exep <- getExecutablePath exep <- getExecutablePath
return $ joinPath [(takeDirectory exep), "..", "Resources", "resource"] 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 ...@@ -3,6 +3,7 @@ module System.Decker.OS
( defaultProvisioning ( defaultProvisioning
, urlPath , urlPath
, preextractedResourceFolder , preextractedResourceFolder
, chrome
) where ) where
import Common import Common
...@@ -20,3 +21,6 @@ preextractedResourceFolder :: IO FilePath ...@@ -20,3 +21,6 @@ preextractedResourceFolder :: IO FilePath
preextractedResourceFolder = do preextractedResourceFolder = do
exep <- getExecutablePath exep <- getExecutablePath
return $ joinPath [(takeDirectory exep), "..", "resource"] return $ joinPath [(takeDirectory exep), "..", "resource"]
chrome :: IO String
chrome = return "start chrome"
...@@ -5,7 +5,10 @@ module Pdf ...@@ -5,7 +5,10 @@ module Pdf
import System.Decker.OS import System.Decker.OS
import Control.Exception
import Data.List
import System.Directory import System.Directory
import System.Exit
import System.IO import System.IO
import System.Process import System.Process
...@@ -48,24 +51,36 @@ getDirs ...@@ -48,24 +51,36 @@ getDirs
print contents print contents
-- This will be what is imported from the OS module -- This will be what is imported from the OS module
chrome :: FilePath -- chrome :: FilePath
chrome = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" -- chrome = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome"
pdfPath = "test.pdf" pdfPath = "test.pdf"
pdfOption :: FilePath -> [Char] pdfOption :: FilePath -> [Char]
pdfOption path = "--print-to-pdf=" ++ path pdfOption path = "--print-to-pdf=" ++ path
launchChrome :: IO () modifySrc :: FilePath -> FilePath
launchChrome = do modifySrc path = path ++ "?print-pdf#/"
(_, _, _, ph2) <- -- 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 createProcess
(proc -- (proc chrome ["--headless", "--disable-gpu", pdfOption out, modifySrc src])
chrome (shell $ command ++ " " ++ options)
[ "--headless" code <- waitForProcess ph
, "--disable-gpu" case code of
, pdfOption pdfPath ExitFailure _ ->
, "http://0.0.0.0:8888/example-deck.html?print-pdf#/" error
]) ("Error: Google Chrome is most likely not installed." ++
waitForProcess ph2 " Make sure Google Chrome is installed to use 'decker pdf'.")
print "test" 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