From 7b050e9fe2d0824fccc0bb458d2146efe940753c Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 6 Jun 2021 11:57:37 +0200 Subject: [PATCH] Fix ghcup-gen validation for global tools --- app/ghcup-gen/Main.hs | 29 +++++++++++++---------------- app/ghcup-gen/Validate.hs | 28 +++++++++++++++------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/app/ghcup-gen/Main.hs b/app/ghcup-gen/Main.hs index cc8248a..9badd71 100644 --- a/app/ghcup-gen/Main.hs +++ b/app/ghcup-gen/Main.hs @@ -69,13 +69,15 @@ tarballFilterP = option readm $ long "tarball-filter" <> short 'u' <> metavar "-" <> value def <> help "Only check certain tarballs (format: -)" where - def = TarballFilter Nothing (makeRegex ("" :: String)) + def = TarballFilter (Right Nothing) (makeRegex ("" :: String)) readm = do s <- str case span (/= '-') s of (_, []) -> fail "invalid format, missing '-' after the tool name" (t, v) | [tool] <- [ tool | tool <- [minBound..maxBound], low (show tool) == low t ] -> - pure (TarballFilter $ Just tool) <*> makeRegexOptsM compIgnoreCase execBlank (drop 1 v) + pure (TarballFilter $ Right $ Just tool) <*> makeRegexOptsM compIgnoreCase execBlank (drop 1 v) + (t, v) | [tool] <- [ tool | tool <- [minBound..maxBound], low (show tool) == low t ] -> + pure (TarballFilter $ Left tool) <*> makeRegexOptsM compIgnoreCase execBlank (drop 1 v) _ -> fail "invalid tool" low = fmap toLower @@ -105,23 +107,18 @@ main :: IO () main = do _ <- customExecParser (prefs showHelpOnError) (info (opts <**> helper) idm) >>= \Options {..} -> case optCommand of - ValidateYAML vopts -> case vopts of - ValidateYAMLOpts { vInput = Nothing } -> - B.getContents >>= valAndExit validate - ValidateYAMLOpts { vInput = Just StdInput } -> - B.getContents >>= valAndExit validate - ValidateYAMLOpts { vInput = Just (FileInput file) } -> - B.readFile file >>= valAndExit validate - ValidateTarballs vopts tarballFilter -> case vopts of - ValidateYAMLOpts { vInput = Nothing } -> - B.getContents >>= valAndExit (validateTarballs tarballFilter) - ValidateYAMLOpts { vInput = Just StdInput } -> - B.getContents >>= valAndExit (validateTarballs tarballFilter) - ValidateYAMLOpts { vInput = Just (FileInput file) } -> - B.readFile file >>= valAndExit (validateTarballs tarballFilter) + ValidateYAML vopts -> withValidateYamlOpts vopts validate + ValidateTarballs vopts tarballFilter -> withValidateYamlOpts vopts (validateTarballs tarballFilter) pure () where + withValidateYamlOpts vopts f = case vopts of + ValidateYAMLOpts { vInput = Nothing } -> + B.getContents >>= valAndExit f + ValidateYAMLOpts { vInput = Just StdInput } -> + B.getContents >>= valAndExit f + ValidateYAMLOpts { vInput = Just (FileInput file) } -> + B.readFile file >>= valAndExit f valAndExit f contents = do (GHCupInfo _ av gt) <- case Y.decodeEither' contents of Right r -> pure r diff --git a/app/ghcup-gen/Validate.hs b/app/ghcup-gen/Validate.hs index 49e116b..6bd0535 100644 --- a/app/ghcup-gen/Validate.hs +++ b/app/ghcup-gen/Validate.hs @@ -184,7 +184,7 @@ validate dls _ = do isBase _ = False data TarballFilter = TarballFilter - { tfTool :: Maybe Tool + { tfTool :: Either GlobalTool (Maybe Tool) , tfVersion :: Regex } @@ -201,17 +201,16 @@ validateTarballs :: ( Monad m -> GHCupDownloads -> M.Map GlobalTool DownloadInfo -> m ExitCode -validateTarballs (TarballFilter tool versionRegex) dls gt = do +validateTarballs (TarballFilter etool versionRegex) dls gt = do ref <- liftIO $ newIORef 0 flip runReaderT ref $ do -- download/verify all tarballs - let dlis = nubOrd $ dls ^.. each %& indices (maybe (const True) (==) tool) %> each %& indices (matchTest versionRegex . T.unpack . prettyVer) % (viSourceDL % _Just `summing` viArch % each % each % each) - when (null dlis) $ $(logError) [i|no tarballs selected by filter|] *> addError - + let dlis = either (const []) (\tool -> nubOrd $ dls ^.. each %& indices (maybe (const True) (==) tool) %> each %& indices (matchTest versionRegex . T.unpack . prettyVer) % (viSourceDL % _Just `summing` viArch % each % each % each)) etool let gdlis = nubOrd $ gt ^.. each - - forM_ (dlis ++ gdlis) downloadAll + let allDls = dlis ++ gdlis + when (null allDls) $ $(logError) [i|no tarballs selected by filter|] *> addError + forM_ allDls downloadAll -- exit e <- liftIO $ readIORef ref @@ -254,18 +253,21 @@ validateTarballs (TarballFilter tool versionRegex) dls gt = do #endif ] $ do - case tool of - Just GHCup -> do - let fn = "ghcup" - p <- liftE $ download (settings appstate) dli (cacheDir dirs) (Just fn) - liftE $ checkDigest (settings appstate) dli p + case etool of + Right (Just GHCup) -> do + tmpUnpack <- lift mkGhcupTmpDir + _ <- liftE $ download (settings appstate) dli tmpUnpack Nothing pure Nothing - _ -> do + Right _ -> do p <- liftE $ downloadCached (settings appstate) dirs dli Nothing fmap (Just . head . splitDirectories . head) . liftE . getArchiveFiles $ p + Left ShimGen -> do + tmpUnpack <- lift mkGhcupTmpDir + _ <- liftE $ download (settings appstate) dli tmpUnpack Nothing + pure Nothing case r of VRight (Just basePath) -> do case _dlSubdir dli of