Skip to content

Commit cc88034

Browse files
committed
Implement group conditions for SDK-style projects
1 parent eed370a commit cc88034

File tree

6 files changed

+191
-28
lines changed

6 files changed

+191
-28
lines changed

src/Paket.Core/Installation/RestoreProcess.fs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -321,20 +321,21 @@ let createPaketPropsFile (lockFile:LockFile) (cliTools:ResolvedPackage seq) (ref
321321
| ExplicitRestriction fw -> ExplicitRestriction fw
322322
| _ -> group.Options.Settings.FrameworkRestrictions
323323
let condition = getExplicitRestriction restrictions
324-
p,condition,packageSettings)
325-
|> Seq.groupBy (fun (_,c,__) -> c)
326-
|> Seq.collect (fun (condition,packages) ->
327-
let condition =
324+
p,condition,packageSettings,group.Options.Settings.ReferenceCondition)
325+
|> Seq.groupBy (fun (_,c,__,rc) -> c,rc)
326+
|> Seq.collect (fun ((condition,referenceCondition),packages) ->
327+
let targets =
328328
match condition with
329-
| FrameworkRestriction.HasNoRestriction -> ""
330-
| restrictions -> restrictions.ToMSBuildCondition()
329+
| FrameworkRestriction.HasNoRestriction -> Set.empty
330+
| restrictions -> restrictions.RepresentedFrameworks
331+
let condition = PlatformMatching.getCondition referenceCondition targets
331332
let condition =
332333
if condition = "" || condition = "true" then "" else
333334
sprintf " AND (%s)" condition
334335

335336
let packageReferences =
336337
packages
337-
|> Seq.collect (fun (p,_,packageSettings) ->
338+
|> Seq.collect (fun (p,_,packageSettings, __) ->
338339
let directReferenceCondition =
339340
if not(allDirectPackages.Contains p.Name) then
340341
"Condition=\" '$(ManagePackageVersionsCentrally)' != 'true' \""
@@ -350,16 +351,13 @@ let createPaketPropsFile (lockFile:LockFile) (cliTools:ResolvedPackage seq) (ref
350351
match excludeAssets with
351352
| [] -> ()
352353
| tags -> yield sprintf """ <ExcludeAssets>%s</ExcludeAssets>""" (tags |> String.concat ";")
353-
354354
match combineCopyLocal p.Settings packageSettings with
355355
| Some true -> yield sprintf """ <PrivateAssets>All</PrivateAssets>"""
356356
| _ -> ()
357-
358357
yield """ </PackageReference>"""])
359-
360358
let packageVersions =
361359
packages
362-
|> Seq.collect (fun (p,_,__) ->
360+
|> Seq.collect (fun (p,_,__,___) ->
363361
[yield sprintf """ <PackageVersion Include="%O">""" p.Name
364362
yield sprintf """ <Version>%O</Version>""" p.Version
365363
yield """ </PackageVersion>"""])
@@ -472,6 +470,7 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
472470
let combinedOmitContent = combineOmitContent resolvedPackage.Settings packageSettings
473471
let combinedImportTargets = combineImportTargets resolvedPackage.Settings packageSettings
474472
let aliases = if direct then packageSettings.Settings.Aliases |> Seq.tryHead else None
473+
let condition = kv.Value.Options.Settings.ReferenceCondition |> Option.defaultValue "true"
475474

476475
let privateAssetsAll =
477476
match combinedCopyLocal with
@@ -506,7 +505,8 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
506505
copyLocal
507506
omitContent
508507
importTargets
509-
alias]
508+
alias
509+
condition]
510510
|> String.concat ","
511511

512512
list.Add line

src/Paket.Core/PublicAPI.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,13 @@ type Dependencies(dependenciesFileName: string) =
600600
|> Seq.map (fun kv -> kv.Key.ToString())
601601
|> Seq.toList
602602

603+
member __.GetConditions(): string list =
604+
let dependenciesFile = DependenciesFile.ReadFromFile dependenciesFileName
605+
dependenciesFile.Groups
606+
|> Seq.choose (fun kv -> kv.Value.Options.Settings.ReferenceCondition)
607+
|> Seq.distinct
608+
|> Seq.toList
609+
603610
/// Returns the direct dependencies for the given package.
604611
member this.GetDirectDependenciesForPackage(groupName,packageName:string): (string * string * string) list =
605612
let resolvedPackages = this.GetLockFile().GetGroupedResolution()
@@ -855,6 +862,9 @@ type Dependencies(dependenciesFileName: string) =
855862
doc.Save fileStream
856863

857864
static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list) =
865+
Dependencies.FixNuspecs (projectFile, referencesFile, nuspecFileList, [])
866+
867+
static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list, conditions:string list) =
858868
let attr (name: string) (node: XmlNode) =
859869
match node.Attributes.[name] with
860870
| null -> None
@@ -880,8 +890,14 @@ type Dependencies(dependenciesFileName: string) =
880890
|> List.map (fun proj -> proj.NameWithoutExtension)
881891
|> Set.ofList
882892
let depsFile = deps.GetDependenciesFile()
893+
let groupMatchesConditions groupName =
894+
let group = depsFile.GetGroup(groupName)
895+
match group.Options.Settings.ReferenceCondition with
896+
| None -> true
897+
| Some condition -> conditions |> List.contains condition
883898
let allFrameworkRestrictions =
884899
locked.GetPackageHull referencesFile
900+
|> Seq.filter (fun kvp -> fst kvp.Key |> groupMatchesConditions)
885901
|> Seq.map(fun kvp -> snd kvp.Key, fst kvp.Key, kvp.Value.Settings.FrameworkRestrictions.GetExplicitRestriction())
886902

