Skip to content

Commit

Permalink
use official pages mirror instead of git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
gutjuri committed Oct 9, 2020
1 parent e42aea9 commit a9e7790
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import Distribution.Simple
import Distribution.Simple
main = defaultMain
2 changes: 1 addition & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Main where

import Tldr.App (appMain)
import Tldr.App ( appMain )

main :: IO ()
main = appMain
8 changes: 6 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -34,10 +34,14 @@ library:
- optparse-applicative
- directory
- filepath
- typed-process
- semigroups
- containers
- http-conduit
- zip-archive

ghc-options:
- -Wall
- -O2

executables:
tldr:
Expand Down
4 changes: 2 additions & 2 deletions src/Tldr/App/Constant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 16 additions & 32 deletions src/Tldr/App/Handler.hs
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
{-#LANGUAGE RecordWildCards#-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}

module Tldr.App.Handler
( handleAboutFlag
, retriveLocale
, 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
)
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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 =
Expand Down
12 changes: 12 additions & 0 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions tldr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -50,6 +50,7 @@ library
Paths_tldr
hs-source-dirs:
src
ghc-options: -Wall
build-depends:
ansi-terminal
, base >=4.7 && <5
Expand All @@ -58,10 +59,11 @@ library
, containers
, directory
, filepath
, http-conduit
, optparse-applicative
, semigroups
, text
, typed-process
, zip-archive
default-language: Haskell2010

executable tldr
Expand All @@ -70,6 +72,7 @@ executable tldr
Paths_tldr
hs-source-dirs:
app
ghc-options: -Wall
build-depends:
base
, tldr
Expand All @@ -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
Expand Down

0 comments on commit a9e7790

Please sign in to comment.