Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert-from-nuget should simplify framework restrictions if possible #1159

Merged
merged 4 commits into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Paket.Core/DependenciesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,27 @@ type DependenciesFile(fileName,groups:Map<GroupName,DependenciesGroup>, textRepr

member __.Groups = groups

member this.SimplifyFrameworkRestrictions() =
let transform (dependenciesFile:DependenciesFile) (group:DependenciesGroup) =
if group.Options.Settings.FrameworkRestrictions <> [] then dependenciesFile else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when there are global restrictions and standalone restrictions per package? The latter override global?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't do anything in this case.
during normal run both restrictions work together as two filters.

match group.Packages with
| [] -> dependenciesFile
| package::rest ->
let sameRequirements =
rest |> Seq.forall (fun p' -> p'.Settings.FrameworkRestrictions = package.Settings.FrameworkRestrictions)

if not sameRequirements then dependenciesFile else

let newDependenciesFile = dependenciesFile.AddFrameworkRestriction(group.Name,package.Settings.FrameworkRestrictions)
group.Packages
|> List.fold (fun (d:DependenciesFile) package ->
let (d:DependenciesFile) = d.Remove(group.Name,package.Name)
d.Add(group.Name,package.Name,package.VersionRequirement.ToString(),{ package.Settings with FrameworkRestrictions = [] })) newDependenciesFile

this.Groups
|> Seq.map (fun kv -> kv.Value)
|> Seq.fold transform this

member this.GetGroup groupName =
match this.Groups |> Map.tryFind groupName with
| Some g -> g
Expand Down Expand Up @@ -449,6 +470,29 @@ type DependenciesFile(fileName,groups:Map<GroupName,DependenciesGroup>, textRepr
groupsToResolve
|> Map.map resolveGroup


member private this.AddFrameworkRestriction(groupName, frameworkRestrictions:FrameworkRestrictions) =
if frameworkRestrictions = [] then this else
let restrictionString = sprintf "framework %s" (String.Join(", ",frameworkRestrictions))

let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation

match groups |> Map.tryFind groupName with
| None -> list.Add(restrictionString)
| Some group ->
let firstGroupLine,_ = findGroupBorders groupName
let pos = ref firstGroupLine
while list.Count > !pos && list.[!pos].TrimStart().StartsWith "source" do
pos := !pos + 1

list.Insert(!pos,restrictionString)

DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName)

member __.AddAdditionalPackage(groupName, packageName:PackageName,versionRequirement,resolverStrategy,settings,?pinDown) =
let pinDown = defaultArg pinDown false
let packageString = DependenciesFileSerializer.packageString packageName versionRequirement resolverStrategy settings
Expand Down
1 change: 1 addition & 0 deletions src/Paket.Core/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ let createDependenciesFileR (rootDirectory : DirectoryInfo) nugetEnv mode =
Paket.DependenciesFile(DependenciesFileParser.parseDependenciesFile dependenciesFileName newLines))

if File.Exists dependenciesFileName then read() else create()
|> lift (fun d -> d.SimplifyFrameworkRestrictions())

let convertPackagesConfigToReferences projectFileName packagesConfig =
let referencesFile = ProjectFile.FindOrCreateReferencesFile(FileInfo projectFileName)
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/Simplifier.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let simplifyDependenciesFile (dependenciesFile : DependenciesFile, groupName, fl
else d) dependenciesFile
}