887903

@@ -898,6 +914,7 @@ type Dependencies(dependenciesFileName: string) =
898914

899915
let projectReferencedDeps =
900916
referencesFile.Groups
917+
|> Seq.filter (fun (KeyValue(group, _)) -> group |> groupMatchesConditions)
901918
|> Seq.collect (fun (KeyValue(group, packages)) -> packages.NugetPackages |> Seq.map (fun p -> group, p))
902919

903920
let groupsForProjectReferencedDeps =

src/Paket.Core/embedded/Paket.Restore.targets

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@
241241
<OmitContent Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 7">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[6])</OmitContent>
242242
<ImportTargets Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 8">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[7])</ImportTargets>
243243
<Aliases Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 9">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[8])</Aliases>
244+
<ReferenceCondition Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 10">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[9])</ReferenceCondition>
244245
</PaketReferencesFileLinesInfo>
245-
<PackageReference Condition=" '$(ManagePackageVersionsCentrally)' != 'true' Or '%(PaketReferencesFileLinesInfo.Reference)' == 'Direct' " Include="%(PaketReferencesFileLinesInfo.PackageName)">
246+
<PackageReference Condition=" ('$(ManagePackageVersionsCentrally)' != 'true' Or '%(PaketReferencesFileLinesInfo.Reference)' == 'Direct') AND ('%(PaketReferencesFileLinesInfo.ReferenceCondition)' == 'true' Or $(%(PaketReferencesFileLinesInfo.ReferenceCondition)) == 'true')" Include="%(PaketReferencesFileLinesInfo.PackageName)">
246247
<Version Condition=" '$(ManagePackageVersionsCentrally)' != 'true' ">%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
247248
<PrivateAssets Condition=" ('%(PaketReferencesFileLinesInfo.AllPrivateAssets)' == 'true') Or ('$(PackAsTool)' == 'true') ">All</PrivateAssets>
248249
<ExcludeAssets Condition=" %(PaketReferencesFileLinesInfo.CopyLocal) == 'false' or %(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
@@ -251,10 +252,8 @@
251252
<Aliases Condition=" %(PaketReferencesFileLinesInfo.Aliases) != ''">%(PaketReferencesFileLinesInfo.Aliases)</Aliases>
252253
<Publish Condition=" '$(PackAsTool)' == 'true' ">true</Publish>
253254
<AllowExplicitVersion>true</AllowExplicitVersion>
254-
255255
</PackageReference>
256-
257-
<PackageVersion Include="%(PaketReferencesFileLinesInfo.PackageName)">
256+
<PackageVersion Condition="('$(ManagePackageVersionsCentrally)' != 'true' Or '%(PaketReferencesFileLinesInfo.Reference)' == 'Direct') AND ('%(PaketReferencesFileLinesInfo.ReferenceCondition)' == 'true' Or $(%(PaketReferencesFileLinesInfo.ReferenceCondition)) == 'true')" Include="%(PaketReferencesFileLinesInfo.PackageName)">
258257
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
259258
</PackageVersion>
260259
</ItemGroup>
@@ -319,7 +318,17 @@
319318
</ItemGroup>
320319

