Skip to content

Commit

Permalink
Add --verbose option in both verbs and allow *.xyz in zip (#7)
Browse files Browse the repository at this point in the history
* Add `--verbose | -v` option to both verbs, which shows the file paths being processed.
* Allow `*.xyz` extension format in `dotnet-combine zip`.
  • Loading branch information
eduherminio authored Nov 26, 2020
1 parent 7304bac commit 4614077
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 30 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
dotnet-version: 5.0.100

- name: '[Windows] Cache SonarCloud packages'
uses: actions/cache@v1
uses: actions/cache@v2.1.3
if: matrix.os == 'windows-latest'
with:
path: ~\sonar\cache
Expand All @@ -42,7 +42,7 @@ jobs:
- name: '[Windows] Cache SonarCloud scanner'
id: cache-sonar-scanner
if: matrix.os == 'windows-latest'
uses: actions/cache@v1
uses: actions/cache@v2.1.3
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
Expand All @@ -59,10 +59,10 @@ jobs:
if: matrix.os == 'windows-latest'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN2 }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ env.PROJECT_NAME }}" /o:"eduherminio-github" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.coverageReportPaths="coveragereport/SonarQube.xml"
.\.sonar\scanner\dotnet-sonarscanner begin /k:"eduherminio_dotnet-combine" /o:"eduherminio-github" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.coverageReportPaths="coveragereport/SonarQube.xml" /d:sonar.scanner.force-deprecated-java-version=true
- name: '[Ubuntu] Set version to -ci-${{ github.run_number }}'
if: matrix.os == 'ubuntu-latest'
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
if: matrix.os == 'windows-latest'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN2 }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Usage: dotnet-combine single-file <INPUT> [options]
If no filename is provided (i.e. --output dir/), a unique name will be used.

-f, --overwrite (Default: false)
Overwrite the output file if it exists.
Overwrites the output file (if it exists).

--exclude (Default: bin/ obj/)
Excluded files and directories, separated by semicolons (;).
Expand All @@ -68,6 +68,9 @@ Usage: dotnet-combine single-file <INPUT> [options]

-s, --suffix Suffix for the output file

-v, --verbose (Default: false)
Verbose output. Shows combined files, progress, etc.

--help Display this help screen.

--version Display version information.
Expand Down Expand Up @@ -117,8 +120,8 @@ If you want your `MyAmazingProject.csproj` files to be combined every time you b
```xml
<Target Name="dotnet-combine single-file" AfterTargets="Build" DependsOnTargets="Build" Condition="'$(Configuration)' == 'Release' ">
<Message Importance="high" Text="Running dotnet-combine tool"/>
<Message Importance="high" Text="dotnet-combine single-file $(MSBuildProjectDirectory) --overwrite --output $(MSBuildProjectDirectory)/bin/ --prefix amazing_ --exclude &quot;ParsedFile.cs;bin/;obj/;Interfaces/&quot;"/>
<Exec Command="dotnet-combine single-file $(MSBuildProjectDirectory) --overwrite --output $(MSBuildProjectDirectory)/bin/ --prefix amazing_ --exclude &quot;ParsedFile.cs;bin/;obj/;Interfaces/&quot;"/>
<Message Importance="high" Text="dotnet-combine single-file $(MSBuildProjectDirectory) --overwrite --output $(MSBuildProjectDirectory)/bin/ --prefix amazing_ --exclude &quot;bin/;obj/;AutogeneratedDir/;AssemblyInfo.cs&quot;"/>
<Exec Command="dotnet-combine single-file $(MSBuildProjectDirectory) --overwrite --output $(MSBuildProjectDirectory)/bin/ --prefix amazing_ --exclude &quot;bin/;obj/;AutogeneratedDir/;AssemblyInfo.cs&quot;"/>
</Target>
```

Expand Down Expand Up @@ -159,7 +162,7 @@ Usage: dotnet-combine zip <INPUT> [options]
File extensions to include, separated by semicolons (;).

-f, --overwrite (Default: false)
Overwrite the output file if it exists.
Overwrites the output file (if it exists).

--exclude (Default: bin/ obj/)
Excluded files and directories, separated by semicolons (;).
Expand All @@ -168,6 +171,9 @@ Usage: dotnet-combine zip <INPUT> [options]

-s, --suffix Suffix for the output file.

-v, --verbose (Default: false)
Verbose output. Shows compressed files, progress, etc.

--help Display this help screen.

