Skip to content

Commit bc93276

Browse files
authored
Merge pull request #332 from LoopPerfect/feat/baobab-solver
Feat/baobab solver
2 parents 432ac62 + 193a27b commit bc93276

29 files changed

+1255
-927
lines changed

buckaroo-cli/Program.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ let main argv =
1515
let! exitCode = async {
1616
try
1717
match Buckaroo.Command.parse input with
18-
| Result.Ok (command, loggingLevel) ->
18+
| Result.Ok (command, loggingLevel, fetchStyle) ->
1919
do!
2020
command
21-
|> Buckaroo.Command.runCommand loggingLevel
21+
|> Buckaroo.Command.runCommand loggingLevel fetchStyle
2222
return 0
2323
| Result.Error error ->
2424
Console.WriteLine error

buckaroo-tests/Command.fs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,37 @@ let private ijkXyz = GitHub { Owner = "ijk"; Project = "xyz" }
1515
[<Fact>]
1616
let ``Command.parse works correctly`` () =
1717
let cases = [
18-
(Result.Ok (Command.Init, defaultLoggingLevel), "init");
18+
(Result.Ok (Command.Init, defaultLoggingLevel, RemoteFirst), "init");
1919

20-
(Result.Ok (Command.Install, defaultLoggingLevel), " install ");
20+
(Result.Ok (Command.Install, defaultLoggingLevel, RemoteFirst), " install ");
2121

22-
(Result.Ok (Command.Resolve Quick, defaultLoggingLevel), "resolve");
23-
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel), "resolve --verbose");
24-
(Result.Ok (Command.Resolve Upgrading, defaultLoggingLevel), "resolve --upgrade ");
25-
(Result.Ok (Command.Resolve Upgrading, verboseLoggingLevel), "resolve --upgrade --verbose");
22+
(Result.Ok (Command.Resolve Quick, defaultLoggingLevel, RemoteFirst), "resolve");
23+
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel, RemoteFirst), "resolve --verbose");
24+
(Result.Ok (Command.Resolve Upgrading, defaultLoggingLevel, RemoteFirst), "resolve --upgrade ");
25+
(Result.Ok (Command.Resolve Upgrading, verboseLoggingLevel, RemoteFirst), "resolve --upgrade --verbose");
26+
(Result.Ok (Command.Resolve Quick, defaultLoggingLevel, CacheFirst), "resolve --cache-first ");
27+
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel, CacheFirst), "resolve --cache-first --verbose");
2628

27-
(Result.Ok (Command.UpgradeDependencies [], defaultLoggingLevel), "upgrade");
28-
(Result.Ok (Command.UpgradeDependencies [ abcDef ], defaultLoggingLevel), "upgrade abc/def");
29-
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel), " upgrade --verbose ");
30-
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel), "upgrade abc/def --verbose ");
29+
(Result.Ok (Command.UpgradeDependencies [], defaultLoggingLevel, RemoteFirst), "upgrade");
30+
(Result.Ok (Command.UpgradeDependencies [ abcDef ], defaultLoggingLevel, RemoteFirst), "upgrade abc/def");
31+
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel, RemoteFirst), " upgrade --verbose ");
32+
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel, RemoteFirst), "upgrade abc/def --verbose ");
33+
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel, CacheFirst), " upgrade --cache-first --verbose ");
34+
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel, CacheFirst), "upgrade abc/def --cache-first --verbose ");
3135

3236
(
3337
Result.Ok
3438
(
3539
Command.AddDependencies
3640
[ { Package = ijkXyz; Constraint = Constraint.wildcard; Targets = None } ],
37-
defaultLoggingLevel
41+
defaultLoggingLevel,
42+
RemoteFirst
3843
),
3944
"add github.com/ijk/xyz "
4045
);
4146

4247
(
43-
Result.Ok (Command.UpgradeDependencies [ abcDef; ijkXyz ], verboseLoggingLevel),
48+
Result.Ok (Command.UpgradeDependencies [ abcDef; ijkXyz ], verboseLoggingLevel, RemoteFirst),
4449
"upgrade abc/def github.com/ijk/xyz --verbose "
4550
);
4651
]

