Skip to content

Commit d19eb66

Browse files
committed
Implement group conditions for SDK-style projects
1 parent 4954f9f commit d19eb66

File tree

2 files changed

+134
-16
lines changed

2 files changed

+134
-16
lines changed

src/Paket.Core/Installation/RestoreProcess.fs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,22 @@ let createPaketPropsFile (lockFile:LockFile) (cliTools:ResolvedPackage seq) (pac
314314
| ExplicitRestriction fw -> ExplicitRestriction fw
315315
| _ -> group.Options.Settings.FrameworkRestrictions
316316
let condition = getExplicitRestriction restrictions
317-
p,condition,packageSettings)
318-
|> Seq.groupBy (fun (_,c,__) -> c)
319-
|> Seq.collect (fun (condition,packages) ->
320-
let condition =
317+
318+
p,condition,packageSettings,group.Options.Settings.ReferenceCondition)
319+
|> Seq.groupBy (fun (_,c,__,rc) -> c,rc)
320+
|> Seq.collect (fun ((condition,referenceCondition),packages) ->
321+
let targets =
321322
match condition with
322-
| FrameworkRestriction.HasNoRestriction -> ""
323-
| restrictions -> restrictions.ToMSBuildCondition()
323+
| FrameworkRestriction.HasNoRestriction -> Set.empty
324+
| restrictions -> restrictions.RepresentedFrameworks
325+
let condition = PlatformMatching.getCondition referenceCondition targets
324326
let condition =
325327
if condition = "" || condition = "true" then "" else
326328
sprintf " AND (%s)" condition
327329

328330
let packageReferences =
329331
packages
330-
|> Seq.collect (fun (p,_,packageSettings) ->
332+
|> Seq.collect (fun (p,_,packageSettings,__) ->
331333
[yield sprintf """ <PackageReference Include="%O">""" p.Name
332334
yield sprintf """ <Version>%O</Version>""" p.Version
333335
let excludeAssets =

tests/Paket.Tests/InstallModel/PaketPropsTests.fs

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ let checkTargetFrameworkCondition msbuildCondition (itemGroup: XElement) =
1919
let checkTargetFrameworkNoRestriction itemGroup =
2020
checkTargetFrameworkCondition "" itemGroup
2121

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

2626
let checkContainsPackageRefs pkgRefs (group: XElement) =
@@ -186,11 +186,127 @@ group Other1
186186
match itemGroups with
187187
| [groupMain; otherGroup] ->
188188
groupMain
189-
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
189+
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
190190
groupMain
191191
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
192192
otherGroup
193-
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
193+
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
194+
otherGroup
195+
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
196+
| l ->
197+
Assert.Fail(sprintf "expected two ItemGroup but was '%A'" l)
198+
199+
[<Test>]
200+
let ``should create props file for design mode with group conditions``() =
201+
202+
let lockFile = """CONDITION: COND_MAIN
203+
NUGET
204+
remote: https://api.nuget.org/v3/index.json
205+
Argu (4.2.1)
206+
FSharp.Core (>= 3.1.2)
207+
FSharp.Core (3.1.2.5)
208+
209+
GROUP Other1
210+
CONDITION: COND_OTHER1
211+
NUGET
212+
remote: https://api.nuget.org/v3/index.json
213+
FsCheck (2.8.2)
214+
FSharp.Core (>= 3.1.2.5)
215+
FSharp.Core (4.3.4)
216+
"""
217+
218+
let refFileContent = """
219+
FSharp.Core
220+
Argu
221+
222+
group Other1
223+
FSharp.Core
224+
FsCheck
225+
"""
226+
227+
let lockFile = LockFile.Parse("", toLines lockFile)
228+
229+
let refFile = ReferencesFile.FromLines(toLines refFileContent)
230+
231+
let packages =
232+
[ for kv in refFile.Groups do
233+
let packagesInGroup,_ = lockFile.GetOrderedPackageHull(kv.Key, refFile)
234+
yield! packagesInGroup ]
235+
236+
let outPath = System.IO.Path.GetTempFileName()
237+
Paket.RestoreProcess.createPaketPropsFile lockFile Seq.empty packages (FileInfo outPath)
238+
239+
let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)
240+
241+
let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList
242+
243+
match itemGroups with
244+
| [groupMain; otherGroup] ->
245+
groupMain
246+
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
247+
groupMain
248+
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
249+
otherGroup
250+
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
251+
otherGroup
252+
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
253+
| l ->
254+
Assert.Fail(sprintf "expected two ItemGroup but was '%A'" l)
255+
256+
[<Test>]
257+
let ``should create props file for design mode with group restrictions and conditions``() =
258+
259+
let lockFile = """CONDITION: COND_MAIN
260+
RESTRICTION: && (>= net461) (< net47)
261+
NUGET
262+
remote: https://api.nuget.org/v3/index.json
263+
Argu (4.2.1)
264+
FSharp.Core (>= 3.1.2)
265+
FSharp.Core (3.1.2.5)
266+
267+
GROUP Other1
268+
CONDITION: COND_OTHER1
269+
RESTRICTION: == netstandard2.0
270+
NUGET
271+
remote: https://api.nuget.org/v3/index.json
272+
FsCheck (2.8.2)
273+
FSharp.Core (>= 3.1.2.5)
274+
FSharp.Core (4.3.4)
275+
"""
276+
277+
let refFileContent = """
278+
FSharp.Core
279+
Argu
280+
281+
group Other1
282+
FSharp.Core
283+
FsCheck
284+
"""
285+
286+
let lockFile = LockFile.Parse("", toLines lockFile)
287+
288+
let refFile = ReferencesFile.FromLines(toLines refFileContent)
289+
290+
let packages =
291+
[ for kv in refFile.Groups do
292+
let packagesInGroup,_ = lockFile.GetOrderedPackageHull(kv.Key, refFile)
293+
yield! packagesInGroup ]
294+
295+
let outPath = System.IO.Path.GetTempFileName()
296+
Paket.RestoreProcess.createPaketPropsFile lockFile Seq.empty packages (FileInfo outPath)
297+
298+
let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)
299+
300+
let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList
301+
302+
match itemGroups with
303+
| [groupMain; otherGroup] ->
304+
groupMain
305+
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
306+
groupMain
307+
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
308+
otherGroup
309+
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other1"].Options.Settings.FrameworkRestrictions
194310
otherGroup
195311
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4"; "FsCheck","2.8.2" ]
196312
| l ->
@@ -243,17 +359,17 @@ group Other2
243359
match itemGroups with
244360
| [groupMain; otherGroup20And21; otherGroupOnly21] ->
245361
groupMain
246-
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
362+
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.FrameworkRestrictions
247363
groupMain
248364
|> checkContainsPackageRefs [ "FSharp.Core","3.1.2.5"; "Argu","4.2.1" ]
249365

250366
otherGroup20And21
251-
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.FrameworkRestrictions
367+
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.FrameworkRestrictions
252368
otherGroup20And21
253369
|> checkContainsPackageRefs [ "FSharp.Core","4.3.4" ]
254370

255371
otherGroupOnly21
256-
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Resolution.[Domain.PackageName "FsCheck"].Settings.FrameworkRestrictions
372+
|> checkTargetFrameworkRestriction lockFile.Groups.[Domain.GroupName "Other2"].Options.Settings.ReferenceCondition lockFile.Groups.[Domain.GroupName "Other2"].Resolution.[Domain.PackageName "FsCheck"].Settings.FrameworkRestrictions
257373
otherGroupOnly21
258374
|> checkContainsPackageRefs [ "FsCheck","2.8.2" ]
259375
| l ->
@@ -286,11 +402,11 @@ Newtonsoft.Json
286402
let doc = XDocument.Load(outPath, LoadOptions.PreserveWhitespace)
287403

288404
let itemGroups = doc.Root.Elements (xname "ItemGroup") |> Seq.toList
289-
405+
290406
match itemGroups with
291407
| [groupMain] ->
292408
groupMain
293-
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Resolution.[Domain.PackageName "Newtonsoft.Json"].Settings.FrameworkRestrictions
409+
|> checkTargetFrameworkRestriction lockFile.Groups.[Constants.MainDependencyGroup].Options.Settings.ReferenceCondition lockFile.Groups.[Constants.MainDependencyGroup].Resolution.[Domain.PackageName "Newtonsoft.Json"].Settings.FrameworkRestrictions
294410
groupMain
295411
|> checkContainsPackageRefs [ "Newtonsoft.Json","11.0.2" ]
296412
| l ->

0 commit comments

Comments
 (0)