--version Display version information.
Expand Down Expand Up @@ -251,10 +257,10 @@ PRs are also more than welcome, but feel free to open an issue before carrying o
[githubactionslink]: https://github.com/eduherminio/FileParser/actions
[nugetlogo]: https://img.shields.io/nuget/v/dotnet-combine.svg?style=flat-square&label=nuget
[nugetlink]: https://www.nuget.org/packages/dotnet-combine
[coveragelogo]: https://sonarcloud.io/api/project_badges/measure?project=DotnetCombine&metric=coverage
[coveragelink]: https://sonarcloud.io/dashboard?id=DotnetCombine
[sonarqubelink]: https://sonarcloud.io/dashboard?id=DotnetCombine
[sonarqualitylogo]: https://sonarcloud.io/api/project_badges/measure?project=DotnetCombine&metric=alert_status
[sonarvulnerabilitieslogo]: https://sonarcloud.io/api/project_badges/measure?project=DotnetCombine&metric=vulnerabilities
[sonarbugslogo]: https://sonarcloud.io/api/project_badges/measure?project=DotnetCombine&metric=bugs
[sonarcodesmellslogo]: https://sonarcloud.io/api/project_badges/measure?project=DotnetCombine&metric=code_smells
[coveragelogo]: https://sonarcloud.io/api/project_badges/measure?project=eduherminio_dotnet-combine&metric=coverage
[coveragelink]: https://sonarcloud.io/dashboard?id=eduherminio_dotnet-combine
[sonarqubelink]: https://sonarcloud.io/dashboard?id=eduherminio_dotnet-combine
[sonarqualitylogo]: https://sonarcloud.io/api/project_badges/measure?project=eduherminio_dotnet-combine&metric=alert_status
[sonarvulnerabilitieslogo]: https://sonarcloud.io/api/project_badges/measure?project=eduherminio_dotnet-combine&metric=vulnerabilities
[sonarbugslogo]: https://sonarcloud.io/api/project_badges/measure?project=eduherminio_dotnet-combine&metric=bugs
[sonarcodesmellslogo]: https://sonarcloud.io/api/project_badges/measure?project=eduherminio_dotnet-combine&metric=code_smells
8 changes: 6 additions & 2 deletions src/DotnetCombine/Options/CombineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ If no filename is provided (i.e. --output dir/), a unique name will be used.")]
public string? Output { get; set; }

