-- ---------------------------------------------------------------------------- -- | -- Module : TemplateGenerator -- Author : Krasimir Angelov -- Copyright : (c) Krasimir Angelov, All Rights Reserved -- -- ---------------------------------------------------------------------------- module TemplateGenerator ( generateTemplate , generateProjectTemplate ) where import Data.Char import Distribution.PackageDescription import Distribution.License import System.Directory import ProjectContent import FilePath generateTemplate :: FilePath -> String -> String -> ProjectMetaInfo -> FilePath -> FilePath -> IO () generateTemplate localFilePath modName prjName metaInfo tmplPath destPath = do cloneTemplate getMacroValue tmplPath destPath where ppLicense GPL = "GPL" ppLicense LGPL = "LGPL" ppLicense BSD3 = "BSD3" ppLicense BSD4 = "BSD4" ppLicense PublicDomain = "Public domain" ppLicense AllRightsReserved = "All Rights Reserved" ppLicense OtherLicense = "Other license" (_,projName,_) = splitFilePath prjName getMacroValue "ModuleName" = modName getMacroValue "ProjectName" = projName getMacroValue "Copyright" = miCopyright metaInfo getMacroValue "License" = ppLicense (miLicense metaInfo) getMacroValue "Maintainer" = miMaintainer metaInfo getMacroValue "Stability" = miStability metaInfo getMacroValue "Author" = miAuthor metaInfo getMacroValue "Homepage" = miHomepage metaInfo getMacroValue "PkgURL" = miPkgURL metaInfo getMacroValue "Synopsis" = miSynopsis metaInfo getMacroValue "Description" = miDescription metaInfo getMacroValue "Category" = miCategory metaInfo getMacroValue _ = "" generateProjectTemplate :: FilePath -> FilePath -> String -> IO PackageDescription generateProjectTemplate tmplPath destLocation destName = do let (path',name,_) = splitFilePath tmplPath path = fst (splitFileName path') `joinFileName` "ProjectSources" tmplDirPath = path `joinFileName` name destProjPath = destLocation `joinFileName` destName (_,projName,_) = splitFilePath destName getMacroValue "ProjectName" = projName getMacroValue _ = "" cloneTemplate getMacroValue tmplPath destProjPath pkgDescr <- readPackageDescription destProjPath let metaInfo = newProjectMetaInfo pkgDescr processFile tmplDirPath destDirPath fileName | fileName == "." || fileName == ".." = return () | otherwise = do isDir <- doesDirectoryExist tmplFilePath if isDir then do createDirectory destFilePath files <- getDirectoryContents tmplFilePath mapM_ (processFile tmplFilePath destFilePath) files else let (_,modName,_) = splitFilePath fileName in generateTemplate fileName modName projName metaInfo tmplFilePath destFilePath where tmplFilePath = tmplDirPath `joinFileName` fileName destFilePath = destDirPath `joinFileName` fileName files <- getDirectoryContents tmplDirPath mapM_ (processFile tmplDirPath destLocation) files return pkgDescr cloneTemplate :: (String -> String) -> FilePath -> FilePath -> IO () cloneTemplate getMacroValue tmplPath destPath = do content <- readFile tmplPath writeFile destPath (expandMacros content) where expandMacros :: String -> String expandMacros [] = [] expandMacros ('@':xs) = let (macro_name,xs') = takeMacroName xs in getMacroValue macro_name ++ expandMacros xs' expandMacros (x:xs) = x : expandMacros xs takeMacroName [] = ([],[]) takeMacroName ('@':xs) = ([],xs) takeMacroName (x:xs) = (x:name,rest) where (name,rest) = takeMacroName xs