Skip to content

Commit

Permalink
add - Added known addons generator
Browse files Browse the repository at this point in the history
---

Refer to commit d437468 for more info.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Sep 10, 2024
1 parent a5a8879 commit 23c8f34
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 236 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,6 @@ DocGen/api/

# Vim
*.swp

# Nitrocid Generators
public/*/Generated/
10 changes: 10 additions & 0 deletions Nitrocid.sln
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nitrocid.LocaleCheckerStand
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Nitrocid.LocaleCommon", "private\Nitrocid.LocaleCommon\Nitrocid.LocaleCommon.shproj", "{709AC61B-B746-46A7-A5A0-390B9091BE99}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Generators", "Generators", "{D0F42572-AC47-4E45-943B-80EF3A7C27D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nitrocid.Generators.KnownAddons", "private\Nitrocid.Generators\Nitrocid.Generators.KnownAddons\Nitrocid.Generators.KnownAddons.csproj", "{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -407,6 +411,10 @@ Global
{73E76BBD-BCFD-4E43-9F17-F937A87A2D1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73E76BBD-BCFD-4E43-9F17-F937A87A2D1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73E76BBD-BCFD-4E43-9F17-F937A87A2D1B}.Release|Any CPU.Build.0 = Release|Any CPU
{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -474,6 +482,8 @@ Global
{A9DBA465-7F73-449F-B5BD-E143767513E4} = {5427F963-3094-4111-AB05-DAE364A8DF67}
{73E76BBD-BCFD-4E43-9F17-F937A87A2D1B} = {5427F963-3094-4111-AB05-DAE364A8DF67}
{709AC61B-B746-46A7-A5A0-390B9091BE99} = {5427F963-3094-4111-AB05-DAE364A8DF67}
{D0F42572-AC47-4E45-943B-80EF3A7C27D0} = {61916ED1-66D6-4938-9F1A-D0DF38D16E20}
{AD6E464F-9B35-4070-AA9C-90EF64CD5F6F} = {D0F42572-AC47-4E45-943B-80EF3A7C27D0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {424D8E7A-CFEB-40AE-A79C-437D603177FD}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Nitrocid KS Copyright (C) 2018-2024 Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Newtonsoft.Json;

namespace Nitrocid.Generators.KnownAddons
{
internal class KnownAddonInfo
{
[JsonProperty("name")]
public string Name { get; set; } = "";

[JsonProperty("display")]
public string Display { get; set; } = "";

[JsonProperty("project")]
public string Project { get; set; } = "";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//
// Nitrocid KS Copyright (C) 2018-2024 Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Text;

namespace Nitrocid.Generators.KnownAddons
{
[Generator]
public class KnownAddonsGen : ISourceGenerator
{
private string knownAddons = "";

public void Execute(GeneratorExecutionContext context)
{
// Get the data content
var asm = typeof(KnownAddonsGen).Assembly;
var stream = asm.GetManifestResourceStream($"{asm.GetName().Name}.Resources.KnownAddons.json");
using var reader = new StreamReader(stream);
knownAddons = reader.ReadToEnd();

// Now, populate the enumerations and data properties
PopulateEnums(context);
PopulateData(context);
}

public void Initialize(GeneratorInitializationContext context)
{ }

private void PopulateData(GeneratorExecutionContext context)
{
string header =
$$"""
//
// Nitrocid KS Copyright (C) 2018-2024 Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

// <auto-generated/>

using System.Collections.Generic;

namespace Nitrocid.Kernel.Extensions
{
public static partial class InterAddonTranslations
{
private static readonly Dictionary<KnownAddons, (string, string)> knownAddons = new()
{
// Note: The names are not to be localized for usage with GetAddonName(), because they are constant addon names.

""";
string footer =
$$"""
};
}
}
""";
var builder = new StringBuilder(header);

// Read all the data
var list = JsonConvert.DeserializeObject<KnownAddonInfo[]>(knownAddons);
var names = list.Select((data) => data.Name).ToArray();
if (list is null)
return;

// Populate all the data properties
foreach (var addonData in list)
{
string name = addonData.Name;
string display = addonData.Display;
string project = addonData.Project;
builder.AppendLine(
$$"""
{ KnownAddons.{{name}}, ("{{project}}", /* Localizable */ "{{display}}") },
"""
);
}
builder.AppendLine();

// Add the source code to the compilation
builder.AppendLine(footer);
context.AddSource("InterAddonTranslations.g.cs", builder.ToString());
}

private void PopulateEnums(GeneratorExecutionContext context)
{
string header =
$$"""
//
// Nitrocid KS Copyright (C) 2018-2024 Aptivi
//
// This file is part of Nitrocid KS
//
// Nitrocid KS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Nitrocid KS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

// <auto-generated/>

namespace Nitrocid.Kernel.Extensions
{
/// <summary>
/// Known addons list for inter-addon communication
/// </summary>
public enum KnownAddons
{

""";
string footer =
$$"""
}
}
""";
var builder = new StringBuilder(header);

// Read all the data
var list = JsonConvert.DeserializeObject<KnownAddonInfo[]>(knownAddons);
if (list is null)
return;

// Populate all the enumerations
foreach (var addonData in list)
{
string name = addonData.Name;
string display = addonData.Display;
string project = addonData.Project;
builder.AppendLine(
$$"""
/// <summary>
/// "{{display}}" from the Nitrocid.{{project}} addon project
/// </summary>
{{name}},
"""
);
}

// Add the source code to the compilation
builder.AppendLine(footer);
context.AddSource("KnownAddons.cs", builder.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<DefineConstants>GENERATOR</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[4.8.0]" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" PrivateAssets="all" GeneratePathProperty="true" />

<PackageReference Remove="Microsoft.SourceLink.GitHub" />
</ItemGroup>

<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\*.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>

<ItemGroup>
<EmbeddedResource Include="Resources\KnownAddons.json" />
</ItemGroup>

</Project>
Loading

0 comments on commit 23c8f34

Please sign in to comment.