diff --git a/CHANGELOG.md b/CHANGELOG.md index b17a5f8..1e2ecf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ `uusi` uses [PVP Versioning][1]. The changelog is available [on GitHub][2]. +## 0.1.0.0 + +* Remove diff + +* Reduce dependencies as much as possible + ## 0.0.1.0 * Support sub-libraries and legacy build tools. diff --git a/app/Lens.hs b/app/Lens.hs new file mode 100644 index 0000000..2b627fe --- /dev/null +++ b/app/Lens.hs @@ -0,0 +1,18 @@ +module Lens + ( module Distribution.Compat.Lens, + mapped, + (|>), + (<&>), + ) +where + +import Data.Functor ((<&>)) +import Data.Functor.Identity (Identity (..)) +import Distribution.Compat.Lens + +mapped :: Functor f => ASetter (f a) (f b) a b +mapped k = Identity . fmap (runIdentity . k) + +-- qwq +(|>) :: a -> (a -> b) -> b +(|>) = (&) \ No newline at end of file diff --git a/app/Main.hs b/app/Main.hs index 2c790a4..327185c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,9 +5,9 @@ module Main (main) where -import qualified Colourista as C import qualified Control.Exception as CE import qualified Data.Text as T +import qualified Data.Text.IO as T import Distribution.PackageDescription.Parsec (readGenericPackageDescription) import Distribution.PackageDescription.PrettyPrint (showGenericPackageDescription) import Distribution.Types.CondTree @@ -17,66 +17,34 @@ import Distribution.Types.LegacyExeDependency (LegacyExeDependency (..)) import Distribution.Types.Lens import Distribution.Types.VersionRange (anyVersion) import qualified Distribution.Verbosity as Verbosity -import Lens.Micro -import Options.Applicative -import System.Directory (getTemporaryDirectory, removeFile) -import System.IO (hClose, hFlush, hPutStr, openTempFile) -import System.Process (readCreateProcessWithExitCode, shell) - -newtype Options = Options - { optPath :: FilePath - } - -cmdOptions :: Parser Options -cmdOptions = Options <$> strArgument (metavar "PATH" <> help "Path to .cabal file") - -runArgsParser :: IO Options -runArgsParser = - execParser $ - info - (cmdOptions <**> helper) - ( fullDesc - <> progDesc "Try to reach the TARGET QAQ." - <> header "uusi - a program removing all version constraints of dependencies in .cabal file" - ) +import Lens +import System.Environment (getArgs) ----------------------------------------------------------------------------- main :: IO () main = CE.catch @CE.IOException ( do - Options {..} <- runArgsParser - C.infoMessage "Start running..." - uusiCabal optPath >>= putStrLn + args <- getArgs + case args of + ["--help"] -> showHelp + ["-help"] -> showHelp + ["help"] -> showHelp + [path] -> uusiCabal path + _ -> showHelp ) - $ \e -> C.errorMessage $ "IOException: " <> (T.pack . show $ e) + $ \e -> T.putStrLn $ "IOException: " <> (T.pack . show $ e) -genPatch :: FilePath -> FilePath -> IO String -genPatch a b = (^. _2) <$> readCreateProcessWithExitCode (shell $ "diff -u " <> a <> " " <> b) "" +showHelp :: IO () +showHelp = putStrLn "uusi - remove all version constraints of dependencies in a .cabal file (replace inplace)\nUsage: uusi PATH_TO_TARGET" -uusiCabal :: FilePath -> IO String +uusiCabal :: FilePath -> IO () uusiCabal originPath = do - C.infoMessage $ "Parsing cabal file from " <> T.pack originPath <> "..." - + T.putStrLn $ "Parsing cabal file from " <> T.pack originPath <> "..." cabal <- readGenericPackageDescription Verbosity.normal originPath - temp <- getTemporaryDirectory - (oldPath, oldHandle) <- openTempFile temp "uusi" - - let old = showGenericPackageDescription cabal - uusied = showGenericPackageDescription $ uusiGenericPackageDescription cabal - - hPutStr oldHandle old - writeFile originPath uusied - - C.infoMessage $ "Write file: " <> T.pack originPath - - hFlush oldHandle - hClose oldHandle - - result <- genPatch oldPath originPath - removeFile oldPath - - return result + let uusi = showGenericPackageDescription $ uusiGenericPackageDescription cabal + writeFile originPath uusi + T.putStrLn $ "Write file: " <> T.pack originPath ----------------------------------------------------------------------------- @@ -94,9 +62,9 @@ uusiExeDependency (ExeDependency name component _) = ExeDependency name componen uusiBuildInfo :: Uusi BuildInfo uusiBuildInfo i = i - & (targetBuildDepends %~ fmap uusiDependency) - & (buildToolDepends %~ fmap uusiExeDependency) - & (buildTools %~ fmap uusiLegacyExeDependency) + |> (targetBuildDepends %~ fmap uusiDependency) + |> (buildToolDepends %~ fmap uusiExeDependency) + |> (buildTools %~ fmap uusiLegacyExeDependency) uusiCondTree :: (HasBuildInfo a) => Uusi (CondTree ConfVar [Dependency] a) uusiCondTree = mapTreeData (buildInfo %~ uusiBuildInfo) . mapTreeConstrs (fmap uusiDependency) @@ -104,10 +72,10 @@ uusiCondTree = mapTreeData (buildInfo %~ uusiBuildInfo) . mapTreeConstrs (fmap u uusiGenericPackageDescription :: Uusi GenericPackageDescription uusiGenericPackageDescription cabal = cabal - & (condExecutables %~ uusiTrees) - & (condTestSuites %~ uusiTrees) - & (condBenchmarks %~ uusiTrees) - & (condSubLibraries %~ uusiTrees) - & (condLibrary . mapped %~ uusiCondTree) + |> (condExecutables %~ uusiTrees) + |> (condTestSuites %~ uusiTrees) + |> (condBenchmarks %~ uusiTrees) + |> (condSubLibraries %~ uusiTrees) + |> (condLibrary . mapped %~ uusiCondTree) where uusiTrees trees = trees <&> _2 %~ uusiCondTree \ No newline at end of file diff --git a/uusi.cabal b/uusi.cabal index 9b4dc93..ebadf12 100644 --- a/uusi.cabal +++ b/uusi.cabal @@ -1,54 +1,50 @@ -cabal-version: 2.4 -name: uusi -version: 0.0.1.0 -synopsis: Remove all version constraints of dependencies in .cabal file +cabal-version: 2.4 +name: uusi +version: 0.1.0.0 +synopsis: + Remove all version constraints of dependencies in .cabal file + description: - @uusi@ is a command-line program to remove all version constraints of dependencies in .cabal files. - It is used in distribution packaging because @--allow-newer@ is not present when calling @Setup.hs@ - instead of cabal-install. -homepage: https://github.com/berberman/uusi -bug-reports: https://github.com/berberman/uusi/issues -license: MIT -license-file: LICENSE -author: berberman -maintainer: berberman <1793913507@qq.com> -copyright: 2020 berberman -category: Distribution -build-type: Simple -extra-doc-files: README.md - CHANGELOG.md -tested-with: GHC == 8.10.2 + @uusi@ is a command-line program to remove all version constraints of dependencies in .cabal files. + It is used in distribution packaging because @--allow-newer@ is not present when calling @Setup.hs@ + instead of cabal-install. + +homepage: https://github.com/berberman/uusi +bug-reports: https://github.com/berberman/uusi/issues +license: MIT +license-file: LICENSE +author: berberman +maintainer: berberman <1793913507@qq.com> +copyright: 2020 berberman +category: Distribution +build-type: Simple +extra-doc-files: + CHANGELOG.md + README.md + +tested-with: GHC ==8.10.2 source-repository head - type: git - location: https://github.com/berberman/uusi.git + type: git + location: https://github.com/berberman/uusi.git + common common-options - build-depends: base ^>= 4.14.0, - Cabal ^>= 3.2.0, - process ^>= 1.6.9, - directory ^>= 1.3.6, - microlens ^>= 0.4.11, - optparse-applicative ^>= 0.15.1 || ^>= 0.16, - colourista ^>= 0.1, - text ^>= 1.2.3 - - ghc-options: -Wall - -Wcompat - -Widentities - -Wincomplete-uni-patterns - -Wincomplete-record-updates - -Wredundant-constraints - -fhide-source-paths - -Wmissing-export-lists - -Wpartial-fields - -Wmissing-deriving-strategies - - default-language: Haskell2010 + build-depends: + , base ^>=4.14.0 + , Cabal ^>=3.2.0 + , text ^>=1.2.3 + + ghc-options: + -Wall -Wcompat -Widentities -Wincomplete-uni-patterns + -Wincomplete-record-updates -Wredundant-constraints + -fhide-source-paths -Wmissing-export-lists -Wpartial-fields + -Wmissing-deriving-strategies + + default-language: Haskell2010 + executable uusi - import: common-options - hs-source-dirs: app - main-is: Main.hs - - ghc-options: -threaded - -rtsopts - -with-rtsopts=-N + import: common-options + hs-source-dirs: app + main-is: Main.hs + other-modules: Lens + ghc-options: -threaded -rtsopts -with-rtsopts=-N