Skip to content

Commit

Permalink
Use NonBlockingFileSystem fs wrapper (#162)
Browse files Browse the repository at this point in the history
* Bump

* Add NonBlockingFileSystem

* Fix CS
  • Loading branch information
trejjam authored Jan 23, 2025
1 parent 13d245e commit dc1b98c
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<Import Project="..\WarningConfiguration.targets" />

<ItemGroup>
<PackageReference Include="H.Generators.Tests.Extensions" Version="1.24.1" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="Verify.XUnit" Version="28.9.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.10.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PackageReference Include="System.Text.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="9.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GitReader.SourceGenerator\GitReader.SourceGenerator.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="GitReaderSnapshotTests.*.g.verified.cs">
<DependentUpon>GitReaderSnapshotTests.cs</DependentUpon>
</None>
</ItemGroup>

<Target Name="ExtractSnapshots" BeforeTargets="Restore;_GenerateRestoreProjectSpec">
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../v-tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../no-tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/v-tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/v-tag-repository')"
OverwriteReadOnlyFiles="true" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/tag-repository')"
OverwriteReadOnlyFiles="true" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/no-tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/no-tag-repository')"
OverwriteReadOnlyFiles="true" />
</Target>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<Import Project="..\WarningConfiguration.targets" />

<ItemGroup>
<PackageReference Include="H.Generators.Tests.Extensions" Version="1.24.1" />
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="Verify.XUnit" Version="28.9.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.10.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PackageReference Include="System.Text.Json" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="9.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GitReader.SourceGenerator\GitReader.SourceGenerator.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="GitReaderSnapshotTests.*.g.verified.cs">
<DependentUpon>GitReaderSnapshotTests.cs</DependentUpon>
</None>
</ItemGroup>

<Target Name="ExtractSnapshots" BeforeTargets="Restore;_GenerateRestoreProjectSpec">
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../v-tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />
<Message Importance="High" Text="Extracting $(MSBuildProjectDirectory)/../../no-tag-repository.zip to the $(MSBuildProjectDirectory)/../../repository-snapshots" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/v-tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/v-tag-repository')"
OverwriteReadOnlyFiles="true" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/tag-repository')"
OverwriteReadOnlyFiles="true" />

<Unzip SourceFiles="$(MSBuildProjectDirectory)/../../repository-snapshots/no-tag-repository.zip"
DestinationFolder="$(MSBuildProjectDirectory)/../../repository-snapshots"
Condition="!Exists('$(MSBuildProjectDirectory)/../../repository-snapshots/no-tag-repository')"
OverwriteReadOnlyFiles="true" />
</Target>

</Project>
15 changes: 5 additions & 10 deletions src/GitReader.SourceGenerator/GitInfoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GitReader.Structures;
using H.Generators;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using System;
Expand All @@ -10,21 +9,16 @@
namespace GitReader.SourceGenerator;

[Generator]
public class GitInfoGenerator : IIncrementalGenerator
public class GitInfoGenerator(
TimeProvider timeProvider
) : IIncrementalGenerator
{
public const string Id = "GI";

private readonly TimeProvider _timeProvider;

public GitInfoGenerator() : this(TimeProvider.System)
{
}

public GitInfoGenerator(TimeProvider timeProvider)
{
_timeProvider = timeProvider;
}

public static GitInfoGenerator Create(TimeProvider timeProvider) => new(
timeProvider
);
Expand Down Expand Up @@ -107,6 +101,7 @@ CancellationToken cancellationToken

using var repository = await Repository.Factory.OpenStructureAsync(
configuration.RootDirectory,
new NonBlockingFileSystem(65536),
cancellationToken
);

Expand Down Expand Up @@ -161,7 +156,7 @@ repository.Head is { } head
}

gitInfoCache = new GitInfo(
_timeProvider.GetUtcNow(),
timeProvider.GetUtcNow(),
configuration.TargetNamespace,
head.Name,
commitAbbreviatedHash,
Expand Down
118 changes: 59 additions & 59 deletions src/GitReader.SourceGenerator/GitReader.SourceGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<ProjectName>GitReader.Generator</ProjectName>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<IsPackable>true</IsPackable>
</PropertyGroup>

<Import Project="..\WarningConfiguration.targets" />

<PropertyGroup Label="NuGet">
<PackageLicense>MIT</PackageLicense>
<RepositoryUrl>https://github.com/aviationexam/git-reader-source-generator</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<DevelopmentDependency>true</DevelopmentDependency>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(AssemblyName).props" Pack="true" PackagePath="build" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="GitReader" Version="1.8.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="GitReader.Core" Version="1.8.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="H.Generators.Extensions" Version="1.24.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>

<Target Name="_AddAnalyzersToOutput">
<Message Importance="High" Text="$(MSBuildProjectDirectory)\$(OutputPath)" />
<Message Importance="High" Text="$(PkgMicrosoft_Bcl_TimeProvider)\lib\netstandard2.0\Microsoft.Bcl.TimeProvider.dll" />
<Message Importance="High" Text="$(PkgGitReader)\lib\netstandard2.0\GitReader.dll" />
<Message Importance="High" Text="$(PkgGitReader_Core)\lib\netstandard2.0\GitReader.Core.dll" />
<Message Importance="High" Text="$(PkgH_Generators_Extensions)\lib\netstandard2.0\H.Generators.Extensions.dll" />

<ItemGroup>
<TfmSpecificPackageFile Include="$(PkgMicrosoft_Bcl_TimeProvider)\lib\netstandard2.0\Microsoft.Bcl.TimeProvider.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgGitReader)\lib\netstandard2.0\GitReader.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgGitReader_Core)\lib\netstandard2.0\GitReader.Core.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgH_Generators_Extensions)\lib\netstandard2.0\H.Generators.Extensions.dll" PackagePath="analyzers/dotnet/cs" />
</ItemGroup>
</Target>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<ProjectName>GitReader.Generator</ProjectName>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<IsPackable>true</IsPackable>
</PropertyGroup>

<Import Project="..\WarningConfiguration.targets" />

<PropertyGroup Label="NuGet">
<PackageLicense>MIT</PackageLicense>
<RepositoryUrl>https://github.com/aviationexam/git-reader-source-generator</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<DevelopmentDependency>true</DevelopmentDependency>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(AssemblyName).props" Pack="true" PackagePath="build" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.TimeProvider" Version="8.0.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="GitReader" Version="1.8.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="GitReader.Core" Version="1.8.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="H.Generators.Extensions" Version="1.24.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="PolySharp" Version="1.15.0" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>

<Target Name="_AddAnalyzersToOutput">
<Message Importance="High" Text="$(MSBuildProjectDirectory)\$(OutputPath)" />
<Message Importance="High" Text="$(PkgMicrosoft_Bcl_TimeProvider)\lib\netstandard2.0\Microsoft.Bcl.TimeProvider.dll" />
<Message Importance="High" Text="$(PkgGitReader)\lib\netstandard2.0\GitReader.dll" />
<Message Importance="High" Text="$(PkgGitReader_Core)\lib\netstandard2.0\GitReader.Core.dll" />
<Message Importance="High" Text="$(PkgH_Generators_Extensions)\lib\netstandard2.0\H.Generators.Extensions.dll" />

<ItemGroup>
<TfmSpecificPackageFile Include="$(PkgMicrosoft_Bcl_TimeProvider)\lib\netstandard2.0\Microsoft.Bcl.TimeProvider.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgGitReader)\lib\netstandard2.0\GitReader.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgGitReader_Core)\lib\netstandard2.0\GitReader.Core.dll" PackagePath="analyzers/dotnet/cs" />
<TfmSpecificPackageFile Include="$(PkgH_Generators_Extensions)\lib\netstandard2.0\H.Generators.Extensions.dll" PackagePath="analyzers/dotnet/cs" />
</ItemGroup>
</Target>

</Project>
58 changes: 58 additions & 0 deletions src/GitReader.SourceGenerator/NonBlockingFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using GitReader.IO;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace GitReader.SourceGenerator;

[SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1035:Do not use APIs banned for analyzers")]
internal sealed class NonBlockingFileSystem(
int bufferSize
) : IFileSystem
{
private readonly IFileSystem _fileSystem = new StandardFileSystem(bufferSize);

public string Combine(
params string[] paths
) => _fileSystem.Combine(paths);

public string GetDirectoryPath(
string path
) => _fileSystem.GetDirectoryPath(path);

public string GetFullPath(
string path
) => _fileSystem.GetFullPath(path);

public bool IsPathRooted(
string path
) => _fileSystem.IsPathRooted(path);

public string ResolveRelativePath(
string basePath, string path
) => _fileSystem.ResolveRelativePath(basePath, path);

public Task<bool> IsFileExistsAsync(
string path, CancellationToken ct
) => _fileSystem.IsFileExistsAsync(path, ct);

public Task<string[]> GetFilesAsync(
string basePath, string match, CancellationToken ct
) => _fileSystem.GetFilesAsync(basePath, match, ct);

public Task<Stream> OpenAsync(
string path, bool isSeekable, CancellationToken ct
) => Task.FromResult<Stream>(new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
65536,
true
));

public Task<TemporaryFileDescriptor> CreateTemporaryAsync(
CancellationToken ct
) => _fileSystem.CreateTemporaryAsync(ct);
}

0 comments on commit dc1b98c

Please sign in to comment.