From fec3eb0f15a10109336705157760f1280fb363a5 Mon Sep 17 00:00:00 2001 From: Christopher League Date: Mon, 19 Jun 2017 05:51:41 -0400 Subject: Restructure .cabal to avoid redundant compilation --- Init.hs | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hakyll.cabal | 52 ++++++++--------------------- src/Hakyll/Init.hs | 96 ------------------------------------------------------ 3 files changed, 109 insertions(+), 135 deletions(-) create mode 100644 Init.hs delete mode 100644 src/Hakyll/Init.hs diff --git a/Init.hs b/Init.hs new file mode 100644 index 0000000..71055f0 --- /dev/null +++ b/Init.hs @@ -0,0 +1,96 @@ +-------------------------------------------------------------------------------- +module Main + ( main + ) where + + +-------------------------------------------------------------------------------- +import Control.Arrow (first) +import Control.Monad (forM_) +import Data.Char (isAlphaNum, isNumber) +import Data.List (foldl') +import Data.List (intercalate, isPrefixOf) +import Data.Version (Version (..)) +import System.Directory (canonicalizePath, copyFile) +import System.Environment (getArgs, getProgName) +import System.Exit (exitFailure) +import System.FilePath (splitDirectories, ()) + + +-------------------------------------------------------------------------------- +import Hakyll.Core.Util.File +import Paths_hakyll + + +-------------------------------------------------------------------------------- +main :: IO () +main = do + progName <- getProgName + args <- getArgs + srcDir <- getDataFileName "example" + files <- getRecursiveContents (const $ return False) srcDir + + case args of + -- When the argument begins with hyphens, it's more likely that the user + -- intends to attempt some arguments like ("--help", "-h", "--version", etc.) + -- rather than create directory with that name. + -- If dstDir begins with hyphens, the guard will prevent it from creating + -- directory with that name so we can fall to the second alternative + -- which prints a usage info for user. + [dstDir] | not ("-" `isPrefixOf` dstDir) -> do + forM_ files $ \file -> do + let dst = dstDir file + src = srcDir file + putStrLn $ "Creating " ++ dst + makeDirectories dst + copyFile src dst + + name <- makeName dstDir + let cabalPath = dstDir name ++ ".cabal" + putStrLn $ "Creating " ++ cabalPath + createCabal cabalPath name + _ -> do + putStrLn $ "Usage: " ++ progName ++ " " + exitFailure + +-- | Figure out a good cabal package name from the given (existing) directory +-- name +makeName :: FilePath -> IO String +makeName dstDir = do + canonical <- canonicalizePath dstDir + return $ case safeLast (splitDirectories canonical) of + Nothing -> fallbackName + Just "/" -> fallbackName + Just x -> repair (fallbackName ++) id x + where + -- Package name repair code comes from + -- cabal-install.Distribution.Client.Init.Heuristics + repair invalid valid x = case dropWhile (not . isAlphaNum) x of + "" -> repairComponent "" + x' -> let (c, r) = first repairComponent $ break (not . isAlphaNum) x' + in c ++ repairRest r + where repairComponent c | all isNumber c = invalid c + | otherwise = valid c + repairRest = repair id ('-' :) + fallbackName = "site" + + safeLast = foldl' (\_ x -> Just x) Nothing + +createCabal :: FilePath -> String -> IO () +createCabal path name = do + writeFile path $ unlines [ + "name: " ++ name + , "version: 0.1.0.0" + , "build-type: Simple" + , "cabal-version: >= 1.10" + , "" + , "executable site" + , " main-is: site.hs" + , " build-depends: base == 4.*" + , " , hakyll == " ++ version' ++ ".*" + , " ghc-options: -threaded" + , " default-language: Haskell2010" + ] + where + -- Major hakyll version + version' = intercalate "." . take 2 . map show $ versionBranch version diff --git a/hakyll.cabal b/hakyll.cabal index 9c44675..43e806a 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -102,16 +102,24 @@ Library Hakyll Hakyll.Commands Hakyll.Core.Compiler + Hakyll.Core.Compiler.Internal Hakyll.Core.Configuration Hakyll.Core.Dependencies Hakyll.Core.File Hakyll.Core.Identifier Hakyll.Core.Identifier.Pattern Hakyll.Core.Item + Hakyll.Core.Logger Hakyll.Core.Metadata + Hakyll.Core.Provider + Hakyll.Core.Provider.Metadata Hakyll.Core.Routes Hakyll.Core.Rules + Hakyll.Core.Rules.Internal + Hakyll.Core.Runtime + Hakyll.Core.Store Hakyll.Core.UnixFilter + Hakyll.Core.Util.File Hakyll.Core.Util.String Hakyll.Core.Writable Hakyll.Main @@ -119,12 +127,12 @@ Library Hakyll.Web.Feed Hakyll.Web.Html Hakyll.Web.Html.RelativizeUrls + Hakyll.Web.Paginate Hakyll.Web.Pandoc Hakyll.Web.Pandoc.Biblio Hakyll.Web.Pandoc.FileType Hakyll.Web.Redirect Hakyll.Web.Tags - Hakyll.Web.Paginate Hakyll.Web.Template Hakyll.Web.Template.Context Hakyll.Web.Template.Internal @@ -136,18 +144,10 @@ Library Data.List.Extended Data.Yaml.Extended Hakyll.Check - Hakyll.Core.Compiler.Internal Hakyll.Core.Compiler.Require Hakyll.Core.Item.SomeItem - Hakyll.Core.Logger - Hakyll.Core.Provider Hakyll.Core.Provider.Internal - Hakyll.Core.Provider.Metadata Hakyll.Core.Provider.MetadataCache - Hakyll.Core.Rules.Internal - Hakyll.Core.Runtime - Hakyll.Core.Store - Hakyll.Core.Util.File Hakyll.Core.Util.Parser Hakyll.Web.Pandoc.Binary Paths_hakyll @@ -218,29 +218,12 @@ Library Test-suite hakyll-tests Type: exitcode-stdio-1.0 - Hs-source-dirs: src tests + Hs-source-dirs: tests Main-is: TestSuite.hs Ghc-options: -Wall - Other-modules: - Hakyll.Core.Dependencies.Tests - Hakyll.Core.Identifier.Tests - Hakyll.Core.Provider.Metadata.Tests - Hakyll.Core.Provider.Tests - Hakyll.Core.Routes.Tests - Hakyll.Core.Rules.Tests - Hakyll.Core.Runtime.Tests - Hakyll.Core.Store.Tests - Hakyll.Core.UnixFilter.Tests - Hakyll.Core.Util.String.Tests - Hakyll.Web.CompressCss.Tests - Hakyll.Web.Html.RelativizeUrls.Tests - Hakyll.Web.Html.Tests - Hakyll.Web.Pandoc.FileType.Tests - Hakyll.Web.Template.Context.Tests - Hakyll.Web.Template.Tests - TestSuite.Util Build-Depends: + hakyll, QuickCheck >= 2.8 && < 2.10, tasty >= 0.11 && < 0.12, tasty-hunit >= 0.9 && < 0.10, @@ -289,9 +272,6 @@ Test-suite hakyll-tests system-filepath >= 0.4.6 && <= 0.5 Cpp-options: -DPREVIEW_SERVER - Other-modules: - Hakyll.Preview.Poll - Hakyll.Preview.Server If flag(watchServer) Build-depends: @@ -299,8 +279,6 @@ Test-suite hakyll-tests system-filepath >= 0.4.6 && <= 0.5 Cpp-options: -DWATCH_SERVER - Other-modules: - Hakyll.Preview.Poll If flag(checkExternal) Build-depends: @@ -311,14 +289,10 @@ Test-suite hakyll-tests Executable hakyll-init Ghc-options: -Wall - Hs-source-dirs: src - Main-is: Hakyll/Init.hs + Main-is: Init.hs Build-depends: + hakyll, base >= 4 && < 5, directory >= 1.0 && < 1.4, filepath >= 1.0 && < 1.5 - - Other-modules: - Hakyll.Core.Util.File - Paths_hakyll diff --git a/src/Hakyll/Init.hs b/src/Hakyll/Init.hs deleted file mode 100644 index 71055f0..0000000 --- a/src/Hakyll/Init.hs +++ /dev/null @@ -1,96 +0,0 @@ --------------------------------------------------------------------------------- -module Main - ( main - ) where - - --------------------------------------------------------------------------------- -import Control.Arrow (first) -import Control.Monad (forM_) -import Data.Char (isAlphaNum, isNumber) -import Data.List (foldl') -import Data.List (intercalate, isPrefixOf) -import Data.Version (Version (..)) -import System.Directory (canonicalizePath, copyFile) -import System.Environment (getArgs, getProgName) -import System.Exit (exitFailure) -import System.FilePath (splitDirectories, ()) - - --------------------------------------------------------------------------------- -import Hakyll.Core.Util.File -import Paths_hakyll - - --------------------------------------------------------------------------------- -main :: IO () -main = do - progName <- getProgName - args <- getArgs - srcDir <- getDataFileName "example" - files <- getRecursiveContents (const $ return False) srcDir - - case args of - -- When the argument begins with hyphens, it's more likely that the user - -- intends to attempt some arguments like ("--help", "-h", "--version", etc.) - -- rather than create directory with that name. - -- If dstDir begins with hyphens, the guard will prevent it from creating - -- directory with that name so we can fall to the second alternative - -- which prints a usage info for user. - [dstDir] | not ("-" `isPrefixOf` dstDir) -> do - forM_ files $ \file -> do - let dst = dstDir file - src = srcDir file - putStrLn $ "Creating " ++ dst - makeDirectories dst - copyFile src dst - - name <- makeName dstDir - let cabalPath = dstDir name ++ ".cabal" - putStrLn $ "Creating " ++ cabalPath - createCabal cabalPath name - _ -> do - putStrLn $ "Usage: " ++ progName ++ " " - exitFailure - --- | Figure out a good cabal package name from the given (existing) directory --- name -makeName :: FilePath -> IO String -makeName dstDir = do - canonical <- canonicalizePath dstDir - return $ case safeLast (splitDirectories canonical) of - Nothing -> fallbackName - Just "/" -> fallbackName - Just x -> repair (fallbackName ++) id x - where - -- Package name repair code comes from - -- cabal-install.Distribution.Client.Init.Heuristics - repair invalid valid x = case dropWhile (not . isAlphaNum) x of - "" -> repairComponent "" - x' -> let (c, r) = first repairComponent $ break (not . isAlphaNum) x' - in c ++ repairRest r - where repairComponent c | all isNumber c = invalid c - | otherwise = valid c - repairRest = repair id ('-' :) - fallbackName = "site" - - safeLast = foldl' (\_ x -> Just x) Nothing - -createCabal :: FilePath -> String -> IO () -createCabal path name = do - writeFile path $ unlines [ - "name: " ++ name - , "version: 0.1.0.0" - , "build-type: Simple" - , "cabal-version: >= 1.10" - , "" - , "executable site" - , " main-is: site.hs" - , " build-depends: base == 4.*" - , " , hakyll == " ++ version' ++ ".*" - , " ghc-options: -threaded" - , " default-language: Haskell2010" - ] - where - -- Major hakyll version - version' = intercalate "." . take 2 . map show $ versionBranch version -- cgit v1.2.3