Skip to content

Commit

Permalink
Better errors for incorrect usage
Browse files Browse the repository at this point in the history
  • Loading branch information
timabell committed Feb 15, 2024
1 parent 9c4383f commit 88aaaa3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
35 changes: 29 additions & 6 deletions src/CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,42 @@ private class Options

public int Run(string[] args)
{
Console.Out.WriteLine("https://github.com/timabell/sln-items-sync");
Console.Out.WriteLine("https://www.nuget.org/packages/sln-items-sync");
Console.Out.WriteLine("A-GPL v3 Licensed");
Console.Error.WriteLine("https://github.com/timabell/sln-items-sync");
Console.Error.WriteLine("https://www.nuget.org/packages/sln-items-sync");
Console.Error.WriteLine("A-GPL v3 Licensed");
Console.Error.WriteLine();
var parserResult = Parser.Default.ParseArguments<Options>(args);
if (parserResult.Errors.Any())
{
WriteUsage();
return 1;
}

parserResult
.WithParsed(opts =>
_slnSync.SyncSlnFile(slnPath: opts.SlnPath, slnFolder: opts.SlnFolder, opts.Paths));
try
{
parserResult
.WithParsed(opts =>
_slnSync.SyncSlnFile(slnPath: opts.SlnPath, slnFolder: opts.SlnFolder, opts.Paths));
} catch(InvalidSlnPathException ex) {
Console.Error.WriteLine(ex.Message);
return 2;
} catch(PathNotFoundException ex) {
Console.Error.WriteLine(ex.Message);
return 3;
} catch(EmptyPathListException) {
Console.Error.WriteLine("No paths supplied to sync with.");
Console.Error.WriteLine("Supply a list of folder/file arguments that you want to be sync'd into the target solution folder.");
Console.Error.WriteLine();
WriteUsage();
return 4;
}

Console.Out.WriteLine("Sync completed.");
return 0;
}

private static void WriteUsage()
{
Console.Error.WriteLine("Usage: sln-items-sync --solution <SolutionFile.sln> paths-to-sync ...");
}
}
15 changes: 14 additions & 1 deletion src/SlnSync.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.Serialization;
using System.Text;
using SlnEditor.Models;

Expand All @@ -19,6 +20,14 @@ public SlnSync() : this(new DefaultGuidGenerator())
/// <param name="paths">list of paths to recursively add/update SolutionItems virtual folders with</param>
public void SyncSlnFile(string slnPath, string slnFolder, IEnumerable<string> paths)
{
if (!File.Exists(slnPath) || !slnPath.EndsWith(".sln"))
{
throw new InvalidSlnPathException($"'{slnPath}' is not a sln file");
}
if (!paths.Any())
{
throw new EmptyPathListException();
}
var contents = File.ReadAllText(slnPath);
var updatedSln = SyncSlnText(contents, slnFolder, paths);
File.WriteAllText(slnPath, updatedSln, Encoding.UTF8); // explicit UTF-8 to get byte-order marker (which sln files seem to have)
Expand All @@ -42,7 +51,7 @@ public string SyncSlnText(string contents, string slnFolder, IEnumerable<string>
}
else
{
throw new Exception($"path not found: '{path}'");
throw new PathNotFoundException(path);
}
}

Expand Down Expand Up @@ -111,3 +120,7 @@ private SolutionFolder FindOrCreateSolutionFolder(ICollection<IProject> solution
private static SolutionFolder? FindSolutionFolder(IEnumerable<IProject> solutionProjects, string folderName)
=> solutionProjects.OfType<SolutionFolder>().FirstOrDefault(project => project.Name == folderName);
}

public class PathNotFoundException(string path) : Exception($"Path '{path}' not found");
public class InvalidSlnPathException(string s) : Exception(s);
public class EmptyPathListException() : Exception();
7 changes: 6 additions & 1 deletion tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,13 @@ public void AddsBOM()
EndGlobal
");

SetupFilesystem(new[]
{
"some-file.txt",
});

// Act
_cli.Run(["-s", TargetSlnFile]);
_cli.Run(["-s", TargetSlnFile, "some-file.txt"]);

File.ReadAllText(Path.Combine(_testFolder, TargetSlnFile));

Expand Down

0 comments on commit 88aaaa3

Please sign in to comment.