Skip to content

Commit

Permalink
Started work on fsprojects#808.
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacabraham committed Sep 3, 2015
1 parent f02af77 commit 5ef9997
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Paket.Core/BindingRedirects.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,33 @@ let private applyBindingRedirects bindingRedirects (configFilePath:string) =
indentAssemblyBindings config
config.Save configFilePath

let private configFiles = [ "app"; "web" ] |> Set.ofList
let private toLower (s:string) = s.ToLower()
let private isAppOrWebConfig = configFiles.Contains << (Path.GetFileNameWithoutExtension >> toLower)
let internal getFoldersWithPaketReferencesAndNoConfig getFiles rootPath =
getFiles(rootPath, "paket.references", SearchOption.AllDirectories)
|> Seq.map Path.GetDirectoryName
|> Seq.filter(fun directory -> getFiles(directory, "*.config", SearchOption.TopDirectoryOnly) |> Seq.forall (not << isAppOrWebConfig))
|> Seq.toList
let private getExistingConfigFiles getFiles rootPath =
getFiles(rootPath, "*.config", SearchOption.AllDirectories)
|> Seq.filter isAppOrWebConfig
let private baseConfig = """<?xml version="1.0" encoding="utf-8"?>
<configuration>
</configuration>
"""
let private createAppConfigFile folder = File.WriteAllText(Path.Combine(folder, "app.config"), baseConfig)

/// Applies a set of binding redirects to all .config files in a specific folder.
let applyBindingRedirectsToFolder rootPath bindingRedirects =
Directory.GetFiles(rootPath, "*.config", SearchOption.AllDirectories)
|> Seq.filter (fun x -> x.EndsWith(Path.DirectorySeparatorChar.ToString() + "web.config", StringComparison.CurrentCultureIgnoreCase) || x.EndsWith(Path.DirectorySeparatorChar.ToString() + "app.config", StringComparison.CurrentCultureIgnoreCase))
// First create missing configuration files.
rootPath
|> getFoldersWithPaketReferencesAndNoConfig Directory.GetFiles
|> Seq.iter createAppConfigFile

// Now ensure all configuration files have binding redirects.
rootPath
|> getExistingConfigFiles Directory.GetFiles
|> Seq.iter (applyBindingRedirects bindingRedirects)

/// Calculates the short form of the public key token for use with binding redirects, if it exists.
Expand Down
35 changes: 35 additions & 0 deletions tests/Paket.Tests/BindingRedirect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,38 @@ let ``redirects got properly indented for readability``() =
let dependency = doc.Descendants(xNameForNs "dependentAssembly") |> Seq.head
dependency.ToString() |> shouldEqual "<dependentAssembly xmlns=\"urn:schemas-microsoft-com:asm.v1\">\r\n <assemblyIdentity name=\"Assembly\" publicKeyToken=\"PUBLIC_KEY\" culture=\"neutral\" />\r\n <bindingRedirect oldVersion=\"0.0.0.0-999.999.999.999\" newVersion=\"1.0.0\" />\r\n </dependentAssembly>"

let buildMockGetFiles outcomes =
fun (path, wildcard, _) ->
outcomes
|> List.tryFind (fst >> (=) (path, wildcard))
|> Option.map snd
|> defaultArg <| []
|> List.toArray

[<Test>]
let ``app.config file is marked for creation in folders containing paket.references``() =
let mockGetFiles =
buildMockGetFiles
[ (@"C:\rootpath", "paket.references"), [ @"C:\rootpath\source\paket.references" ]
(@"C:\rootpath\source", "*.config"), [ @"C:\rootpath\source\paket.references" ]
]
let foldersToCreateConfigFor = getFoldersWithPaketReferencesAndNoConfig mockGetFiles @"C:\rootpath"
foldersToCreateConfigFor |> shouldEqual [ @"C:\rootpath\source"]

[<Test>]
let ``app.config file is not marked for creation in folders not containing paket.references``() =
let mockGetFiles = buildMockGetFiles [ (@"C:\rootpath", "paket.references"), [] ]
let foldersToCreateConfigFor = getFoldersWithPaketReferencesAndNoConfig mockGetFiles @"C:\rootpath"
foldersToCreateConfigFor |> shouldEqual []

[<Test>]
let ``app.config file is not marked for creation in folders if one already exists``() =
let mockGetFiles =
buildMockGetFiles
[ (@"C:\rootpath", "paket.references"), [ @"C:\rootpath\source\paket.references" ]
(@"C:\rootpath\source", "*.config"), [ @"C:\rootpath\source\paket.references"; @"C:\rootpath\source\app.config" ]
]
let foldersToCreateConfigFor = getFoldersWithPaketReferencesAndNoConfig mockGetFiles @"C:\rootpath"
foldersToCreateConfigFor |> shouldEqual []


0 comments on commit 5ef9997

Please sign in to comment.