Skip to content

Commit

Permalink
Add properties in API definition. Generated file headers are removed …
Browse files Browse the repository at this point in the history
…for better change detection.
  • Loading branch information
mingyaulee committed Mar 26, 2021
1 parent 1e9abb4 commit 24efd91
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/WebExtension.Net.Generator/ApiDefinitionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private List<ApiDefinition> MergeByNamespace(IEnumerable<ApiDefinition> apiDefin
{
apiDefinition.URL = $"{apiDefinition.URL}|{duplicateApiDefinition.URL}";
apiDefinition.Json = apiDefinition.Json.Concat(duplicateApiDefinition.Json);
apiDefinition.Properties = apiDefinition.Properties.Concat(duplicateApiDefinition.Properties);
apiDefinition.Functions = apiDefinition.Functions.Concat(duplicateApiDefinition.Functions);
apiDefinition.Types = apiDefinition.Types.Concat(duplicateApiDefinition.Types);
}
Expand Down
10 changes: 4 additions & 6 deletions src/WebExtension.Net.Generator/ApiDefinitionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class ApiDefinitionWriter
private readonly List<string> includeNamespaces;
private readonly List<string> excludeNamespaces;
private readonly ApiDefinitionTranslator translator;
private readonly string header;
private readonly JsonSerializerOptions jsonSerializerOptions;

public ApiDefinitionWriter(ILogger logger, List<string> includeNamespaces, List<string> excludeNamespaces)
Expand All @@ -26,7 +25,6 @@ public ApiDefinitionWriter(ILogger logger, List<string> includeNamespaces, List<
this.includeNamespaces = includeNamespaces;
this.excludeNamespaces = excludeNamespaces;
translator = new ApiDefinitionTranslator(logger);
header = $"// This file is auto generated at {DateTime.UtcNow:s}{Environment.NewLine}{Environment.NewLine}";
jsonSerializerOptions = new JsonSerializerOptions()
{
WriteIndented = true
Expand Down Expand Up @@ -82,7 +80,7 @@ public async Task Write(ApiDefinitionRoot apiDefinitionRoot)
if (!Directory.Exists(apiDefinition.Directory))
{
Directory.CreateDirectory(apiDefinition.Directory);
await File.WriteAllTextAsync(apiDefinition.GetFilePath("generated.txt"), string.Empty);
await File.WriteAllTextAsync(apiDefinition.GetFilePath("generated.txt"), $"This file is auto generated at {DateTime.UtcNow:s}");
}
await WriteFile(apiDefinition, $"{apiDefinitionName}.json", JsonSerializer.Serialize(apiDefinition.Json, jsonSerializerOptions));

Expand All @@ -91,10 +89,10 @@ public async Task Write(ApiDefinitionRoot apiDefinitionRoot)
await WriteTypes(apiDefinition);
}

if (apiDefinition.Functions.Any())
if (apiDefinition.Properties.Any() || apiDefinition.Functions.Any())
{
var apiDefinitionNamePostfix = apiDefinitionRoot.DefinitionClassNamePostfix;
var apiDefinitionContent = translator.TranslateApiDefinition(apiDefinition, $"{apiDefinitionName}{apiDefinitionNamePostfix}");
var apiDefinitionContent = translator.TranslateApiDefinition(apiDefinition, $"{apiDefinitionName}{apiDefinitionNamePostfix}", apiDefinitionRoot.DefinitionBaseClassName);
await WriteFile(apiDefinition, $"{apiDefinitionName}{apiDefinitionNamePostfix}.cs", apiDefinitionContent);
var iApiDefinitionContent = translator.TranslateApiDefinitionInterface(apiDefinition, $"{apiDefinitionName}{apiDefinitionNamePostfix}");
await WriteFile(apiDefinition, $"I{apiDefinitionName}{apiDefinitionNamePostfix}.cs", iApiDefinitionContent);
Expand Down Expand Up @@ -141,7 +139,7 @@ private async Task WriteFile(IDirectoryNode directoryNode, string path, string?
}

var filePath = directoryNode.GetFilePath(path);
await File.WriteAllTextAsync(filePath, header + text);
await File.WriteAllTextAsync(filePath, text);
}
}
}
2 changes: 2 additions & 0 deletions src/WebExtension.Net.Generator/Models/ApiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public ApiDefinition(JsonElement json)
Json = new [] { json };
Permissions = Enumerable.Empty<string>();
Types = Enumerable.Empty<TypeDefinition>();
Properties = Enumerable.Empty<PropertyDefinition>();
Functions = Enumerable.Empty<FunctionDefinition>();
Directory = string.Empty;
}
Expand All @@ -25,6 +26,7 @@ public ApiDefinition(JsonElement json)
public IEnumerable<JsonElement> Json { get; set; }
public IEnumerable<string> Permissions { get;set; }
public IEnumerable<TypeDefinition> Types { get;set; }
public IEnumerable<PropertyDefinition> Properties { get; set; }
public IEnumerable<FunctionDefinition> Functions { get;set; }
public string? Directory { get; set; }
public string? RootNamespace { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/WebExtension.Net.Generator/Models/ApiDefinitionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public ApiDefinitionRoot()
DefinitionUrls = new List<string>();
ApiDefinitions = new List<ApiDefinition>();
RootNamespace = string.Empty;
DefinitionBaseClassName = string.Empty;
DefinitionClassNamePostfix = string.Empty;
}

