Skip to content

Commit

Permalink
Merge pull request #332 from LoopPerfect/feat/baobab-solver
Browse files Browse the repository at this point in the history
Feat/baobab solver
  • Loading branch information
njlr authored Mar 13, 2019
2 parents 432ac62 + 193a27b commit bc93276
Show file tree
Hide file tree
Showing 29 changed files with 1,255 additions and 927 deletions.
4 changes: 2 additions & 2 deletions buckaroo-cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ let main argv =
let! exitCode = async {
try
match Buckaroo.Command.parse input with
| Result.Ok (command, loggingLevel) ->
| Result.Ok (command, loggingLevel, fetchStyle) ->
do!
command
|> Buckaroo.Command.runCommand loggingLevel
|> Buckaroo.Command.runCommand loggingLevel fetchStyle
return 0
| Result.Error error ->
Console.WriteLine error
Expand Down
29 changes: 17 additions & 12 deletions buckaroo-tests/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,37 @@ let private ijkXyz = GitHub { Owner = "ijk"; Project = "xyz" }
[<Fact>]
let ``Command.parse works correctly`` () =
let cases = [
(Result.Ok (Command.Init, defaultLoggingLevel), "init");
(Result.Ok (Command.Init, defaultLoggingLevel, RemoteFirst), "init");

(Result.Ok (Command.Install, defaultLoggingLevel), " install ");
(Result.Ok (Command.Install, defaultLoggingLevel, RemoteFirst), " install ");

(Result.Ok (Command.Resolve Quick, defaultLoggingLevel), "resolve");
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel), "resolve --verbose");
(Result.Ok (Command.Resolve Upgrading, defaultLoggingLevel), "resolve --upgrade ");
(Result.Ok (Command.Resolve Upgrading, verboseLoggingLevel), "resolve --upgrade --verbose");
(Result.Ok (Command.Resolve Quick, defaultLoggingLevel, RemoteFirst), "resolve");
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel, RemoteFirst), "resolve --verbose");
(Result.Ok (Command.Resolve Upgrading, defaultLoggingLevel, RemoteFirst), "resolve --upgrade ");
(Result.Ok (Command.Resolve Upgrading, verboseLoggingLevel, RemoteFirst), "resolve --upgrade --verbose");
(Result.Ok (Command.Resolve Quick, defaultLoggingLevel, CacheFirst), "resolve --cache-first ");
(Result.Ok (Command.Resolve Quick, verboseLoggingLevel, CacheFirst), "resolve --cache-first --verbose");

(Result.Ok (Command.UpgradeDependencies [], defaultLoggingLevel), "upgrade");
(Result.Ok (Command.UpgradeDependencies [ abcDef ], defaultLoggingLevel), "upgrade abc/def");
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel), " upgrade --verbose ");
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel), "upgrade abc/def --verbose ");
(Result.Ok (Command.UpgradeDependencies [], defaultLoggingLevel, RemoteFirst), "upgrade");
(Result.Ok (Command.UpgradeDependencies [ abcDef ], defaultLoggingLevel, RemoteFirst), "upgrade abc/def");
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel, RemoteFirst), " upgrade --verbose ");
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel, RemoteFirst), "upgrade abc/def --verbose ");
(Result.Ok (Command.UpgradeDependencies [], verboseLoggingLevel, CacheFirst), " upgrade --cache-first --verbose ");
(Result.Ok (Command.UpgradeDependencies [ abcDef ], verboseLoggingLevel, CacheFirst), "upgrade abc/def --cache-first --verbose ");

(
Result.Ok
(
Command.AddDependencies
[ { Package = ijkXyz; Constraint = Constraint.wildcard; Targets = None } ],
defaultLoggingLevel
defaultLoggingLevel,
RemoteFirst
),
"add github.com/ijk/xyz "
);

