From 5e564ef5b79a9a4961c22160a416c7a67fdcdab4 Mon Sep 17 00:00:00 2001 From: Scott Fleischman Date: Fri, 20 Mar 2020 15:31:33 -0700 Subject: [PATCH 1/3] Add hpackHash config. --- src/Hpack/Config.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Hpack/Config.hs b/src/Hpack/Config.hs index aba76b9b..936aeb1c 100644 --- a/src/Hpack/Config.hs +++ b/src/Hpack/Config.hs @@ -148,6 +148,7 @@ package name version = Package { , packageDataDir = Nothing , packageSourceRepository = Nothing , packageCustomSetup = Nothing + , packageHpackHash = Nothing , packageLibrary = Nothing , packageInternalLibraries = mempty , packageExecutables = mempty @@ -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 @@ -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) @@ -1111,6 +1114,7 @@ toPackage_ dir (Product g PackageConfig{..}) = do , packageDataDir = packageConfigDataDir , packageSourceRepository = sourceRepository , packageCustomSetup = mCustomSetup + , packageHpackHash = packageConfigHpackHash , packageLibrary = mLibrary , packageInternalLibraries = internalLibraries , packageExecutables = executables From 25c109cd9413c118dfd53b3598293e7406c2bf33 Mon Sep 17 00:00:00 2001 From: Scott Fleischman Date: Fri, 20 Mar 2020 15:43:03 -0700 Subject: [PATCH 2/3] Implement omission of hash. --- src/Hpack.hs | 30 ++++++++++++++++++++++-------- test/Hpack/CabalFileSpec.hs | 4 ++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Hpack.hs b/src/Hpack.hs index f843e2b1..5f5836c3 100644 --- a/src/Hpack.hs +++ b/src/Hpack.hs @@ -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 @@ -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 diff --git a/test/Hpack/CabalFileSpec.hs b/test/Hpack/CabalFileSpec.hs index 8b8bb2fc..41678a3c 100644 --- a/test/Hpack/CabalFileSpec.hs +++ b/test/Hpack/CabalFileSpec.hs @@ -21,12 +21,12 @@ 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 "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 From 222b7e26622b02d22a0a9516f03eea41b0b69770 Mon Sep 17 00:00:00 2001 From: Scott Fleischman Date: Fri, 20 Mar 2020 15:57:13 -0700 Subject: [PATCH 3/3] Add tests. --- test/Hpack/CabalFileSpec.hs | 5 +++++ test/HpackSpec.hs | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/test/Hpack/CabalFileSpec.hs b/test/Hpack/CabalFileSpec.hs index 41678a3c..6ea6f4e8 100644 --- a/test/Hpack/CabalFileSpec.hs +++ b/test/Hpack/CabalFileSpec.hs @@ -24,6 +24,11 @@ spec = do 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 (Just hash)) diff --git a/test/HpackSpec.hs b/test/HpackSpec.hs index a718caa0..aa32777a 100644 --- a/test/HpackSpec.hs +++ b/test/HpackSpec.hs @@ -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