Skip to content

Commit 5cb3a1c

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

File tree

6 files changed

+186
-22
lines changed

6 files changed

+186
-22
lines changed

src/Paket.Core/Installation/RestoreProcess.fs

Lines changed: 12 additions & 8 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 =
@@ -447,6 +449,7 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
447449
let combinedOmitContent = combineOmitContent resolvedPackage.Settings packageSettings
448450
let combinedImportTargets = combineImportTargets resolvedPackage.Settings packageSettings
449451
let aliases = if direct then packageSettings.Settings.Aliases |> Seq.tryHead else None
452+
let condition = kv.Value.Options.Settings.ReferenceCondition |> Option.defaultValue "true"
450453

451454
let privateAssetsAll =
452455
match combinedCopyLocal with
@@ -481,7 +484,8 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
481484
copyLocal
482485
omitContent
483486
importTargets
484-
alias]
487+
alias
488+
condition]
485489
|> String.concat ","
486490

487491
list.Add line

src/Paket.Core/PublicAPI.fs

Lines changed: 15 additions & 1 deletion
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()
@@ -854,7 +861,7 @@ type Dependencies(dependenciesFileName: string) =
854861
use fileStream = File.Open (nuspecFile, FileMode.Create)
855862
doc.Save fileStream
856863

857-
static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list) =
864+
static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list, conditions:string list) =
858865
let attr (name: string) (node: XmlNode) =
859866
match node.Attributes.[name] with
860867
| null -> None
@@ -880,8 +887,14 @@ type Dependencies(dependenciesFileName: string) =
880887
|> List.map (fun proj -> proj.NameWithoutExtension)
881888
|> Set.ofList
882889
let depsFile = deps.GetDependenciesFile()
890+
let groupMatchesConditions groupName =
891+
let group = depsFile.GetGroup(groupName)
892+
match group.Options.Settings.ReferenceCondition with
893+
| None -> true
894+
| Some condition -> conditions |> List.contains condition
883895
let allFrameworkRestrictions =
884896
locked.GetPackageHull referencesFile
897+
|> Seq.filter (fun kvp -> fst kvp.Key |> groupMatchesConditions)
885898
|> Seq.map(fun kvp -> snd kvp.Key, fst kvp.Key, kvp.Value.Settings.FrameworkRestrictions.GetExplicitRestriction())
886899

887900

@@ -898,6 +911,7 @@ type Dependencies(dependenciesFileName: string) =
898911

899912
let projectReferencedDeps =
900913
referencesFile.Groups
914+
|> Seq.filter (fun (KeyValue(group, _)) -> group |> groupMatchesConditions)
901915
|> Seq.collect (fun (KeyValue(group, packages)) -> packages.NugetPackages |> Seq.map (fun p -> group, p))
902916

903917
let groupsForProjectReferencedDeps =

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@
240240
<OmitContent Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 7">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[6])</OmitContent>
241241
<ImportTargets Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 8">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[7])</ImportTargets>
242242
<Aliases Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 9">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[8])</Aliases>
243+
<ReferenceCondition Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 10">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[9])</ReferenceCondition>
243244
</PaketReferencesFileLinesInfo>
244-
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
245+
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)" Condition="'%(PaketReferencesFileLinesInfo.ReferenceCondition)' == 'true' Or $(%(PaketReferencesFileLinesInfo.ReferenceCondition)) == 'true'">
245246
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
246247
<PrivateAssets Condition=" ('%(PaketReferencesFileLinesInfo.AllPrivateAssets)' == 'true') Or ('$(PackAsTool)' == 'true') ">All</PrivateAssets>
247248
<ExcludeAssets Condition=" %(PaketReferencesFileLinesInfo.CopyLocal) == 'false' or %(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
@@ -250,7 +251,6 @@
250251
<Aliases Condition=" %(PaketReferencesFileLinesInfo.Aliases) != ''">%(PaketReferencesFileLinesInfo.Aliases)</Aliases>
251252
<Publish Condition=" '$(PackAsTool)' == 'true' ">true</Publish>
252253
<AllowExplicitVersion>true</AllowExplicitVersion>
253-
254254
</PackageReference>
255255
</ItemGroup>
256256

@@ -314,7 +314,17 @@
314314
</ItemGroup>
315315

316316
<Error Text="Error Because of PAKET_ERROR_ON_MSBUILD_EXEC (not calling fix-nuspecs)" Condition=" '$(PAKET_ERROR_ON_MSBUILD_EXEC)' == 'true' " />
317-
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" ' />
317+
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) show-conditions -s' ConsoleToMSBuild="true" StandardOutputImportance="low">
318+
<Output TaskParameter="ConsoleOutput" ItemName="_ConditionProperties"/>
319+
</Exec>
320+
<ItemGroup>
321+
<_DefinedConditionProperties Include="@(_ConditionProperties)" Condition="$(%(Identity)) == 'true'"/>
322+
</ItemGroup>
323+
<PropertyGroup>
324+
<_ConditionsParameter></_ConditionsParameter>
325+
<_ConditionsParameter Condition="@(_DefinedConditionProperties) != ''">--conditions @(_DefinedConditionProperties)</_ConditionsParameter>
326+
</PropertyGroup>
327+
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" $(_ConditionsParameter)' />
318328
<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!' />
319329

320330
<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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,12 +680,16 @@ 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.GetResult FixNuspecsArgs.Conditions
685+
|> List.collect (fun s -> s.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList)
686+
|> List.map (fun s -> s.Trim())
683687

684688
match results.TryGetResult FixNuspecsArgs.ProjectFile with
685689
| Some projectFile ->
686690
let projectFile = Paket.ProjectFile.LoadFromFile(projectFile)
687691
let refFile = RestoreProcess.FindOrCreateReferencesFile projectFile
688-
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles)
692+
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles, conditions)
689693
| None ->
690694
match results.TryGetResult FixNuspecsArgs.ReferencesFile with
691695
| Some referenceFile ->
@@ -736,6 +740,11 @@ let showGroups (results : ParseResults<ShowGroupsArgs>) =
736740
for groupName in dependenciesFile.GetGroups() do
737741
tracefn "%s" groupName
738742

743+
let showConditions (results : ParseResults<ShowConditionsArgs>) =
744+
let dependenciesFile = Dependencies.Locate()
745+
for condition in dependenciesFile.GetConditions() do
746+
tracefn "%s" condition
747+
739748
let findPackageVersions (results : ParseResults<_>) =
740749
let maxResults =
741750
let arg = (results.TryGetResult FindPackageVersionsArgs.Max_Results,
@@ -894,6 +903,7 @@ let handleCommand silent command =
894903
| FixNuspecs r -> processCommand silent (fixNuspecs silent) r
895904
| ShowInstalledPackages r -> processCommand silent showInstalledPackages r
896905
| ShowGroups r -> processCommand silent showGroups r
906+
| ShowConditions r -> processCommand silent showConditions r
897907
| Pack r -> processCommand silent pack r
898908
| Push r -> processCommand silent (push paketVersion) r
899909
| GenerateIncludeScripts r ->

0 commit comments

Comments
 (0)