Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 757ed04

Browse files
committed
refactor; cleanup (primarly resolve code)
1 parent bd44264 commit 757ed04

File tree

4 files changed

+33
-53
lines changed

4 files changed

+33
-53
lines changed

Diff for: Lake/CLI/Main.lean

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Lake.CLI.Error
1616
-- # CLI
1717

1818
open System
19-
open Lean (Json toJson fromJson?)
19+
open Lean (Json toJson fromJson? LeanPaths)
2020

2121
namespace Lake
2222

@@ -190,7 +190,12 @@ def printPaths (config : LoadConfig) (imports : List String := []) : MainM PUnit
190190
let ws ← MainM.runLogIO (loadWorkspace config) config.verbosity
191191
let ctx ← mkBuildContext ws
192192
let dynlibs ← ws.root.buildImportsAndDeps imports |>.run (MonadLog.eio config.verbosity) ctx
193-
IO.println <| Json.compress <| toJson {ws.leanPaths with loadDynlibPaths := dynlibs}
193+
IO.println <| Json.compress <| toJson {
194+
oleanPath := ws.leanPath
195+
srcPath := ws.leanSrcPath
196+
loadDynlibPaths := dynlibs
197+
: LeanPaths
198+
}
194199
else
195200
exit noConfigFileCode
196201

Diff for: Lake/Config/Package.lean

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ abbrev name (self : Package) : Name :=
222222
@[inline] def deps (self : Package) : Array Package :=
223223
self.opaqueDeps.map (·.get)
224224

225+
/-- The package's `packagesDir` configuration. -/
226+
def packagesDir (self : Package) : FilePath :=
227+
self.dir / self.config.packagesDir
228+
225229
/-- The package's JSON manifest of remote dependencies. -/
226230
def manifestFile (self : Package) : FilePath :=
227231
self.dir / self.config.packagesDir / manifestFileName

Diff for: Lake/Config/Workspace.lean

+7-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Lake.Config.Env
1010
import Lake.Util.Log
1111

1212
open System
13-
open Lean (LeanPaths)
1413

1514
namespace Lake
1615

@@ -35,21 +34,17 @@ hydrate_opaque_type OpaqueWorkspace Workspace
3534
namespace Workspace
3635

3736
/-- The path to the workspace's directory (i.e., the directory of the root package). -/
38-
def dir (self : Workspace) : FilePath :=
37+
@[inline] def dir (self : Workspace) : FilePath :=
3938
self.root.dir
4039

4140
/-- The workspace's configuration. -/
42-
def config (self : Workspace) : WorkspaceConfig :=
41+
@[inline] def config (self : Workspace) : WorkspaceConfig :=
4342
self.root.config.toWorkspaceConfig
4443

4544
/-- The workspace's `dir` joined with its `packagesDir` configuration. -/
46-
def packagesDir (self : Workspace) : FilePath :=
45+
@[inline] def packagesDir (self : Workspace) : FilePath :=
4746
self.dir / self.config.packagesDir
4847

49-
/-- The workspace's JSON manifest of packages. -/
50-
def manifestFile (self : Workspace) : FilePath :=
51-
self.packagesDir / manifestFileName
52-
5348
/-- The `List` of packages to the workspace. -/
5449
def packageList (self : Workspace) : List Package :=
5550
self.packageMap.revFold (fun pkgs _ pkg => pkg :: pkgs) []
@@ -63,7 +58,7 @@ def addPackage (pkg : Package) (self : Workspace) : Workspace :=
6358
{self with packageMap := self.packageMap.insert pkg.name pkg}
6459

6560
/-- Get a package within the workspace by name. -/
66-
def findPackage? (pkg : Name) (self : Workspace) : Option Package :=
61+
@[inline] def findPackage? (pkg : Name) (self : Workspace) : Option Package :=
6762
self.packageMap.find? pkg
6863

6964
/-- Check if the module is local to any package in the workspace. -/
@@ -99,23 +94,23 @@ def addModuleFacetConfig (cfg : ModuleFacetConfig name) (self : Workspace) : Wor
9994
{self with moduleFacetConfigs := self.moduleFacetConfigs.insert name cfg}
10095

