-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal build tool to package-with-hooks test
- Loading branch information
Showing
13 changed files
with
216 additions
and
156 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 4 additions & 87 deletions
91
cabal-testsuite/PackageTests/HooksPreprocessor/custom-build-tool/exe/Main.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,14 @@ | ||
module Main where | ||
|
||
-- base | ||
import Control.Monad | ||
( unless ) | ||
import Data.Char | ||
( isSpace ) | ||
import Data.List | ||
( dropWhileEnd ) | ||
import System.Environment | ||
( getArgs ) | ||
|
||
-- containers | ||
import Data.Map.Strict | ||
( Map ) | ||
import qualified Data.Map.Strict as Map | ||
|
||
-- directory | ||
import System.Directory | ||
( doesFileExist, getCurrentDirectory ) | ||
|
||
-- custom-build-tool | ||
import CustomBuildTool | ||
( mkCustomBuildTool ) | ||
import Paths_custom_build_tool -- (Cabal autogenerated module) | ||
( getDataFileName ) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "Starting custom-build-tool" | ||
-- Get all the constants defined in the data file for the build tool. | ||
customDataFile <- getDataFileName "CustomBuildToolData.txt" | ||
customDataFileExists <- doesFileExist customDataFile | ||
unless customDataFileExists $ do | ||
cwd <- getCurrentDirectory | ||
error $ | ||
unlines | ||
[ "Custom preprocessor could not access its data file." | ||
, "Tried to look in: " ++ customDataFile | ||
, "cwd: " ++ show cwd ] | ||
customDataLines <- lines <$> readFile customDataFile | ||
let customConstants :: Map String Int | ||
customConstants = Map.fromList $ map read customDataLines | ||
|
||
-- Obtain input/output file paths from arguments to the preprocessor. | ||
args <- getArgs | ||
case args of | ||
[inputFile, outputFile] -> do | ||
inputFileExists <- doesFileExist inputFile | ||
unless inputFileExists $ | ||
error $ | ||
unlines | ||
[ "Custom preprocessor could not read input file." | ||
, "Input file: " ++ inputFile ] | ||
-- Read the input file, substitute constants for their values, | ||
-- and write the result to the output file path. | ||
inputLines <- lines <$> readFile inputFile | ||
let outputLines = map ( preprocessLine customConstants ) ( zip [1..] inputLines ) | ||
writeFile outputFile ( unlines outputLines ) | ||
[] -> | ||
putStrLn "Custom preprocessor: no arguments." | ||
_ -> | ||
error $ | ||
unlines | ||
[ "Custom preprocessor was given incorrect arguments." | ||
, "Expected input and output file paths, but got " ++ what ++ "." ] | ||
where | ||
what = case args of | ||
[] -> "none" | ||
[_] -> "a single argument" | ||
_ -> show (length args) ++ " arguments" | ||
|
||
-- | Substitute any occurrence of {# ConstantName #} with the value of ConstantName, | ||
-- looked up in the data file for the preprocessor. | ||
preprocessLine :: Map String Int -> ( Int, String ) -> String | ||
preprocessLine constants ( ln_no, ln ) = go "" ln | ||
where | ||
go reversedPrev [] = reverse reversedPrev | ||
go reversedPrev ('{':'#':rest) = reverse reversedPrev ++ inner "" rest | ||
go reversedPrev (c:rest) = go (c:reversedPrev) rest | ||
|
||
inner reversedNm ('#':'}':rest) = | ||
let constName = trimWhitespace $ reverse reversedNm | ||
in case Map.lookup constName constants of | ||
Just val -> show val ++ go "" rest | ||
Nothing -> | ||
error $ unlines | ||
[ "Could not preprocess line " ++ show ln_no ++ ":" | ||
, "unknown constant \"" ++ constName ++ "\"." ] | ||
inner reversedNm (c:rest) = inner (c:reversedNm) rest | ||
inner reversedNm "" = | ||
error $ unlines | ||
[ "Could not preprocess line " ++ show ln_no ++ ":" | ||
, "unterminated constant \"{# " ++ reverse reversedNm ++ "\"." ] | ||
|
||
trimWhitespace :: String -> String | ||
trimWhitespace = dropWhile isSpace . dropWhileEnd isSpace | ||
customDataFile <- getDataFileName "CustomPP1Data.txt" | ||
mkCustomBuildTool "custom-pp1" customDataFile |
93 changes: 93 additions & 0 deletions
93
cabal-testsuite/PackageTests/HooksPreprocessor/custom-build-tool/src/CustomBuildTool.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module CustomBuildTool | ||
( mkCustomBuildTool ) | ||
where | ||
|
||
-- base | ||
import Control.Monad | ||
( unless ) | ||
import Data.Char | ||
( isSpace ) | ||
import Data.List | ||
( dropWhileEnd ) | ||
import System.Environment | ||
( getArgs ) | ||
|
||
-- containers | ||
import Data.Map.Strict | ||
( Map ) | ||
import qualified Data.Map.Strict as Map | ||
|
||
-- directory | ||
import System.Directory | ||
( doesFileExist, getCurrentDirectory ) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
mkCustomBuildTool :: String -> FilePath -> IO () | ||
mkCustomBuildTool buildToolName customDataFile = do | ||
putStrLn $ "Starting " ++ buildToolName | ||
-- Get all the constants defined in the data file for the build tool. | ||
customDataFileExists <- doesFileExist customDataFile | ||
unless customDataFileExists $ do | ||
cwd <- getCurrentDirectory | ||
error $ | ||
unlines | ||
[ "Custom preprocessor " ++ buildToolName ++ " could not access its data file." | ||
, "Tried to look in: " ++ customDataFile | ||
, "cwd: " ++ show cwd ] | ||
customDataLines <- lines <$> readFile customDataFile | ||
let customConstants :: Map String Int | ||
customConstants = Map.fromList $ map read customDataLines | ||
|
||
-- Obtain input/output file paths from arguments to the preprocessor. | ||
args <- getArgs | ||
case args of | ||
[inputFile, outputFile] -> do | ||
inputFileExists <- doesFileExist inputFile | ||
unless inputFileExists $ | ||
error $ | ||
unlines | ||
[ "Custom preprocessor " ++ buildToolName ++ " could not read input file." | ||
, "Input file: " ++ inputFile ] | ||
-- Read the input file, substitute constants for their values, | ||
-- and write the result to the output file path. | ||
inputLines <- lines <$> readFile inputFile | ||
let outputLines = map ( preprocessLine customConstants ) ( zip [1..] inputLines ) | ||
Check warning on line 55 in cabal-testsuite/PackageTests/HooksPreprocessor/custom-build-tool/src/CustomBuildTool.hs GitHub Actions / hlint
|
||
writeFile outputFile ( unlines outputLines ) | ||
[] -> | ||
putStrLn $ "Custom preprocessor " ++ buildToolName ++ ": no arguments." | ||
_ -> | ||
error $ | ||
unlines | ||
[ "Custom preprocessor " ++ buildToolName ++ " was given incorrect arguments." | ||
, "Expected input and output file paths, but got " ++ what ++ "." ] | ||
where | ||
what = case args of | ||
[_] -> "a single argument" | ||
_ -> show (length args) ++ " arguments" | ||
|
||
-- | Substitute any occurrence of {# ConstantName #} with the value of ConstantName, | ||
-- looked up in the data file for the preprocessor. | ||
preprocessLine :: Map String Int -> ( Int, String ) -> String | ||
preprocessLine constants ( ln_no, ln ) = go "" ln | ||
where | ||
go reversedPrev [] = reverse reversedPrev | ||
go reversedPrev ('{':'#':rest) = reverse reversedPrev ++ inner "" rest | ||
go reversedPrev (c:rest) = go (c:reversedPrev) rest | ||
|
||
inner reversedNm ('#':'}':rest) = | ||
let constName = trimWhitespace $ reverse reversedNm | ||
in case Map.lookup constName constants of | ||
Just val -> show val ++ go "" rest | ||
Nothing -> | ||
error $ unlines | ||
[ "Could not preprocess line " ++ show ln_no ++ ":" | ||
, "unknown constant \"" ++ constName ++ "\"." ] | ||
inner reversedNm (c:rest) = inner (c:reversedNm) rest | ||
inner reversedNm "" = | ||
error $ unlines | ||
[ "Could not preprocess line " ++ show ln_no ++ ":" | ||
, "unterminated constant \"{# " ++ reverse reversedNm ++ "\"." ] | ||
|
||
trimWhitespace :: String -> String | ||
trimWhitespace = dropWhile isSpace . dropWhileEnd isSpace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
cabal-testsuite/PackageTests/HooksPreprocessor/package-with-hooks/data/CustomPP2Data.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
("MyConstant", 1717) |
16 changes: 16 additions & 0 deletions
16
cabal-testsuite/PackageTests/HooksPreprocessor/package-with-hooks/exe/Main.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Main where | ||
|
||
-- custom-build-tool | ||
import CustomBuildTool | ||
( mkCustomBuildTool ) | ||
|
||
-- package-with-hooks | ||
import Paths_package_with_hooks -- (Cabal autogenerated module) | ||
( getDataFileName ) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
main :: IO () | ||
main = do | ||
customDataFile <- getDataFileName "CustomPP2Data.txt" | ||
mkCustomBuildTool "custom-pp2" customDataFile |
Oops, something went wrong.