Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity separate generator project #155

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if !ROSLYN4_0_OR_GREATER
namespace Jab
{
public partial class ContainerGenerator: ISourceGenerator
#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
public partial class ContainerGenerator : ISourceGenerator
#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
{
public void Initialize(GeneratorInitializationContext context)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Jab.Roslyn3/Jab.Roslyn3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="ContainerGenerator.Roslyn3.cs" />
</ItemGroup>

<Import Project="../Jab/Jab.Common.props" />

</Project>
25 changes: 25 additions & 0 deletions src/Jab.Unity/ContainerGenerator.Unity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Jab
{
/// <summary>
/// In order to use inside Unity follow:
/// https://docs.unity3d.com/Manual/roslyn-analyzers.html
/// Also requires the Jab.Attributes.dll
/// </summary>
#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
public partial class ContainerGenerator: ISourceGenerator
#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
{
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new SyntaxCollector());
}

public void Execute(GeneratorExecutionContext context)
{
if(!KnownTypes.HasKnownTypes(context.Compilation.SourceModule))
return;

Execute(new GeneratorContext(context));
}
}
}
19 changes: 19 additions & 0 deletions src/Jab.Unity/Jab.Unity.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? How will users get this dll then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unity users have only 2 options: downloading/building and dragging manually the dll into their Unity project or using an npm package (which I created here)

Unity doesn't support Nuget unfortunately

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about https://docs.unity3d.com/Manual/upm-git.html ? Do you know what needs to be added to this repo for unity customers to be able to reference it via a git reference? Or should we add a minimal jab npm package with the generator dll only?

Copy link
Contributor Author

@AlonTalmi AlonTalmi Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can build the Jab.Unity.dll and Jab.Attributes.dll into any directory in this repository then we only need to add a package.json like this inside that directory and then users can install it using:
https://github.com/pakrym/jab?path=the_path_to_that_directory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh and we need to add Unity specific meta file for each dll by opening them inside Unity

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the meta file generation a one time operation or is it required for every new dll build?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One time

<NoWarn>$(NoWarn);RS2008</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
pakrym marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<ItemGroup>
<Compile Include="ContainerGenerator.Unity.cs" />
</ItemGroup>

<Import Project="../Jab/Jab.Common.props" />

</Project>
2 changes: 2 additions & 0 deletions src/Jab/ContainerGenerator.Roslyn4.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if ROSLYN4_0_OR_GREATER
namespace Jab
{
#pragma warning disable RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
public partial class ContainerGenerator : IIncrementalGenerator
#pragma warning restore RS1001 // We don't want this to be discovered as analyzer but it simplifies testing
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Jab/ServiceProviderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ static INamedTypeSymbol GetTypeFromCompilationByMetadataNameOrThrow(Compilation

ModuleAttribute = GetTypeByMetadataNameOrThrow(assemblySymbol, ServiceProviderModuleAttributeMetadataName);
}

public static bool HasKnownTypes(IModuleSymbol sourceModule)
{
return sourceModule.ReferencedAssemblySymbols.Any(s => s.Name == JabAttributesAssemblyName);
}
}

internal class ServiceProviderBuilder
Expand Down
14 changes: 14 additions & 0 deletions src/jab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.Attributes", "Jab.Attri
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.FunctionalTests.Attributes", "Jab.FunctionalTests.Attributes\Jab.FunctionalTests.Attributes.csproj", "{471D59B3-518B-4821-9643-6010AACF1433}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jab.Unity", "Jab.Unity\Jab.Unity.csproj", "{6BD07051-FFDD-4801-9440-BBC73E46F80C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -170,5 +172,17 @@ Global
{471D59B3-518B-4821-9643-6010AACF1433}.Release|x64.Build.0 = Release|Any CPU
{471D59B3-518B-4821-9643-6010AACF1433}.Release|x86.ActiveCfg = Release|Any CPU
{471D59B3-518B-4821-9643-6010AACF1433}.Release|x86.Build.0 = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x64.ActiveCfg = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x64.Build.0 = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x86.ActiveCfg = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Debug|x86.Build.0 = Debug|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|Any CPU.Build.0 = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x64.ActiveCfg = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x64.Build.0 = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x86.ActiveCfg = Release|Any CPU
{6BD07051-FFDD-4801-9440-BBC73E46F80C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading