Skip to content

Commit

Permalink
prefer full .NET Frameworks over PCL, even if older
Browse files Browse the repository at this point in the history
See @cdrnet's comments on fsprojects#380.
  • Loading branch information
Christian Lang committed Nov 24, 2014
1 parent b125d30 commit 387eab6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
40 changes: 28 additions & 12 deletions src/Paket.Core/PlatformMatching.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,41 @@ let getPenalty (requiredPlatforms:FrameworkIdentifier list) (path:string) =
requiredPlatforms
|> List.sumBy (getPathPenalty path)

type PathPenalty = (string * int)

let comparePaths (p1 : PathPenalty) (p2 : PathPenalty) =
let platformCount1 = (extractPlatforms (fst p1)).Length
let platformCount2 = (extractPlatforms (fst p2)).Length

// prefer full framwork over portable
if platformCount1 = 1 && platformCount2 > 1 then
-1
else if platformCount1 > 1 && platformCount2 = 1 then
1
// prefer lower version penalty
else if snd p1 < snd p2 then
-1
else if snd p1 > snd p2 then
1
// prefer portable platform whith less platforms
else if platformCount1 < platformCount2 then
-1
else if platformCount1 > platformCount2 then
1
else
0

let findBestMatch (paths : string list) (targetProfile : TargetProfile) =
let requiredPlatforms =
match targetProfile with
| PortableProfile(_, platforms) -> platforms
| SinglePlatform(platform) -> [ platform ]

let pathPenalties =
paths
|> List.map (fun path -> (path, getPenalty requiredPlatforms path))

let minPenalty =
pathPenalties
|> Seq.map snd
|> Seq.min

pathPenalties
|> Seq.filter (fun (path, penalty) -> penalty = minPenalty && minPenalty < maxPenalty)
paths
|> List.map (fun path -> path, (getPenalty requiredPlatforms path))
|> List.filter (fun (_, penalty) -> penalty < maxPenalty)
|> List.sortWith comparePaths
|> Seq.map fst
|> Seq.sortBy (fun path -> (extractPlatforms path).Length)
|> Seq.tryFind (fun _ -> true)

// For a given list of paths and target profiles return tuples of paths with their supported target profiles.
Expand Down
6 changes: 5 additions & 1 deletion tests/Paket.Tests/InstallModel/Penalty/PenaltySpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module ``Given an empty path`` =

module ``Given a list of paths`` =
let paths =
[ "net40"; "net45"; "portable-monotouch+monoandroid"; "portable-net40+sl5+win8+wp8+wpa81";
[ "net40"; "portable-monotouch+monoandroid"; "portable-net40+sl5+win8+wp8+wpa81";
"portable-net45+winrt45+wp8+wpa81"; "portable-win81+wpa81"; "portable-windows8+net45+wp8"; "sl5"; "win8";
"wp8" ]

Expand All @@ -83,6 +83,10 @@ module ``Given a list of paths`` =
let ``it should find no match for Silverlight 4``() =
findBestMatch paths (SinglePlatform(Silverlight "v4.0")) |> shouldEqual None

[<Test>]
let ``it should prefer (older) full .NET frameworks over portable class libraries``() =
findBestMatch paths (SinglePlatform(DotNetFramework FrameworkVersion.V4_5)) |> shouldEqual (Some "net40")

module ``when I get the supported target profiles`` =
let supportedTargetProfiles = getSupportedTargetProfiles paths

Expand Down

0 comments on commit 387eab6

Please sign in to comment.