Skip to content

Commit 2aac256

Browse files
authored
Merge pull request #248 from LoopPerfect/feat/better-output
Feat/better output
2 parents 672f620 + 4a5f1a8 commit 2aac256

File tree

10 files changed

+188
-128
lines changed

10 files changed

+188
-128
lines changed

buckaroo-tests/Solver.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ type TestingSourceExplorer (cookBook : CookBook, lockBook : LockBook) =
121121

122122
let solve (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
123123
let lockBook = lockBookOf lockBookEntries
124-
let console = new ConsoleManager(LoggingLevel.Debug);
124+
let console = new ConsoleManager(LoggingLevel.Silent);
125125
let context : TaskContext = {
126126
Console = console;
127127
DownloadManager = DownloadManager(console, "/tmp");
128-
GitManager = new GitManager(new GitCli(console), "/tmp");
128+
GitManager = new GitManager(console, new GitCli(console), "/tmp");
129129
SourceExplorer = TestingSourceExplorer(cookBook, lockBook)
130130
}
131131

buckaroo/ConsoleManager.fs

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,11 @@ module Buckaroo.Console
33
open System
44
open Buckaroo.RichOutput
55

6-
[<CustomComparison>]
7-
[<CustomEquality>]
86
type LoggingLevel =
9-
| Trace
10-
| Debug
11-
| Info
12-
13-
override this.Equals (obj) =
14-
match obj with
15-
| :? LoggingLevel as other ->
16-
match (this, other) with
17-
| (Trace, Trace) -> true
18-
| (Debug, Debug) -> true
19-
| (Info, Info) -> true
20-
| _ -> false
21-
| _ -> false
22-
23-
override this.GetHashCode () =
24-
match this with
25-
| Trace -> 0
26-
| Debug -> 1
27-
| Info -> 2
28-
29-
interface IComparable with
30-
override this.CompareTo obj =
31-
let score x =
32-
match x with
33-
| Trace -> 0
34-
| Debug -> 1
35-
| Info -> 2
36-
37-
match obj with
38-
| :? LoggingLevel as other -> (score this) - (score other)
39-
| _ -> 0
40-
7+
| Trace
8+
| Debug
9+
| Info
10+
| Silent
4111

4212
type OutputCategory =
4313
| Normal
@@ -97,7 +67,6 @@ type ConsoleManager (minimumLoggingLevel : LoggingLevel) =
9767
let! message = inbox.Receive()
9868
match message with
9969
| Output (m, l, c) ->
100-
// TODO: Respect minimum logging level
10170
if l >= minimumLoggingLevel
10271
then
10372
match c with
@@ -106,11 +75,12 @@ type ConsoleManager (minimumLoggingLevel : LoggingLevel) =
10675
| Error -> Console.Error.WriteLine(m)
10776
()
10877
| RichOutput (m, l, c) ->
109-
// TODO: Respect minimum logging level
110-
match c with
111-
| Normal -> renderRichOutput m
112-
| Warning -> renderRichOutput m
113-
| Error -> renderRichOutput m
78+
if l >= minimumLoggingLevel
79+
then
80+
match c with
81+
| Normal -> renderRichOutput m
82+
| Warning -> renderRichOutput m
83+
| Error -> renderRichOutput m
11484
()
11585
| Input channel ->
11686
let response = Console.ReadLine()
@@ -151,3 +121,11 @@ type ConsoleManager (minimumLoggingLevel : LoggingLevel) =
151121

152122
member this.Flush() =
153123
actor.PostAndAsyncReply(fun channel -> Flush channel)
124+
125+
let namespacedLogger (console : ConsoleManager) (componentName : string) (x : RichOutput, logLevel : LoggingLevel) =
126+
(
127+
"[" + componentName + "] "
128+
|> RichOutput.text
129+
|> RichOutput.foreground System.ConsoleColor.DarkGray
130+
) +
131+
x |> fun x -> console.Write (x, logLevel)

buckaroo/DefaultSourceExplorer.fs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ namespace Buckaroo
33
open FSharp.Control
44
open Buckaroo.Console
55
open FSharpx
6+
open RichOutput
67

78
type DefaultSourceExplorer (console : ConsoleManager, downloadManager : DownloadManager, gitManager : GitManager) =
9+
let log = namespacedLogger console "explorer"
810
let toOptional = Async.Catch >> (Async.map Choice.toOption)
911

10-
let fromCache url revision path =
12+
let fromFileCache url revision path =
1113
gitManager.FetchFile url revision path |> toOptional
1214

13-
let cacheOrGit (f: string->string->Async<string>, url:string, rev:string, path: string) = async {
14-
let! cached = fromCache url rev path
15+
let cacheOrGit (f: string->string->Async<string>, url : string, rev : string, path : string) = async {
16+
let! cached = fromFileCache url rev path
1517
match cached with
1618
| Some data -> return data
1719
| None -> return! f rev path
@@ -143,7 +145,6 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download
143145

144146
let fetchVersionsFromGit gitUrl = asyncSeq {
145147
let! refs = gitManager.FetchRefs gitUrl
146-
147148
// Sem-vers
148149
yield!
149150
refs
@@ -306,11 +307,20 @@ type DefaultSourceExplorer (console : ConsoleManager, downloadManager : Download
306307

307308
member this.FetchLock location =
308309
async {
309-
let! content = fetchFile location Constants.LockFileName
310+
let! maybeContent = fetchFile location Constants.LockFileName |> Async.Catch |> Async.map(Choice.toOption)
310311
return
311-
match Lock.parse content with
312-
| Result.Ok manifest -> manifest
313-
| Result.Error errorMessage ->
314-
new System.Exception("Invalid " + Constants.LockFileName + " file. \n" + errorMessage)
315-
|> raise
312+
match maybeContent with
313+
| None ->
314+
log(
315+
(warn "warning ") + (text "Could not fetch ") + (highlight Constants.LockFileName) + (text " from ") +
316+
(PackageLock.show location |> highlight) + (warn " 404"), LoggingLevel.Info)
317+
raise <| new System.Exception("Could not fetch " + Constants.LockFileName + " file")
318+
| Some content ->
319+
match Lock.parse content with
320+
| Result.Ok manifest -> manifest
321+
| Result.Error errorMessage ->
322+
log(
323+
(warn "warning ") + (text "Could not parse ") + (highlight Constants.LockFileName) + (text " from ") +
324+
(PackageLock.show location |> highlight) + (text " ") + (warn errorMessage), LoggingLevel.Info)
325+
new System.Exception("Invalid " + Constants.LockFileName + " file") |> raise
316326
}

buckaroo/GitCli.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ namespace Buckaroo
33
open System
44
open System.Text
55
open Buckaroo.Console
6+
open RichOutput
67

78
type GitCli (console : ConsoleManager) =
9+
10+
let log = namespacedLogger console "git"
11+
812
let nl = System.Environment.NewLine
913
let runBash command = async {
1014
let rt =
@@ -105,6 +109,7 @@ type GitCli (console : ConsoleManager) =
105109
}
106110

107111
member this.ShallowClone (url : String) (directory : string) = async {
112+
log((text "shallow cloning ") + (highlight url), LoggingLevel.Info)
108113
do!
109114
runBash ("git clone --bare --depth=1 " + url + " " + directory)
110115
|> Async.Ignore

buckaroo/GitManager.fs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ open System.Text.RegularExpressions
77
open FSharpx.Control
88
open FSharp.Control
99
open FSharpx
10+
open Console
11+
open RichOutput
1012

1113
type CloneRequest =
1214
| CloneRequest of string * AsyncReplyChannel<Async<string>>
1315

14-
type GitManager (git : IGit, cacheDirectory : string) =
16+
type GitManager (console : ConsoleManager, git : IGit, cacheDirectory : string) =
17+
18+
let log = namespacedLogger console "git"
1519

1620
let mutable refsCache = Map.empty
1721

@@ -37,7 +41,7 @@ type GitManager (git : IGit, cacheDirectory : string) =
3741
let folder = sanitizeFilename(url).ToLower() + "-" + hash.Substring(0, 16)
3842
Path.Combine(cacheDirectory, folder)
3943

40-
let mailboxProcessor = MailboxProcessor.Start(fun inbox -> async {
44+
let mailboxCloneProcessor = MailboxProcessor.Start(fun inbox -> async {
4145
let mutable cloneCache : Map<string, Async<string>> = Map.empty
4246
while true do
4347
let! message = inbox.Receive()
@@ -63,11 +67,11 @@ type GitManager (git : IGit, cacheDirectory : string) =
6367
return! this.DefaultBranch targetDirectory
6468
}
6569
member this.Clone (url : string) : Async<string> = async {
66-
let! res = mailboxProcessor.PostAndAsyncReply(fun ch -> CloneRequest(url, ch))
70+
let! res = mailboxCloneProcessor.PostAndAsyncReply(fun ch -> CloneRequest(url, ch))
6771
return! res
6872
}
6973

70-
member this.CopyFromCache (gitUrl : string) (revision : Revision) (installPath : string) : Async<Unit> = async {
74+
member this.CopyFromCache (gitUrl : string) (revision : Revision) (installPath : string) : Async<Unit> = async {
7175
let! hasGit = Files.directoryExists (Path.Combine (installPath, ".git/"))
7276
if hasGit then
7377
do! git.Unshallow installPath
@@ -116,7 +120,9 @@ type GitManager (git : IGit, cacheDirectory : string) =
116120
match refsCache |> Map.tryFind url with
117121
| Some refs -> return refs
118122
| None ->
123+
log( (text "fetching refs from ") + (highlight url), LoggingLevel.Info)
119124
let cacheDir = cloneFolderName url
125+
let startTime = System.DateTime.Now
120126
let! refs =
121127
Async.Parallel
122128
(
@@ -135,6 +141,12 @@ type GitManager (git : IGit, cacheDirectory : string) =
135141
else b
136142
)
137143
refsCache <- refsCache |> Map.add url refs
144+
let endTime = System.DateTime.Now
145+
log((success "success ") +
146+
(text "fetched ") +
147+
((refs|>List.length).ToString() |> info) +
148+
(text " refs in ") +
149+
((endTime-startTime).TotalSeconds.ToString("N3")|>info), LoggingLevel.Info)
138150
return refs
139151
}
140152
member this.FetchFile (url : string) (revision : Revision) (file : string) : Async<string> =

buckaroo/PackageIdentifier.fs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ module PackageIdentifier =
2020
| GitLab x -> "gitlab.com/" + x.Owner + "/" + x.Project
2121
| Adhoc x -> x.Owner + "/" + x.Project
2222

23-
let showRich (id : PackageIdentifier) =
24-
let host = text >> (foreground System.ConsoleColor.Magenta)
25-
let owner = text >> (foreground System.ConsoleColor.Cyan)
26-
let project = text >> (foreground System.ConsoleColor.Green)
27-
let subtle = text >> (foreground System.ConsoleColor.DarkGray)
23+
let showRich (id : PackageIdentifier) =
24+
let host = highlight
25+
let owner = highlight
26+
let project = highlight
2827

2928
match id with
3029
| GitHub x -> (host "github.com") + (subtle "/") + (owner x.Owner) + (subtle "/") + (project x.Project)
3130
| BitBucket x -> (host "bitbucket.org") + (subtle "/") + x.Owner + (subtle "/") + (project x.Project)
3231
| GitLab x -> (host "gitlab.com") + (subtle "/") + x.Owner + (subtle "/") + (project x.Project)
3332
| Adhoc x -> (owner x.Owner) + (subtle "/") + (project x.Project)
34-
33+
3534
let private gitHubIdentifierParser =
3635
CharParsers.regex @"[a-zA-Z.\d](?:[a-zA-Z_.\d]|-(?=[a-zA-Z_.\d])){0,38}"
3736

0 commit comments

Comments
 (0)