From d0d5ec92d912a286d51ea4df2c95f7cc5fd44bb1 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 9 Mar 2018 23:11:20 -0500 Subject: implement pdf-autoprint --- .gitignore | 3 +-- ChangeLog.md | 3 --- LICENSE | 3 --- Main.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 1 - Setup.hs | 2 -- app/Main.hs | 6 ------ package.yaml | 46 +++++++++------------------------------------- src/Lib.hs | 6 ------ test/Spec.hs | 2 -- 10 files changed, 55 insertions(+), 62 deletions(-) delete mode 100644 ChangeLog.md delete mode 100644 LICENSE create mode 100644 Main.hs delete mode 100644 README.md delete mode 100644 Setup.hs delete mode 100644 app/Main.hs delete mode 100644 src/Lib.hs delete mode 100644 test/Spec.hs diff --git a/.gitignore b/.gitignore index c0b8e96..a7c533f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .stack-work/ -new.cabal -*~ \ No newline at end of file +pdf-autoprint.cabal diff --git a/ChangeLog.md b/ChangeLog.md deleted file mode 100644 index e415883..0000000 --- a/ChangeLog.md +++ /dev/null @@ -1,3 +0,0 @@ -# Changelog for new - -## Unreleased changes diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 960c7f7..0000000 --- a/LICENSE +++ /dev/null @@ -1,3 +0,0 @@ -Copyright Andrew Cady (c) 2018 - -All rights reserved. diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..4df4426 --- /dev/null +++ b/Main.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +module Main where + +import Rebase.Prelude +import System.Directory (createDirectoryIfMissing, renameFile) +import System.FilePath (takeFileName, ()) +import System.FSNotify (Event (..), watchDir, withManager) +import System.Process.Typed (proc, runProcess) + +verbose :: Bool +verbose = True + +pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath +pdfDirectory = "." +seenDir = pdfDirectory "seen" +pdfPrinterExecutable = "PDFtoPrinter.exe" + +main :: IO () +main = do + createDirectoryIfMissing False seenDir + withManager $ \mgr -> do + void $ watchDir mgr pdfDirectory (const True) handleEvent + forever $ threadDelay 1000000 + +handleEvent :: Event -> IO () +handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f +handleEvent x = when verbose $ print x + +handleNewPdf :: FilePath -> IO () +handleNewPdf f = + -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe + -- returns success even when it fails to parse the PDF. + runProcessVerbose pdfPrinterExecutable [f] >> + moveFileIntoDir f seenDir + + +runProcessVerbose :: FilePath -> [String] -> IO ExitCode +runProcessVerbose exe args = do + when verbose $ putStrLn $ "+ " ++ exe ++ " " ++ unwords args + runProcess (proc exe args) + +moveFileIntoDir :: FilePath -> FilePath -> IO () +moveFileIntoDir f d = renameFile f $ d takeFileName f + diff --git a/README.md b/README.md deleted file mode 100644 index 4f7fd6a..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# new diff --git a/Setup.hs b/Setup.hs deleted file mode 100644 index 9a994af..0000000 --- a/Setup.hs +++ /dev/null @@ -1,2 +0,0 @@ -import Distribution.Simple -main = defaultMain diff --git a/app/Main.hs b/app/Main.hs deleted file mode 100644 index de1c1ab..0000000 --- a/app/Main.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Main where - -import Lib - -main :: IO () -main = someFunc diff --git a/package.yaml b/package.yaml index 03cb634..435c2f7 100644 --- a/package.yaml +++ b/package.yaml @@ -1,52 +1,24 @@ -name: new +name: pdf-autoprint version: 0.1.0.0 -github: "afcady/new" license: AllRightsReserved author: "Andrew Cady" maintainer: "d@jerkface.net" copyright: "2018 Andrew Cady" -extra-source-files: -- README.md -- ChangeLog.md - -# Metadata used when publishing your package -# synopsis: Short description of your package -# category: Web - -# To avoid duplicated efforts in documentation and dealing with the -# complications of embedding Haddock markup inside cabal files, it is -# common to point users to the README.md file. - -description: Please see the README on Github at - dependencies: -- base >= 4.7 && < 5 +- base - rebase -- lens - -library: - source-dirs: src +- directory +- fsnotify +- typed-process +- filepath executables: - new-exe: + pdf-autoprint: main: Main.hs - source-dirs: app ghc-options: - -threaded - -rtsopts - -with-rtsopts=-N - - -W -Wall - dependencies: - - new - -# tests: -# new-test: -# main: Spec.hs -# source-dirs: test -# ghc-options: -# - -threaded -# - -rtsopts -# - -with-rtsopts=-N -# dependencies: -# - new + - -W + - -Wall diff --git a/src/Lib.hs b/src/Lib.hs deleted file mode 100644 index d36ff27..0000000 --- a/src/Lib.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Lib - ( someFunc - ) where - -someFunc :: IO () -someFunc = putStrLn "someFunc" diff --git a/test/Spec.hs b/test/Spec.hs deleted file mode 100644 index cd4753f..0000000 --- a/test/Spec.hs +++ /dev/null @@ -1,2 +0,0 @@ -main :: IO () -main = putStrLn "Test suite not yet implemented" -- cgit v1.2.3