Skip to content

Commit efa5c17

Browse files
committed
Don't use submoduels
1 parent a93f9fa commit efa5c17

File tree

7 files changed

+144
-1
lines changed

7 files changed

+144
-1
lines changed

BuildInformation

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\LinkDotNet.BuildInformation\LinkDotNet.BuildInformation.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using System.Threading.Channels;
4+
5+
Console.WriteLine($"Build at: {BuildInformation.BuildAt}");
6+
Console.WriteLine($"Platform: {BuildInformation.Platform}");
7+
Console.WriteLine($"Warning level: {BuildInformation.WarningLevel}");
8+
Console.WriteLine($"Configuration: {BuildInformation.Configuration}");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkDotNet.BuildInformation", "LinkDotNet.BuildInformation\LinkDotNet.BuildInformation.csproj", "{6D32EA91-61D4-4F8E-B1AF-4B0199EAF9A6}"
4+
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkDotNet.BuildInformation.Sample", "LinkDotNet.BuildInformation.Sample\LinkDotNet.BuildInformation.Sample.csproj", "{B9A197AA-2580-4AC7-8E60-2ECF12F2A198}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{6D32EA91-61D4-4F8E-B1AF-4B0199EAF9A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{6D32EA91-61D4-4F8E-B1AF-4B0199EAF9A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{6D32EA91-61D4-4F8E-B1AF-4B0199EAF9A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{6D32EA91-61D4-4F8E-B1AF-4B0199EAF9A6}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{B9A197AA-2580-4AC7-8E60-2ECF12F2A198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{B9A197AA-2580-4AC7-8E60-2ECF12F2A198}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{B9A197AA-2580-4AC7-8E60-2ECF12F2A198}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{B9A197AA-2580-4AC7-8E60-2ECF12F2A198}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
[Generator]
4+
public sealed class IncrementalBuildInformationGenerator : IIncrementalGenerator
5+
{
6+
public void Initialize(IncrementalGeneratorInitializationContext context)
7+
{
8+
var compilerOptions = context.CompilationProvider.Select((s, _) => s.Options);
9+
10+
context.RegisterSourceOutput(compilerOptions, static (productionContext, options) =>
11+
{
12+
var buildInformation = new BuildInformationInfo
13+
{
14+
BuildAt = DateTime.UtcNow.ToString("O"),
15+
Platform = options.Platform.ToString(),
16+
WarningLevel = options.WarningLevel,
17+
Configuration = options.OptimizationLevel.ToString(),
18+
};
19+
20+
productionContext.AddSource("LinkDotNet.BuildInformation.g", GenerateBuildInformationClass(buildInformation));
21+
});
22+
}
23+
24+
private static string GenerateBuildInformationClass(BuildInformationInfo buildInformation)
25+
{
26+
return $@"
27+
using System;
28+
using System.Globalization;
29+
30+
public static class BuildInformation
31+
{{
32+
/// <summary>
33+
/// Returns the build date (UTC).
34+
/// </summary>
35+
/// <remarks>Value is: {buildInformation.BuildAt}</remarks>
36+
public static readonly DateTime BuildAt = DateTime.ParseExact(""{buildInformation.BuildAt}"", ""O"", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
37+
38+
/// <summary>
39+
/// Returns the platform.
40+
/// </summary>
41+
/// <remarks>Value is: {buildInformation.Platform}</remarks>
42+
public const string Platform = ""{buildInformation.Platform}"";
43+
44+
/// <summary>
45+
/// Returns the warning level.
46+
/// </summary>
47+
/// <remarks>Value is: {buildInformation.WarningLevel}</remarks>
48+
public const int WarningLevel = {buildInformation.WarningLevel};
49+
50+
/// <summary>
51+
/// Returns the configuration.
52+
/// </summary>
53+
/// <remarks>Value is: {buildInformation.Configuration}</remarks>
54+
public const string Configuration = ""{buildInformation.Configuration}"";
55+
}}
56+
";
57+
}
58+
59+
private sealed class BuildInformationInfo
60+
{
61+
public string BuildAt { get; set; } = string.Empty;
62+
public string Platform { get; set; } = string.Empty;
63+
public int WarningLevel { get; set; }
64+
public string Configuration { get; set; } = string.Empty;
65+
}
66+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<IncludeBuildOutput>false</IncludeBuildOutput>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
10+
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
11+
<IsRoslynComponent>true</IsRoslynComponent>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" PrivateAssets="all"/>
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
24+
</ItemGroup>
25+
26+
</Project>

BuildInformation/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# C# Source Generators: How to get build information?
2+
3+
Source generators are a powerful feature introduced to C#, allowing developers to generate additional code during the compilation process automatically. They can help reduce boilerplate, improve performance, and simplify your codebase.
4+
5+
This blog post will introduce source generators, discuss how they work, and walk through an example of a source generator for generating build information.
6+
7+
Found [here](https://steven-giesel.com/blogPost/cec8df6e-b271-4b4c-8ff6-e9f3aa5e26a1)

0 commit comments

Comments
 (0)