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

Commit 79321cd

Browse files
committed
refactor: cleanup logging API (unify BuildIO with LogIO)
1 parent ca87bef commit 79321cd

13 files changed

+125
-115
lines changed

Diff for: Lake/Build/Actions.lean

+17-37
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,21 @@ Authors: Gabriel Ebner, Sebastian Ullrich, Mac Malone
55
-/
66
import Lake.Build.Job
77
import Lake.Config.Env
8+
import Lake.Util.Proc
89

910
namespace Lake
1011
open System
1112

1213
def createParentDirs (path : FilePath) : IO Unit := do
1314
if let some dir := path.parent then IO.FS.createDirAll dir
1415

15-
def proc (args : IO.Process.SpawnArgs) : BuildIO Unit := do
16-
let envStr := String.join <| args.env.toList.filterMap fun (k, v) =>
17-
if k == "PATH" then none else some s!"{k}={v.getD ""} " -- PATH too big
18-
let cmdStr := " ".intercalate (args.cmd :: args.args.toList)
19-
logVerbose <| "> " ++ envStr ++
20-
match args.cwd with
21-
| some cwd => s!"{cmdStr} # in directory {cwd}"
22-
| none => cmdStr
23-
let out ← IO.Process.output args
24-
let logOutputWith (log : String → BuildIO PUnit) := do
25-
unless out.stdout.isEmpty do
26-
log s!"stdout:\n{out.stdout}"
27-
unless out.stderr.isEmpty do
28-
log s!"stderr:\n{out.stderr}"
29-
if out.exitCode = 0 then
30-
logOutputWith logInfo
31-
else
32-
logOutputWith logError
33-
logError s!"external command {args.cmd} exited with status {out.exitCode}"
34-
failure
35-
3616
def compileLeanModule (name : String) (leanFile : FilePath)
3717
(oleanFile? ileanFile? cFile? : Option FilePath)
3818
(leanPath : SearchPath := []) (rootDir : FilePath := ".")
3919
(dynlibs : Array FilePath := #[]) (dynlibPath : SearchPath := {})
4020
(leanArgs : Array String := #[]) (lean : FilePath := "lean")
41-
: BuildIO PUnit := do
42-
logAuxInfo s!"Building {name}"
21+
: LogIO PUnit := do
22+
logInfo s!"Building {name}"
4323
let mut args := leanArgs ++
4424
#[leanFile.toString, "-R", rootDir.toString]
4525
if let some oleanFile := oleanFile? then
@@ -63,44 +43,44 @@ def compileLeanModule (name : String) (leanFile : FilePath)
6343
}
6444

6545
def compileO (name : String) (oFile srcFile : FilePath)
66-
(moreArgs : Array String := #[]) (compiler : FilePath := "cc") : BuildIO Unit := do
67-
logAuxInfo s!"Compiling {name}"
46+
(moreArgs : Array String := #[]) (compiler : FilePath := "cc") : LogIO Unit := do
47+
logInfo s!"Compiling {name}"
6848
createParentDirs oFile
6949
proc {
7050
cmd := compiler.toString
7151
args := #["-c", "-o", oFile.toString, srcFile.toString] ++ moreArgs
7252
}
7353

7454
def compileStaticLib (name : String) (libFile : FilePath)
75-
(oFiles : Array FilePath) (ar : FilePath := "ar") : BuildIO Unit := do
76-
logAuxInfo s!"Linking {name}"
55+
(oFiles : Array FilePath) (ar : FilePath := "ar") : LogIO Unit := do
56+
logInfo s!"Linking {name}"
7757
createParentDirs libFile
7858
proc {
7959
cmd := ar.toString
8060
args := #["rcs", libFile.toString] ++ oFiles.map toString
8161
}
8262

8363
def compileSharedLib (name : String) (libFile : FilePath)
84-
(linkArgs : Array String) (linker : FilePath := "cc") : BuildIO Unit := do
85-
logAuxInfo s!"Linking {name}"
64+
(linkArgs : Array String) (linker : FilePath := "cc") : LogIO Unit := do
65+
logInfo s!"Linking {name}"
8666
createParentDirs libFile
8767
proc {
8868
cmd := linker.toString
8969
args := #["-shared", "-o", libFile.toString] ++ linkArgs
9070
}
9171

9272
def compileExe (name : String) (binFile : FilePath) (linkFiles : Array FilePath)
93-
(linkArgs : Array String := #[]) (linker : FilePath := "cc") : BuildIO Unit := do
94-
logAuxInfo s!"Linking {name}"
73+
(linkArgs : Array String := #[]) (linker : FilePath := "cc") : LogIO Unit := do
74+
logInfo s!"Linking {name}"
9575
createParentDirs binFile
9676
proc {
9777
cmd := linker.toString
9878
args := #["-o", binFile.toString] ++ linkFiles.map toString ++ linkArgs
9979
}
10080

10181
/-- Download a file using `curl`, clobbering any existing file. -/
102-
def download (name : String) (url : String) (file : FilePath) : BuildIO PUnit := do
103-
logAuxInfo s!"Downloading {name}"
82+
def download (name : String) (url : String) (file : FilePath) : LogIO PUnit := do
83+
logInfo s!"Downloading {name}"
10484
if (← file.pathExists) then
10585
IO.FS.removeFile file
10686
else
@@ -113,8 +93,8 @@ def download (name : String) (url : String) (file : FilePath) : BuildIO PUnit :=
11393
}
11494

11595
/-- Unpack an archive `file` using `tar` into the directory `dir`. -/
116-
def untar (name : String) (file : FilePath) (dir : FilePath) (gzip := true) : BuildIO PUnit := do
117-
logAuxInfo s!"Unpacking {name}"
96+
def untar (name : String) (file : FilePath) (dir : FilePath) (gzip := true) : LogIO PUnit := do
97+
logInfo s!"Unpacking {name}"
11898
let mut opts := "-x"
11999
if (← getIsVerbose) then
120100
opts := opts.push 'v'
@@ -127,8 +107,8 @@ def untar (name : String) (file : FilePath) (dir : FilePath) (gzip := true) : Bu
127107

128108
/-- Pack a directory `dir` using `tar` into the archive `file`. -/
129109
def tar (name : String) (dir : FilePath) (file : FilePath)
130-
(gzip := true) (excludePaths : Array FilePath := #[]) : BuildIO PUnit := do
131-
logAuxInfo s!"Packing {name}"
110+
(gzip := true) (excludePaths : Array FilePath := #[]) : LogIO PUnit := do
111+
logInfo s!"Packing {name}"
132112
createParentDirs file
133113
let mut args := #["-c"]
134114
if gzip then

Diff for: Lake/Build/Context.lean

+1-10
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ abbrev BuildT := ReaderT BuildContext
2525
/-- The monad for the Lake build manager. -/
2626
abbrev SchedulerM := BuildT <| LogT BaseIO
2727

28-
/-- The base IO monad for Lake builds. -/
29-
abbrev BuildIO := MonadLogT BaseIO OptionIO
30-
3128
/-- The core monad for Lake builds. -/
32-
abbrev BuildM := BuildT BuildIO
29+
abbrev BuildM := BuildT LogIO
3330

3431
/-- A transformer to equip a monad with a Lake build store. -/
3532
abbrev BuildStoreT := StateT BuildStore
@@ -43,15 +40,9 @@ abbrev BuildCycleT := CycleT BuildKey
4340
/-- A recursive build of a Lake build store that may encounter a cycle. -/
4441
abbrev RecBuildM := BuildCycleT <| BuildStoreT BuildM
4542

46-
instance : MonadError BuildIO := ⟨MonadLog.error⟩
47-
instance : MonadLift IO BuildIO := ⟨MonadError.runIO⟩
48-
4943
instance [Pure m] : MonadLift LakeM (BuildT m) where
5044
monadLift x := fun ctx => pure <| x.run ctx.toContext
5145

52-
instance : MonadLift LogIO BuildIO where
53-
monadLift x := fun meths => liftM (n := BuildIO) (x.run meths.lift) meths
54-
5546
def BuildM.run (logMethods : MonadLog BaseIO) (ctx : BuildContext) (self : BuildM α) : IO α :=
5647
self ctx logMethods |>.toIO fun _ => IO.userError "build failed"
5748

Diff for: Lake/Build/Trace.lean

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def getFileMTime (file : FilePath) : IO MTime :=
171171
instance : GetMTime FilePath := ⟨getFileMTime⟩
172172

173173
/-- Check if the info's `MTIme` is at least `depMTime`. -/
174-
def checkIfNewer [GetMTime i] (info : i) (depMTime : MTime) : IO Bool := do
175-
try pure ((← getMTime info) >= depMTime) catch _ => pure false
174+
def checkIfNewer [GetMTime i] (info : i) (depMTime : MTime) : BaseIO Bool :=
175+
(do pure ((← getMTime info) >= depMTime : Bool)).catchExceptions fun _ => pure false
176176

177177
--------------------------------------------------------------------------------
178178
/-! # Lake Build Trace (Hash + MTIme) -/

Diff for: Lake/CLI/Init.lean

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Gabriel Ebner, Sebastian Ullrich, Mac Malone
55
-/
66
import Lake.Util.Git
7+
import Lake.Util.Sugar
78
import Lake.Config.Package
89
import Lake.Config.Workspace
910
import Lake.Load.Config
@@ -176,7 +177,7 @@ def initPkg (dir : FilePath) (name : String) (tmp : InitTemplate) : LogIO PUnit
176177
repo.quietInit
177178
unless upstreamBranch = "master" do
178179
repo.checkoutBranch upstreamBranch
179-
catch _ =>
180+
else
180181
logWarning "failed to initialize git repository"
181182

182183
def init (pkgName : String) (tmp : InitTemplate) : LogIO PUnit :=

Diff for: Lake/CLI/Main.lean

+3-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def serve (config : LoadConfig) (args : Array String) : LogIO UInt32 := do
203203
let ws ← loadWorkspace config
204204
let ctx := mkLakeContext ws
205205
pure (← LakeT.run ctx getAugmentedEnv, ws.root.moreServerArgs)
206-
catch _ =>
206+
else
207207
logWarning "package configuration has errors, falling back to plain `lean --server`"
208208
pure (config.env.installVars.push (invalidConfigEnvVar, "1"), #[])
209209
(← IO.Process.spawn {
@@ -221,14 +221,14 @@ def exe (name : Name) (args : Array String := #[]) (verbosity : Verbosity) : La
221221
else
222222
error s!"unknown executable `{name}`"
223223

224-
def uploadRelease (pkg : Package) (tag : String) : BuildIO Unit := do
224+
def uploadRelease (pkg : Package) (tag : String) : LogIO Unit := do
225225
let mut args :=
226226
#["release", "upload", tag, pkg.buildArchiveFile.toString, "--clobber"]
227227
if let some repo := pkg.releaseRepo? then
228228
args := args.append #["-R", repo]
229229
tar pkg.buildArchive pkg.buildDir pkg.buildArchiveFile
230230
(excludePaths := #["*.tar.gz", "*.tar.gz.trace"])
231-
logAuxInfo s!"Uploading {tag}/{pkg.buildArchive}"
231+
logInfo s!"Uploading {tag}/{pkg.buildArchive}"
232232
proc {cmd := "gh", args}
233233

234234
def parseScriptSpec (ws : Workspace) (spec : String) : Except CliError (Package × String) :=

Diff for: Lake/Load/Main.lean

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def resolveDeps (ws : Workspace) (pkg : Package) (shouldUpdate := true) : LogIO
111111
log := log ++ s!"`{name}` locked `{inputRev}` at `{contrib.rev}`\n"
112112
let inputRev := entry.inputRev?.getD Git.upstreamBranch
113113
log := log ++ s!"Using `{inputRev}` at `{entry.rev}`"
114-
logVerbose log
114+
logInfo log
115115
manifest.saveToFile ws.manifestFile
116116
return pkg
117117

Diff for: Lake/Util/Error.lean

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ protected def MonadError.runEIO [Monad m]
3636
Perform an IO action.
3737
If it throws an error, invoke `error` with its string representation.
3838
-/
39-
protected def MonadError.runIO [Monad m] [MonadError m] [MonadLiftT BaseIO m] (x : IO α) : m α :=
39+
@[inline] protected def MonadError.runIO
40+
[Monad m] [MonadError m] [MonadLiftT BaseIO m] (x : IO α) : m α :=
4041
MonadError.runEIO x

Diff for: Lake/Util/Git.lean

+23-54
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2017 Microsoft Corporation. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Gabriel Ebner, Sebastian Ullrich, Mac Malone
55
-/
6-
import Lake.Util.Log
6+
import Lake.Util.Proc
77
import Lake.Util.Lift
88

99
open System
@@ -33,37 +33,6 @@ def filterUrl? (url : String) : Option String :=
3333
def isFullObjectName (rev : String) : Bool :=
3434
rev.length == 40 && rev.all fun c => c.isDigit || ('a' <= c && c <= 'f')
3535

36-
def capture (args : Array String) (wd : Option FilePath := none) : LogIO String := do
37-
let out ← IO.Process.output {cmd := "git", args, cwd := wd}
38-
if out.exitCode != 0 then
39-
let mut log := s!"\n> git {" ".intercalate args.toList}\n"
40-
unless out.stdout.isEmpty do
41-
log := log ++ s!"stdout:\n{out.stdout.trim}\n"
42-
unless out.stderr.isEmpty do
43-
log := log ++ s!"stderr:\n{out.stderr.trim}\n"
44-
logError log.trim
45-
error <| "git exited with code " ++ toString out.exitCode
46-
return out.stdout.trim -- remove, e.g., newline at end
47-
48-
def capture? (args : Array String) (wd : Option FilePath := none) : BaseIO (Option String) := do
49-
EIO.catchExceptions (h := fun _ => pure none) do
50-
let out ← IO.Process.output {cmd := "git", args, cwd := wd}
51-
if out.exitCode = 0 then
52-
return some out.stdout.trim -- remove, e.g., newline at end
53-
else
54-
return none
55-
56-
def exec (args : Array String) (wd : Option FilePath := none) : LogIO PUnit := do
57-
discard <| capture args wd
58-
59-
def test (args : Array String) (wd : Option FilePath := none) : BaseIO Bool :=
60-
EIO.catchExceptions (h := fun _ => pure false) do
61-
let child ← IO.Process.spawn {
62-
cmd := "git", args, cwd := wd,
63-
stdout := IO.Process.Stdio.null, stderr := IO.Process.Stdio.null
64-
}
65-
return (← child.wait) == 0
66-
6736
end Git
6837

6938
structure GitRepo where
@@ -75,43 +44,43 @@ namespace GitRepo
7544

7645
def cwd : GitRepo := ⟨"."
7746

78-
def dirExists (repo : GitRepo) : BaseIO Bool :=
47+
@[inline] def dirExists (repo : GitRepo) : BaseIO Bool :=
7948
repo.dir.isDir
8049

81-
def captureGit (args : Array String) (repo : GitRepo) : LogIO String :=
82-
Git.capture args repo.dir
50+
@[inline] def captureGit (args : Array String) (repo : GitRepo) : LogIO String :=
51+
captureProc {cmd := "git", args, cwd := repo.dir}
8352

84-
def captureGit? (args : Array String) (repo : GitRepo) : BaseIO (Option String) :=
85-
Git.capture? args repo.dir
53+
@[inline] def captureGit? (args : Array String) (repo : GitRepo) : BaseIO (Option String) :=
54+
captureProc? {cmd := "git", args, cwd := repo.dir}
8655

87-
def execGit (args : Array String) (repo : GitRepo) : LogIO PUnit :=
88-
Git.exec args repo.dir
56+
@[inline] def execGit (args : Array String) (repo : GitRepo) : LogIO PUnit :=
57+
proc {cmd := "git", args, cwd := repo.dir}
8958

90-
def testGit (args : Array String) (repo : GitRepo) : BaseIO Bool :=
91-
Git.test args repo.dir
59+
@[inline] def testGit (args : Array String) (repo : GitRepo) : BaseIO Bool :=
60+
testProc {cmd := "git", args, cwd := repo.dir}
9261

93-
def clone (url : String) (repo : GitRepo) : LogIO PUnit :=
94-
Git.exec #["clone", url, repo.dir.toString]
62+
@[inline] def clone (url : String) (repo : GitRepo) : LogIO PUnit :=
63+
proc {cmd := "git", args := #["clone", url, repo.dir.toString]}
9564

96-
def quietInit (repo : GitRepo) : LogIO PUnit :=
65+
@[inline] def quietInit (repo : GitRepo) : LogIO PUnit :=
9766
repo.execGit #["init", "-q"]
9867

99-
def fetch (repo : GitRepo) (remote := Git.defaultRemote) : LogIO PUnit :=
68+
@[inline] def fetch (repo : GitRepo) (remote := Git.defaultRemote) : LogIO PUnit :=
10069
repo.execGit #["fetch", remote]
10170

102-
def checkoutBranch (branch : String) (repo : GitRepo) : LogIO PUnit :=
71+
@[inline] def checkoutBranch (branch : String) (repo : GitRepo) : LogIO PUnit :=
10372
repo.execGit #["checkout", "-B", branch]
10473

105-
def checkoutDetach (hash : String) (repo : GitRepo) : LogIO PUnit :=
74+
@[inline] def checkoutDetach (hash : String) (repo : GitRepo) : LogIO PUnit :=
10675
repo.execGit #["checkout", "--detach", hash]
10776

108-
def resolveRevision? (rev : String) (repo : GitRepo) : BaseIO (Option String) := do
77+
@[inline] def resolveRevision? (rev : String) (repo : GitRepo) : BaseIO (Option String) := do
10978
repo.captureGit? #["rev-parse", "--verify", rev]
11079

111-
def resolveRevision (rev : String) (repo : GitRepo) : LogIO String := do
80+
@[inline] def resolveRevision (rev : String) (repo : GitRepo) : LogIO String := do
11281
repo.captureGit #["rev-parse", "--verify", rev]
11382

114-
def headRevision (repo : GitRepo) : LogIO String :=
83+
@[inline] def headRevision (repo : GitRepo) : LogIO String :=
11584
repo.resolveRevision "HEAD"
11685

11786
def resolveRemoteRevision (rev : String) (remote := Git.defaultRemote) (repo : GitRepo) : LogIO String := do
@@ -123,16 +92,16 @@ def resolveRemoteRevision (rev : String) (remote := Git.defaultRemote) (repo : G
12392
def findRemoteRevision (repo : GitRepo) (rev? : Option String := none) (remote := Git.defaultRemote) : LogIO String := do
12493
repo.fetch remote; repo.resolveRemoteRevision (rev?.getD Git.upstreamBranch) remote
12594

126-
def branchExists (rev : String) (repo : GitRepo) : BaseIO Bool := do
95+
@[inline] def branchExists (rev : String) (repo : GitRepo) : BaseIO Bool := do
12796
repo.testGit #["show-ref", "--verify", s!"refs/heads/{rev}"]
12897

129-
def revisionExists (rev : String) (repo : GitRepo) : BaseIO Bool := do
98+
@[inline] def revisionExists (rev : String) (repo : GitRepo) : BaseIO Bool := do
13099
repo.testGit #["rev-parse", "--verify", rev ++ "^{commit}"]
131100

132-
def findTag? (rev : String := "HEAD") (repo : GitRepo) : BaseIO (Option String) := do
101+
@[inline] def findTag? (rev : String := "HEAD") (repo : GitRepo) : BaseIO (Option String) := do
133102
repo.captureGit? #["describe", "--tags", "--exact-match", rev]
134103

135-
def getRemoteUrl? (remote := Git.defaultRemote) (repo : GitRepo) : BaseIO (Option String) := do
104+
@[inline] def getRemoteUrl? (remote := Git.defaultRemote) (repo : GitRepo) : BaseIO (Option String) := do
136105
repo.captureGit? #["remote", "get-url", remote]
137106

138107
def getFilteredRemoteUrl? (remote := Git.defaultRemote) (repo : GitRepo) : BaseIO (Option String) := OptionT.run do

Diff for: Lake/Util/Lift.lean

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Copyright (c) 2022 Mac Malone. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Mac Malone
55
-/
6+
import Lake.Util.OptionIO
67

78
namespace Lake
89

@@ -33,3 +34,6 @@ instance [Monad m] [MonadExceptOf ε m] [MonadLiftT n m] : MonadLiftT (ExceptT
3334

3435
instance [Monad m] [MonadExceptOf ε m] [MonadLiftT BaseIO m] : MonadLiftT (EIO ε) m where
3536
monadLift act := act.toBaseIO >>= liftM
37+
38+
instance [Monad m] [Alternative m] [MonadLiftT BaseIO m] : MonadLiftT OptionIO m where
39+
monadLift act := act.toBaseIO >>= liftM

0 commit comments

Comments
 (0)