Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hpack-hash feature. #381

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Hpack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,21 @@ import Hpack.CabalFile
programVersion :: Version -> String
programVersion v = "hpack version " ++ Version.showVersion v

header :: FilePath -> Version -> Hash -> String
header p v hash = unlines [
header :: FilePath -> Version -> Maybe Hash -> String
header p v mHash = unlines ([
"-- This file has been generated from " ++ takeFileName p ++ " by " ++ programVersion v ++ "."
, "--"
, "-- see: https://github.com/sol/hpack"
, "--"
, "-- hash: " ++ hash
, ""
]
++ case mHash of
Nothing -> []
Just hash ->
[ "--"
, "-- hash: " ++ hash
]
++ [
""
])

data Options = Options {
optionsDecodeOptions :: DecodeOptions
Expand Down Expand Up @@ -176,13 +182,21 @@ hpackResultWithVersion v (Options options force toStdout) = do
body = renderPackage (maybe [] cabalFileContents oldCabalFile) pkg
withoutHeader = cabalVersion ++ body
let
useHash = case packageHpackHash pkg of
Just False -> False
Just True -> True
Nothing -> True
status = case force of
Force -> Generated
NoForce -> maybe Generated (mkStatus (lines withoutHeader) v) oldCabalFile
NoForce -> if useHash
then maybe Generated (mkStatus (lines withoutHeader) v) oldCabalFile
else Generated -- always rebuild cabal file if omitting hash
case status of
Generated -> do
let hash = sha256 withoutHeader
out = cabalVersion ++ header (decodeOptionsTarget options) v hash ++ body
let mHash = if useHash
then Just (sha256 withoutHeader)
else Nothing
out = cabalVersion ++ header (decodeOptionsTarget options) v mHash ++ body
if toStdout
then Utf8.putStr out
else Utf8.writeFile cabalFile out
Expand Down
4 changes: 4 additions & 0 deletions src/Hpack/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ package name version = Package {
, packageDataDir = Nothing
, packageSourceRepository = Nothing
, packageCustomSetup = Nothing
, packageHpackHash = Nothing
, packageLibrary = Nothing
, packageInternalLibraries = mempty
, packageExecutables = mempty
Expand Down Expand Up @@ -534,6 +535,7 @@ data PackageConfig_ library executable = PackageConfig {
, packageConfigGithub :: Maybe Text
, packageConfigGit :: Maybe String
, packageConfigCustomSetup :: Maybe CustomSetupSection
, packageConfigHpackHash :: Maybe Bool
, packageConfigLibrary :: Maybe library
, packageConfigInternalLibraries :: Maybe (Map String library)
, packageConfigExecutable :: Maybe executable
Expand Down Expand Up @@ -837,6 +839,7 @@ data Package = Package {
, packageDataDir :: Maybe FilePath
, packageSourceRepository :: Maybe SourceRepository
, packageCustomSetup :: Maybe CustomSetup
, packageHpackHash :: Maybe Bool
, packageLibrary :: Maybe (Section Library)
, packageInternalLibraries :: Map String (Section Library)
, packageExecutables :: Map String (Section Executable)
Expand Down Expand Up @@ -1111,6 +1114,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do
, packageDataDir = packageConfigDataDir
, packageSourceRepository = sourceRepository
, packageCustomSetup = mCustomSetup
, packageHpackHash = packageConfigHpackHash
, packageLibrary = mLibrary
, packageInternalLibraries = internalLibraries
, packageExecutables = executables
Expand Down
9 changes: 7 additions & 2 deletions test/Hpack/CabalFileSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ spec = do

it "includes hash" $ do
inTempDirectory $ do
writeFile file $ header "package.yaml" version hash
writeFile file $ header "package.yaml" version (Just hash)
readCabalFile file `shouldReturn` Just (CabalFile (Just version) (Just hash) [])

it "hpack-hash:false omits hash" $ do
inTempDirectory $ do
writeFile file $ header "package.yaml" version Nothing
readCabalFile file `shouldReturn` Just (CabalFile (Just version) Nothing [])

it "accepts cabal-version at the beginning of the file" $ do
inTempDirectory $ do
writeFile file $ ("cabal-version: 2.2\n" ++ header "package.yaml" version hash)
writeFile file $ ("cabal-version: 2.2\n" ++ header "package.yaml" version (Just hash))
readCabalFile file `shouldReturn` Just (CabalFile (Just version) (Just hash) ["cabal-version: 2.2"])

describe "extractVersion" $ do
Expand Down
9 changes: 9 additions & 0 deletions test/HpackSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ spec = do
old <- readFile file
hpack `shouldReturn` outputUnchanged
readFile file `shouldReturn` old

context "hpack-hash: false" $ do
it "always generates file" $ do
writeFile packageConfig $ unlines [
"name: foo"
, "hpack-hash: false"
]
hpack `shouldReturn` generated
hpack `shouldReturn` generated