Skip to content

Commit

Permalink
Switch to dotnet8 for compilation and target framework (#28)
Browse files Browse the repository at this point in the history
* Switch to dotnet 8 for compiling and as target framework

* Upgrade source to C#12

* Use nuget cache also for codeql

* Fix launch.json

* Publish artifacts in github validation build

* Remove explicit architecture from publish command

* Remove --no-restore from dotnet publish.
  • Loading branch information
m-ringler authored Nov 16, 2023
1 parent 39124fe commit 41bac9f
Show file tree
Hide file tree
Showing 25 changed files with 111 additions and 153 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/czishrink_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x

- name: Cache nugets
uses: actions/cache@v3
with:
path: ${{ github.workspace }}/.nuget/packages
key: ${{ runner.os }}-czishrink-nuget-${{ hashFiles('czishrink/**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-czishrink-nuget-
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand All @@ -49,7 +57,7 @@ jobs:
queries: security-and-quality

- name: Restore dependencies
run: dotnet restore
run: dotnet restore --locked-mode

- name: Build
run: dotnet build --no-restore -c Release
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/czishrink_dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x

- name: Get Version from Directory.Build.props
id: getversion
Expand Down Expand Up @@ -117,12 +117,11 @@ jobs:
if: ${{ always() }}

- name: Publish
if: ${{ (github.event_name == 'push') && (matrix.build == 'Release') }}
if: matrix.build == 'Release'
run: >
dotnet publish netczicompress.Desktop/netczicompress.Desktop.csproj
-c ${{ matrix.build }}
-a x64
--no-restore
--self-contained
-p:PublishSingleFile=true
-p:PublishReadyToRun=true
Expand Down
2 changes: 1 addition & 1 deletion czishrink/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/netczicompress.Desktop/bin/x64/Debug/net7.0/CziShrink",
"program": "${workspaceFolder}/netczicompress.Desktop/bin/x64/Debug/net8.0/CziShrink",
"args": [],
"cwd": "${workspaceFolder}/netczicompress.Desktop",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
4 changes: 2 additions & 2 deletions czishrink/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<MSBuildCopyContentTransitively>True</MSBuildCopyContentTransitively>
Expand All @@ -17,6 +17,6 @@

<!-- Version -->
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<VersionPrefix>1.0.3<!--dependabot/nuget/czishrink/Microsoft.Extensions.Logging.Abstractions-8.0.0--></VersionPrefix>
<VersionPrefix>1.1.0<!--feature/dotnet8--></VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion czishrink/global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "7.0.0",
"version": "8.0.0",
"allowPrerelease": false,
"rollForward": "latestFeature"
}
Expand Down
2 changes: 1 addition & 1 deletion czishrink/netczicompress.Desktop/packages.lock.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": 2,
"dependencies": {
"net7.0": {
"net8.0": {
"Avalonia.Desktop": {
"type": "Direct",
"requested": "[11.0.5, )",
Expand Down
12 changes: 3 additions & 9 deletions czishrink/netczicompress/Models/CompositeObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ public static IObserver<T> Create<T>(params IObserver<T>[] observers)
/// A composite <see cref="IObserver{T}"/>.
/// </summary>
/// <typeparam name="T">The observed type.</typeparam>
public sealed class CompositeObserverImpl<T> : IObserver<T>
public sealed class CompositeObserverImpl<T>(params IObserver<T>[] observers)
: IObserver<T>
{
private readonly IObserver<T>[] observers;

public CompositeObserverImpl(params IObserver<T>[] observers)
{
this.observers = observers;
}

public void OnCompleted()
{
this.ForEachObserver(o => o.OnCompleted());
Expand All @@ -46,7 +40,7 @@ public void OnNext(T value)

private void ForEachObserver(Action<IObserver<T>> action)
{
foreach (var item in this.observers)
foreach (var item in observers)
{
action(item);
}
Expand Down
2 changes: 1 addition & 1 deletion czishrink/netczicompress/Models/CompressionLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static CompressionLevel()
public static int DefaultValue { get; } = 1;

[Range(0, 22)]
public int Value
public required int Value
{
get => this.internalValue;
init =>
Expand Down
9 changes: 2 additions & 7 deletions czishrink/netczicompress/Models/CsvLoggingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ namespace netczicompress.Models;
/// <summary>
/// A logging strategy that writes data to a CSV log file.
/// </summary>
public class CsvLoggingStrategy : ILoggingStrategy
public class CsvLoggingStrategy(IFileLauncher fileLauncher) : ILoggingStrategy
{
private readonly IFileLauncher fileLauncher;

public CsvLoggingStrategy(IFileLauncher fileLauncher)
{
this.fileLauncher = fileLauncher;
}
private readonly IFileLauncher fileLauncher = fileLauncher;

/// <summary>
/// Gets the scheduler to use for logging.
Expand Down
21 changes: 6 additions & 15 deletions czishrink/netczicompress/Models/FileLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace netczicompress.Models;
/// Launches a file with its associated app,
/// or displays a file in its folder with the operating system's file manager.
/// </summary>
public class FileLauncher : IFileLauncher
public class FileLauncher(IFileSystem fs, Func<string, string?> getEnvironmentVariable) : IFileLauncher
{
// Cf. https://github.com/dotnet/runtime/blob/52806bc157decf345005249c9ea7969c8b9e7e1b/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs#L14C9-L41C6
private const string Windows = "WINDOWS";
Expand All @@ -23,23 +23,14 @@ public class FileLauncher : IFileLauncher

private const string Unsupported = "UNSUPPORTED";
private static readonly string[] SupportedPlatforms =
{
[
Windows,
Osx,
Linux,
FreeBsd,
NetBsd,
Solaris,
};

private readonly IFileSystem fs;
private readonly Func<string, string?> getEnvironmentVariable;

public FileLauncher(IFileSystem fs, Func<string, string?> getEnvironmentVariable)
{
this.fs = fs;
this.getEnvironmentVariable = getEnvironmentVariable;
}
];

protected string CurrentPlatform { get; init; } = GetPlatform();

Expand Down Expand Up @@ -100,10 +91,10 @@ private static ProcessStartInfo GetLaunchStartInfo(string? path)
private ProcessStartInfo GetRevealStartInfoWindows(string path)
{
var fileName = "explorer";
var sysRoot = this.getEnvironmentVariable("SYSTEMROOT");
var sysRoot = getEnvironmentVariable("SYSTEMROOT");
if (sysRoot != null)
{
fileName = this.fs.Path.Combine(sysRoot, fileName);
fileName = fs.Path.Combine(sysRoot, fileName);
}

return new ProcessStartInfo
Expand All @@ -126,7 +117,7 @@ private ProcessStartInfo GetRevealStartInfoDBus(string path)

private ProcessStartInfo GetOpenParentFolderStartInfo(string path)
{
return GetLaunchStartInfo(this.fs.Path.GetDirectoryName(path));
return GetLaunchStartInfo(fs.Path.GetDirectoryName(path));
}

private ProcessStartInfo GetRevealStartInfo(string path)
Expand Down
22 changes: 6 additions & 16 deletions czishrink/netczicompress/Models/MultiThreadedFolderCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@ namespace netczicompress.Models;
/// <summary>
/// A multi-threaded <see cref="IFolderCompressor"/> that uses <see cref="IFileProcessor"/>s to do the actual work.
/// </summary>
public class MultiThreadedFolderCompressor : IFolderCompressor
{
private readonly CreateProcessor createProcessor;
private readonly FileProcessingFailedHandler fileProcessingFailedHandler;
private readonly int maxTriesToCreateTemporaryFile;

public MultiThreadedFolderCompressor(
public class MultiThreadedFolderCompressor(
CreateProcessor createProcessor,
FileProcessingFailedHandler fileProcessingFailedHandler,
int maxTriesToCreateTemporaryFiles = 100)
{
this.createProcessor = createProcessor;
this.fileProcessingFailedHandler = fileProcessingFailedHandler ?? throw new ArgumentNullException(nameof(fileProcessingFailedHandler));
this.maxTriesToCreateTemporaryFile = maxTriesToCreateTemporaryFiles;
}

: IFolderCompressor
{
// Public only for testing. MockFileSystem does not support CaseInsensitive
public MatchCasing MatchExtensionCasing { get; init; } = MatchCasing.CaseInsensitive;

Expand Down Expand Up @@ -83,7 +73,7 @@ async IAsyncEnumerable<CompressorMessage> CoreAsync([EnumeratorCancellation] Can

if (activeTasks.Count < maxNumberOfThreads)
{
var processor = this.createProcessor(mode, processingOptions);
var processor = createProcessor(mode, processingOptions);
processors.Add(processor);
activeTasks.Add(Run(processor));
}
Expand Down Expand Up @@ -218,7 +208,7 @@ private CompressorMessage.FileFinished ProcessFile(
EnsureParentDirectoryExists(outFile);
}

temporaryFile = new TemporaryFile(outFile, this.maxTriesToCreateTemporaryFile);
temporaryFile = new TemporaryFile(outFile, maxTriesToCreateTemporaryFiles);
if (temporaryFile.TemporaryFileCreationFailed)
{
var tempOutFile = temporaryFile.Info;
Expand Down Expand Up @@ -247,7 +237,7 @@ private CompressorMessage.FileFinished ProcessFile(
// We only cover those exceptions, that have been reported when processing the file, i.e. processedByProcessor is false.
var tempOutFile = temporaryFile.Info;
return MakeTheReturnValue(
this.fileProcessingFailedHandler.FileProcessingFailed(
fileProcessingFailedHandler.FileProcessingFailed(
inFile,
outFile,
tempOutFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@ public static IObservable<T> CompleteOnError<T>(this IObservable<T> obs)
return new CompleteOnErrorDecorator<T>(obs);
}

public class CompleteOnErrorDecorator<T> : IObservable<T>
public class CompleteOnErrorDecorator<T>(IObservable<T> core) : IObservable<T>
{
private readonly IObservable<T> core;

public CompleteOnErrorDecorator(IObservable<T> core)
{
this.core = core;
}

public IDisposable Subscribe(IObserver<T> observer)
{
return this.core.Subscribe(
return core.Subscribe(
onNext: observer.OnNext,
onCompleted: observer.OnCompleted,
onError: ex => observer.OnCompleted());
Expand Down
9 changes: 3 additions & 6 deletions czishrink/netczicompress/Models/PInvokeFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ public void Dispose()

private void CheckDisposed()
{
if (this.IsDisposed)
{
throw new ObjectDisposedException(this.ToString());
}
ObjectDisposedException.ThrowIf(this.IsDisposed, this);
}

internal static partial class NativeMethods
Expand Down Expand Up @@ -199,7 +196,7 @@ public static string GetLibFullName(int suggestedBufferLength = 64)

[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool GetLibVersionString(byte[] buffer, ref ulong bufferLength);
public static partial bool GetLibVersionString([Out] byte[] buffer, ref ulong bufferLength);

[LibraryImport(LibName, StringMarshalling = LibStringEncoding)]
public static partial void GetLibVersion(out int major, out int minor, out int patch);
Expand All @@ -215,7 +212,7 @@ public static partial int ProcessFile(
IntPtr file_processor,
string input_path,
string output_path,
byte[] error_message,
[Out] byte[] error_message,
ref int error_message_length,
ReportProgressCheckCancel reportProgress);
}
Expand Down
28 changes: 9 additions & 19 deletions czishrink/netczicompress/Models/StatisticsRunObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,28 @@ namespace netczicompress.Models;
/// <summary>
/// An <see cref="IFolderCompressorRunObserver{TMessage}"/> that forwards <see cref="AggregateStatistics"/> to an observer of these.
/// </summary>
public class StatisticsRunObserver : IFolderCompressorRunObserver<CompressorMessage.FileFinished>
{
private readonly IObserver<AggregateStatistics> statisticsObserver;
private readonly TimeSpan samplingInterval;
private readonly IScheduler scheduler;

public StatisticsRunObserver(
public class StatisticsRunObserver(
IObserver<AggregateStatistics> statisticsObserver,
TimeSpan samplingInterval,
IScheduler scheduler)
{
this.statisticsObserver = statisticsObserver;
this.samplingInterval = samplingInterval;
this.scheduler = scheduler;
}

: IFolderCompressorRunObserver<CompressorMessage.FileFinished>
{
public void ObserveRun(FolderCompressorParameters runParameters, IObservable<CompressorMessage.FileFinished> runMessages)
{
var start = this.scheduler.Now;
var start = scheduler.Now;

var emptyStats = AggregateStatistics.Empty;
this.statisticsObserver.OnNext(emptyStats);
statisticsObserver.OnNext(emptyStats);
var cumulativeStatistics = runMessages
.CompleteOnError()
.Scan(
emptyStats,
(stats, msg) => stats.AddData(msg, this.scheduler.Now - start));
(stats, msg) => stats.AddData(msg, scheduler.Now - start));

// Subscriptions will expire upon completion, no need for explicit disposal.
_ = cumulativeStatistics
.Sample(this.samplingInterval)
.ObserveOn(this.scheduler)
.Subscribe(this.statisticsObserver);
.Sample(samplingInterval)
.ObserveOn(scheduler)
.Subscribe(statisticsObserver);
}
}
6 changes: 3 additions & 3 deletions czishrink/netczicompress/Models/TemporaryFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class TemporaryFile
/// immediately created, if possible.
/// By this <see cref="TemporaryFileCreationFailed"/> is set accordingly.
/// </summary>
/// <param name="outfile">The basic output file name.</param>
/// <param name="outFile">The basic output file name.</param>
/// <param name="maxNumberOfTriesToCreate">The maximum number or tries.</param>
public TemporaryFile(
IFileInfo outfile,
IFileInfo outFile,
int maxNumberOfTriesToCreate = 100)
{
this.OutFile = outfile;
this.OutFile = outFile;
this.MaxNumberOfTriesToCreate = maxNumberOfTriesToCreate;
this.Info = this.TryCreate();
}
Expand Down
2 changes: 1 addition & 1 deletion czishrink/netczicompress/Models/ThreadCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static ThreadCount()
/// </summary>
public static int DefaultValue { get; } = Maximum;

public int Value
public required int Value
{
get => this.internalValue;
init =>
Expand Down
Loading

0 comments on commit 41bac9f

Please sign in to comment.