diff --git a/Setup.hs b/Setup.hs index 9a994af..4467109 100644 --- a/Setup.hs +++ b/Setup.hs @@ -1,2 +1,2 @@ -import Distribution.Simple +import Distribution.Simple main = defaultMain diff --git a/app/Main.hs b/app/Main.hs index 961012d..57b556d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,6 @@ module Main where -import Tldr.App (appMain) +import Tldr.App ( appMain ) main :: IO () main = appMain diff --git a/package.yaml b/package.yaml index b112ccb..c101252 100644 --- a/package.yaml +++ b/package.yaml @@ -1,5 +1,5 @@ name: tldr -version: '0.8.0' +version: '0.8.1' synopsis: Haskell tldr client description: | Haskell tldr client with support for viewing tldr pages. Has offline @@ -34,10 +34,14 @@ library: - optparse-applicative - directory - filepath - - typed-process - semigroups - containers + - http-conduit + - zip-archive +ghc-options: + - -Wall + - -O2 executables: tldr: diff --git a/src/Tldr/App/Constant.hs b/src/Tldr/App/Constant.hs index 688a246..a45fcba 100644 --- a/src/Tldr/App/Constant.hs +++ b/src/Tldr/App/Constant.hs @@ -3,8 +3,8 @@ module Tldr.App.Constant where tldrDirName :: String tldrDirName = "tldr" -repoHttpsUrl :: String -repoHttpsUrl = "https://github.com/tldr-pages/tldr.git" +pagesUrl :: String +pagesUrl = "https://tldr.sh/assets/tldr.zip" checkDirs :: [String] checkDirs = "common" : platformDirs diff --git a/src/Tldr/App/Handler.hs b/src/Tldr/App/Handler.hs index fb5ad6e..904edc8 100644 --- a/src/Tldr/App/Handler.hs +++ b/src/Tldr/App/Handler.hs @@ -1,5 +1,6 @@ -{-#LANGUAGE RecordWildCards#-} -{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE OverloadedStrings #-} module Tldr.App.Handler ( handleAboutFlag @@ -7,25 +8,24 @@ module Tldr.App.Handler , checkLocale , englishViewOptions , getCheckDirs - , initializeTldrPages , pageExists , getPagePath , updateTldrPages , handleTldrOpts ) where -import Control.Monad (unless) import Data.Char (toLower) import Data.List (intercalate) import Data.Semigroup ((<>)) import qualified Data.Set as Set import Data.Version (showVersion) + import Options.Applicative import Paths_tldr (version) import System.Directory ( XdgDirectory(..) - , createDirectoryIfMissing - , doesDirectoryExist + , createDirectory + , removePathForcibly , doesFileExist , getXdgDirectory ) @@ -33,7 +33,9 @@ import System.Environment (lookupEnv, getExecutablePath) import System.Exit (exitFailure) import System.FilePath ((<.>), ()) import System.IO (hPutStrLn, stderr, stdout) -import System.Process.Typed +import Network.HTTP.Simple +import Codec.Archive.Zip + import Tldr import Tldr.App.Constant import Tldr.Types @@ -89,14 +91,12 @@ handleTldrOpts opts@TldrOpts {..} = updateTldrPages :: IO () updateTldrPages = do dataDir <- getXdgDirectory XdgData tldrDirName - let repoDir = dataDir "tldr" - repoExists <- doesDirectoryExist repoDir - if repoExists - then do - putStrLn $ "Downloading tldr pages to " ++ repoDir - runProcess_ $ - setWorkingDir repoDir $ proc "git" ["pull", "origin", "master"] - else initializeTldrPages + removePathForcibly dataDir + createDirectory dataDir + putStrLn $ "Downloading tldr pages to " ++ dataDir + response <- httpLBS $ parseRequest_ pagesUrl + let zipArchive = toArchive $ getResponseBody response + extractFilesFromArchive [OptDestination dataDir] zipArchive computeLocale :: Maybe String -> Locale computeLocale lang = case map toLower <$> lang of @@ -114,7 +114,7 @@ getPagePath locale page pDirs = do Other xs -> "pages." <> xs Unknown xs -> "pages." <> xs Missing -> "pages" - pageDir = dataDir "tldr" currentLocale + pageDir = dataDir currentLocale paths = map (\x -> pageDir x page <.> "md") pDirs foldr1 (<|>) <$> mapM pageExists paths @@ -125,22 +125,6 @@ pageExists fname = do then return $ Just fname else return Nothing -tldrInitialized :: IO Bool -tldrInitialized = do - dataDir <- getXdgDirectory XdgData tldrDirName - let dir2 = dataDir "tldr" - pages = dataDir "tldr" "pages" - exists <- mapM doesDirectoryExist [dataDir, dir2, pages] - return $ all (== True) exists - -initializeTldrPages :: IO () -initializeTldrPages = do - initialized <- tldrInitialized - unless initialized $ do - dataDir <- getXdgDirectory XdgData tldrDirName - createDirectoryIfMissing False dataDir - putStrLn $ "Initialising tldr page storage in " ++ dataDir - runProcess_ $ setWorkingDir dataDir $ proc "git" ["clone", repoHttpsUrl] getCheckDirs :: ViewOptions -> [String] getCheckDirs voptions = diff --git a/stack.yaml.lock b/stack.yaml.lock new file mode 100644 index 0000000..8d62222 --- /dev/null +++ b/stack.yaml.lock @@ -0,0 +1,12 @@ +# This file was autogenerated by Stack. +# You should not edit this file by hand. +# For more information, please see the documentation at: +# https://docs.haskellstack.org/en/stable/lock_files + +packages: [] +snapshots: +- completed: + size: 523700 + url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/14/7.yaml + sha256: 8e3f3c894be74d71fa4bf085e0a8baae7e4d7622d07ea31a52736b80f8b9bb1a + original: lts-14.7 diff --git a/tldr.cabal b/tldr.cabal index 2ae9e2f..36e0fa8 100644 --- a/tldr.cabal +++ b/tldr.cabal @@ -4,10 +4,10 @@ cabal-version: 1.12 -- -- see: https://github.com/sol/hpack -- --- hash: 6227f2af49b8d2a6bce7e1b2f3b3dd1df2bfe9922b13aaa8e10824c73f2d1b51 +-- hash: 21691ba2cc19cdb8ae6d8a3bd0e84803e28b8866530a0da9fe44e6187df4fec1 name: tldr -version: 0.8.0 +version: 0.8.1 synopsis: Haskell tldr client description: Haskell tldr client with support for viewing tldr pages. Has offline cache for accessing pages. Visit https://tldr.sh for more details. @@ -50,6 +50,7 @@ library Paths_tldr hs-source-dirs: src + ghc-options: -Wall build-depends: ansi-terminal , base >=4.7 && <5 @@ -58,10 +59,11 @@ library , containers , directory , filepath + , http-conduit , optparse-applicative , semigroups , text - , typed-process + , zip-archive default-language: Haskell2010 executable tldr @@ -70,6 +72,7 @@ executable tldr Paths_tldr hs-source-dirs: app + ghc-options: -Wall build-depends: base , tldr @@ -87,7 +90,7 @@ test-suite tldr-test Paths_tldr hs-source-dirs: test - ghc-options: -threaded -rtsopts -with-rtsopts=-N + ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N build-depends: base , tasty