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 21, 2024
1 parent 4954f9f commit 5cb3a1c
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 22 deletions.
20 changes: 12 additions & 8 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 Expand Up @@ -447,6 +449,7 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
let combinedOmitContent = combineOmitContent resolvedPackage.Settings packageSettings
let combinedImportTargets = combineImportTargets resolvedPackage.Settings packageSettings
let aliases = if direct then packageSettings.Settings.Aliases |> Seq.tryHead else None
let condition = kv.Value.Options.Settings.ReferenceCondition |> Option.defaultValue "true"

let privateAssetsAll =
match combinedCopyLocal with
Expand Down Expand Up @@ -481,7 +484,8 @@ let createProjectReferencesFiles (lockFile:LockFile) (projectFile:ProjectFile) (
copyLocal
omitContent
importTargets
alias]
alias
condition]
|> String.concat ","

list.Add line
Expand Down
16 changes: 15 additions & 1 deletion src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,13 @@ type Dependencies(dependenciesFileName: string) =
|> Seq.map (fun kv -> kv.Key.ToString())
|> Seq.toList

member __.GetConditions(): string list =
let dependenciesFile = DependenciesFile.ReadFromFile dependenciesFileName
dependenciesFile.Groups
|> Seq.choose (fun kv -> kv.Value.Options.Settings.ReferenceCondition)
|> Seq.distinct
|> Seq.toList

/// Returns the direct dependencies for the given package.
member this.GetDirectDependenciesForPackage(groupName,packageName:string): (string * string * string) list =
let resolvedPackages = this.GetLockFile().GetGroupedResolution()
Expand Down Expand Up @@ -854,7 +861,7 @@ type Dependencies(dependenciesFileName: string) =
use fileStream = File.Open (nuspecFile, FileMode.Create)
doc.Save fileStream

static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list) =
static member FixNuspecs (projectFile: ProjectFile, referencesFile:ReferencesFile, nuspecFileList:string list, conditions:string list) =
let attr (name: string) (node: XmlNode) =
match node.Attributes.[name] with
| null -> None
Expand All @@ -880,8 +887,14 @@ type Dependencies(dependenciesFileName: string) =
|> List.map (fun proj -> proj.NameWithoutExtension)
|> Set.ofList
let depsFile = deps.GetDependenciesFile()
let groupMatchesConditions groupName =
let group = depsFile.GetGroup(groupName)
match group.Options.Settings.ReferenceCondition with
| None -> true
| Some condition -> conditions |> List.contains condition
let allFrameworkRestrictions =
locked.GetPackageHull referencesFile
|> Seq.filter (fun kvp -> fst kvp.Key |> groupMatchesConditions)
|> Seq.map(fun kvp -> snd kvp.Key, fst kvp.Key, kvp.Value.Settings.FrameworkRestrictions.GetExplicitRestriction())


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

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

let groupsForProjectReferencedDeps =
Expand Down
16 changes: 13 additions & 3 deletions src/Paket.Core/embedded/Paket.Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@
<OmitContent Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 7">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[6])</OmitContent>
<ImportTargets Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 8">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[7])</ImportTargets>
<Aliases Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 9">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[8])</Aliases>
<ReferenceCondition Condition="%(PaketReferencesFileLinesInfo.Splits) &gt;= 10">$([System.String]::Copy('%(PaketReferencesFileLines.Identity)').Split(',')[9])</ReferenceCondition>
</PaketReferencesFileLinesInfo>
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)" Condition="'%(PaketReferencesFileLinesInfo.ReferenceCondition)' == 'true' Or $(%(PaketReferencesFileLinesInfo.ReferenceCondition)) == 'true'">
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
<PrivateAssets Condition=" ('%(PaketReferencesFileLinesInfo.AllPrivateAssets)' == 'true') Or ('$(PackAsTool)' == 'true') ">All</PrivateAssets>
<ExcludeAssets Condition=" %(PaketReferencesFileLinesInfo.CopyLocal) == 'false' or %(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
Expand All @@ -250,7 +251,6 @@
<Aliases Condition=" %(PaketReferencesFileLinesInfo.Aliases) != ''">%(PaketReferencesFileLinesInfo.Aliases)</Aliases>
<Publish Condition=" '$(PackAsTool)' == 'true' ">true</Publish>
<AllowExplicitVersion>true</AllowExplicitVersion>

</PackageReference>
</ItemGroup>

Expand Down Expand Up @@ -314,7 +314,17 @@
</ItemGroup>

<Error Text="Error Because of PAKET_ERROR_ON_MSBUILD_EXEC (not calling fix-nuspecs)" Condition=" '$(PAKET_ERROR_ON_MSBUILD_EXEC)' == 'true' " />
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" ' />
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) show-conditions -s' ConsoleToMSBuild="true" StandardOutputImportance="low">
<Output TaskParameter="ConsoleOutput" ItemName="_ConditionProperties"/>
</Exec>
<ItemGroup>
<_DefinedConditionProperties Include="@(_ConditionProperties)" Condition="$(%(Identity)) == 'true'"/>
</ItemGroup>
<PropertyGroup>
<_ConditionsParameter></_ConditionsParameter>
<_ConditionsParameter Condition="@(_DefinedConditionProperties) != ''">--conditions @(_DefinedConditionProperties)</_ConditionsParameter>
</PropertyGroup>
<Exec Condition="@(_NuspecFiles) != ''" Command='$(PaketCommand) fix-nuspecs files "@(_NuspecFiles)" project-file "$(PaketProjectFile)" $(_ConditionsParameter)' />
<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!' />