321320
<Error Text="Error Because of PAKET_ERROR_ON_MSBUILD_EXEC (not calling fix-nuspecs)" Condition=" '$(PAKET_ERROR_ON_MSBUILD_EXEC)' == 'true' " />
322-
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" ' />
321+
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) show-conditions -s' ConsoleToMSBuild="true" StandardOutputImportance="low">
322+
<Output TaskParameter="ConsoleOutput" ItemName="_ConditionProperties"/>
323+
</Exec>
324+
<ItemGroup>
325+
<_DefinedConditionProperties Include="@(_ConditionProperties)" Condition="$(%(Identity)) == 'true'"/>
326+
</ItemGroup>
327+
<PropertyGroup>
328+
<_ConditionsParameter></_ConditionsParameter>
329+
<_ConditionsParameter Condition="@(_DefinedConditionProperties) != ''">--conditions @(_DefinedConditionProperties)</_ConditionsParameter>
330+
</PropertyGroup>
331+
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" $(_ConditionsParameter)' />
323332
<Error Condition="@(_NuspecFiles) == ''" Text='Could not find nuspec files in "$(AdjustedNuspecOutputPath)" (Version: "$(PackageVersion)"), therefore we cannot call "paket fix-nuspecs" and have to error out!' />
324333

325334
<ConvertToAbsolutePath Condition="@(_NuspecFiles) != ''" Paths="@(_NuspecFiles)">

src/Paket/Commands.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,15 @@ type FixNuspecsArgs =
412412
| [<ExactlyOnce;CustomCommandLine("files")>] Files of nuspecPaths:string list
413413
| [<CustomCommandLine("references-file")>] ReferencesFile of referencePath:string
414414
| [<CustomCommandLine("project-file")>] ProjectFile of referencePath:string
415+
| [<Unique>] Conditions of conditions:string list
415416
with
416417
interface IArgParserTemplate with
417418
member this.Usage =
418419
match this with
419420
| Files _ -> ".nuspec files to fix transitive dependencies within"
420421
| ReferencesFile _ -> "paket.references to use"
421422
| ProjectFile _ -> "the project file to use"
423+
| Conditions _ -> "group conditions to filter by"
422424

423425
type GenerateNuspecArgs =
424426
| [<ExactlyOnce;CustomCommandLine "project">] Project of project:string
@@ -451,6 +453,12 @@ with
451453
interface IArgParserTemplate with
452454
member __.Usage = ""
453455

