-
Notifications
You must be signed in to change notification settings - Fork 710
Add pre and post build hooks #10799
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
Draft
erikd
wants to merge
2
commits into
haskell:master
Choose a base branch
from
erikd:erikd/pre-post-build-hooks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add pre and post build hooks #10799
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,97 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Distribution.Client.HookAccept | ||
( HookAccept (..) | ||
, assertHookHash | ||
, loadHookHasheshMap | ||
, parseHooks | ||
) where | ||
|
||
import Distribution.Client.Compat.Prelude | ||
|
||
import Data.ByteString.Char8 (ByteString) | ||
import qualified Data.ByteString.Char8 as BS | ||
|
||
import qualified Data.Map.Strict as Map | ||
|
||
import Distribution.Client.Config (getConfigFilePath) | ||
import Distribution.Client.Errors (CabalInstallException (..)) | ||
import Distribution.Client.HashValue (HashValue, hashValueFromHex, readFileHashValue, showHashValue) | ||
import Distribution.Simple.Setup (Flag (..)) | ||
import Distribution.Simple.Utils (dieWithException) | ||
import Distribution.Verbosity (normal) | ||
|
||
import System.FilePath (takeDirectory, (</>)) | ||
|
||
data HookAccept | ||
= AcceptAlways | ||
| AcceptHash HashValue | ||
deriving (Eq, Show, Generic) | ||
|
||
instance Monoid HookAccept where | ||
mempty = AcceptAlways -- Should never be needed. | ||
mappend = (<>) | ||
|
||
instance Semigroup HookAccept where | ||
AcceptAlways <> AcceptAlways = AcceptAlways | ||
AcceptAlways <> AcceptHash h = AcceptHash h | ||
AcceptHash h <> AcceptAlways = AcceptHash h | ||
AcceptHash h <> _ = AcceptHash h | ||
|
||
instance Binary HookAccept | ||
instance Structured HookAccept | ||
|
||
assertHookHash :: Map FilePath HookAccept -> FilePath -> IO () | ||
assertHookHash m fpath = do | ||
actualHash <- readFileHashValue fpath | ||
hsPath <- getHooksSecurityFilePath NoFlag | ||
case Map.lookup fpath m of | ||
Nothing -> | ||
dieWithException normal $ | ||
HookAcceptUnknown hsPath fpath (showHashValue actualHash) | ||
Just AcceptAlways -> pure () | ||
Just (AcceptHash expectedHash) -> | ||
when (actualHash /= expectedHash) $ | ||
dieWithException normal $ | ||
HookAcceptHashMismatch | ||
hsPath | ||
fpath | ||
(showHashValue expectedHash) | ||
(showHashValue actualHash) | ||
|
||
getHooksSecurityFilePath :: Flag FilePath -> IO FilePath | ||
getHooksSecurityFilePath configFileFlag = do | ||
hfpath <- getConfigFilePath configFileFlag | ||
pure $ takeDirectory hfpath </> "hooks-security" | ||
|
||
loadHookHasheshMap :: Flag FilePath -> IO (Map FilePath HookAccept) | ||
loadHookHasheshMap configFileFlag = do | ||
hookFilePath <- getHooksSecurityFilePath configFileFlag | ||
handleNotExists $ fmap parseHooks (BS.readFile hookFilePath) | ||
where | ||
handleNotExists :: IO (Map FilePath HookAccept) -> IO (Map FilePath HookAccept) | ||
handleNotExists action = catchIO action $ \_ -> return mempty | ||
|
||
parseHooks :: ByteString -> Map FilePath HookAccept | ||
parseHooks = Map.fromList . map parse . cleanUp . BS.lines | ||
where | ||
cleanUp :: [ByteString] -> [ByteString] | ||
cleanUp = filter (not . BS.null) . map rmComments | ||
|
||
rmComments :: ByteString -> ByteString | ||
rmComments = fst . BS.breakSubstring "--" | ||
|
||
parse :: ByteString -> (FilePath, HookAccept) | ||
parse bs = | ||
case BS.words bs of | ||
[fp, "AcceptAlways"] -> (BS.unpack fp, AcceptAlways) | ||
[fp, "AcceptHash"] -> buildAcceptHash fp "00" | ||
[fp, "AcceptHash", h] -> buildAcceptHash fp h | ||
_ -> error $ "Not able to parse:" ++ show bs | ||
where | ||
buildAcceptHash :: ByteString -> ByteString -> (FilePath, HookAccept) | ||
buildAcceptHash fp h = | ||
case hashValueFromHex h of | ||
Left err -> error $ "Distribution.Client.HookAccept.parse :" ++ err | ||
Right hv -> (BS.unpack fp, AcceptHash hv) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this line look? Can you print a command that likely inserts the right value?
?