Skip to content

Commit

Permalink
Merge pull request #35 from farmergreg/fixes
Browse files Browse the repository at this point in the history
Let the user know that they need to specify a directory
  • Loading branch information
Webreaper authored Apr 9, 2024
2 parents 3c9cfe4 + d52e6b5 commit 0e2b382
Showing 1 changed file with 27 additions and 20 deletions.
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 @@ public void ProcessConversion(CommandLineOptions o)
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 @@ public void ProcessConversion(CommandLineOptions o)
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 @@ private void RevertProject(FileInfo project, bool dryRun, Encoding encoding, str

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 @@ private void RevertProject(FileInfo project, bool dryRun, Encoding encoding, str
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 @@ private void ReadDirectoryPackagePropsFile(string packageConfigPath)
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 @@ private void ReadDirectoryPackagePropsFile(string packageConfigPath)
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 @@ private void WriteDirectoryPackagesConfig(string packageConfigPath, bool dryRun,
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 IEnumerable<XElement> GetDescendants(XDocument xml, string name)
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 @@ private static void RemoveAttributes(XElement elem, string name)
.Where(x => string.Equals(x.Name.LocalName, name, StringComparison.OrdinalIgnoreCase))
.ToList();

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

/// <summary>
Expand Down Expand Up @@ -349,7 +356,7 @@ private void ConvertProject(FileInfo csprojFile, bool dryRun, Encoding encoding,
// 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

0 comments on commit 0e2b382

Please sign in to comment.