Expand All @@ -18,6 +19,7 @@ public ApiDefinitionRoot()
public string? Directory { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public string DefinitionBaseClassName { get; set; }
public string DefinitionClassNamePostfix { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public FunctionDefinition()
public bool Unsupported { get; set; }
public bool Deprecated { get; set; }
public string? DeprecatedMessage { get; set; }
public string? FunctionAccessor { get; set; }
public TypeReference? ReturnType { get; set; }
public List<ParameterDefinition> Parameters { get; set; }
}
Expand Down
2 changes: 2 additions & 0 deletions src/WebExtension.Net.Generator/Models/PropertyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class PropertyDefinition : ICommonDefinition
public string? DeprecatedMessage { get; set; }
public TypeReference? Type { get; set; }
public bool Optional { get; set; }
public bool Constant { get; set; }
public string? ConstantValue { get; set; }
}
}
31 changes: 27 additions & 4 deletions src/WebExtension.Net.Generator/Parsers/ApiDefinitionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public IEnumerable<ApiDefinition> ParseApiDefinitions(JsonElement jsonApiDefinit
apiDefinition.Types = ParseTypes(jsonTypeDefinitionArray).ToArray();
}

if (jsonApiDefinition.TryGetProperty("functions", out var jsonFunctionDefinitionArray))
if (jsonApiDefinition.TryGetProperty("properties", out var jsonPropertyDefinitionArray))
{
apiDefinition.Functions = ParseFunctions(jsonFunctionDefinitionArray).ToArray();
apiDefinition.Properties = ParseProperties(jsonPropertyDefinitionArray).ToArray();
}

foreach (var function in apiDefinition.Functions)
if (jsonApiDefinition.TryGetProperty("functions", out var jsonFunctionDefinitionArray))
{
function.FunctionAccessor = $"{apiDefinition.Name}.{function.Name}";
apiDefinition.Functions = ParseFunctions(jsonFunctionDefinitionArray).ToArray();
}

yield return apiDefinition;
Expand Down Expand Up @@ -140,6 +140,29 @@ private IEnumerable<PropertyDefinition> ParseProperties(JsonElement jsonProperty
propertyDefinition.Name = jsonProperty.Name;
propertyDefinition.Type = ParseTypeReference(jsonPropertyDefinition);
propertyDefinition.Optional = jsonPropertyDefinition.GetBooleanProperty("optional");
if (jsonPropertyDefinition.TryGetProperty("value", out var jsonValue))
{
propertyDefinition.Constant = true;
propertyDefinition.ConstantValue = jsonValue.ValueKind switch
{
JsonValueKind.String => $"\"{jsonValue.GetString()}\"",
_ => jsonValue.ToString() ?? "null"
};
if (propertyDefinition.Type is null)
{
propertyDefinition.Type = new TypeReference()
{
Type = jsonValue.ValueKind switch
{
JsonValueKind.String => "string",
JsonValueKind.Number => propertyDefinition.ConstantValue.Contains(".") ? "number" : "integer",
JsonValueKind.True => "boolean",
JsonValueKind.False => "boolean",
_ => "any"
}
};
}
}
yield return propertyDefinition;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/WebExtension.Net.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void Main(string[] args)
var rootNamespace = "WebExtension.Net";
var rootApiDefinitionName = "WebExtensionAPI";
var rootApiDefinitionDescription = "Web Extension API";
var apiDefinitionBaseClassName = "BaseAPI";
var apiDefinitionClassNamePostfix = "API";
var includeNamespaces = new List<string>()
{
Expand Down Expand Up @@ -108,6 +109,7 @@ public static void Main(string[] args)
{
apiDefinitionRoot.Name = rootApiDefinitionName;
apiDefinitionRoot.Description = rootApiDefinitionDescription;
apiDefinitionRoot.DefinitionBaseClassName = apiDefinitionBaseClassName;
apiDefinitionRoot.DefinitionClassNamePostfix = apiDefinitionClassNamePostfix;
apiDefinitionRoot.Directory = rootDirectory;
apiDefinitionRoot.RootNamespace = rootNamespace;
Expand Down
Loading

0 comments on commit 24efd91

Please sign in to comment.