Skip to content

Commit

Permalink
Implement group conditions for SDK-style projects
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeronimos committed Feb 15, 2024
1 parent 4954f9f commit d19eb66
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/Paket.Core/Installation/RestoreProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,22 @@ let createPaketPropsFile (lockFile:LockFile) (cliTools:ResolvedPackage seq) (pac
| ExplicitRestriction fw -> ExplicitRestriction fw
| _ -> group.Options.Settings.FrameworkRestrictions
let condition = getExplicitRestriction restrictions
p,condition,packageSettings)
|> Seq.groupBy (fun (_,c,__) -> c)
|> Seq.collect (fun (condition,packages) ->
let condition =

p,condition,packageSettings,group.Options.Settings.ReferenceCondition)
|> Seq.groupBy (fun (_,c,__,rc) -> c,rc)
|> Seq.collect (fun ((condition,referenceCondition),packages) ->
let targets =
match condition with
| FrameworkRestriction.HasNoRestriction -> ""
| restrictions -> restrictions.ToMSBuildCondition()
| FrameworkRestriction.HasNoRestriction -> Set.empty
| restrictions -> restrictions.RepresentedFrameworks
let condition = PlatformMatching.getCondition referenceCondition targets
let condition =
if condition = "" || condition = "true" then "" else
sprintf " AND (%s)" condition

let packageReferences =
packages
|> Seq.collect (fun (p,_,packageSettings) ->
|> Seq.collect (fun (p,_,packageSettings,__) ->
[yield sprintf """ <PackageReference Include="%O">""" p.Name
yield sprintf """ <Version>%O</Version>""" p.Version
let excludeAssets =
Expand Down
134 changes: 125 additions & 9 deletions tests/Paket.Tests/InstallModel/PaketPropsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ let checkTargetFrameworkCondition msbuildCondition (itemGroup: XElement) =
let checkTargetFrameworkNoRestriction itemGroup =
checkTargetFrameworkCondition "" itemGroup

let checkTargetFrameworkRestriction r itemGroup =
let msbuildCond = r |> Paket.Requirements.getExplicitRestriction |> fun c -> c.ToMSBuildCondition()
let checkTargetFrameworkRestriction rc r itemGroup =
let msbuildCond = r |> Paket.Requirements.getExplicitRestriction |> fun c -> PlatformMatching.getCondition rc c.RepresentedFrameworks
checkTargetFrameworkCondition (sprintf " AND (%s)" msbuildCond) itemGroup

let checkContainsPackageRefs pkgRefs (group: XElement) =
Expand Down Expand Up @@ -186,11 +186,127 @@ group Other1
match itemGroups with
| [groupMain; otherGroup] ->
groupMain
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
groupMain
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
otherGroup
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
otherGroup
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
| l ->
Assert.Fail(sprintf "expected two ItemGroup but was '%A'" l)

[<Test>]
let ``should create props file for design mode with group conditions``() =

let lockFile = """CONDITION: COND_MAIN
NUGET
remote: https://api.nuget.org/v3/index.json
Argu (4.2.1)
FSharp.Core (>= 3.1.2)
FSharp.Core (3.1.2.5)
GROUP Other1
CONDITION: COND_OTHER1
NUGET
remote: https://api.nuget.org/v3/index.json
FsCheck (2.8.2)
FSharp.Core (>= 3.1.2.5)
FSharp.Core (4.3.4)
"""

let refFileContent = """
FSharp.Core
Argu
group Other1
FSharp.Core
FsCheck
"""

let lockFile = LockFile.Parse("", toLines lockFile)

let refFile = ReferencesFile.FromLines(toLines refFileContent)

let packages =
[ for kv in refFile.Groups do
let packagesInGroup,_ = lockFile.GetOrderedPackageHull(kv.Key, refFile)
yield! packagesInGroup ]

let outPath = System.IO.Path.GetTempFileName()
Paket.RestoreProcess.createPaketPropsFile lockFile Seq.empty packages (FileInfo outPath)

let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)

let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList

match itemGroups with
| [groupMain; otherGroup] ->
groupMain
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
groupMain
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
otherGroup
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
otherGroup
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
| l ->
Assert.Fail(sprintf "expected two ItemGroup but was '%A'" l)

[<Test>]
let ``should create props file for design mode with group restrictions and conditions``() =

let lockFile = """CONDITION: COND_MAIN
RESTRICTION: && (>= net461) (< net47)
NUGET
remote: https://api.nuget.org/v3/index.json
Argu (4.2.1)
FSharp.Core (>= 3.1.2)
FSharp.Core (3.1.2.5)
GROUP Other1
CONDITION: COND_OTHER1
RESTRICTION: == netstandard2.0
NUGET
remote: https://api.nuget.org/v3/index.json
FsCheck (2.8.2)
FSharp.Core (>= 3.1.2.5)
FSharp.Core (4.3.4)
"""

let refFileContent = """
FSharp.Core
Argu
group Other1
FSharp.Core
FsCheck
"""

let lockFile = LockFile.Parse("", toLines lockFile)

let refFile = ReferencesFile.FromLines(toLines refFileContent)

let packages =
[ for kv in refFile.Groups do
let packagesInGroup,_ = lockFile.GetOrderedPackageHull(kv.Key, refFile)
yield! packagesInGroup ]

let outPath = System.IO.Path.GetTempFileName()
Paket.RestoreProcess.createPaketPropsFile lockFile Seq.empty packages (FileInfo outPath)

let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)

let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList

match itemGroups with
| [groupMain; otherGroup] ->
groupMain
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
groupMain
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
otherGroup
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
otherGroup
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
| l ->
Expand Down Expand Up @@ -243,17 +359,17 @@ group Other2
match itemGroups with
| [groupMain; otherGroup20And21; otherGroupOnly21] ->
groupMain
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
groupMain
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]

otherGroup20And21
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.FrameworkRestrictions
otherGroup20And21
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4" ]

otherGroupOnly21
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Resolution.[Domain.PackageName "FsCheck"].Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other2"].Resolution.[Domain.PackageName "FsCheck"].Settings.FrameworkRestrictions
otherGroupOnly21
|> checkContainsPackageRefs [ "FsCheck","2.8.2" ]
| l ->
Expand Down Expand Up @@ -286,11 +402,11 @@ Newtonsoft.Json
let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)

let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList

match itemGroups with
| [groupMain] ->
groupMain
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Resolution.[Domain.PackageName "Newtonsoft.Json"].Settings.FrameworkRestrictions
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Resolution.[Domain.PackageName "Newtonsoft.Json"].Settings.FrameworkRestrictions
groupMain
|> checkContainsPackageRefs [ "Newtonsoft.Json","11.0.2" ]
| l ->
Expand Down

0 comments on commit d19eb66

Please sign in to comment.