10196
/-- Try to find a module facet configuration in the workspace with the given name. -/
102-
def findModuleFacetConfig? (name : Name) (self : Workspace) : Option (ModuleFacetConfig name) :=
97+
@[inline] def findModuleFacetConfig? (name : Name) (self : Workspace) : Option (ModuleFacetConfig name) :=
10398
self.moduleFacetConfigs.find? name
10499

105100
/-- Add a package facet to the workspace. -/
106101
def addPackageFacetConfig (cfg : PackageFacetConfig name) (self : Workspace) : Workspace :=
107102
{self with packageFacetConfigs := self.packageFacetConfigs.insert name cfg}
108103

109104
/-- Try to find a package facet configuration in the workspace with the given name. -/
110-
def findPackageFacetConfig? (name : Name) (self : Workspace) : Option (PackageFacetConfig name) :=
105+
@[inline] def findPackageFacetConfig? (name : Name) (self : Workspace) : Option (PackageFacetConfig name) :=
111106
self.packageFacetConfigs.find? name
112107

113108
/-- Add a library facet to the workspace. -/
114109
def addLibraryFacetConfig (cfg : LibraryFacetConfig name) (self : Workspace) : Workspace :=
115110
{self with libraryFacetConfigs := self.libraryFacetConfigs.insert cfg.name cfg}
116111

117112
/-- Try to find a library facet configuration in the workspace with the given name. -/
118-
def findLibraryFacetConfig? (name : Name) (self : Workspace) : Option (LibraryFacetConfig name) :=
113+
@[inline] def findLibraryFacetConfig? (name : Name) (self : Workspace) : Option (LibraryFacetConfig name) :=
119114
self.libraryFacetConfigs.find? name
120115

121116
/-- The `LEAN_PATH` of the workspace. -/
@@ -133,11 +128,6 @@ This is added to the `sharedLibPathEnvVar` by `lake env`.
133128
def libPath (self : Workspace) : SearchPath :=
134129
self.packageList.map (·.libDir)
135130

136-
/-- The `LeanPaths` of the workspace. -/
137-
def leanPaths (self : Workspace) : LeanPaths where
138-
oleanPath := self.packageList.map (·.oleanDir)
139-
srcPath := self.packageList.map (·.srcDir)
140-
141131
/--
142132
Rhe detected `LEAN_PATH` of the environment
143133
augmented with the workspace's `leanPath` and Lake's `libDir`.

Diff for: Lake/Load/Main.lean

+15-34
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,20 @@ open System Lean
1919
namespace Lake
2020

