<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <Nullable>enable</Nullable> </PropertyGroup> <!-- Add NoteBoxGenerator --> <ItemGroup> <ProjectReference Include=".\NoteBoxGenerator\NoteBoxGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"/> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" /> </ItemGroup></Project>
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Library</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"/> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" /> </ItemGroup></Project>
/home/amber/dox/dev/note-box.git/main/NoteBoxGenerator/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [/home/amber/dox/dev/note-box.git/main/note-box.csproj]
using Microsoft.CodeAnalysis;namespace SourceGenerator;[Generator]public class NoteBoxGenerator : ISourceGenerator{ public void Execute(GeneratorExecutionContext context) { // Code generation // List of utilities string[] utils = { "true", "false", "echo", }; // Construct switch block string switchBlock = string.Empty; foreach (var util in utils) { switchBlock += $@" case ""{util}"": ret = {util.First().ToString().ToUpper() + util[1..]}.{util.First().ToString().ToUpper() + util[1..] + "Main"}; break; "; } string source = $@" using System; namespace NoteBox; class NoteBox {{ static void Main(string[] args) {{ int ret = 0; // switch block - e.g. switch (args[0]) {{ {switchBlock} }} Console.WriteLine(""Hello, NoteBox!""); Environment.Exit(ret); }} }} "; context.AddSource("NoteBox.g.cs", source); } public void Initialize(GeneratorInitializationContext context) { // Initialization }}
<Target Name="PostBuild" AfterTargets="PostBuild"> <PropertyGroup> <BuildDir>$(OutputPath)/note-box/</BuildDir> </PropertyGroup> <!-- Ensure the builddir/note-box directory exists --> <Exec Command="mkdir -p $(BuildDir)" /> <!-- Create symbolic links --> <Exec Command="ln -sf note-box $(BuildDir)/true" /> <Exec Command="ln -sf note-box $(BuildDir)/false" /> <Exec Command="ln -sf note-box $(BuildDir)/echo" /> </Target>
cd binln -sf note-box truecd ..dotnet build./bin/true
string linkName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
note-box.dll
string cmd = Environment.CommandLine.Split(' ')[1];
GetCommandLineArgs()
argv
Environment.CommandLine
/home/amber/dox/dev/note-box.git/main/bin/Debug/net8.0/note-box.dll true
System.Environment
./bin/echo
./bin/note-box echo
note-box.csprojbin/ - note-box* - echo@ -> note-box - true@ -> note-box - false@ -> note-box
./bin/<util>
./bin/note-box <util>
7.21.5.5 The strrchr functionSynopsis1 #include <string.h>char *strrchr(const char *s, int c);Description2 The strrchr function locates the last occurrence of c (converted to a char) in thestring pointed to by s. The terminating null character is considered to be part of thestring.Returns3 The strrchr function returns a pointer to the character, or a null pointer if c does notoccur in the string.