Skip to content

Commit

Permalink
Minor: Use JsBind.Net for all JavaScript bindings and disable integra…
Browse files Browse the repository at this point in the history
…tion tests temporarily
  • Loading branch information
mingyaulee committed Aug 26, 2021
1 parent b8301f0 commit cdafece
Show file tree
Hide file tree
Showing 412 changed files with 2,735 additions and 20,935 deletions.
11 changes: 8 additions & 3 deletions WebExtensions.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tool", "tool", "{4C80786C-7
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Icon", "Icon", "{12B00534-99DE-4E2A-AD2B-C73285B6541D}"
ProjectSection(SolutionItems) = preProject
src\Icon\Icon.svg = src\Icon\Icon.svg
src\Icon\Icon.png = src\Icon\Icon.png
src\Icon\Icon.svg = src\Icon\Icon.svg
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebExtensions.Net.Extensions.DependencyInjection", "src\WebExtensions.Net.Extensions.DependencyInjection\WebExtensions.Net.Extensions.DependencyInjection.csproj", "{2C1ED236-60A4-47D5-8467-33A456F5B458}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -59,11 +61,13 @@ Global
{C8382E8F-80E4-4EA5-B64E-D8AD46D84F90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C8382E8F-80E4-4EA5-B64E-D8AD46D84F90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C8382E8F-80E4-4EA5-B64E-D8AD46D84F90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C8382E8F-80E4-4EA5-B64E-D8AD46D84F90}.Release|Any CPU.Build.0 = Release|Any CPU
{6F6D0020-DE47-47F5-8D85-25A4D328B0A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F6D0020-DE47-47F5-8D85-25A4D328B0A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F6D0020-DE47-47F5-8D85-25A4D328B0A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F6D0020-DE47-47F5-8D85-25A4D328B0A1}.Release|Any CPU.Build.0 = Release|Any CPU
{2C1ED236-60A4-47D5-8467-33A456F5B458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C1ED236-60A4-47D5-8467-33A456F5B458}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C1ED236-60A4-47D5-8467-33A456F5B458}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C1ED236-60A4-47D5-8467-33A456F5B458}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,6 +78,7 @@ Global
{C8382E8F-80E4-4EA5-B64E-D8AD46D84F90} = {402C7B21-F25A-4EB2-AAAE-A8F827CD0ED4}
{6F6D0020-DE47-47F5-8D85-25A4D328B0A1} = {402C7B21-F25A-4EB2-AAAE-A8F827CD0ED4}
{12B00534-99DE-4E2A-AD2B-C73285B6541D} = {37728F80-FC95-4451-B92C-3D915C0D11B6}
{2C1ED236-60A4-47D5-8467-33A456F5B458} = {37728F80-FC95-4451-B92C-3D915C0D11B6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {500E229A-A512-45E3-B66E-46EB228929D3}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using JsBind.Net;
using WebExtensions.Net;
using WebExtensions.Net.Mock;

namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension method for service registration.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the required services for using WebExtensions API.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the services to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddWebExtensions(this IServiceCollection services)
{
services.AddJsBind(options => options.UseInProcessJsRuntime());
services.AddTransient<IWebExtensionsApi, WebExtensionsApi>();
return services;
}

/// <summary>
/// Adds the required services for using Mock WebExtensions API.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the services to.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddMockWebExtensions(this IServiceCollection services)
{
AddWebExtensions(services);
// Replace registered IJsRuntimeAdapter with the mock adapter
services.Remove(services.First(service => service.ServiceType == typeof(IJsRuntimeAdapter)));
services.AddSingleton<IJsRuntimeAdapter, MockJsRuntimeAdapter>();
return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- Build properties. -->
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateDependencyFile>false</GenerateDependencyFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsShippingPackage>true</IsShippingPackage>
<HasReferenceAssembly>false</HasReferenceAssembly>
</PropertyGroup>

<!-- Package properties. -->
<PropertyGroup>
<IsPackable>true</IsPackable>
<PackageOutputPath>..\PackageOutput</PackageOutputPath>
<Authors>mingyaulee</Authors>
<Description>A package for service registration in WebExtensions.Net.</Description>
<RepositoryUrl>https://github.com/mingyaulee/WebExtensions.Net</RepositoryUrl>
<PackageTags>Browser Chrome Firefox Edge Extension Addons .Net</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>Icon.png</PackageIcon>
</PropertyGroup>

<ItemGroup>
<Content Include="..\Icon\Icon.png">
<Pack>true</Pack>
<PackagePath></PackagePath>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="JsBind.Net.Extensions.DependencyInjection" Version="0.1.3" PrivateAssets="contentfiles" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\WebExtensions.Net\WebExtensions.Net.csproj" PrivateAssets="contentfiles" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IEnumerable<ClrTypeInfo> ShallowTranslate(ClassEntity classEntity)
Metadata = new Dictionary<string, object>()
{
{ Constants.TypeMetadata.ClassType, classEntity.Type },
{ Constants.TypeMetadata.ApiNamespace, classEntity.NamespaceEntity.FullName }
{ Constants.TypeMetadata.ApiNamespace, classEntity.NamespaceEntity.Name }
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using WebExtensions.Net.Generator.Extensions;
using WebExtensions.Net.Generator.Helpers;
using WebExtensions.Net.Generator.Models.ClrTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public void AddInterfaceConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile c

public void AddConvertersToCodeFile(ClrTypeInfo clrTypeInfo, CodeFile codeFile)
{
codeFile.UsingNamespaces.Add("JsBind.Net");

codeFile.Comments.Add(new CommentCodeConverter("Type Class"));
codeFile.Comments.Add(new CommentSummaryCodeConverter(clrTypeInfo.Description));
codeFile.Comments.Add(new AttributeCodeConverter("BindAllProperties"));

if (clrTypeInfo.IsObsolete)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
.WriteStartBlock()
.WriteLine($"if (_{privatePropertyName} is null)")
.WriteStartBlock()
.WriteLine($"_{privatePropertyName} = new {propertyType}(webExtensionsJSRuntime);")
.WriteLine($"_{privatePropertyName} = new {propertyType}(JsRuntime, AccessPath);")
.WriteEndBlock()
.WriteLine($"return _{privatePropertyName};")
// end property get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public ApiConstructorCodeConverter(string className, string apiName)

public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
{
codeWriter.WriteUsingStatement("JsBind.Net");

codeWriter.Constructors
.WriteWithConverter(new CommentSummaryCodeConverter($"Creates a new instance of <see cref=\"{className}\" />."))
.WriteWithConverter(new CommentParamCodeSectionConverter("webExtensionsJSRuntime", "Web Extension JS Runtime"))
.WriteLine($"public {className}(IWebExtensionsJSRuntime webExtensionsJSRuntime) : base(webExtensionsJSRuntime, \"{apiName}\")")
.WriteWithConverter(new CommentParamCodeSectionConverter("jsRuntime", "The JS runtime adapter."))
.WriteWithConverter(new CommentParamCodeSectionConverter("accessPath", "The base API access path."))
.WriteLine($"public {className}(IJsRuntimeAdapter jsRuntime, string accessPath) : base(jsRuntime, AccessPaths.Combine(accessPath, \"{apiName}\"))")
.WriteStartBlock()
.WriteEndBlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
{
// Event property
var privatePropertyName = clrPropertyInfo.PrivateName;

codeWriter.WriteUsingStatement("JsBind.Net");

codeWriter.Properties
.WriteLine($"private {clrPropertyInfo.PropertyType.CSharpName} _{privatePropertyName};");

Expand All @@ -44,7 +47,7 @@ public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
.WriteLine($"if (_{privatePropertyName} is null)")
.WriteStartBlock()
.WriteLine($"_{privatePropertyName} = new {clrPropertyInfo.PropertyType.CSharpName}();")
.WriteLine($"InitializeProperty(\"{privatePropertyName}\", _{privatePropertyName});")
.WriteLine($"_{privatePropertyName}.Initialize(JsRuntime, AccessPaths.Combine(AccessPath, \"{privatePropertyName}\"));")
.WriteEndBlock()
.WriteLine($"return _{privatePropertyName};")
// end property get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ public ApiRootConstructorCodeConverter(string apiRootName)

public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
{
codeWriter.Properties
.WriteLine($"private readonly IWebExtensionsJSRuntime webExtensionsJSRuntime;");
codeWriter.WriteUsingStatement("JsBind.Net");

codeWriter.Constructors
.WriteWithConverter(new CommentSummaryCodeConverter($"Creates a new instance of <see cref=\"{apiRootName}\" />."))
.WriteLine($"public {apiRootName}(IWebExtensionsJSRuntime webExtensionsJSRuntime)")
.WriteWithConverter(new CommentParamCodeSectionConverter("jsRuntime", "The JS runtime adapter."))
.WriteLine($"public {apiRootName}(IJsRuntimeAdapter jsRuntime) : base(jsRuntime, \"browser\")")
.WriteStartBlock()
.WriteLine($"this.webExtensionsJSRuntime = webExtensionsJSRuntime;")
.WriteEndBlock();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,12 @@ public void WriteTo(CodeWriter codeWriter, CodeWriterOptions options)
{
codeWriter.WriteUsingStatement("System.Text.Json.Serialization");

var privatePropertyName = clrPropertyInfo.PrivateName;
codeWriter.Properties
.WriteLine($"private {clrPropertyInfo.PropertyType.CSharpName} _{privatePropertyName};");

codeWriter.PublicProperties
.WriteWithConverter(new CommentSummaryCodeConverter(clrPropertyInfo.Description))
.WriteWithConverter(clrPropertyInfo.IsObsolete ? new AttributeObsoleteCodeConverter(clrPropertyInfo.ObsoleteMessage) : null)
.WriteWithConverter(new AttributeCodeConverter($"JsonPropertyName(\"{privatePropertyName}\")"))
.WriteWithConverter(new AttributeCodeConverter($"JsonPropertyName(\"{clrPropertyInfo.Name}\")"))
.WriteWithConverter(clrPropertyInfo.PropertyType.IsNullable ? new AttributeCodeConverter($"JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)") : null)
.WriteLine($"public {clrPropertyInfo.PropertyType.CSharpName} {clrPropertyInfo.PublicName}")
// start property body
.WriteStartBlock()
.WriteLine($"get")
.WriteStartBlock()
.WriteLine($"InitializeProperty(\"{privatePropertyName}\", _{privatePropertyName});")
.WriteLine($"return _{privatePropertyName};")
.WriteEndBlock()

.WriteLine($"set")
.WriteStartBlock()
.WriteLine($"_{privatePropertyName} = value;")
.WriteEndBlock()
// end property body
.WriteEndBlock();
.WriteLine($"public {clrPropertyInfo.PropertyType.CSharpName} {clrPropertyInfo.PublicName} {{ get; set; }}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ namespace WebExtensions.Net.Generator.EntitiesRegistration.ClassEntityRegistrars
{
public class ApiRootClassEntityRegistrar : BaseClassEntityRegistrar
{
public ApiRootClassEntityRegistrar(EntitiesContext entitiesContext) : base(entitiesContext)
private readonly RegistrationOptions registrationOptions;

public ApiRootClassEntityRegistrar(EntitiesContext entitiesContext, RegistrationOptions registrationOptions) : base(entitiesContext)
{
this.registrationOptions = registrationOptions;
}

protected override ClassType GetClassType() => ClassType.ApiRootClass;
protected override string? GetBaseClassName() => registrationOptions.ApiClassBaseClassName;
protected override bool ShouldImplementInterface() => true;
}
}
76 changes: 0 additions & 76 deletions src/WebExtensions.Net/ArgumentsHandler.cs

This file was deleted.

17 changes: 7 additions & 10 deletions src/WebExtensions.Net/BaseApi.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
namespace WebExtensions.Net
using JsBind.Net;

namespace WebExtensions.Net
{
/// <summary>
/// Base API class.
/// </summary>
public class BaseApi : BaseObject
public class BaseApi : ObjectBindingBase<BaseApi>
{
/// <summary>
/// Gets the WebExtensionsJsRuntime instance.
/// </summary>
protected new IWebExtensionsJSRuntime webExtensionsJSRuntime;

internal BaseApi(IWebExtensionsJSRuntime webExtensionsJSRuntime, string apiNamespace)
internal BaseApi(IJsRuntimeAdapter jsRuntime, string apiNamespace)
{
this.webExtensionsJSRuntime = webExtensionsJSRuntime;
Initialize(webExtensionsJSRuntime, "browser", apiNamespace);
SetAccessPath(apiNamespace);
Initialize(jsRuntime);
}
}
}
Loading

0 comments on commit cdafece

Please sign in to comment.