(
Result.Ok (Command.UpgradeDependencies [ abcDef; ijkXyz ], verboseLoggingLevel),
Result.Ok (Command.UpgradeDependencies [ abcDef; ijkXyz ], verboseLoggingLevel, RemoteFirst),
"upgrade abc/def github.com/ijk/xyz --verbose "
);
]
Expand Down
19 changes: 10 additions & 9 deletions buckaroo-tests/Constraint.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ let ``Constraint.parse works correctly`` () =
("*", Constraint.wildcard |> Some);
("revision=aabbccddee", Version.Git(GitVersion.Revision "aabbccddee") |> Exactly |> Some);
("!*", Constraint.wildcard |> Constraint.Complement |> Some);
("any(branch=master)", Some(Any [Exactly (Version.Git(GitVersion.Branch "master"))]));
("any(any(branch=master))", Some(Any [ Any [Exactly (Version.Git(GitVersion.Branch "master"))]]));
("any(revision=aabbccddee branch=master)", Some (Any [
("any(branch=master)", Some(Any <| Set [ Exactly (Version.Git(GitVersion.Branch "master"))]));
("any(any(branch=master))", Some(Any <| Set[ Any <| Set [ Exactly (Version.Git(GitVersion.Branch "master"))]]));
("any(revision=aabbccddee branch=master)", Some (Any <| Set[
Exactly (Version.Git(GitVersion.Revision "aabbccddee"));
Exactly (Version.Git(GitVersion.Branch "master"))]));
("all(*)", Some(All [Constraint.wildcard]));
("all(*)", Some(All <| Set[Constraint.wildcard]));
(
"all(branch=master !revision=aabbccddee)",
Some (All [Exactly (Version.Git(GitVersion.Branch "master")); Complement (Exactly (Version.Git(GitVersion.Revision "aabbccddee")))])
Some (All <| Set[Exactly (Version.Git(GitVersion.Branch "master")); Complement (Exactly (Version.Git(GitVersion.Revision "aabbccddee")))])
);
(
"all(branch=master !any(revision=aabbccddee branch=develop))",
Some (All [
Some (All <| Set[
Exactly (Version.Git(GitVersion.Branch "master"));
Complement (Any([
Complement (Any(Set[
Exactly (Version.Git(GitVersion.Revision "aabbccddee"));
Exactly (Version.Git(GitVersion.Branch "develop"));
]))
Expand All @@ -52,12 +52,12 @@ let ``Constraint.parse works correctly`` () =
"+1.0.0",
Some (Constraint.rangeToConstraint RangeType.Patch (SemVer.create (1, 0, 0, 0)))
);
("all(branch=master ^1.0.0)", Some (All [
("all(branch=master ^1.0.0)", Some (All <| Set[
Exactly (Git (GitVersion.Branch "master"));
Constraint.rangeToConstraint RangeType.Major (SemVer.create (1, 0, 0, 0))
]
));
("all(^1.0.0 branch=master)", Some (All [
("all(^1.0.0 branch=master)", Some (All <| Set[
Constraint.rangeToConstraint RangeType.Major (SemVer.create (1, 0, 0, 0));
Exactly (Git (GitVersion.Branch "master"))
]))
Expand Down Expand Up @@ -100,6 +100,7 @@ let ``Constraint.simplify works correctly`` () =
("any(all(revision=aabbccddee))", "revision=aabbccddee");
("all(any(revision=aabbccddee))", "revision=aabbccddee");
("any(branch=master any(revision=aabbccddee))", "any(revision=aabbccddee branch=master)");
("any(all() revision=aabbccddee)", "any(revision=aabbccddee all())");
]
for (input, expected) in cases do
let actual =
Expand Down
6 changes: 3 additions & 3 deletions buckaroo-tests/Manifest.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let ``Manifest.toToml roundtrip 1`` () =
]
Dependencies = Set [{
Targets = Some ([{Folders=["foo"; "bar"]; Name = "xxx"}])
Constraint = All [Constraint.Exactly (Version.SemVer SemVer.zero)]
Constraint = All <| Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
}]
}
Expand Down Expand Up @@ -123,12 +123,12 @@ let ``Manifest.toToml roundtrip 2`` () =
]
Dependencies = Set [{
Targets = Some ([{Folders=["foo"; "bar"]; Name = "xxx"}])
Constraint = All [Constraint.Exactly (Version.SemVer SemVer.zero)]
Constraint = All <| Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
}]
PrivateDependencies = Set [{
Targets = Some ([{Folders=["foo"; "bar"]; Name = "yyy"}])
Constraint = Any [Constraint.Exactly (Version.SemVer SemVer.zero)]
Constraint = Any <|Set[Constraint.Exactly (Version.SemVer SemVer.zero)]
Package = PackageIdentifier.GitHub { Owner = "abc"; Project = "def" }
}]
}
Expand Down
23 changes: 12 additions & 11 deletions buckaroo-tests/Solver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ open Buckaroo.Tests
type CookBook = List<PackageIdentifier * Set<Version> * Manifest>
type LockBookEntries = List<(string*int) * List<string*int*Set<Version>>>
type LockBook = Map<PackageLock, Lock>

let package name = PackageIdentifier.Adhoc {
Owner = "test";
Owner = "test"
Project = name
}

Expand Down Expand Up @@ -89,7 +90,7 @@ type TestingSourceExplorer (cookBook : CookBook, lockBook : LockBook) =
Revision = r
})
|> AsyncSeq.ofSeq
| _ -> raise <| new System.SystemException "package not found"
| _ -> raise <| System.SystemException "Package not found"
}

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

let solve (partial : Solution) (cookBook : CookBook) (lockBookEntries : LockBookEntries) root style =
let lockBook = lockBookOf lockBookEntries
let console = new ConsoleManager(LoggingLevel.Silent);
let console = ConsoleManager (LoggingLevel.Silent)
let context : TaskContext = {
Console = console;
DownloadManager = DownloadManager(console, "/tmp");
GitManager = new GitManager(console, new GitCli(console), "/tmp");
Console = console
DownloadManager = DownloadManager(console, "/tmp")
GitManager = new GitManager(CacheFirst, console, new GitCli(console), "/tmp")
SourceExplorer = TestingSourceExplorer(cookBook, lockBook)
}

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

let getLockedRev (p : string) (r: Resolution) =
let getLockedRev (p : string) (r : _) =
match r with
| Ok solution ->
let (resolved, _) = solution.Resolutions.[package p]
Expand All @@ -143,7 +144,7 @@ let getLockedRev (p : string) (r: Resolution) =
| _ -> ""
()

let isOk (r: Resolution) =
let isOk (r : _) =
match r with
| Ok _ -> true
| _ -> false
Expand Down Expand Up @@ -228,7 +229,7 @@ let ``Solver can compute version intersections`` () =
let ``Solver can compute intersection of branches`` () =

let root = manifest [
("a", All [Exactly (br "b"); Exactly (br "a")])
("a", All <| Set[Exactly (br "b"); Exactly (br "a")])
]

let spec = [
Expand Down Expand Up @@ -346,7 +347,7 @@ let ``Solver handles negated constraints also`` () =

let root = manifest [
("a", Exactly (br "a"))
("b", Any [Exactly (br "a"); Exactly (br "b")])
("b", Any <|Set[Exactly (br "a"); Exactly (br "b")])
]

let spec = [
Expand All @@ -357,7 +358,7 @@ let ``Solver handles negated constraints also`` () =
Set[ver 2; br "a"],
manifest [])
(package "b",
Set[ver 2; br "b"],
Set[ver 2; br "a"],
manifest [])
(package "a",
Set[ver 3; br "a"],
Expand Down
22 changes: 13 additions & 9 deletions buckaroo/AddCommand.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Buckaroo.AddCommand

open System
open System.IO
open Buckaroo.RichOutput
open Buckaroo
open Buckaroo.Logger

let task (context : Tasks.TaskContext) dependencies = async {
context.Console.Write (
(text "Adding ") +
let logger = createLogger context.Console None

logger.RichInfo (
(text "Adding dependency on ") +
(
dependencies
|> Seq.map Dependency.showRich
Expand All @@ -16,6 +17,7 @@ let task (context : Tasks.TaskContext) dependencies = async {
)

let! manifest = Tasks.readManifest "."

let newManifest = {
manifest with
Dependencies =
Expand All @@ -26,6 +28,7 @@ let task (context : Tasks.TaskContext) dependencies = async {

if manifest = newManifest
then
logger.Warning ("The dependency already exists in the manifest")
return ()
else
let! maybeLock = async {
Expand All @@ -37,15 +40,16 @@ let task (context : Tasks.TaskContext) dependencies = async {
return None
}

let! resolution = Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock
let! resolution =
Solver.solve context Solution.empty newManifest ResolutionStyle.Quick maybeLock

match resolution with
| Resolution.Ok solution ->
| Result.Ok solution ->
do! Tasks.writeManifest newManifest
do! Tasks.writeLock (Lock.fromManifestAndSolution newManifest solution)
do! InstallCommand.task context
| _ ->
()

context.Console.Write ("Success. " |> text |> foreground ConsoleColor.Green)
logger.Success ("The dependency was added to the manifest and installed")
| _ ->
logger.Error ("Failed to add the dependency")
}
43 changes: 31 additions & 12 deletions buckaroo/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module Command =
return Option.isSome maybeSkip
}

let cacheFirstParser : Parser<bool, Unit> = parse {
let! cacheFirst =
CharParsers.skipString "--cache-first"
|> Primitives.opt
return Option.isSome cacheFirst
}

let startParser : Parser<Command, Unit> = parse {
do! CharParsers.spaces
return Start
Expand Down Expand Up @@ -170,13 +177,27 @@ module Command =

do! CharParsers.spaces

let! isVerbose = verboseParser

let! isCacheFirst = cacheFirstParser
do! CharParsers.spaces

let loggingLevel = if isVerbose then LoggingLevel.Trace else LoggingLevel.Info
let! isVerbose = verboseParser
do! CharParsers.spaces

return (command, loggingLevel)
let loggingLevel =
if isVerbose
then
LoggingLevel.Trace
else
LoggingLevel.Info

let fetchStyle =
if isCacheFirst
then
CacheFirst
else
RemoteFirst

return (command, loggingLevel, fetchStyle)
}

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

match resolution with
| Resolution.Ok solution ->
| Result.Ok solution ->
do! Tasks.writeManifest newManifest
do! Tasks.writeLock (Lock.fromManifestAndSolution newManifest solution)
do! InstallCommand.task context
| _ -> ()

System.Console.WriteLine ("Success. ")

return ()
System.Console.WriteLine ("Success. ")
| _ -> ()
}

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

let runCommand loggingLevel command = async {
let! context = Tasks.getContext loggingLevel
let runCommand loggingLevel fetchStyle command = async {
let! context = Tasks.getContext loggingLevel fetchStyle

do!
match command with
| Start -> StartCommand.task context
| Init -> init context
| Help -> HelpCommand.task context
| Version -> VersionCommand.task context
| Resolve style -> ResolveCommand.task context Solution.empty style
| Resolve style -> ResolveCommand.task context Solution.empty style |> Async.Ignore
| Install -> InstallCommand.task context
| Quickstart -> QuickstartCommand.task context
| UpgradeDependencies dependencies -> UpgradeCommand.task context dependencies
Expand Down
24 changes: 8 additions & 16 deletions buckaroo/ConsoleManager.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,11 @@ type ConsoleManager (minimumLoggingLevel : LoggingLevel) =
member this.Error (message, loggingLevel) =
actor.Post (Output (message, loggingLevel, OutputCategory.Error))

member this.Read() =
actor.PostAndAsyncReply(fun channel -> Input channel)

member this.ReadSecret() =
actor.PostAndAsyncReply(fun channel -> InputSecret channel)

member this.Flush() =
actor.PostAndAsyncReply(fun channel -> Flush channel)

let namespacedLogger (console : ConsoleManager) (componentName : string) (x : RichOutput, logLevel : LoggingLevel) =
(
"[" + componentName + "] "
|> RichOutput.text
|> RichOutput.foreground System.ConsoleColor.DarkGray
) +
x |> fun x -> console.Write (x, logLevel)
member this.Read () =
actor.PostAndAsyncReply Input

member this.ReadSecret () =
actor.PostAndAsyncReply InputSecret

member this.Flush () =
actor.PostAndAsyncReply Flush
Loading

0 comments on commit bc93276

Please sign in to comment.