2121
/-- Load the tagged `Dependency` definitions from a package configuration environment. -/
22-
def loadDeps (env : Environment) (opts : Options) : Except String (Array Dependency) := do
22+
def loadDepsFromEnv (env : Environment) (opts : Options) : Except String (Array Dependency) := do
2323
packageDepAttr.ext.getState env |>.foldM (init := #[]) fun arr name => do
2424
return arr.push <| ← evalConstCheck env opts Dependency ``Dependency name
2525

26-
/-- Add entries from `pkg`'s manifest to this one. -/
27-
def Manifest.appendPackageManifest (self : Manifest) (pkg : Package) : LogIO Manifest := do
28-
let mut result := self
29-
let pkgManifest ← Manifest.loadOrEmpty pkg.manifestFile
30-
for pkgEntry in pkgManifest do
31-
if let some entry := self.find? pkgEntry.name then
32-
let shouldUpdate :=
33-
match entry.inputRev?, pkgEntry.inputRev? with
34-
| none, none => entry.rev != pkgEntry.rev
35-
| none, some _ => false
36-
| some _, none => false
37-
| some rev, some dep => rev = dep ∧ entry.rev ≠ pkgEntry.rev
38-
let contributors := entry.contributors.insert pkg.name pkgEntry.toPersistentPackageEntry
39-
result := result.insert {entry with contributors, shouldUpdate}
40-
else
41-
let contributors := NameMap.empty.insert pkg.name pkgEntry.toPersistentPackageEntry
42-
result := result.insert {pkgEntry with contributors}
43-
return result
44-
4526
/--
4627
Resolve a single dependency and load the resulting package.
4728
Resolution is based on the `Dependency` configuration and the package manifest.
4829
-/
49-
def resolveDep (ws : Workspace) (pkg : Package) (dep : Dependency)
30+
def resolveDep (packagesDir : FilePath) (pkg : Package) (dep : Dependency)
5031
(topLevel : Bool) (shouldUpdate : Bool) : StateT Manifest LogIO Package := do
5132
let entry? := (← get).find? dep.name
5233
let entry? := entry?.map fun entry =>
5334
{entry with shouldUpdate := if topLevel then shouldUpdate else entry.shouldUpdate}
54-
let ⟨dir, url?, tag?, entry?⟩ ← materializeDep ws.packagesDir pkg.dir dep entry?
35+
let ⟨dir, url?, tag?, entry?⟩ ← materializeDep packagesDir pkg.dir dep entry?
5536
let configEnv ← elabConfigFile dir dep.options pkg.leanOpts (dir / defaultConfigFile)
5637
let config ← IO.ofExcept <| PackageConfig.loadFromEnv configEnv pkg.leanOpts
5738
let depPkg : Package := {
@@ -85,20 +66,20 @@ def resolveDep (ws : Workspace) (pkg : Package) (dep : Dependency)
8566
Resolves the package's dependencies,
8667
downloading and/or updating them as necessary.
8768
-/
88-
def resolveDeps (ws : Workspace) (pkg : Package) (shouldUpdate := true) : LogIO Package := do
89-
let manifest ← Manifest.loadOrEmpty pkg.manifestFile
90-
let (pkg, manifest) ← StateT.run (s := manifest) do
69+
def Package.resolveDeps (root : Package) (shouldUpdate := true) : LogIO Package := do
70+
let manifest ← Manifest.loadOrEmpty root.manifestFile
71+
let (root, manifest) ← StateT.run (s := manifest) do
9172
let res ← EStateT.run' (mkNameMap Package) do
92-
buildAcyclic (·.name) pkg fun pkg resolve => do
93-
let topLevel := pkg.name = ws.root.name
94-
let deps ← IO.ofExcept <| loadDeps pkg.configEnv pkg.leanOpts
73+
buildAcyclic (·.name) root fun pkg resolve => do
74+
let topLevel := pkg.name = root.name
75+
let deps ← IO.ofExcept <| loadDepsFromEnv pkg.configEnv pkg.leanOpts
9576
let depPkgs ← deps.mapM fun dep => do
9677
fetchOrCreate dep.name do
97-
liftM <| resolveDep ws pkg dep topLevel shouldUpdate
78+
liftM <| resolveDep root.packagesDir pkg dep topLevel shouldUpdate
9879
return {pkg with opaqueDeps := ← depPkgs.mapM (.mk <$> resolve ·)}
9980
match res with
100-
| Except.ok pkg =>
101-
return pkg
81+
| Except.ok root =>
82+
return root
10283
| Except.error cycle =>
10384
let cycle := cycle.map (s!" {·}")
10485
error s!"dependency cycle detected:\n{"\n".intercalate cycle}"
@@ -112,8 +93,8 @@ def resolveDeps (ws : Workspace) (pkg : Package) (shouldUpdate := true) : LogIO
11293
let inputRev := entry.inputRev?.getD Git.upstreamBranch
11394
log := log ++ s!"Using `{inputRev}` at `{entry.rev}`"
11495
logInfo log
115-
manifest.saveToFile ws.manifestFile
116-
return pkg
96+
manifest.saveToFile root.manifestFile
97+
return root
11798

11899
/--
119100
Finalize the workspace's root and its transitive dependencies
@@ -160,5 +141,5 @@ def loadWorkspace (config : LoadConfig) : LogIO Workspace := do
160141
packageFacetConfigs := initPackageFacetConfigs
161142
libraryFacetConfigs := initLibraryFacetConfigs
162143
}
163-
let root ← resolveDeps ws root config.updateDeps
144+
let root ← root.resolveDeps config.updateDeps
164145
{ws with root}.finalize

0 commit comments

Comments
 (0)