Skip to content

Commit 13673a3

Browse files
authored
Merge pull request #711 from asherber/smarter-install
2 parents 0e7febc + dbd3cd8 commit 13673a3

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

src/nbgv/Program.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private static int OnInstallCommand(string path, string version, IReadOnlyList<s
299299
var existingOptions = context.VersionFile.GetVersion();
300300
if (existingOptions is not null)
301301
{
302-
if (!string.IsNullOrEmpty(version))
302+
if (!string.IsNullOrEmpty(version) && version != DefaultVersionSpec)
303303
{
304304
var setVersionExitCode = OnSetVersionCommand(path, version);
305305
if (setVersionExitCode != (int)ExitCodes.OK)
@@ -320,43 +320,53 @@ private static int OnInstallCommand(string path, string version, IReadOnlyList<s
320320
? ProjectRootElement.Open(directoryBuildPropsPath)
321321
: ProjectRootElement.Create(directoryBuildPropsPath);
322322

323-
const string PackageReferenceItemType = "PackageReference";
324-
if (!propsFile.Items.Any(i => i.ItemType == PackageReferenceItemType && i.Include == PackageId))
323+
// Validate given sources
324+
foreach (var src in source)
325325
{
326-
// Validate given sources
327-
foreach (var src in source)
326+
// TODO: Can declare Option<Uri> to validate argument during parsing.
327+
if (!Uri.TryCreate(src, UriKind.Absolute, out var _))
328328
{
329-
// TODO: Can declare Option<Uri> to validate argument during parsing.
330-
if (!Uri.TryCreate(src, UriKind.Absolute, out var _))
331-
{
332-
Console.Error.WriteLine($"\"{src}\" is not a valid NuGet package source.");
333-
return (int)ExitCodes.InvalidNuGetPackageSource;
334-
}
329+
Console.Error.WriteLine($"\"{src}\" is not a valid NuGet package source.");
330+
return (int)ExitCodes.InvalidNuGetPackageSource;
335331
}
332+
}
336333

337-
string packageVersion = GetLatestPackageVersionAsync(PackageId, path, source).GetAwaiter().GetResult();
338-
if (string.IsNullOrEmpty(packageVersion))
339-
{
340-
string verifyPhrase = source.Any()
341-
? "Please verify the given 'source' option(s)."
342-
: "Please verify the package sources in the NuGet.Config files.";
343-
Console.Error.WriteLine($"Latest stable version of the {PackageId} package could not be determined. " + verifyPhrase);
344-
return (int)ExitCodes.PackageIdNotFound;
345-
}
334+
string packageVersion = GetLatestPackageVersionAsync(PackageId, path, source).GetAwaiter().GetResult();
335+
if (string.IsNullOrEmpty(packageVersion))
336+
{
337+
string verifyPhrase = source.Any()
338+
? "Please verify the given 'source' option(s)."
339+
: "Please verify the package sources in the NuGet.Config files.";
340+
Console.Error.WriteLine($"Latest stable version of the {PackageId} package could not be determined. " + verifyPhrase);
341+
return (int)ExitCodes.PackageIdNotFound;
342+
}
346343

347-
var item = propsFile.AddItem(
344+
const string PackageReferenceItemType = "PackageReference";
345+
const string PrivateAssetsMetadataName = "PrivateAssets";
346+
const string VersionMetadataName = "Version";
347+
348+
var item = propsFile.Items.FirstOrDefault(i => i.ItemType == PackageReferenceItemType && i.Include == PackageId);
349+
350+
if (item is null)
351+
{
352+
item = propsFile.AddItem(
348353
PackageReferenceItemType,
349354
PackageId,
350355
new Dictionary<string, string>
351356
{
352-
{ "Version", packageVersion },
353-
{ "PrivateAssets", "all" },
357+
{ PrivateAssetsMetadataName, "all" },
358+
{ VersionMetadataName, packageVersion }
354359
});
355-
item.Condition = "!Exists('packages.config')";
356-
357-
propsFile.Save(directoryBuildPropsPath);
358360
}
361+
else
362+
{
363+
var versionMetadata = item.Metadata.Single(m => m.Name == VersionMetadataName);
364+
versionMetadata.Value = packageVersion;
365+
}
366+
367+
item.Condition = "!Exists('packages.config')";
359368

369+
propsFile.Save(directoryBuildPropsPath);
360370
context.Stage(directoryBuildPropsPath);
361371

362372
return (int)ExitCodes.OK;

0 commit comments

Comments
 (0)