Skip to content

Commit

Permalink
Don't write files that aren't changed, other improvements to XamlStyl…
Browse files Browse the repository at this point in the history
…er.Console (#379)

Co-authored-by: Dave Grochocki <grochocki@users.noreply.github.com>
  • Loading branch information
michael-hawker and grochocki committed Jun 21, 2022
1 parent d26238b commit 7a4282c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions build/pipelines/templates/build-console-public.yaml
Expand Up @@ -15,10 +15,10 @@ jobs:
pwsh: true

- task: UseDotNet@2
displayName: 'Use .NET Core 3.1 SDK'
displayName: 'Use .NET 6 SDK'
inputs:
packageType: sdk
version: 3.1.x
version: 6.0.x
installationPath: $(Agent.ToolsDirectory)/dotnet

# The first task is the dotnet command build, pointing to our csproj file
Expand Down
2 changes: 1 addition & 1 deletion src/XamlStyler.Console/Options.cs
Expand Up @@ -25,7 +25,7 @@ public sealed partial class CommandLineOptions
[Option('r', "recursive", Default = false, HelpText = "Recursively process specified directory.")]
public bool IsRecursive { get; set; }

[Option('p', "passive", Default = false, HelpText = "Check files follow proper formatting without making any modifications.")]
[Option('p', "passive", Default = false, HelpText = "Check files follow proper formatting without making any modifications. Returns error status if files fail the check.")]
public bool IsPassive { get; set; }

[Option('l', "loglevel", Default = LogLevel.Default,
Expand Down
6 changes: 5 additions & 1 deletion src/XamlStyler.Console/Program.cs
Expand Up @@ -8,7 +8,7 @@ namespace Xavalon.XamlStyler.Console
{
public sealed partial class Program
{
public static void Main(string[] args)
public static int Main(string[] args)
{
var writer = new StringWriter();
var parser = new CommandLine.Parser(_ => _.HelpWriter = writer);
Expand Down Expand Up @@ -43,8 +43,12 @@ public static void Main(string[] args)
: "Must specify file(s) or directory";
System.Console.WriteLine($"\nError: {errorString}\n");
Environment.Exit(1);
}
});

return 0;
}
}
}
6 changes: 3 additions & 3 deletions src/XamlStyler.Console/XamlStyler.Console.csproj
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/Xavalon/XamlStyler</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Xavalon.XamlStyler.Console</RootNamespace>
<AssemblyName>xstyler</AssemblyName>
<PackAsTool>true</PackAsTool>
Expand All @@ -29,7 +29,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser">
<Version>2.6.0</Version>
<Version>2.8.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -39,7 +39,7 @@
<Version>12.0.3</Version>
</PackageReference>
<PackageReference Include="NuGet.CommandLine">
<Version>5.7.2</Version>
<Version>6.0.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
24 changes: 16 additions & 8 deletions src/XamlStyler.Console/XamlStylerConsole.cs
Expand Up @@ -267,17 +267,25 @@ private bool TryProcessFile(string file)
{
this.Log($"\nFormatted Output:\n\n{formattedOutput}\n", LogLevel.Insanity);

using var writer = new StreamWriter(path, false, encoding);
try
// Only modify the file on disk if the content would be changed
if (!formattedOutput.Equals(originalContent, StringComparison.Ordinal))
{
writer.Write(formattedOutput);
this.Log($"Finished Processing: {file}", LogLevel.Verbose);
using var writer = new StreamWriter(path, false, encoding);
try
{
writer.Write(formattedOutput);
this.Log($"Finished Processing: {file}", LogLevel.Verbose);
}
catch (Exception e)
{
this.Log("Skipping... Error formatting XAML. Increase log level for more details.");
this.Log($"Exception: {e.Message}", LogLevel.Verbose);
this.Log($"StackTrace: {e.StackTrace}", LogLevel.Debug);
}
}
catch (Exception e)
else
{
this.Log("Skipping... Error formatting XAML. Increase log level for more details.");
this.Log($"Exception: {e.Message}", LogLevel.Verbose);
this.Log($"StackTrace: {e.StackTrace}", LogLevel.Debug);
this.Log($"Finished Processing (unmodified): {file}", LogLevel.Verbose);
}
}

Expand Down

0 comments on commit 7a4282c

Please sign in to comment.