<ConvertToAbsolutePath Condition="@(_NuspecFiles) != ''" Paths="@(_NuspecFiles)">
Expand Down
10 changes: 10 additions & 0 deletions src/Paket/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,15 @@ type FixNuspecsArgs =
| [<ExactlyOnce;CustomCommandLine("files")>] Files of nuspecPaths:string list
| [<CustomCommandLine("references-file")>] ReferencesFile of referencePath:string
| [<CustomCommandLine("project-file")>] ProjectFile of referencePath:string
| [<Unique>] Conditions of conditions:string list
with
interface IArgParserTemplate with
member this.Usage =
match this with
| Files _ -> ".nuspec files to fix transitive dependencies within"
| ReferencesFile _ -> "paket.references to use"
| ProjectFile _ -> "the project file to use"
| Conditions _ -> "group conditions to filter by"

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

type ShowConditionsArgs =
| [<Hidden;NoCommandLine>] NoArgs
with
interface IArgParserTemplate with
member __.Usage = ""

type FindPackageVersionsArgs =
| [<ExactlyOnce;MainCommand>] NuGet of package_ID:string
| [<Hidden;ExactlyOnce;CustomCommandLine("nuget", "name")>] NuGet_Legacy of package_ID:string
Expand Down Expand Up @@ -696,6 +704,7 @@ type Command =
| [<CustomCommandLine("generate-nuspec")>] GenerateNuspec of ParseResults<GenerateNuspecArgs>
| [<CustomCommandLine("show-installed-packages")>] ShowInstalledPackages of ParseResults<ShowInstalledPackagesArgs>
| [<CustomCommandLine("show-groups")>] ShowGroups of ParseResults<ShowGroupsArgs>
| [<CustomCommandLine("show-conditions")>] ShowConditions of ParseResults<ShowConditionsArgs>
| [<CustomCommandLine("pack")>] Pack of ParseResults<PackArgs>
| [<CustomCommandLine("push")>] Push of ParseResults<PushArgs>
| [<Hidden;CustomCommandLine("generate-include-scripts")>] GenerateIncludeScripts of ParseResults<GenerateLoadScriptsArgs>
Expand Down Expand Up @@ -729,6 +738,7 @@ with
| GenerateNuspec _ -> "generate a default nuspec for a project including its direct dependencies"
| ShowInstalledPackages _ -> "show installed top-level packages"
| ShowGroups _ -> "show groups"
| ShowConditions _ -> "show conditions defined on groups"
| Pack _ -> "create NuGet packages from paket.template files"
| Push _ -> "push a NuGet package"
| GenerateIncludeScripts _ -> "[obsolete]"
Expand Down
12 changes: 11 additions & 1 deletion src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,16 @@ let fixNuspecs silent (results : ParseResults<_>) =
results.GetResult FixNuspecsArgs.Files
|> List.collect (fun s -> s.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList)
|> List.map (fun s -> s.Trim())
let conditions =
results.GetResult FixNuspecsArgs.Conditions
|> List.collect (fun s -> s.Split([|';'|], StringSplitOptions.RemoveEmptyEntries) |> Array.toList)
|> List.map (fun s -> s.Trim())

match results.TryGetResult FixNuspecsArgs.ProjectFile with
| Some projectFile ->
let projectFile = Paket.ProjectFile.LoadFromFile(projectFile)
let refFile = RestoreProcess.FindOrCreateReferencesFile projectFile
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles)
Dependencies.FixNuspecs (projectFile, refFile, nuspecFiles, conditions)
| None ->
match results.TryGetResult FixNuspecsArgs.ReferencesFile with
| Some referenceFile ->
Expand Down Expand Up @@ -736,6 +740,11 @@ let showGroups (results : ParseResults<ShowGroupsArgs>) =
for groupName in dependenciesFile.GetGroups() do
tracefn "%s" groupName

let showConditions (results : ParseResults<ShowConditionsArgs>) =
let dependenciesFile = Dependencies.Locate()
for condition in dependenciesFile.GetConditions() do
tracefn "%s" condition

let findPackageVersions (results : ParseResults<_>) =
let maxResults =
let arg = (results.TryGetResult FindPackageVersionsArgs.Max_Results,
Expand Down Expand Up @@ -894,6 +903,7 @@ let handleCommand silent command =
| FixNuspecs r -> processCommand silent (fixNuspecs silent) r
| ShowInstalledPackages r -> processCommand silent showInstalledPackages r
| ShowGroups r -> processCommand silent showGroups r
| ShowConditions r -> processCommand silent showConditions r
| Pack r -> processCommand silent pack r
| Push r -> processCommand silent (push paketVersion) r
| GenerateIncludeScripts r ->
Expand Down
Loading

0 comments on commit 5cb3a1c

Please sign in to comment.