-
-
Notifications
You must be signed in to change notification settings - Fork 177
Add Central Package Management (CPM) support to nbgv install command #1208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -382,23 +382,61 @@ private static async Task<int> OnInstallCommand(string path, string version, str | |
| const string PrivateAssetsMetadataName = "PrivateAssets"; | ||
| const string VersionMetadataName = "Version"; | ||
|
|
||
| // Check if Central Package Management is enabled | ||
| bool isCpmEnabled = IsCentralPackageManagementEnabled(path); | ||
|
|
||
| if (isCpmEnabled) | ||
| { | ||
| // Update Directory.Packages.props with PackageVersion | ||
| UpdateDirectoryPackagesProps(path, PackageId, packageVersion); | ||
| string directoryPackagesPropsPath = Path.Combine(path, "Directory.Packages.props"); | ||
| context.Stage(directoryPackagesPropsPath); | ||
| } | ||
|
|
||
| ProjectItemElement item = propsFile.Items.FirstOrDefault(i => i.ItemType == PackageReferenceItemType && i.Include == PackageId); | ||
|
|
||
| if (item is null) | ||
| { | ||
| var metadata = new Dictionary<string, string> | ||
| { | ||
| { PrivateAssetsMetadataName, "all" }, | ||
| }; | ||
|
|
||
| // Only add Version if CPM is not enabled | ||
| if (!isCpmEnabled) | ||
| { | ||
| metadata[VersionMetadataName] = packageVersion; | ||
| } | ||
|
|
||
| item = propsFile.AddItem( | ||
| PackageReferenceItemType, | ||
| PackageId, | ||
| new Dictionary<string, string> | ||
| { | ||
| { PrivateAssetsMetadataName, "all" }, | ||
| { VersionMetadataName, packageVersion }, | ||
| }); | ||
| metadata); | ||
| } | ||
| else | ||
| { | ||
| ProjectMetadataElement versionMetadata = item.Metadata.Single(m => m.Name == VersionMetadataName); | ||
| versionMetadata.Value = packageVersion; | ||
| if (isCpmEnabled) | ||
| { | ||
| // Remove Version metadata if CPM is enabled | ||
| ProjectMetadataElement versionMetadata = item.Metadata.FirstOrDefault(m => m.Name == VersionMetadataName); | ||
| if (versionMetadata is not null) | ||
| { | ||
| item.RemoveChild(versionMetadata); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Update Version metadata if CPM is not enabled | ||
| ProjectMetadataElement versionMetadata = item.Metadata.FirstOrDefault(m => m.Name == VersionMetadataName); | ||
| if (versionMetadata is not null) | ||
| { | ||
| versionMetadata.Value = packageVersion; | ||
| } | ||
| else | ||
| { | ||
| item.AddMetadata(VersionMetadataName, packageVersion); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| item.Condition = "!Exists('packages.config')"; | ||
|
|
@@ -866,6 +904,56 @@ private static async Task<string> GetLatestPackageVersionAsync(string packageId, | |
| return pkg.LatestVersion?.ToNormalizedString(); | ||
| } | ||
|
|
||
| private static bool IsCentralPackageManagementEnabled(string path) | ||
| { | ||
| string directoryPackagesPropsPath = Path.Combine(path, "Directory.Packages.props"); | ||
| if (!File.Exists(directoryPackagesPropsPath)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
Comment on lines
+907
to
+916
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existence of Directory.Packages.props does not necessarily mean that central package management is enabled. The ManagePackageVersionsCentrally property might be set in neither Directory.Packages.props nor Directory.Build.props.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the property must be set. And Copilot had originally included that check, but it was only looking in Directory.Packages.props for it, which as you say, would be an incomplete check. |
||
|
|
||
| private static void UpdateDirectoryPackagesProps(string path, string packageId, string packageVersion) | ||
| { | ||
| string directoryPackagesPropsPath = Path.Combine(path, "Directory.Packages.props"); | ||
| ProjectRootElement propsFile = File.Exists(directoryPackagesPropsPath) | ||
| ? ProjectRootElement.Open(directoryPackagesPropsPath) | ||
| : ProjectRootElement.Create(directoryPackagesPropsPath); | ||
|
|
||
| const string PackageVersionItemType = "PackageVersion"; | ||
| const string VersionMetadataName = "Version"; | ||
|
|
||
| ProjectItemElement item = propsFile.Items.FirstOrDefault(i => | ||
| i.ItemType == PackageVersionItemType && i.Include == packageId); | ||
|
|
||
| if (item is null) | ||
| { | ||
| item = propsFile.AddItem( | ||
| PackageVersionItemType, | ||
| packageId, | ||
| new Dictionary<string, string> | ||
| { | ||
| { VersionMetadataName, packageVersion }, | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| ProjectMetadataElement versionMetadata = item.Metadata.FirstOrDefault(m => m.Name == VersionMetadataName); | ||
| if (versionMetadata is not null) | ||
| { | ||
| versionMetadata.Value = packageVersion; | ||
| } | ||
| else | ||
| { | ||
| item.AddMetadata(VersionMetadataName, packageVersion); | ||
| } | ||
| } | ||
|
|
||
| propsFile.Save(directoryPackagesPropsPath); | ||
| } | ||
|
|
||
| private static string GetSpecifiedOrCurrentDirectoryPath(string versionJsonRoot) | ||
| { | ||
| return ShouldHaveTrailingDirectorySeparator(Path.GetFullPath(string.IsNullOrEmpty(versionJsonRoot) ? "." : versionJsonRoot)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't metadata names in MSBuild case-insensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. I didn't catch that. I'll send a follow-up PR to fix. Thanks.