Skip to content
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

Let the user know that they need to specify a directory #35

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions CentralisedPackageConverter/PackageConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
if (o.DryRun)
Console.WriteLine("Dry run enabled - no changes will be made on disk.");

if (File.Exists(o.RootDirectory))
{
Console.WriteLine("Please specifiy a directory to scan instead of a file.");
Console.WriteLine("Aborting...");
return;
}

var rootDir = new DirectoryInfo(o.RootDirectory);

// Find all the csproj files to process
Expand All @@ -56,11 +63,11 @@
return;
}
}

if (o.Revert)
{
ReadDirectoryPackagePropsFile(packageConfigPath);

projects.ForEach(proj => RevertProject(proj, o.DryRun, encoding, linewrap));

if (!o.DryRun)
Expand Down Expand Up @@ -103,9 +110,9 @@

var needToWriteChanges = false;

foreach (var packageReference in packagesReferences )
foreach (var packageReference in packagesReferences)
{
if( packageReference.Parent is not null )
if (packageReference.Parent is not null)
{
var condition = GetAttributeValue( packageReference.Parent, "Condition" ) ?? string.Empty;
if (!TryGetAttributeValue(packageReference, "Include", out var package))
Expand All @@ -116,27 +123,27 @@
continue;
}
}

if( this.referencesByConditionThenName.TryGetValue( condition, out var packagesByName ) )
if (this.referencesByConditionThenName.TryGetValue(condition, out var packagesByName))
{
if( packagesByName.TryGetValue( package, out var version ) )
if (packagesByName.TryGetValue(package, out var version))

Check warning on line 129 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, NuGetVersion>.TryGetValue(string key, out NuGetVersion value)'.

Check warning on line 129 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, NuGetVersion>.TryGetValue(string key, out NuGetVersion value)'.

Check warning on line 129 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, NuGetVersion>.TryGetValue(string key, out NuGetVersion value)'.

Check warning on line 129 in CentralisedPackageConverter/PackageConverter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'key' in 'bool Dictionary<string, NuGetVersion>.TryGetValue(string key, out NuGetVersion value)'.
{
packageReference.SetAttributeValue( "Version", version );
packageReference.SetAttributeValue("Version", version);
needToWriteChanges = true;
}
else
{
Console.WriteLine( $"No version found in {s_DirPackageProps} file for {package}! Skipping..." );
Console.WriteLine($"No version found in {s_DirPackageProps} file for {package}! Skipping...");
}
}
else
{
Console.WriteLine( $"No condition found in {s_DirPackageProps} file for {condition}! Skipping..." );
Console.WriteLine($"No condition found in {s_DirPackageProps} file for {condition}! Skipping...");
}
}
else
{
Console.WriteLine( "Package reference does not have parent. Skipping..." );
Console.WriteLine("Package reference does not have parent. Skipping...");
}
}

Expand All @@ -156,7 +163,7 @@
var xml = XDocument.Load(packageConfigPath);

var packageVersions = GetDescendants(xml, "PackageVersion");

foreach (var packageVersion in packageVersions)
{
var package = GetAttributeValue(packageVersion, "Include") ??
Expand All @@ -169,10 +176,10 @@
packagesByName = new Dictionary<string, NuGetVersion>(StringComparer.OrdinalIgnoreCase);
this.referencesByConditionThenName[condition] = packagesByName;
}

packagesByName[package] = version;
}

Console.WriteLine("Read {0} references from {1}", this.referencesByConditionThenName.Count, packageConfigPath);
}

Expand All @@ -194,9 +201,9 @@
foreach (var byConditionNames in this.referencesByConditionThenName)
{
var condition = byConditionNames.Key;
var referencesByName= byConditionNames.Value;
var referencesByName = byConditionNames.Value;
lines.Add(string.IsNullOrEmpty(condition)
? " <ItemGroup>"
? " <ItemGroup>"
: $" <ItemGroup Condition=\"{condition}\">");

foreach (var packageAndVersion in referencesByName.OrderBy(x => x.Key))
Expand Down Expand Up @@ -245,8 +252,8 @@
private static string? GetAttributeValue(XElement? elem, string name)
{
// Use case-insensitive attribute lookup
var attr = elem?.Attributes().FirstOrDefault( x =>
string.Equals( x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase ));
var attr = elem?.Attributes().FirstOrDefault(x =>
string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase));

return attr?.Value;
}
Expand Down Expand Up @@ -279,7 +286,7 @@
.Where(x => string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase))
.ToList();

attr.Remove();
attr.Remove();
}

/// <summary>
Expand Down Expand Up @@ -349,7 +356,7 @@
// TODO change to self closing?
}
}

// If there is only an Update attribute left, and no child elements, then this node
// isn't useful any more, so we can remove it entirely
if (removeNodeIfEmpty && packageReference.Attributes().Count() == 1 && !packageReference.Elements().Any())
Expand Down
Loading