Skip to content

Commit

Permalink
Adds only required binding redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinaldi committed Nov 3, 2015
1 parent a9ea669 commit 142e28d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
24 changes: 12 additions & 12 deletions integrationtests/Paket.IntegrationTests/BindingRedirect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open NUnit.Framework
open FsUnit

[<Test>]
let ``install should redirect referenced assemblies only``() =
let ``install should redirect required assemblies only``() =
paket "install --redirects --createnewbindingfiles" "binding-redirect-adds-referenced-assemblies-only" |> ignore

let path = Path.Combine(scenarioTempPath "binding-redirect-adds-referenced-assemblies-only")
Expand Down Expand Up @@ -34,22 +34,22 @@ let ``install should redirect referenced assemblies only``() =

config1 |> shouldContainText Albedo
config1 |> shouldContainText AutoFixture
config1 |> shouldContainText ``AutoFixture.Idioms``
config1 |> shouldContainText ``AutoFixture.Xunit``
config1 |> shouldContainText log4net
config1.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config1.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config1.Contains log4net |> shouldEqual false
config1 |> shouldContainText ``Newtonsoft.Json``
config1 |> shouldContainText ``Newtonsoft.Json.Schema``
config1 |> shouldContainText xunit
config1.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config1.Contains xunit |> shouldEqual false
config1 |> shouldContainText ``xunit.extensions``
config1 |> shouldContainText ``Castle.Core``
config1 |> shouldContainText ``Castle.Windsor``
config1.Contains ``Castle.Windsor`` |> shouldEqual false

config2.Contains Albedo |> shouldEqual false
config2.Contains AutoFixture |> shouldEqual false
config2.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config2.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config2.Contains log4net |> shouldEqual false
config2 |> shouldContainText ``Newtonsoft.Json``
config2.Contains ``Newtonsoft.Json`` |> shouldEqual false
config2.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config2.Contains xunit |> shouldEqual false
config2.Contains ``xunit.extensions`` |> shouldEqual false
Expand All @@ -60,13 +60,13 @@ let ``install should redirect referenced assemblies only``() =
config3.Contains AutoFixture |> shouldEqual false
config3.Contains ``AutoFixture.Idioms`` |> shouldEqual false
config3.Contains ``AutoFixture.Xunit`` |> shouldEqual false
config3 |> shouldContainText log4net
config3.Contains log4net |> shouldEqual false
config3 |> shouldContainText ``Newtonsoft.Json``
config3 |> shouldContainText ``Newtonsoft.Json.Schema``
config3.Contains ``Newtonsoft.Json.Schema`` |> shouldEqual false
config3.Contains xunit |> shouldEqual false
config3.Contains ``xunit.extensions`` |> shouldEqual false
config3 |> shouldContainText ``Castle.Core``
config3 |> shouldContainText ``Castle.Windsor``
config3.Contains ``Castle.Windsor`` |> shouldEqual false

config4.Contains Albedo |> shouldEqual false
config4.Contains AutoFixture |> shouldEqual false
Expand All @@ -78,4 +78,4 @@ let ``install should redirect referenced assemblies only``() =
config4.Contains xunit |> shouldEqual false
config4.Contains ``xunit.extensions`` |> shouldEqual false
config4 |> shouldContainText ``Castle.Core``
config4 |> shouldContainText ``Castle.Windsor``
config4.Contains ``Castle.Windsor`` |> shouldEqual false
63 changes: 36 additions & 27 deletions src/Paket.Core/InstallProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -181,33 +181,42 @@ let private applyBindingRedirects (loadedLibs:Dictionary<_,_>) createNewBindingF
match ProjectFile.FindReferencesFile (FileInfo projectFile.FileName) with
| Some fileName ->
let dependencies = dependencies projectFile
extractedPackages
|> Seq.map snd
|> Seq.filter (fun model -> dependencies |> Seq.contains model.PackageName)
|> Seq.map (fun model -> model.GetLibReferences(projectFile.GetTargetProfile()))
|> Seq.concat
|> Seq.groupBy (fun p -> FileInfo(p).Name)
|> Seq.choose(fun (_,librariesForPackage) ->
librariesForPackage
|> Seq.choose(fun library ->
try
let key = FileInfo(library).FullName.ToLowerInvariant()
let assembly =
match loadedLibs.TryGetValue key with
| true,v -> v
| _ ->
let v = Assembly.ReflectionOnlyLoadFrom library
loadedLibs.Add(key,v)
v

assembly
|> BindingRedirects.getPublicKeyToken
|> Option.map(fun token -> assembly, token)
with exn -> None)
|> Seq.sortBy(fun (assembly,_) -> assembly.GetName().Version)
|> Seq.toList
|> List.rev
|> function | head :: _ -> Some head | _ -> None)

let assemblies =
extractedPackages
|> Seq.map snd
|> Seq.filter (fun model -> dependencies |> Seq.contains model.PackageName)
|> Seq.map (fun model -> model.GetLibReferences(projectFile.GetTargetProfile()))
|> Seq.concat
|> Seq.groupBy (fun p -> FileInfo(p).Name)
|> Seq.choose(fun (_,librariesForPackage) ->
librariesForPackage
|> Seq.choose(fun library ->
try
let key = FileInfo(library).FullName.ToLowerInvariant()
let assembly =
match loadedLibs.TryGetValue key with
| true,v -> v
| _ ->
let v = Assembly.ReflectionOnlyLoadFrom library
loadedLibs.Add(key,v)
v

Some (assembly, BindingRedirects.getPublicKeyToken assembly)
with exn -> None)
|> Seq.sortBy(fun (assembly,_) -> assembly.GetName().Version)
|> Seq.toList
|> List.rev
|> function | head :: _ -> Some head | _ -> None)
|> Seq.cache

assemblies
|> Seq.choose (fun (assembly,token) -> token |> Option.map (fun token -> (assembly,token)))
|> Seq.filter (fun (assembly,_) ->
assemblies
|> Seq.collect (fun (a,_) -> a.GetReferencedAssemblies())
|> Seq.filter (fun a -> assembly.GetName().Name = a.Name)
|> Seq.exists (fun a -> assembly.GetName().Version > a.Version))
|> Seq.map(fun (assembly, token) ->
{ BindingRedirect.AssemblyName = assembly.GetName().Name
Version = assembly.GetName().Version.ToString()
Expand Down

0 comments on commit 142e28d

Please sign in to comment.