on master help sign in
Connect to <package.elm-lang.org> less often

Do not connect: - when elm-stuff/ is valid - exploring constraints Optional connect: - when elm-stuff/ is invalid - install Mandatory connect: - diff - bump - publish

commit5c327b7b3894e89304ad0fe9454b24b291e1b721 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 15 May 2018, 22:40 (8 years ago) parentaf7a6b4
8 files changed+140−86
builder/src/Deps/Get.hs → builder/src/Deps/Cache.hs+98−48 builder/src/Deps/Explorer.hs+10−11 builder/src/Deps/Verify.hs+6−4 builder/src/Elm/Bump.hs+4−4 builder/src/Elm/Diff.hs+6−6 builder/src/Elm/Install.hs+11−8 builder/src/Elm/Publish.hs+4−4 elm.cabal+1−1
builder/src/Deps/Get.hs → builder/src/Deps/Cache.hsrenamed+98−48
@@ -1,25 +1,28 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings #-}
3−module Deps.Get
4− ( all
5− , AllPackages
6− , Mode(..)
7− , versions
8− , info
9− , docs
3+module Deps.Cache
4+ ( update
5+ , optionalUpdate
6+ , mandatoryUpdate
7+ , PackageRegistry
8+ , getPackageRegistry
9+ , getVersions
10+ , getElmJson
11+ , getDocs
1012 )
1113 where
1214
1315
1416import Prelude hiding (all)
17+import Control.Monad (liftM2)
1518import Control.Monad.Except (catchError, liftIO)
19+import Data.Binary (Binary, get, put)
1620import qualified Data.ByteString as BS
1721import Data.Function (on)
1822import qualified Data.List as List
1923import qualified Data.Map as Map
2024import Data.Map (Map)
2125import qualified Data.Text as Text
22−import qualified System.Directory as Dir
2326import System.FilePath ((</>))
2427
2528import qualified Elm.Docs as Docs
@@ -38,53 +41,91 @@ import qualified Reporting.Task as Task
3841
3942
4043
41−-- ALL VERSIONS
44+-- PACKAGE REGISTRY
4245
4346
44−data Mode = RequireLatest | AllowOffline
47+data PackageRegistry =
48+ PackageRegistry Int (Map Pkg.Name [Pkg.Version])
4549
4650
47−newtype AllPackages = AllPackages (Map Pkg.Name [Pkg.Version])
51+getPackageRegistry :: Task.Task PackageRegistry
52+getPackageRegistry =
53+ do path <- getRegistryPath
54+ exists <- IO.exists path
55+ if exists
56+ then IO.readBinary path
57+ else rebuild
4858
4959
50−all :: Mode -> Task.Task AllPackages
51−all mode =
60+getRegistryPath :: Task.Task FilePath
61+getRegistryPath =
5262 do dir <- Task.getPackageCacheDir
53− let versionsFile = dir </> "versions.dat"
54− exists <- liftIO $ Dir.doesFileExist versionsFile
55− if exists
56− then fetchNew versionsFile mode
57− else fetchAll versionsFile
63+ return (dir </> "versions.dat")
64+
65+
66+
67+-- REBUILD
5868
5969
60−fetchAll :: FilePath -> Task.Task AllPackages
61−fetchAll versionsFile =
62− do packages <- Website.getAllPackages
70+rebuild :: Task.Task PackageRegistry
71+rebuild =
72+ do path <- getRegistryPath
73+ packages <- Website.getAllPackages
6374 let size = Map.foldr ((+) . length) 0 packages
64− IO.writeBinary versionsFile (size, packages)
65− return (AllPackages packages)
75+ let registry = PackageRegistry size packages
76+ IO.writeBinary path registry
77+ return registry
6678
6779
68−fetchNew :: FilePath -> Mode -> Task.Task AllPackages
69−fetchNew versionsFile mode =
70− do (size, packages) <- IO.readBinary versionsFile
7180
72− news <-
73− case mode of
74− RequireLatest ->
75− Website.getNewPackages size
81+-- UPDATE
7682
77− AllowOffline ->
78− Website.getNewPackages size `catchError` \_ ->
79− do Task.report Progress.UnableToLoadLatestPackages
80− return []
8183
82− if null news
83− then return (AllPackages packages)
84+optionalUpdate :: Task.Task PackageRegistry
85+optionalUpdate =
86+ update False
87+
88+
89+mandatoryUpdate :: Task.Task PackageRegistry
90+mandatoryUpdate =
91+ update True
92+
93+
94+update :: Bool -> Task.Task PackageRegistry
95+update isMandatory =
96+ do path <- getRegistryPath
97+ exists <- IO.exists path
98+ if not exists
99+ then rebuild
84100 else
85− do let newAllPkgs = List.foldl' addNew packages news
86− IO.writeBinary versionsFile (size + length news, newAllPkgs)
87− return (AllPackages newAllPkgs)
101+ do registry <- IO.readBinary path
102+ if isMandatory
103+ then attemptUpdate registry
104+ else attemptUpdate registry `catchError` recoverUpdate registry
105+
106+
107+recoverUpdate :: PackageRegistry -> a -> Task.Task PackageRegistry
108+recoverUpdate registry _ =
109+ do Task.report Progress.UnableToLoadLatestPackages
110+ return registry
111+
112+
113+attemptUpdate :: PackageRegistry -> Task.Task PackageRegistry
114+attemptUpdate oldRegistry@(PackageRegistry size packages) =
115+ do news <- Website.getNewPackages size
116+ case news of
117+ [] ->
118+ return oldRegistry
119+
120+ _:_ ->
121+ let
122+ newSize = size + length news
123+ newPkgs = List.foldl' addNew packages news
124+ newRegistry = PackageRegistry newSize newPkgs
125+ in
126+ do path <- getRegistryPath
127+ IO.writeBinary path newRegistry
128+ return newRegistry
88129
89130
90131addNew :: Map Pkg.Name [Pkg.Version] -> (Pkg.Name, Pkg.Version) -> Map Pkg.Name [Pkg.Version]
@@ -93,11 +134,11 @@ addNew packages (name, version) =
93134
94135
95136
96−-- VERSIONS
137+-- GET VERSIONS
97138
98139
99−versions :: Pkg.Name -> AllPackages -> Either [Pkg.Name] [Pkg.Version]
100−versions name (AllPackages pkgs) =
140+getVersions :: Pkg.Name -> PackageRegistry -> Either [Pkg.Name] [Pkg.Version]
141+getVersions name (PackageRegistry _ pkgs) =
101142 case Map.lookup name pkgs of
102143 Just vsns ->
103144 Right vsns
@@ -137,12 +178,12 @@ projectDistance bad possibility =
137178-- PACKAGE INFO
138179
139180
140−info :: Pkg.Name -> Pkg.Version -> Task.Task Project.PkgInfo
141−info name version =
181+getElmJson :: Pkg.Name -> Pkg.Version -> Task.Task Project.PkgInfo
182+getElmJson name version =
142183 do dir <- Task.getPackageCacheDirFor name version
143184 let elmJson = dir </> "elm.json"
144185
145− exists <- liftIO $ Dir.doesFileExist elmJson
186+ exists <- IO.exists elmJson
146187
147188 json <-
148189 if exists
@@ -165,12 +206,12 @@ info name version =
165206-- DOCS
166207
167208
168−docs :: Pkg.Name -> Pkg.Version -> Task.Task Docs.Documentation
169−docs name version =
209+getDocs :: Pkg.Name -> Pkg.Version -> Task.Task Docs.Documentation
210+getDocs name version =
170211 do dir <- Task.getPackageCacheDirFor name version
171212 let docsJson = dir </> "docs.json"
172213
173− exists <- liftIO $ Dir.doesFileExist docsJson
214+ exists <- IO.exists docsJson
174215
175216 json <-
176217 if exists
@@ -209,3 +250,12 @@ errorToDocs err =
209250 ,"<https://github.com/elm-lang/package.elm-lang.org/issues>"
210251 ,"if","you","think","it","is","on","the","Elm","side!"
211252 ]
253+
254+
255+
256+-- BINARY
257+
258+
259+instance Binary PackageRegistry where
260+ get = liftM2 PackageRegistry get get
261+ put (PackageRegistry a b) = put a >> put b
builder/src/Deps/Explorer.hs+10−11
@@ -26,7 +26,7 @@ import qualified Data.Map as Map
2626
2727import Elm.Package (Name, Version)
2828
29−import qualified Deps.Get as Get
29+import qualified Deps.Cache as Cache
3030import qualified Elm.PerUserCache as PerUserCache
3131import qualified Elm.Project.Json as Project
3232import Elm.Project.Constraint (Constraint)
@@ -45,7 +45,7 @@ type Explorer =
4545
4646data Metadata =
4747 Metadata
48− { _vsns :: Get.AllPackages
48+ { _registry :: Cache.PackageRegistry
4949 , _info :: Map (Name, Version) Info
5050 }
5151
@@ -57,10 +57,9 @@ data Info =
5757 }
5858
5959
60−run :: Explorer a -> Task.Task a
61−run explorer =
62− do versions <- Get.all Get.AllowOffline
63− evalStateT explorer (Metadata versions Map.empty)
60+run :: Cache.PackageRegistry -> Explorer a -> Task.Task a
61+run registry explorer =
62+ evalStateT explorer (Metadata registry Map.empty)
6463
6564
6665
@@ -69,8 +68,8 @@ run explorer =
6968
7069exists :: Name -> Explorer ()
7170exists name =
72− do pkgs <- gets _vsns
73− case Get.versions name pkgs of
71+ do registry <- gets _registry
72+ case Cache.getVersions name registry of
7473 Right _ ->
7574 return ()
7675
@@ -84,8 +83,8 @@ exists name =
8483
8584getVersions :: Name -> Explorer [Version]
8685getVersions name =
87− do pkgs <- gets _vsns
88− case Get.versions name pkgs of
86+ do registry <- gets _registry
87+ case Cache.getVersions name registry of
8988 Right versions ->
9089 return versions
9190
@@ -106,7 +105,7 @@ getConstraints name version =
106105 return info
107106
108107 Nothing ->
109− do pkgInfo <- lift $ Get.info name version
108+ do pkgInfo <- lift $ Cache.getElmJson name version
110109
111110 let elm = Project._pkg_elm_version pkgInfo
112111 let pkgs = Project._pkg_deps pkgInfo
builder/src/Deps/Verify.hs+6−4
@@ -23,8 +23,8 @@ import qualified Elm.PerUserCache as PerUserCache
2323import Elm.Project.Json (Project(..), AppInfo(..), PkgInfo(..))
2424import qualified Json.Encode as Encode
2525
26+import qualified Deps.Cache as Cache
2627import qualified Deps.Explorer as Explorer
27−import qualified Deps.Get as Get
2828import qualified Deps.Solver as Solver
2929import qualified Deps.Website as Website
3030import qualified Elm.Compiler as Compiler
@@ -73,7 +73,8 @@ verifyApp info =
7373 else
7474 do let oldSolution = Project.appSolution info
7575 let solver = Solver.solve (Map.map Con.exactly oldSolution)
76− maybeSolution <- Explorer.run (Solver.run solver)
76+ registry <- Cache.optionalUpdate
77+ maybeSolution <- Explorer.run registry (Solver.run solver)
7778 case maybeSolution of
7879 Nothing ->
7980 throw E.BadDeps
@@ -90,7 +91,8 @@ verifyPkg info =
9091 else
9192 do let deps = Map.union (_pkg_deps info) (_pkg_test_deps info)
9293 let solver = Solver.solve deps
93− maybeSolution <- Explorer.run (Solver.run solver)
94+ registry <- Cache.optionalUpdate
95+ maybeSolution <- Explorer.run registry (Solver.run solver)
9496 case maybeSolution of
9597 Nothing ->
9698 throw E.BadDeps
@@ -145,7 +147,7 @@ verifyBuild
145147 -> Task.Task (MVar Answer)
146148verifyBuild pkgInfoMVar ifacesMVar name version =
147149 do mvar <- liftIO newEmptyMVar
148− info <- Get.info name version
150+ info <- Cache.getElmJson name version
149151 report <- Task.getReporter
150152 runner <- Task.getSilentRunner
151153
builder/src/Elm/Bump.hs+4−4
@@ -9,8 +9,8 @@ module Elm.Bump
99import Control.Monad.Trans (liftIO)
1010import qualified Data.List as List
1111
12+import qualified Deps.Cache as Cache
1213import qualified Deps.Diff as Diff
13−import qualified Deps.Get as Get
1414import qualified Elm.Package as Pkg
1515import qualified Elm.Project as Project
1616import qualified Elm.Project.Json as Project
@@ -35,8 +35,8 @@ bump summary@(Summary.Summary root project _ _ _) =
3535 Task.throw (Exit.Bump E.Application)
3636
3737 Project.Pkg info@(Project.PkgInfo name _ _ version _ _ _ _) ->
38− do pkgs <- Get.all Get.RequireLatest
39− case Get.versions name pkgs of
38+ do registry <- Cache.mandatoryUpdate
39+ case Cache.getVersions name registry of
4040 Left _suggestions ->
4141 checkNewPackage root info
4242
@@ -101,7 +101,7 @@ checkNewPackage root info@(Project.PkgInfo _ _ _ version _ _ _ _) =
101101
102102suggestVersion :: Summary.Summary -> Project.PkgInfo -> Task.Task ()
103103suggestVersion summary@(Summary.Summary root _ _ _ _) info@(Project.PkgInfo name _ _ version _ _ _ _) =
104− do oldDocs <- Get.docs name version
104+ do oldDocs <- Cache.getDocs name version
105105 newDocs <- Task.silently (Project.generateDocs summary)
106106 let changes = Diff.diff oldDocs newDocs
107107 let newVersion = Diff.bump changes version
builder/src/Elm/Diff.hs+6−6
@@ -12,9 +12,9 @@ import qualified Data.List as List
1212import qualified Data.Map as Map
1313import qualified Data.Maybe as Maybe
1414
15+import qualified Deps.Cache as Cache
1516import Deps.Diff (PackageChanges(..), ModuleChanges(..), Changes(..), Magnitude(..))
1617import qualified Deps.Diff as Diff
17−import qualified Deps.Get as Get
1818import qualified Elm.Compiler.Module as Module
1919import qualified Elm.Compiler.Type as Type
2020import qualified Elm.Docs as Docs
@@ -47,8 +47,8 @@ diff :: Args -> Task.Task ()
4747diff args =
4848 case args of
4949 GlobalInquiry name v1 v2 ->
50− do pkgs <- Get.all Get.RequireLatest
51− case Get.versions name pkgs of
50+ do registry <- Cache.mandatoryUpdate
51+ case Cache.getVersions name registry of
5252 Left suggestions ->
5353 throw $ E.UnknownPackage name suggestions
5454
@@ -88,7 +88,7 @@ throw exit =
8888getDocs :: Pkg.Name -> [Pkg.Version] -> Pkg.Version -> Task.Task Docs.Documentation
8989getDocs name allVersions version =
9090 if elem version allVersions then
91− Get.docs name version
91+ Cache.getDocs name version
9292 else
9393 throw $ E.UnknownVersion name version allVersions
9494
@@ -101,8 +101,8 @@ getPackageInfo =
101101 throw $ E.Application
102102
103103 Project.Pkg (Project.PkgInfo name _ _ _ _ _ _ _) ->
104− do pkgs <- Get.all Get.RequireLatest
105− let vsns = either (const []) id (Get.versions name pkgs)
104+ do registry <- Cache.mandatoryUpdate
105+ let vsns = either (const []) id (Cache.getVersions name registry)
106106 return ( summary, name, vsns )
107107
108108
builder/src/Elm/Install.hs+11−8
@@ -15,6 +15,7 @@ import System.IO (stdout)
1515import qualified Elm.Package as Pkg
1616import Elm.Package (Name, Version)
1717
18+import qualified Deps.Cache as Cache
1819import Deps.Explorer (Explorer)
1920import qualified Deps.Explorer as Explorer
2021import qualified Deps.Verify as Verify
@@ -58,7 +59,8 @@ makePlan :: Name -> Project.Project -> (Project.Project -> Task.Task ()) -> Task
5859makePlan pkg project attemptInstall =
5960 case project of
6061 Project.App info@(Project.AppInfo elm srcDirs deps test trans) ->
61− do changes <- addToApp pkg info
62+ do registry <- Cache.optionalUpdate
63+ changes <- addToApp registry pkg info
6264
6365 let news = Map.mapMaybe keepNew changes
6466 let newDeps = addNews (Just pkg) news deps
@@ -70,7 +72,8 @@ makePlan pkg project attemptInstall =
7072 withApproval Pkg.versionToString changes (attemptInstall newProject)
7173
7274 Project.Pkg info@(Project.PkgInfo _ _ _ _ _ deps test _) ->
73− do changes <- addToPkg pkg info
75+ do registry <- Cache.optionalUpdate
76+ changes <- addToPkg registry pkg info
7477
7578 let news = Map.mapMaybe keepNew changes
7679 let newProject = Project.Pkg $
@@ -137,9 +140,9 @@ keepNew change =
137140-- ADD TO APP
138141
139142
140−addToApp :: Name -> Project.AppInfo -> Task.Task (Map Name (Change Version))
141−addToApp pkg info@(Project.AppInfo _ _ deps tests trans) =
142− Explorer.run $
143+addToApp :: Cache.PackageRegistry -> Name -> Project.AppInfo -> Task.Task (Map Name (Change Version))
144+addToApp registry pkg info@(Project.AppInfo _ _ deps tests trans) =
145+ Explorer.run registry $
143146 do Explorer.exists pkg
144147 let old = Map.unions [ deps, tests, trans ]
145148 result <- Solver.run (addToAppHelp pkg info)
@@ -177,9 +180,9 @@ addToAppHelp pkg (Project.AppInfo _ _ deps tests trans) =
177180-- ADD TO PKG
178181
179182
180−addToPkg :: Name -> Project.PkgInfo -> Task.Task (Map Name (Change Constraint))
181−addToPkg pkg info@(Project.PkgInfo _ _ _ _ _ deps tests _) =
182− Explorer.run $
183+addToPkg :: Cache.PackageRegistry -> Name -> Project.PkgInfo -> Task.Task (Map Name (Change Constraint))
184+addToPkg registry pkg info@(Project.PkgInfo _ _ _ _ _ deps tests _) =
185+ Explorer.run registry $
183186 do Explorer.exists pkg
184187 let old = Map.union deps tests
185188 result <- Solver.run (addToPkgHelp pkg info)
builder/src/Elm/Publish.hs+4−4
@@ -13,8 +13,8 @@ import System.FilePath ((</>))
1313import qualified System.IO as IO
1414import qualified System.Process as Process
1515
16+import qualified Deps.Cache as Cache
1617import qualified Deps.Diff as Diff
17−import qualified Deps.Get as Get
1818import qualified Deps.Website as Website
1919import qualified Elm.Bump as Bump
2020import qualified Elm.Docs as Docs
@@ -41,8 +41,8 @@ publish summary@(Summary.Summary root project _ _ _) =
4141
4242 Project.Pkg (Project.PkgInfo name smry _ version exposed _ _ _) ->
4343 do
44− pkgs <- Get.all Get.RequireLatest
45− let maybePublishedVersions = either (const Nothing) Just (Get.versions name pkgs)
44+ registry <- Cache.mandatoryUpdate
45+ let maybePublishedVersions = either (const Nothing) Just (Cache.getVersions name registry)
4646
4747 Task.report (Progress.PublishStart name version maybePublishedVersions)
4848
@@ -244,7 +244,7 @@ verifyBump name statedVersion newDocs publishedVersions =
244244 throw $ E.InvalidBump statedVersion (last publishedVersions)
245245
246246 Just (old, new, magnitude) ->
247− do oldDocs <- Get.docs name old
247+ do oldDocs <- Cache.getDocs name old
248248 let changes = Diff.diff oldDocs newDocs
249249 let realNew = Diff.bump changes old
250250 if new == realNew
elm.cabal+1−1
@@ -88,9 +88,9 @@ Executable elm
8888 Reporting.Task,
8989
9090 -- private from builder/
91+ Deps.Cache,
9192 Deps.Diff,
9293 Deps.Explorer,
93− Deps.Get,
9494 Deps.Solver,
9595 Deps.Verify,
9696 Deps.Website,