-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds appschema, and generator for it
- Loading branch information
Showing
9 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Our.Umbraco.FullTextSearch.SchemaGenerator/AppSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Our.Umbraco.FullTextSearch.Options; | ||
|
||
namespace Our.Umbraco.FullTextSearch.SchemaGenerator | ||
{ | ||
internal class AppSettings | ||
{ | ||
public UmbracoDefinition Umbraco { get; set; } | ||
|
||
/// <summary> | ||
/// Configuration of settings | ||
/// </summary> | ||
internal class UmbracoDefinition | ||
{ | ||
/// <summary> | ||
/// FullTextSearch settings | ||
/// </summary> | ||
public FullTextSearchOptions FullTextSearch { get; set; } | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using CommandLine; | ||
|
||
namespace Our.Umbraco.FullTextSearch.SchemaGenerator | ||
{ | ||
internal class Options | ||
{ | ||
[Option('o', "outputFile", Required = false, | ||
HelpText = "", | ||
Default = "Our.Umbraco.FullTextSearch\\appsettings-schema.umbraco-fulltextsearch.json")] | ||
public string OutputFile { get; set; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
....Umbraco.FullTextSearch.SchemaGenerator/Our.Umbraco.FullTextSearch.SchemaGenerator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NJsonSchema" Version="10.9.0" /> | ||
<PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Our.Umbraco.FullTextSearch\Our.Umbraco.FullTextSearch.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>bin\Release\$(TargetFramework)\Our.Umbraco.FullTextSearch.SchemaGenerator.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<Target Name="CopyPackagesXml" BeforeTargets="Build"> | ||
<ItemGroup> | ||
<PackageReferenceFiles Include="$(NugetPackageRoot)%(PackageReference.Identity)\%(PackageReference.Version)%(PackageReference.CopyToOutputDirectory)\lib\**\*.xml" /> | ||
</ItemGroup> | ||
<Copy SourceFiles="@(PackageReferenceFiles)" DestinationFolder="$(OutDir)" /> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using CommandLine; | ||
|
||
using NPoco.fastJSON; | ||
|
||
namespace Our.Umbraco.FullTextSearch.SchemaGenerator | ||
{ | ||
/// <summary> | ||
/// Generate the JSON Schema file for Full Text Search. | ||
/// just like in the Umbraco Core - https://github.com/umbraco/Umbraco-CMS/tree/v9/contrib/src/JsonSchema | ||
/// #h5yr Kevin Jump! | ||
/// </summary> | ||
internal class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
try | ||
{ | ||
await Parser.Default.ParseArguments<Options>(args) | ||
.WithParsedAsync(Execute); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
throw; | ||
} | ||
} | ||
|
||
private static async Task Execute(Options options) | ||
{ | ||
var generator = new SchemaGenerator(); | ||
|
||
var schema = generator.Generate(); | ||
|
||
var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, options.OutputFile)); | ||
Console.WriteLine("Path to use {0}", path); | ||
Directory.CreateDirectory(Path.GetDirectoryName(path)); | ||
Console.WriteLine("Ensured directory exists"); | ||
await File.WriteAllTextAsync(path, schema); | ||
|
||
Console.WriteLine("File written at {0}", path); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/Our.Umbraco.FullTextSearch.SchemaGenerator/SchemaGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Contracts; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
using NJsonSchema.Generation; | ||
|
||
namespace Our.Umbraco.FullTextSearch.SchemaGenerator | ||
{ | ||
internal class SchemaGenerator | ||
{ | ||
private readonly JsonSchemaGenerator _schemaGenerator; | ||
|
||
public SchemaGenerator() | ||
{ | ||
_schemaGenerator = new JsonSchemaGenerator( | ||
new FullTextSearchSchemaGeneratorSettings()); | ||
} | ||
|
||
public string Generate() | ||
{ | ||
var schema = GenerateFullTextSearchSchema(); | ||
return schema.ToString(); | ||
} | ||
|
||
private JObject GenerateFullTextSearchSchema() | ||
{ | ||
var schema = _schemaGenerator.Generate(typeof(AppSettings)); | ||
return JsonConvert.DeserializeObject<JObject>(schema.ToJson()); | ||
} | ||
|
||
} | ||
|
||
internal class FullTextSearchSchemaGeneratorSettings : JsonSchemaGeneratorSettings | ||
{ | ||
public FullTextSearchSchemaGeneratorSettings() | ||
{ | ||
AlwaysAllowAdditionalObjectProperties = true; | ||
SerializerSettings = new JsonSerializerSettings() | ||
{ | ||
ContractResolver = new WritablePropertiesOnlyResolver(), | ||
}; | ||
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull; | ||
SchemaNameGenerator = new NamespacePrefixedSchemaNameGenerator(); | ||
SerializerSettings.Converters.Add(new StringEnumConverter()); | ||
IgnoreObsoleteProperties = true; | ||
GenerateExamples = true; | ||
} | ||
|
||
private class WritablePropertiesOnlyResolver : DefaultContractResolver | ||
{ | ||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) | ||
{ | ||
IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); | ||
var result = props.Where(p => p.Writable).ToList(); | ||
result.ForEach(x => x.PropertyName = ToPascalCase(x.PropertyName)); | ||
return result; | ||
} | ||
|
||
private string ToPascalCase(string str) | ||
{ | ||
if (!string.IsNullOrEmpty(str)) | ||
{ | ||
return char.ToUpperInvariant(str[0]) + str.Substring(1); | ||
} | ||
|
||
return str; | ||
|
||
} | ||
} | ||
} | ||
|
||
internal class NamespacePrefixedSchemaNameGenerator : DefaultSchemaNameGenerator | ||
{ | ||
public override string Generate(Type type) => type.Namespace.Replace(".", string.Empty) + base.Generate(type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Our.Umbraco.FullTextSearch/appsettings-schema.umbraco-fulltextsearch.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "OurUmbracoFullTextSearchSchemaGeneratorAppSettings", | ||
"type": "object", | ||
"properties": { | ||
"Umbraco": { | ||
"$ref": "#/definitions/OurUmbracoFullTextSearchSchemaGeneratorUmbracoDefinition" | ||
} | ||
}, | ||
"definitions": { | ||
"OurUmbracoFullTextSearchSchemaGeneratorUmbracoDefinition": { | ||
"type": "object", | ||
"description": "Configuration of settings", | ||
"properties": { | ||
"FullTextSearch": { | ||
"description": "FullTextSearch settings", | ||
"oneOf": [ | ||
{ | ||
"$ref": "#/definitions/OurUmbracoFullTextSearchOptionsFullTextSearchOptions" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"OurUmbracoFullTextSearchOptionsFullTextSearchOptions": { | ||
"type": "object", | ||
"properties": { | ||
"Enabled": { | ||
"type": "boolean" | ||
}, | ||
"DefaultTitleField": { | ||
"type": "string" | ||
}, | ||
"RenderingActiveKey": { | ||
"type": "string" | ||
}, | ||
"DisallowedContentTypeAliases": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"DisallowedPropertyAliases": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"XPathsToRemove": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"FullTextContentField": { | ||
"type": "string" | ||
}, | ||
"FullTextPathField": { | ||
"type": "string" | ||
}, | ||
"HighlightPattern": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Our.Umbraco.FullTextSearch/buildTransitive/Our.Umbraco.FullTextSearch.props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<UmbracoJsonSchemaFiles Include="$(MSBuildThisFileDirectory)..\appsettings-schema.umbraco-fulltextsearch.json" Weight="-49" /> | ||
</ItemGroup> | ||
</Project> |