buckaroo-tests/Constraint.fs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ let ``Constraint.parse works correctly`` () =
1616
("*", Constraint.wildcard |> Some);
1717
("revision=aabbccddee", Version.Git(GitVersion.Revision "aabbccddee") |> Exactly |> Some);
1818
("!*", Constraint.wildcard |> Constraint.Complement |> Some);
19-
("any(branch=master)", Some(Any [Exactly (Version.Git(GitVersion.Branch "master"))]));
20-
("any(any(branch=master))", Some(Any [ Any [Exactly (Version.Git(GitVersion.Branch "master"))]]));
21-
("any(revision=aabbccddee branch=master)", Some (Any [
19+
("any(branch=master)", Some(Any <| Set [ Exactly (Version.Git(GitVersion.Branch "master"))]));
20+
("any(any(branch=master))", Some(Any <| Set[ Any <| Set [ Exactly (Version.Git(GitVersion.Branch "master"))]]));
21+
("any(revision=aabbccddee branch=master)", Some (Any <| Set[
2222
Exactly (Version.Git(GitVersion.Revision "aabbccddee"));
2323
Exactly (Version.Git(GitVersion.Branch "master"))]));
24-
("all(*)", Some(All [Constraint.wildcard]));
24+
("all(*)", Some(All <| Set[Constraint.wildcard]));
2525
(
2626
"all(branch=master !revision=aabbccddee)",
27-
Some (All [Exactly (Version.Git(GitVersion.Branch "master")); Complement (Exactly (Version.Git(GitVersion.Revision "aabbccddee")))])
27+
Some (All <| Set[Exactly (Version.Git(GitVersion.Branch "master")); Complement (Exactly (Version.Git(GitVersion.Revision "aabbccddee")))])
2828
);
2929
(
3030
"all(branch=master !any(revision=aabbccddee branch=develop))",
31-
Some (All [
31+
Some (All <| Set[
3232
Exactly (Version.Git(GitVersion.Branch "master"));
33-
Complement (Any([
33+
Complement (Any(Set[
3434
Exactly (Version.Git(GitVersion.Revision "aabbccddee"));
3535
Exactly (Version.Git(GitVersion.Branch "develop"));
3636
]))
@@ -52,12 +52,12 @@ let ``Constraint.parse works correctly`` () =
5252
"+1.0.0",
5353
Some (Constraint.rangeToConstraint RangeType.Patch (SemVer.create (1, 0, 0, 0)))
5454
);
55-
("all(branch=master ^1.0.0)", Some (All [
55+
("all(branch=master ^1.0.0)", Some (All <| Set[
5656
Exactly (Git (GitVersion.Branch "master"));
5757
Constraint.rangeToConstraint RangeType.Major (SemVer.create (1, 0, 0, 0))
5858
]
5959
));
60-
("all(^1.0.0 branch=master)", Some (All [
60+
("all(^1.0.0 branch=master)", Some (All <| Set[
6161
Constraint.rangeToConstraint RangeType.Major (SemVer.create (1, 0, 0, 0));
6262
Exactly (Git (GitVersion.Branch "master"))
6363
]))
@@ -100,6 +100,7 @@ let ``Constraint.simplify works correctly`` () =
100100
("any(all(revision=aabbccddee))", "revision=aabbccddee");
101101
("all(any(revision=aabbccddee))", "revision=aabbccddee");
102102
("any(branch=master any(revision=aabbccddee))", "any(revision=aabbccddee branch=master)");
103+
("any(all() revision=aabbccddee)", "any(revision=aabbccddee all())");
103104
]
104105
for (input, expected) in cases do
105106
let actual =

buckaroo-tests/Manifest.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let ``Manifest.toToml roundtrip 1`` () =
8787
]
8888
Dependencies = Set [{
8989
Targets = Some ([{Folders=["foo"; "bar"]; Name = "xxx"}])
90-
Constraint = All [Constraint.Exactly (Version.SemVer SemVer.zero)]
90+
Constraint = All <| Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
9191
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
9292
}]
9393
}
@@ -123,12 +123,12 @@ let ``Manifest.toToml roundtrip 2`` () =
123123
]
124124
Dependencies = Set [{
125125
Targets = Some ([{Folders=["foo"; "bar"]; Name = "xxx"}])
126-
Constraint = All [Constraint.Exactly (Version.SemVer SemVer.zero)]
126+
Constraint = All <| Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
127127
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
128128
}]
129129
PrivateDependencies = Set [{
130130
Targets = Some ([{Folders=["foo"; "bar"]; Name = "yyy"}])
131-
Constraint = Any [Constraint.Exactly (Version.SemVer SemVer.zero)]
131+
Constraint = Any <|Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
132132
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
133133
}]
134134
}

buckaroo-tests/Solver.fs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ open Buckaroo.Tests
1111
type CookBook = List<PackageIdentifier * Set<Version> * Manifest>
1212
type LockBookEntries = List<(string*int) * List<string*int*Set<Version>>>
1313
type LockBook = Map<PackageLock, Lock>
14+
1415
let package name = PackageIdentifier.Adhoc {
15-
Owner = "test";
16+
Owner = "test"
1617
Project = name
1718
}
1819

@@ -89,7 +90,7 @@ type TestingSourceExplorer (cookBook : CookBook, lockBook : LockBook) =
8990
Revision = r
9091
})
9192
|> AsyncSeq.ofSeq
92-
| _ -> raise <| new System.SystemException "package not found"
93+
| _ -> raise <| System.SystemException "Package not found"
9394
}
9495

9596
member this.LockLocation (location : PackageLocation) : Async<PackageLock> = async {
@@ -120,11 +121,11 @@ type TestingSourceExplorer (cookBook : CookBook, lockBook : LockBook) =
120121

121122
let solve (partial : Solution) (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
122123
let lockBook = lockBookOf lockBookEntries
123-
let console = new ConsoleManager(LoggingLevel.Silent);
124+
let console = ConsoleManager (LoggingLevel.Silent)
124125
let context : TaskContext = {
125-
Console = console;
126-
DownloadManager = DownloadManager(console, "/tmp");
127-
GitManager = new GitManager(console, new GitCli(console), "/tmp");
126+
Console = console
127+
DownloadManager = DownloadManager(console, "/tmp")
128+
GitManager = new GitManager(CacheFirst, console, new GitCli(console), "/tmp")
128129
SourceExplorer = TestingSourceExplorer(cookBook, lockBook)
129130
}
130131

@@ -133,7 +134,7 @@ let solve (partial : Solution) (cookBook : CookBook) (lockBookEntries : LockBook
133134
root style
134135
(lockBook |> Map.tryFind (packageLock ("root", 0)))
135136

136-
let getLockedRev (p : string) (r: Resolution) =
137+
let getLockedRev (p : string) (r : _) =
137138
match r with
138139
| Ok solution ->
139140
let (resolved, _) = solution.Resolutions.[package p]
@@ -143,7 +144,7 @@ let getLockedRev (p : string) (r: Resolution) =
143144
| _ -> ""
144145
()
145146

146-
let isOk (r: Resolution) =
147+
let isOk (r : _) =
147148
match r with
148149
| Ok _ -> true
149150
| _ -> false
@@ -228,7 +229,7 @@ let ``Solver can compute version intersections`` () =
228229
let ``Solver can compute intersection of branches`` () =
229230

230231
let root = manifest [
231-
("a", All [Exactly (br "b"); Exactly (br "a")])
232+
("a", All <| Set[Exactly (br "b"); Exactly (br "a")])
232233
]
233234

234235
let spec = [
@@ -346,7 +347,7 @@ let ``Solver handles negated constraints also`` () =
346347

347348
let root = manifest [
348349
("a", Exactly (br "a"))
349-
("b", Any [Exactly (br "a"); Exactly (br "b")])
350+
("b", Any <|Set[Exactly (br "a"); Exactly (br "b")])
350351
]
351352

352353
let spec = [
@@ -357,7 +358,7 @@ let ``Solver handles negated constraints also`` () =
357358
Set[ver 2; br "a"],
358359
manifest [])
359360
(package "b",
360-
Set[ver 2; br "b"],
361+
Set[ver 2; br "a"],
361362
manifest [])
362363
(package "a",
363364
Set[ver 3; br "a"],

buckaroo/AddCommand.fs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Buckaroo.AddCommand
22

3-
open System
43
open System.IO
54
open Buckaroo.RichOutput
6-
open Buckaroo
5+
open Buckaroo.Logger
76

87
let task (context : Tasks.TaskContext) dependencies = async {
9-
context.Console.Write (
10-
(text "Adding ") +
8+
let logger = createLogger context.Console None
9+
10+
logger.RichInfo (
11+
(text "Adding dependency on ") +
1112
(
1213
dependencies
1314
|> Seq.map Dependency.showRich
@@ -16,6 +17,7 @@ let task (context : Tasks.TaskContext) dependencies = async {
1617
)
1718

1819
let! manifest = Tasks.readManifest "."
20+
1921
let newManifest = {
2022
manifest with
2123
Dependencies =
@@ -26,6 +28,7 @@ let task (context : Tasks.TaskContext) dependencies = async {
2628

2729
if manifest = newManifest
2830
then
31+
logger.Warning ("The dependency already exists in the manifest")
2932
return ()
3033
else
3134
let! maybeLock = async {
@@ -37,15 +40,16 @@ let task (context : Tasks.TaskContext) dependencies = async {
3740
return None
3841
}
3942

40-
let! resolution = Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock
43+
let! resolution =
44+
Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock
4145

4246
match resolution with
43-
| Resolution.Ok solution ->
47+
| Result.Ok solution ->
4448
do! Tasks.writeManifest newManifest
4549
do! Tasks.writeLock (Lock.fromManifestAndSolution newManifest solution)
4650
do! InstallCommand.task context
47-
| _ ->
48-
()
4951

50-
context.Console.Write ("Success. " |> text |> foreground ConsoleColor.Green)
52+
logger.Success ("The dependency was added to the manifest and installed")
53+
| _ ->
54+
logger.Error ("Failed to add the dependency")
5155
}

buckaroo/Command.fs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ module Command =
3131
return Option.isSome maybeSkip
3232
}
3333

34+
let cacheFirstParser : Parser<bool, Unit> = parse {
35+
let! cacheFirst =
36+
CharParsers.skipString "--cache-first"
37+
|> Primitives.opt
38+
return Option.isSome cacheFirst
39+
}
40+
3441
let startParser : Parser<Command, Unit> = parse {
3542
do! CharParsers.spaces
3643
return Start
@@ -170,13 +177,27 @@ module Command =
170177

171178
do! CharParsers.spaces
172179

173-
let! isVerbose = verboseParser
174-
180+
let! isCacheFirst = cacheFirstParser
175181
do! CharParsers.spaces
176182

177-
let loggingLevel = if isVerbose then LoggingLevel.Trace else LoggingLevel.Info
183+
let! isVerbose = verboseParser
184+
do! CharParsers.spaces
178185

179-
return (command, loggingLevel)
186+
let loggingLevel =
187+
if isVerbose
188+
then
189+
LoggingLevel.Trace
190+
else
191+
LoggingLevel.Info
192+
193+
let fetchStyle =
194+
if isCacheFirst
195+
then
196+
CacheFirst
197+
else
198+
RemoteFirst
199+
200+
return (command, loggingLevel, fetchStyle)
180201
}
181202

182203
let parse (x : string) =
@@ -210,15 +231,13 @@ module Command =
210231
let! resolution = Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock
211232

212233
match resolution with
213-
| Resolution.Ok solution ->
234+
| Result.Ok solution ->
214235
do! Tasks.writeManifest newManifest
215236
do! Tasks.writeLock (Lock.fromManifestAndSolution newManifest solution)
216237
do! InstallCommand.task context
217-
| _ -> ()
218-
219-
System.Console.WriteLine ("Success. ")
220238

221-
return ()
239+
System.Console.WriteLine ("Success. ")
240+
| _ -> ()
222241
}
223242

224243
let init context = async {
@@ -232,16 +251,16 @@ module Command =
232251
context.Console.Write( ("warning " |> warn) + ("There is already a buckaroo.toml file in this directory" |> text))
233252
}
234253

235-
let runCommand loggingLevel command = async {
236-
let! context = Tasks.getContext loggingLevel
254+
let runCommand loggingLevel fetchStyle command = async {
255+
let! context = Tasks.getContext loggingLevel fetchStyle
237256

238257
do!
239258
match command with
240259
| Start -> StartCommand.task context
241260
| Init -> init context
242261
| Help -> HelpCommand.task context
243262
| Version -> VersionCommand.task context
244-
| Resolve style -> ResolveCommand.task context Solution.empty style
263+
| Resolve style -> ResolveCommand.task context Solution.empty style |> Async.Ignore
245264
| Install -> InstallCommand.task context
246265
| Quickstart -> QuickstartCommand.task context
247266
| UpgradeDependencies dependencies -> UpgradeCommand.task context dependencies

buckaroo/ConsoleManager.fs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,11 @@ type ConsoleManager (minimumLoggingLevel : LoggingLevel) =
113113
member this.Error (message, loggingLevel) =
114114
actor.Post (Output (message, loggingLevel, OutputCategory.Error))
115115

116-
member this.Read() =
117-
actor.PostAndAsyncReply(fun channel -> Input channel)
118-
119-
member this.ReadSecret() =
120-
actor.PostAndAsyncReply(fun channel -> InputSecret channel)
121-
122-
member this.Flush() =
123-
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)
116+
member this.Read () =
117+
actor.PostAndAsyncReply Input
118+
119+
member this.ReadSecret () =
120+
actor.PostAndAsyncReply InputSecret
121+
122+
member this.Flush () =
123+
actor.PostAndAsyncReply Flush

0 commit comments

Comments
 (0)