let simplifyReferencesFile (refFile:ReferencesFile, groupName, flatLookup, interactive) = trial {
let simplifyReferencesFile (refFile:ReferencesFile, groupName, flatLookup, interactive) = trial {
match refFile.Groups |> Map.tryFind groupName with
| None -> return refFile
| Some g ->
Expand All @@ -66,7 +66,7 @@ let simplify interactive environment = trial {
let! lockFile = environment |> PaketEnv.ensureLockFileExists

let flatLookup = lockFile.GetDependencyLookupTable()
let dependenciesFileRef = ref environment.DependenciesFile
let dependenciesFileRef = ref (environment.DependenciesFile.SimplifyFrameworkRestrictions())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will happen also on paket simplify command?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on simplify and convert

let projectsRef = ref None

for kv in lockFile.Groups do
Expand Down
4 changes: 2 additions & 2 deletions src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
<StartArguments>update group Build</StartArguments>
<StartArguments>pack output D:\code\paketbug\output</StartArguments>
<StartArguments>install</StartArguments>
<StartArguments>update</StartArguments>
<StartArguments>convert-from-nuget -f</StartArguments>
<StartAction>Project</StartAction>
<StartProgram>paket.exe</StartProgram>
<StartWorkingDirectory>c:\code\Paketkopie</StartWorkingDirectory>
<StartWorkingDirectory>C:\Temp\paket_test\</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\paketkopie</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\paketbug</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\paketrepro</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\paketbug</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\NServiceBus</StartWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
93 changes: 93 additions & 0 deletions tests/Paket.Tests/Simplifier/BasicScenarioSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,96 @@ nuget C 1.0"""
depFile.ToString()
|> shouldEqual (normalizeLineEndings expected)

[<Test>]
let ``should simplify framework restrictions in main group``() =
let before = """source https://www.nuget.org/api/v2/

nuget angularjs 1.4.3 framework: >= net45
nuget AngularTemplates.Compile 1.0.0 framework: >= net45
nuget Antlr 3.4.1.9004 framework: >= net45
nuget Autofac 3.5.0 framework: >= net45
nuget Autofac.Owin 3.1.0 framework: >= net45
nuget Autofac.WebApi 3.1.0 framework: >= net45
nuget Autofac.WebApi2 3.4.0 framework: >= net45
nuget Autofac.WebApi2.Owin 3.2.0 framework: >= net45"""

let expected = """source https://www.nuget.org/api/v2/
framework >= net45

nuget angularjs 1.4.3
nuget AngularTemplates.Compile 1.0.0
nuget Antlr 3.4.1.9004
nuget Autofac 3.5.0
nuget Autofac.Owin 3.1.0
nuget Autofac.WebApi 3.1.0
nuget Autofac.WebApi2 3.4.0
nuget Autofac.WebApi2.Owin 3.2.0"""


let originalLockFile = DependenciesFile.FromCode(before)
originalLockFile.SimplifyFrameworkRestrictions().ToString() |> shouldEqual expected

[<Test>]
let ``should not simplify framework restrictions when not equal``() =
let before = """source https://www.nuget.org/api/v2/

nuget angularjs 1.4.3 framework: >= net45
nuget AngularTemplates.Compile 1.0.0 framework: >= net45
nuget Antlr 3.4.1.9004 framework: >= net45
nuget Autofac 3.5.0 framework: >= net45
nuget Autofac.Owin 3.1.0 framework: >= net40
nuget Autofac.WebApi 3.1.0 framework: >= net45
nuget Autofac.WebApi2 3.4.0 framework: >= net45
nuget Autofac.WebApi2.Owin 3.2.0 framework: >= net45"""

let originalLockFile = DependenciesFile.FromCode(before)
originalLockFile.SimplifyFrameworkRestrictions().ToString()
|> normalizeLineEndings
|> shouldEqual (normalizeLineEndings before)

[<Test>]
let ``should simplify framework restrictions in every group``() =
let before = """source https://www.nuget.org/api/v2/

nuget angularjs 1.4.3 framework: >= net45
nuget AngularTemplates.Compile 1.0.0 framework: >= net45
nuget Antlr 3.4.1.9004 framework: >= net45
nuget Autofac 3.5.0 framework: >= net45

group Build
source https://www.nuget.org/api/v2/
nuget Autofac.Owin 3.1.0 framework: >= net40
nuget Autofac.WebApi 3.1.0 framework: >= net40
nuget Autofac.WebApi2 3.4.0 framework: >= net40
nuget Autofac.WebApi2.Owin 3.2.0 framework: >= net40"""

let expected = """source https://www.nuget.org/api/v2/
framework >= net45

nuget angularjs 1.4.3
nuget AngularTemplates.Compile 1.0.0
nuget Antlr 3.4.1.9004
nuget Autofac 3.5.0

group Build
source https://www.nuget.org/api/v2/
framework >= net40
nuget Autofac.Owin 3.1.0
nuget Autofac.WebApi 3.1.0
nuget Autofac.WebApi2 3.4.0
nuget Autofac.WebApi2.Owin 3.2.0"""


let originalLockFile = DependenciesFile.FromCode(before)
originalLockFile.SimplifyFrameworkRestrictions().ToString()
|> normalizeLineEndings
|> shouldEqual (normalizeLineEndings expected)

[<Test>]
let ``should not simplify framework restrictions in empty file``() =
let before = ""

let originalLockFile = DependenciesFile.FromCode(before)
originalLockFile.SimplifyFrameworkRestrictions().ToString()
|> normalizeLineEndings
|> shouldEqual (normalizeLineEndings before)