[Option(shortName: 'f', longName: "overwrite", Required = false, Default = false, HelpText = @"
Overwrite the output file if it exists.")]
Overwrites the output file (if it exists).")]
public bool OverWrite { get; set; }

[Option(longName: "exclude", Required = false, Separator = ';', Default = new[] { "bin/", "obj/" }, HelpText = @"
Excluded files and directories, separated by semicolons (;)")]
Excluded files and directories, separated by semicolons (;).")]
public IEnumerable<string> ExcludedItems { get; set; } = new[] { "bin/", "obj/" };

[Option(shortName: 'p', longName: "prefix", Required = false, HelpText = @"
Expand All @@ -34,6 +34,10 @@ If no filename is provided (i.e. --output dir/), a unique name will be used.")]
Suffix for the output file")]
public string? Suffix { get; set; }

[Option(shortName: 'v', longName: "verbose", Required = false, Default = false, HelpText = @"
Verbose output. Shows combined files, progress, etc.")]
public bool Verbose { get; set; }

public void Validate()
{
if (Input is null)
Expand Down
6 changes: 5 additions & 1 deletion src/DotnetCombine/Options/ZipOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If no filename is provided (i.e. --output dir/), a unique name will be used.")]
public IEnumerable<string> Extensions { get; set; } = new[] { ".cs", ".csproj", ".sln" };

[Option(shortName: 'f', longName: "overwrite", Required = false, Default = false, HelpText = @"
Overwrite the output file if it exists.")]
Overwrites the output file (if it exists).")]
public bool OverWrite { get; set; }

[Option(longName: "exclude", Required = false, Separator = ';', Default = new[] { "bin/", "obj/" }, HelpText = @"
Expand All @@ -38,6 +38,10 @@ If no filename is provided (i.e. --output dir/), a unique name will be used.")]
Suffix for the output file.")]
public string? Suffix { get; set; }

[Option(shortName: 'v', longName: "verbose", Required = false, Default = false, HelpText = @"
Verbose output. Shows compressed files, progress, etc.")]
public bool Verbose { get; set; }

public void Validate()
{
if (Input is null)
Expand Down
18 changes: 14 additions & 4 deletions src/DotnetCombine/Services/Combiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public async Task<int> Run()
var filePaths = FindFilesToInclude();
var parsedFiles = await ParseFiles(filePaths);
await AggregateFiles(parsedFiles);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Output file: {_outputFilePath}");
return 0;
}
catch (UnauthorizedAccessException e)
{
Expand Down Expand Up @@ -65,9 +69,6 @@ public async Task<int> Run()
{
Console.ResetColor();
}

Console.WriteLine($"Output file: {_outputFilePath}");
return 0;
}

private void ValidateInput()
Expand Down Expand Up @@ -157,8 +158,17 @@ private async Task AggregateFiles(ICollection<SourceFile> parsedFiles)

var codeSection = new StringBuilder();

foreach (var parsedFile in parsedFiles.OrderBy(file => file.Namespace?.Length)) // Top level statements first
var orderedFiles = parsedFiles.OrderBy(file => file.Namespace?.Length).ToList(); // Top level statements first

for (int i = 0; i < parsedFiles.Count; ++i)
{
var parsedFile = orderedFiles[i];

if (_options.Verbose)
{
Console.WriteLine($"\t* [{i + 1}/{orderedFiles.Count}] Aggregating {parsedFile.Filepath}");
}

codeSection.Append(Environment.NewLine);
codeSection.Append(string.Join(Environment.NewLine, parsedFile.Code));
codeSection.Append(Environment.NewLine);
Expand Down
22 changes: 15 additions & 7 deletions src/DotnetCombine/Services/Compressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public int Run()

var filesToInclude = FindFilesToInclude();
GenerateZipFile(filesToInclude);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Output file: {_outputFilePath}");
return 0;
}
catch (UnauthorizedAccessException e)
{
Expand Down Expand Up @@ -62,9 +66,6 @@ public int Run()
{
Console.ResetColor();
}

Console.WriteLine($"Output file: {_outputFilePath}");
return 0;
}

private void ValidateInput()
Expand Down Expand Up @@ -116,7 +117,7 @@ string composeFileName(string fileNameWithoutExtension) =>
return Path.Combine(basePath, fileName);
}

private ICollection<string> FindFilesToInclude()
private IList<string> FindFilesToInclude()
{
if (File.Exists(_options.Input))
{
Expand All @@ -133,15 +134,15 @@ private ICollection<string> FindFilesToInclude()
foreach (var extension in _options.Extensions)
{
filesToInclude.AddRange(
Directory.GetFiles(_options.Input, $"*.{extension.TrimStart('.')}", SearchOption.AllDirectories)
Directory.GetFiles(_options.Input, $"*.{extension.TrimStart('*').TrimStart('.')}", SearchOption.AllDirectories)
.Where(f => !dirsToExclude.Any(exclusion => $"{Path.GetDirectoryName(f)}{Path.DirectorySeparatorChar}"?.Contains(exclusion, StringComparison.OrdinalIgnoreCase) == true)
&& !filesToExclude.Any(exclusion => string.Equals(Path.GetFileName(f), exclusion, StringComparison.OrdinalIgnoreCase))));
}

return filesToInclude;
}

private void GenerateZipFile(IEnumerable<string> filesToInclude)
private void GenerateZipFile(IList<string> filesToInclude)
{
#pragma warning disable S1854 // Sonar FP: github.com/SonarSource/sonar-dotnet/issues/3348
var pathToTrim = Directory.Exists(_options.Input)
Expand All @@ -150,8 +151,15 @@ private void GenerateZipFile(IEnumerable<string> filesToInclude)

using var fs = new FileStream(_outputFilePath, _options.OverWrite ? FileMode.Create : FileMode.CreateNew);
using var zip = new ZipArchive(fs, ZipArchiveMode.Create);
foreach (var file in filesToInclude)
for (int i = 0; i < filesToInclude.Count; ++i)
{
var file = filesToInclude[i];

if (_options.Verbose)
{
Console.WriteLine($"\t* [{i + 1}/{filesToInclude.Count}] Aggregating {file}");
}

zip.CreateEntryFromFile(file, file[pathToTrim.Length..]);
}
#pragma warning restore S1854
Expand Down
40 changes: 40 additions & 0 deletions tests/DotnetCombine.Test/CompressorTests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using DotnetCombine.Options;
using DotnetCombine.Services;
using System.IO;
using System.IO.Compression;
using Xunit;

namespace DotnetCombine.Test.CompressorTests
{
public class ExtensionsTests : BaseCompressorTests
{
[Theory]
[InlineData("cs")]
[InlineData(".cs")]
[InlineData("*.cs")]
public void SupportedExtensionFormats(string extension)
{
var outputPath = Path.Combine(DefaultOutputDir, nameof(SupportedExtensionFormats) + Compressor.OutputExtension);

// Act
var options = new ZipOptions()
{
Output = outputPath,
OverWrite = true,
Input = InputDir,
Extensions = new[] { extension }
};

var exitCode = new Compressor(options).Run();

// Assert
Assert.Equal(0, exitCode);
Assert.True(File.Exists(outputPath));

using var fs = new FileStream(outputPath, FileMode.Open);
using ZipArchive zip = new ZipArchive(fs, ZipArchiveMode.Read);

Assert.Equal(TotalCsFiles, zip.Entries.Count);
}
}
}

0 comments on commit 4614077

Please sign in to comment.