456+
type ShowConditionsArgs =
457+
| [<Hidden;NoCommandLine>] NoArgs
458+
with
459+
interface IArgParserTemplate with
460+
member __.Usage = ""
461+
454462
type FindPackageVersionsArgs =
455463
| [<ExactlyOnce;MainCommand>] NuGet of package_ID:string
456464
| [<Hidden;ExactlyOnce;CustomCommandLine("nuget", "name")>] NuGet_Legacy of package_ID:string
@@ -696,6 +704,7 @@ type Command =
696704
| [<CustomCommandLine("generate-nuspec")>] GenerateNuspec of ParseResults<GenerateNuspecArgs>
697705
| [<CustomCommandLine("show-installed-packages")>] ShowInstalledPackages of ParseResults<ShowInstalledPackagesArgs>
698706
| [<CustomCommandLine("show-groups")>] ShowGroups of ParseResults<ShowGroupsArgs>
707+
| [<CustomCommandLine("show-conditions")>] ShowConditions of ParseResults<ShowConditionsArgs>
699708
| [<CustomCommandLine("pack")>] Pack of ParseResults<PackArgs>
700709
| [<CustomCommandLine("push")>] Push of ParseResults<PushArgs>
701710
| [<Hidden;CustomCommandLine("generate-include-scripts")>] GenerateIncludeScripts of ParseResults<GenerateLoadScriptsArgs>
@@ -729,6 +738,7 @@ with
729738
| GenerateNuspec _ -> "generate a default nuspec for a project including its direct dependencies"
730739
| ShowInstalledPackages _ -> "show installed top-level packages"
731740
| ShowGroups _ -> "show groups"
741+
| ShowConditions _ -> "show conditions defined on groups"
732742
| Pack _ -> "create NuGet packages from paket.template files"
733743
| Push _ -> "push a NuGet package"
734744
| GenerateIncludeScripts _ -> "[obsolete]"

src/Paket/Program.fs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ let pack (results : ParseResults<_>) =
577577
|> legacyBool results (ReplaceArgument("--symbols", "symbols"))
578578
let includeReferencedProjects =
579579
(results.Contains PackArgs.Include_Referenced_Projects,
580-
results.Contains PackArgs.Include_Referenced_Projects_Legacy)
580+
results.Contains PackArgs.Include_Referenced_Projects_Legacy)
581581
|> legacyBool results (ReplaceArgument("--include-referenced-projects", "Include_Referenced_Projects"))
582582
let projectUrl =
583583
(results.TryGetResult PackArgs.Project_Url,
@@ -680,12 +680,17 @@ let fixNuspecs silent (results : ParseResults<_>) =
680680
results.GetResult FixNuspecsArgs.Files
681681
|> List.collect (fun s -> s.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList)
682682
|> List.map (fun s -> s.Trim())
683+
let conditions =
684+
results.TryGetResult FixNuspecsArgs.Conditions
685+
|> Option.defaultValue []
686+
|> List.collect (fun s -> s.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList)
687+
|> List.map (fun s -> s.Trim())
683688

684689
match results.TryGetResult FixNuspecsArgs.ProjectFile with
685690
| Some projectFile ->
686691
let projectFile = Paket.ProjectFile.LoadFromFile(projectFile)
687692
let refFile = RestoreProcess.FindOrCreateReferencesFile projectFile
688-
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles)
693+
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles, conditions)
689694
| None ->
690695
match results.TryGetResult FixNuspecsArgs.ReferencesFile with
691696
| Some referenceFile ->
@@ -736,6 +741,11 @@ let showGroups (results : ParseResults<ShowGroupsArgs>) =
736741
for groupName in dependenciesFile.GetGroups() do
737742
tracefn "%s" groupName
738743

744+
let showConditions (results : ParseResults<ShowConditionsArgs>) =
745+
let dependenciesFile = Dependencies.Locate()
746+
for condition in dependenciesFile.GetConditions() do
747+
tracefn "%s" condition
748+
739749
let findPackageVersions (results : ParseResults<_>) =
740750
let maxResults =
741751
let arg = (results.TryGetResult FindPackageVersionsArgs.Max_Results,
@@ -894,6 +904,7 @@ let handleCommand silent command =
894904
| FixNuspecs r -> processCommand silent (fixNuspecs silent) r
895905
| ShowInstalledPackages r -> processCommand silent showInstalledPackages r
896906
| ShowGroups r -> processCommand silent showGroups r
907+
| ShowConditions r -> processCommand silent showConditions r
897908
| Pack r -> processCommand silent pack r
898909
| Push r -> processCommand silent (push paketVersion) r
899910
| GenerateIncludeScripts r ->

0 commit comments

Comments
 (0)