-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for converting projects within sln files on the command…
… line
- Loading branch information
Showing
9 changed files
with
307 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/PackageReferenceVersionToAttributeTool/ProgramCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// <copyright file="ProgramCommandHandler.cs" company="Rami Abughazaleh"> | ||
// Copyright (c) Rami Abughazaleh. All rights reserved. | ||
// </copyright> | ||
|
||
namespace PackageReferenceVersionToAttributeTool | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using PackageReferenceVersionToAttribute; | ||
using SlnParser; | ||
using SlnParser.Contracts; | ||
|
||
/// <summary> | ||
/// Program command handler. | ||
/// </summary> | ||
internal class ProgramCommandHandler | ||
{ | ||
/// <summary> | ||
/// Executes the command logic asynchronously based on the provided command line options. | ||
/// </summary> | ||
/// <param name="options">An instance of <see cref="ProgramCommandLineOptions"/> containing the parsed options from the command line.</param> | ||
/// <returns>A task representing the asynchronous operation.</returns> | ||
public static async Task HandleAsync(ProgramCommandLineOptions options) | ||
{ | ||
if (options.Version) | ||
{ | ||
var version = Assembly.GetExecutingAssembly().GetName().Version; | ||
Console.WriteLine($"Version: {version}"); | ||
return; | ||
} | ||
|
||
foreach (var input in options.Inputs) | ||
{ | ||
await ConvertPackageReferencesAsync(input, options); | ||
} | ||
} | ||
|
||
private static async Task ConvertPackageReferencesAsync( | ||
string input, ProjectConverterOptions options) | ||
{ | ||
FilePatternMatcher filePatternMatcher = new(); | ||
|
||
// get matching csproj and sln files | ||
List<string> matchingFiles = filePatternMatcher.GetMatchingFiles(input) | ||
.Where(x => x.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) | ||
|| x.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) | ||
.ToList(); | ||
|
||
// parse sln files to get only csproj files | ||
List<string> projectFiles = GetCsprojFiles(matchingFiles); | ||
if (projectFiles.Count == 0) | ||
{ | ||
Console.WriteLine($"No matching project files found for pattern: {input}"); | ||
return; | ||
} | ||
|
||
if (options.Backup) | ||
{ | ||
Console.WriteLine("Backup option is enabled."); | ||
} | ||
|
||
if (options.Force) | ||
{ | ||
Console.WriteLine("Force option is enabled."); | ||
} | ||
|
||
if (options.DryRun) | ||
{ | ||
Console.WriteLine("Dry run mode is enabled. No changes will be made."); | ||
} | ||
|
||
using var serviceProvider = new ServiceCollection() | ||
.AddSingleton(Microsoft.Extensions.Options.Options.Create(options)) | ||
.AddLogging(configure => configure.AddConsole()) | ||
.AddSingleton<ProjectConverter>() | ||
.AddSingleton<IFileService, FileService>() | ||
.AddSingleton<ISourceControlService, NullSourceControlService>() | ||
.BuildServiceProvider(); | ||
|
||
var projectConverter = serviceProvider.GetRequiredService<ProjectConverter>(); | ||
|
||
await projectConverter.ConvertAsync(projectFiles); | ||
} | ||
|
||
private static List<string> GetCsprojFiles(List<string> files) | ||
{ | ||
var csprojFiles = new List<string>(); | ||
|
||
foreach (var file in files) | ||
{ | ||
if (file.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
csprojFiles.Add(file); | ||
} | ||
else if (file.EndsWith(".sln", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
csprojFiles.AddRange(GetCsprojFiles(file)); | ||
} | ||
} | ||
|
||
return csprojFiles; | ||
} | ||
|
||
private static IEnumerable<string> GetCsprojFiles(string solutionFilePath) | ||
{ | ||
SolutionParser solutionParser = new SolutionParser(); | ||
ISolution solution = solutionParser.Parse(solutionFilePath); | ||
|
||
return solution.AllProjects | ||
.OfType<SolutionProject>() | ||
.Where(x => x.File.FullName.EndsWith(".csproj")) | ||
.Select(x => x.File.FullName); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/PackageReferenceVersionToAttributeTool/ProgramCommandLineOptionsValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// <copyright file="ProgramCommandLineOptionsValidator.cs" company="Rami Abughazaleh"> | ||
// Copyright (c) Rami Abughazaleh. All rights reserved. | ||
// </copyright> | ||
|
||
namespace PackageReferenceVersionToAttributeTool | ||
{ | ||
using System.CommandLine; | ||
using System.CommandLine.Parsing; | ||
using PackageReferenceVersionToAttribute; | ||
|
||
/// <summary> | ||
/// Validates command-line options for the program. | ||
/// </summary> | ||
/// <remarks> | ||
/// Initializes a new instance of the <see cref="ProgramCommandLineOptionsValidator"/> class. | ||
/// </remarks> | ||
/// <param name="backupOption">The backup option.</param> | ||
/// <param name="forceOption">The force option.</param> | ||
/// <param name="dryRunOption">The dry run option.</param> | ||
internal class ProgramCommandLineOptionsValidator( | ||
Option<bool> backupOption, | ||
Option<bool> forceOption, | ||
Option<bool> dryRunOption) | ||
{ | ||
private readonly Option<bool> backupOption = backupOption; | ||
private readonly Option<bool> forceOption = forceOption; | ||
private readonly Option<bool> dryRunOption = dryRunOption; | ||
|
||
/// <summary> | ||
/// Validates the specified <see cref="CommandResult"/>. | ||
/// </summary> | ||
/// <param name="result">The <see cref="CommandResult"/> containing the parsed command-line options.</param> | ||
public void Validate(CommandResult result) | ||
{ | ||
// Extract option values | ||
bool backup = result.GetValueForOption(this.backupOption); | ||
bool force = result.GetValueForOption(this.forceOption); | ||
bool dryRun = result.GetValueForOption(this.dryRunOption); | ||
|
||
var options = new ProjectConverterOptions | ||
{ | ||
Backup = backup, | ||
Force = force, | ||
DryRun = dryRun, | ||
}; | ||
|
||
var validator = new ProjectConverterOptionsValidator(); | ||
var validationResult = validator.Validate(nameof(ProjectConverterOptions), options); | ||
if (validationResult.Failed) | ||
{ | ||
result.ErrorMessage = validationResult.FailureMessage; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.