Skip to content

[generator] Generator refactor #674

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

Merged
merged 16 commits into from
Aug 14, 2020
14 changes: 14 additions & 0 deletions Java.Interop.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Generato
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "java-source-utils", "tools\java-source-utils\java-source-utils.csproj", "{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xamarin.SourceWriter", "src\Xamarin.SourceWriter\Xamarin.SourceWriter.csproj", "{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xamarin.SourceWriter-Tests", "tests\Xamarin.SourceWriter-Tests\Xamarin.SourceWriter-Tests.csproj", "{6CF94627-BA74-4336-88CD-7EDA20C8F292}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Java.Interop.NamingCustomAttributes\Java.Interop.NamingCustomAttributes.projitems*{58b564a1-570d-4da2-b02d-25bddb1a9f4f}*SharedItemsImports = 5
Expand Down Expand Up @@ -257,6 +261,14 @@ Global
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74}.Release|Any CPU.Build.0 = Release|Any CPU
{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA}.Release|Any CPU.Build.0 = Release|Any CPU
{6CF94627-BA74-4336-88CD-7EDA20C8F292}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CF94627-BA74-4336-88CD-7EDA20C8F292}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CF94627-BA74-4336-88CD-7EDA20C8F292}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CF94627-BA74-4336-88CD-7EDA20C8F292}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -301,6 +313,8 @@ Global
{C2FD2F12-DE3B-4FB9-A0D3-FA3EF597DD04} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
{7F4828AB-3908-458C-B09F-33C74A1368F9} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
{F46EDFA5-C52A-4F0C-B5A2-5BB67E0D8C74} = {C8F58966-94BF-407F-914A-8654F8B8AE3B}
{C5B732C8-7AF3-41D3-B903-AEDFC392E5BA} = {0998E45F-8BCE-4791-A944-962CD54E2D80}
{6CF94627-BA74-4336-88CD-7EDA20C8F292} = {271C9F30-F679-4793-942B-0D9527CB3E2F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {29204E0C-382A-49A0-A814-AD7FBF9774A5}
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ TESTS = \
bin/Test$(CONFIGURATION)/generator-Tests.dll \
bin/Test$(CONFIGURATION)/Xamarin.Android.Tools.ApiXmlAdjuster-Tests.dll \
bin/Test$(CONFIGURATION)/Xamarin.Android.Tools.Bytecode-Tests.dll \
bin/Test$(CONFIGURATION)/Java.Interop.Tools.Generator-Tests.dll
bin/Test$(CONFIGURATION)/Java.Interop.Tools.Generator-Tests.dll \
bin/Test$(CONFIGURATION)/Xamarin.SourceWriter-Tests.dll

PTESTS = \
bin/Test$(CONFIGURATION)/Java.Interop-PerformanceTests.dll
Expand Down
2 changes: 1 addition & 1 deletion build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
inputs:
solution: build-tools/scripts/RunNUnitTests.targets
configuration: $(Build.Configuration)
msbuildArguments: /p:TestAssembly="bin\Test$(Build.Configuration)\generator-Tests.dll;bin\Test$(Build.Configuration)\Java.Interop.Tools.JavaCallableWrappers-Tests.dll;bin\Test$(Build.Configuration)\logcat-parse-Tests.dll;bin\Test$(Build.Configuration)\Xamarin.Android.Tools.ApiXmlAdjuster-Tests.dll;bin\Test$(Build.Configuration)\Xamarin.Android.Tools.Bytecode-Tests.dll;bin\Test$(Build.Configuration)\Java.Interop.Tools.Generator-Tests.dll"
msbuildArguments: /p:TestAssembly="bin\Test$(Build.Configuration)\generator-Tests.dll;bin\Test$(Build.Configuration)\Java.Interop.Tools.JavaCallableWrappers-Tests.dll;bin\Test$(Build.Configuration)\logcat-parse-Tests.dll;bin\Test$(Build.Configuration)\Xamarin.Android.Tools.ApiXmlAdjuster-Tests.dll;bin\Test$(Build.Configuration)\Xamarin.Android.Tools.Bytecode-Tests.dll;bin\Test$(Build.Configuration)\Java.Interop.Tools.Generator-Tests.dll;bin\Test$(Build.Configuration)\Xamarin.SourceWriter-Tests.dll"
condition: succeededOrFailed()

- task: PublishTestResults@2
Expand Down
7 changes: 7 additions & 0 deletions build-tools/automation/templates/core-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ steps:
arguments: bin/Test$(Build.Configuration)/Java.Interop.Tools.JavaSource-Tests.dll
continueOnError: true

- task: DotNetCoreCLI@2
displayName: 'Tests: Xamarin.SourceWriter'
inputs:
command: test
arguments: bin/Test$(Build.Configuration)/Xamarin.SourceWriter-Tests.dll
continueOnError: true

# Running native Java.Interop tests are not yet supported on .NET Core
#- task: DotNetCoreCLI@2
# displayName: 'Tests: Java.Interop'
Expand Down
74 changes: 74 additions & 0 deletions src/Xamarin.SourceWriter/CodeWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Xamarin.SourceWriter
{
public class CodeWriter : IDisposable
{
TextWriter stream;
bool owns_stream;
int indent;
bool need_indent = true;
string base_indent;

public CodeWriter (string filename)
{
stream = File.CreateText (filename);
owns_stream = true;
}

public CodeWriter (TextWriter streamWriter, string baseIndent = "")
{
stream = streamWriter;
base_indent = baseIndent;
}

public void Write (string value)
{
WriteIndent ();
stream.Write (value);
}

public void WriteLine ()
{
WriteIndent ();
stream.WriteLine ();
need_indent = true;
}

public void WriteLine (string value)
{
WriteIndent ();
stream.WriteLine (value);
need_indent = true;
}

public void WriteLine (string format, params object[] args)
{
WriteIndent ();
stream.WriteLine (format, args);
need_indent = true;
}

public void Indent (int count = 1) => indent += count;
public void Unindent (int count = 1) => indent -= count;

private void WriteIndent ()
{
if (!need_indent)
return;

stream.Write (base_indent + new string ('\t', indent));

need_indent = false;
}

public void Dispose ()
{
if (owns_stream)
stream?.Dispose ();
}
}
}
15 changes: 15 additions & 0 deletions src/Xamarin.SourceWriter/Enumerations/Visibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public enum Visibility
{
Default = 0,
Private = 1,
Public = 2,
Protected = 4,
Internal = 8
}
}
11 changes: 11 additions & 0 deletions src/Xamarin.SourceWriter/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public static class StringExtensions
{
public static bool HasValue (this string str) => !string.IsNullOrWhiteSpace (str);
}
}
11 changes: 11 additions & 0 deletions src/Xamarin.SourceWriter/Models/AttributeWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public abstract class AttributeWriter
{
public virtual void WriteAttribute (CodeWriter writer) { }
}
}
25 changes: 25 additions & 0 deletions src/Xamarin.SourceWriter/Models/ClassWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Xamarin.SourceWriter
{
public class ClassWriter : TypeWriter
{
public ObservableCollection<ConstructorWriter> Constructors { get; } = new ObservableCollection<ConstructorWriter> ();

public ClassWriter ()
{
Constructors.CollectionChanged += MemberAdded;
}

public override void WriteConstructors (CodeWriter writer)
{
foreach (var ctor in Constructors) {
ctor.Write (writer);
writer.WriteLine ();
}
}
}
}
22 changes: 22 additions & 0 deletions src/Xamarin.SourceWriter/Models/CommentWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public class CommentWriter : ISourceWriter
{
public string Value { get; set; }
public int Priority { get; set; }

public CommentWriter (string value)
{
Value = value;
}

public virtual void Write (CodeWriter writer)
{
writer.WriteLine (Value);
}
}
}
21 changes: 21 additions & 0 deletions src/Xamarin.SourceWriter/Models/ConstructorWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public class ConstructorWriter : MethodWriter
{
public string BaseCall { get; set; }

protected override void WriteReturnType (CodeWriter writer)
{
}

protected override void WriteConstructorBaseCall (CodeWriter writer)
{
if (BaseCall.HasValue ())
writer.Write ($" : {BaseCall}");
}
}
}
114 changes: 114 additions & 0 deletions src/Xamarin.SourceWriter/Models/DelegateWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.SourceWriter
{
public class DelegateWriter : ISourceWriter, ITakeParameters
{
Visibility visibility;

public string Name { get; set; }
public List<MethodParameterWriter> Parameters { get; } = new List<MethodParameterWriter> ();
public TypeReferenceWriter Type { get; set; }
public List<string> Comments { get; } = new List<string> ();
public List<AttributeWriter> Attributes { get; } = new List<AttributeWriter> ();
public bool IsPublic { get => visibility.HasFlag (Visibility.Public); set => visibility = value ? Visibility.Public : Visibility.Default; }
public bool UseExplicitPrivateKeyword { get; set; }
public bool IsInternal { get => visibility.HasFlag (Visibility.Internal); set => visibility = value ? Visibility.Internal : Visibility.Default; }
public bool IsConst { get; set; }
public string Value { get; set; }
public bool IsStatic { get; set; }
public bool IsReadonly { get; set; }
public bool IsPrivate { get => visibility.HasFlag (Visibility.Private); set => visibility = value ? Visibility.Private : Visibility.Default; }
public bool IsProtected { get => visibility.HasFlag (Visibility.Protected); set => visibility = value ? Visibility.Protected : Visibility.Default; }
public int Priority { get; set; }
public bool IsShadow { get; set; }

public void SetVisibility (string visibility)
{
switch (visibility?.ToLowerInvariant ()) {
case "public":
IsPublic = true;
break;
case "internal":
IsInternal = true;
break;
case "protected":
IsProtected = true;
break;
case "private":
IsPrivate = true;
break;
}
}

public virtual void Write (CodeWriter writer)
{
WriteComments (writer);
WriteAttributes (writer);
WriteSignature (writer);
}

public virtual void WriteComments (CodeWriter writer)
{
foreach (var c in Comments)
writer.WriteLine (c);
}

public virtual void WriteAttributes (CodeWriter writer)
{
foreach (var att in Attributes)
att.WriteAttribute (writer);
}

public virtual void WriteSignature (CodeWriter writer)
{
if (IsPublic)
writer.Write ("public ");
else if (IsInternal)
writer.Write ("internal ");
else if (IsPrivate)
writer.Write ("private ");

if (IsStatic)
writer.Write ("static ");
if (IsReadonly)
writer.Write ("readonly ");
if (IsConst)
writer.Write ("const ");

if (IsShadow)
writer.Write ("new ");

writer.Write ("delegate ");

WriteType (writer);

writer.Write (Name + " ");
writer.Write ("(");

WriteParameters (writer);

writer.Write (")");

writer.Write (";");
}

protected virtual void WriteParameters (CodeWriter writer)
{
for (var i = 0; i < Parameters.Count; i++) {
var p = Parameters [i];
p.WriteParameter (writer);

if (i < Parameters.Count - 1)
writer.Write (", ");
}
}

protected virtual void WriteType (CodeWriter writer)
{
Type.WriteTypeReference (writer);
}
}
}
Loading