Skip to content

Commit

Permalink
Includes only the assemblies from the target platform in binding redi…
Browse files Browse the repository at this point in the history
…rects
  • Loading branch information
mrinaldi committed Sep 19, 2015
1 parent 2019f91 commit c40e4c2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
44 changes: 30 additions & 14 deletions src/Paket.Core/BindingRedirects.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Paket.BindingRedirects
module Paket.BindingRedirects

open System
open System.Text
open System.Xml
open System.Xml.Linq
open System.IO
open System.Reflection
open System.Runtime.Versioning
open Paket.Xml.Linq
open Paket.FrameworkConversion

/// Represents a binding redirection
type BindingRedirect =
Expand Down Expand Up @@ -68,19 +70,6 @@ let internal indentAssemblyBindings config =
let newAssemblyBindingNode = XElement.Parse(sb.ToString(), LoadOptions.PreserveWhitespace)
parent.Add(newAssemblyBindingNode)

/// Applies a set of binding redirects to a single configuration file.
let private applyBindingRedirects bindingRedirects (configFilePath:string) =
let config =
try
XDocument.Load(configFilePath, LoadOptions.PreserveWhitespace)
with
| :? System.Xml.XmlException as ex ->
Logging.verbosefn "Illegal XML in file: %s" configFilePath
raise ex
let config = Seq.fold setRedirect config bindingRedirects
indentAssemblyBindings config
config.Save configFilePath

let private configFiles = [ "app"; "web" ] |> Set.ofList
let private projectFiles = [ ".csproj"; ".vbproj"; ".fsproj" ] |> Set.ofList
let private toLower (s:string) = s.ToLower()
Expand Down Expand Up @@ -118,6 +107,33 @@ let private addConfigFileToProject projectFile =
|> ignore
project.Save())

/// Applies a set of binding redirects to a single configuration file.
let private applyBindingRedirects bindingRedirects (configFilePath:string) =
let projectFile =
getProjectFilesInDirectory (Path.GetDirectoryName(configFilePath))
|> Seq.map ProjectFile.Load
|> Seq.tryHead
|> Option.bind id

let bindingRedirects =
match projectFile with
| None -> Seq.empty
| Some p ->
FrameworkName(p.TargetFramework)
|> convertToFrameworkIdentifier
|> bindingRedirects

let config =
try
XDocument.Load(configFilePath, LoadOptions.PreserveWhitespace)
with
| :? System.Xml.XmlException as ex ->
Logging.verbosefn "Illegal XML in file: %s" configFilePath
raise ex
let config = Seq.fold setRedirect config bindingRedirects
indentAssemblyBindings config
config.Save configFilePath

/// Applies a set of binding redirects to all .config files in a specific folder.
let applyBindingRedirectsToFolder createNewBindingFiles rootPath bindingRedirects =
if createNewBindingFiles then
Expand Down
56 changes: 31 additions & 25 deletions src/Paket.Core/InstallProcess.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Contains methods for the install process.
/// Contains methods for the install process.
module Paket.InstallProcess

open Paket
Expand Down Expand Up @@ -138,30 +138,36 @@ let createModel(root, force, dependenciesFile:DependenciesFile, lockFile : LockF

/// Applies binding redirects for all strong-named references to all app. and web. config files.
let private applyBindingRedirects createNewBindingFiles root (extractedPackages:seq<_*InstallModel>) =
extractedPackages
|> Seq.map (fun (package, model:InstallModel) -> model.GetLibReferencesLazy.Force())
|> Set.unionMany
|> Seq.choose(function | Reference.Library path -> Some path | _-> None)
|> Seq.groupBy (fun p -> FileInfo(p).Name)
|> Seq.choose(fun (_,librariesForPackage) ->
librariesForPackage
|> Seq.choose(fun library ->
try
let assembly = Assembly.ReflectionOnlyLoadFrom library
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)
|> Seq.map(fun (assembly, token) ->
{ BindingRedirect.AssemblyName = assembly.GetName().Name
Version = assembly.GetName().Version.ToString()
PublicKeyToken = token
Culture = None })
|> applyBindingRedirectsToFolder createNewBindingFiles root
let bindingRedirects (frameworkIdentifier : FrameworkIdentifier option) =
extractedPackages
|> Seq.map (fun (package, model:InstallModel) ->
match frameworkIdentifier with
| Some x -> model.GetLibReferences(x)
| None ->
model.GetLibReferencesLazy.Force()
|> Seq.choose(function | Reference.Library path -> Some path | _-> None))
|> Seq.concat
|> Seq.groupBy (fun p -> FileInfo(p).Name)
|> Seq.choose(fun (_,librariesForPackage) ->
librariesForPackage
|> Seq.choose(fun library ->
try
let assembly = Assembly.ReflectionOnlyLoadFrom library
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)
|> Seq.map(fun (assembly, token) ->
{ BindingRedirect.AssemblyName = assembly.GetName().Name
Version = assembly.GetName().Version.ToString()
PublicKeyToken = token
Culture = None })

applyBindingRedirectsToFolder createNewBindingFiles root bindingRedirects

let findAllReferencesFiles root =
root
Expand Down

0 comments on commit c40e4c2

Please sign in to comment.