Skip to content

Commit a53b3cc

Browse files
committed
Common
- GlobalOptions renamed to GlobalSettings - Service for license check added - File compare before overwrite fixed - Formatter support for tools like Prettier added Fluent - force overwrite a file added - Formatter support added - rename a member added - change return type added - documentation improved
1 parent c0d9fee commit a53b3cc

30 files changed

+488
-167
lines changed

AspDotNet/Fluent/IAspDotNetReadSyntax.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace KY.Generator.AspDotNet.Fluent
1+
using System;
2+
3+
namespace KY.Generator.AspDotNet.Fluent
24
{
35
public interface IAspDotNetReadSyntax
46
{

Common.Tests/Models/TestOutput.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TestOutput : IOutput
1010

1111
public long Lines { get; }
1212

13-
public void Write(string fileName, string content, bool ignoreOutputId = false, bool forceOverwrite = false)
13+
public void Write(string fileName, string content, IOptions options, bool ignoreOutputId = false, bool forceOverwrite = false)
1414
{
1515
this.Files.Add(new TestFile(fileName, content));
1616
}

Common/Commands/OptionsCommand.cs

+7-18
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,43 @@
22
using System.Collections.Generic;
33
using KY.Core;
44
using KY.Generator.Command;
5+
using KY.Generator.Settings;
56
using KY.Generator.Statistics;
67

78
namespace KY.Generator.Commands
89
{
910
internal class OptionsCommand : GeneratorCommand<OptionsCommandParameters>
1011
{
11-
private readonly GlobalOptions globalOptions;
1212
private readonly StatisticsService statisticsService;
1313
private readonly GlobalStatisticsService globalStatisticsService;
14+
private readonly GlobalSettingsService globalSettingsService;
1415
public override string[] Names { get; } = { "set" };
1516

16-
public OptionsCommand(GlobalOptions globalOptions, StatisticsService statisticsService, GlobalStatisticsService globalStatisticsService)
17+
public OptionsCommand(StatisticsService statisticsService, GlobalStatisticsService globalStatisticsService, GlobalSettingsService globalSettingsService)
1718
{
18-
this.globalOptions = globalOptions;
1919
this.statisticsService = statisticsService;
2020
this.globalStatisticsService = globalStatisticsService;
21+
this.globalSettingsService = globalSettingsService;
2122
}
2223

2324
public override IGeneratorCommandResult Run()
2425
{
2526
if ("statistics".Equals(this.Parameters.Option, StringComparison.CurrentCultureIgnoreCase))
2627
{
28+
List<Guid> ids = this.globalStatisticsService.GetIds();
2729
if ("disable".Equals(this.Parameters.Value, StringComparison.CurrentCultureIgnoreCase))
2830
{
29-
this.globalOptions.StatisticsEnabled = false;
31+
this.statisticsService.Disable(ids);
3032
}
3133
else if ("enable".Equals(this.Parameters.Value, StringComparison.CurrentCultureIgnoreCase))
3234
{
33-
this.globalOptions.StatisticsEnabled = true;
35+
this.statisticsService.Enable(ids);
3436
}
3537
else
3638
{
3739
Logger.Error($"Invalid value '{this.Parameters.Value}' for option '{this.Parameters.Option}'. Valid values are 'enable' or 'disable'");
3840
return this.Error();
3941
}
40-
this.globalOptions.Save();
41-
List<Guid> ids = this.globalStatisticsService.GetIds();
42-
if (ids.Count > 0)
43-
{
44-
if (this.globalOptions.StatisticsEnabled)
45-
{
46-
this.statisticsService.Enable(ids);
47-
}
48-
else
49-
{
50-
this.statisticsService.Disable(ids);
51-
}
52-
}
5342
}
5443
else
5544
{

Common/Commands/StatisticsCommand.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using KY.Core.DataAccess;
22
using KY.Generator.Command;
3+
using KY.Generator.Settings;
34
using KY.Generator.Statistics;
45

56
namespace KY.Generator.Commands
@@ -8,12 +9,14 @@ internal class StatisticsCommand : GeneratorCommand<StatisticsCommandParameters>
89
{
910
private readonly GlobalStatisticsService globalStatisticsService;
1011
private readonly StatisticsService statisticsService;
12+
private readonly GlobalSettingsService globalSettingsService;
1113
public override string[] Names { get; } = { "statistics", "statistic", "stats", "stat" };
1214

13-
public StatisticsCommand(GlobalStatisticsService globalStatisticsService, StatisticsService statisticsService)
15+
public StatisticsCommand(GlobalStatisticsService globalStatisticsService, StatisticsService statisticsService, GlobalSettingsService globalSettingsService)
1416
{
1517
this.globalStatisticsService = globalStatisticsService;
1618
this.statisticsService = statisticsService;
19+
this.globalSettingsService = globalSettingsService;
1720
}
1821

1922
public override IGeneratorCommandResult Run()
@@ -27,8 +30,11 @@ public override IGeneratorCommandResult Run()
2730
this.globalStatisticsService.Append(statistic);
2831
this.globalStatisticsService.Analyze();
2932
this.globalStatisticsService.Write();
30-
this.statisticsService.Anonymize(statistic);
31-
this.statisticsService.Submit(statistic);
33+
if (this.globalSettingsService.Read().StatisticsEnabled)
34+
{
35+
this.statisticsService.Anonymize(statistic);
36+
this.statisticsService.Submit(statistic);
37+
}
3238
return this.Success();
3339
}
3440
}

Common/Extensions/FileTemplateExtension.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void Write(this FileTemplate file, IOutput output)
1717
// Logger.Trace($"Start generate file {file.Name}");
1818
FileWriter writer = new(file.Options);
1919
writer.Add(file);
20-
output.Write(file.FullPath, writer.ToString(), !file.WriteOutputId, file.ForceOverwrite);
20+
output.Write(file.FullPath, writer.ToString(), file.Options, !file.WriteOutputId, file.ForceOverwrite);
2121
// Logger.Trace($"Finish generate file {file.Name}");
2222
}
2323
}

Common/Generator.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
using KY.Generator.Commands;
1616
using KY.Generator.Extensions;
1717
using KY.Generator.Languages;
18+
using KY.Generator.Licensing;
1819
using KY.Generator.Mappings;
1920
using KY.Generator.Models;
2021
using KY.Generator.Output;
22+
using KY.Generator.Settings;
2123
using KY.Generator.Statistics;
2224
using KY.Generator.Syntax;
2325
using KY.Generator.Templates;
@@ -35,9 +37,8 @@ public class Generator : IGeneratorRunSyntax
3537

3638
public Generator()
3739
{
40+
DateTime start = DateTime.Now;
3841
Logger.CatchAll();
39-
this.statisticsService = new StatisticsService(this.environment);
40-
this.statisticsService.ProgramStart();
4142
Assembly callingAssembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
4243
FrameworkName framework = callingAssembly.GetTargetFramework();
4344
Logger.Trace($"KY-Generator v{callingAssembly.GetName().Version} ({framework.Identifier.Replace("App", string.Empty)} {framework.Version.Major}.{framework.Version.Minor})");
@@ -56,9 +57,14 @@ public Generator()
5657
this.output = new FileOutput(this.resolver.Get<IEnvironment>(), Environment.CurrentDirectory);
5758
this.resolver.Bind<IOutput>().To(this.output);
5859
this.resolver.Bind<List<FileTemplate>>().To(new List<FileTemplate>());
59-
this.resolver.Bind<StatisticsService>().To(this.statisticsService);
60+
this.resolver.Bind<StatisticsService>().ToSingleton();
6061
this.resolver.Bind<GlobalStatisticsService>().ToSingleton();
61-
this.resolver.Bind<IGlobalOptions>().ToSingleton<GlobalOptions>();
62+
this.resolver.Bind<GlobalSettingsService>().ToSingleton();
63+
this.resolver.Bind<LicenseService>().ToSingleton();
64+
this.resolver.Get<LicenseService>().Check();
65+
66+
this.statisticsService = this.resolver.Get<StatisticsService>();
67+
this.statisticsService.ProgramStart(start);
6268

6369
ModuleFinder moduleFinder = this.resolver.Get<ModuleFinder>();
6470
this.InitializeModules(moduleFinder.Modules);
@@ -140,6 +146,10 @@ public bool Run()
140146
IGeneratorCommandResult switchContext = null;
141147
bool switchAsync = false;
142148
this.commands.ForEach(command => command.Prepare());
149+
if (!this.resolver.Get<LicenseService>().Wait(TimeSpan.FromMilliseconds(250)))
150+
{
151+
Logger.Warning($"Can not check license. Some modules may be deactivated.");
152+
}
143153
foreach (IGeneratorCommand command in this.commands)
144154
{
145155
IGeneratorCommandResult result = runner.Run(command);
@@ -176,7 +186,7 @@ public bool Run()
176186
return this.SwitchContext(switchContext, asyncCommands);
177187
}
178188
this.statisticsService.ProgramEnd(files.Count);
179-
if (!this.commands.OfType<StatisticsCommand>().Any() && this.resolver.Get<GlobalOptions>().StatisticsEnabled)
189+
if (!this.commands.OfType<StatisticsCommand>().Any() && this.resolver.Get<GlobalSettingsService>().Read().StatisticsEnabled)
180190
{
181191
string fileName = this.statisticsService.Write();
182192
this.resolver.Get<GlobalStatisticsService>().StartCalculation(fileName);

Common/Languages/BaseLanguage.cs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using KY.Generator.Templates;
1010
using KY.Generator.Writers;
1111
using FileWriter = KY.Generator.Writers.FileWriter;
12-
using OutputFileWriter = KY.Generator.Output.FileWriter;
1312

1413
namespace KY.Generator.Languages
1514
{

Common/Licensing/LicenseService.cs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using KY.Core;
8+
using KY.Generator.Settings;
9+
using Newtonsoft.Json;
10+
11+
namespace KY.Generator.Licensing
12+
{
13+
public class LicenseService
14+
{
15+
private readonly GlobalSettingsService globalSettingsService;
16+
private readonly ManualResetEvent waitForCheck = new(false);
17+
private string license;
18+
19+
public LicenseService(GlobalSettingsService globalSettingsService)
20+
{
21+
this.globalSettingsService = globalSettingsService;
22+
}
23+
24+
public async void Check()
25+
{
26+
try
27+
{
28+
this.license = await this.SendCommand<string>($"{this.globalSettingsService.Read().License}/check");
29+
this.waitForCheck.Set();
30+
}
31+
catch (Exception exception)
32+
{
33+
Logger.Warning(exception.Message + Environment.NewLine + exception.StackTrace);
34+
}
35+
}
36+
37+
public string Get()
38+
{
39+
return this.license;
40+
}
41+
42+
/// <summary>Blocks the current thread until the license is checked, using a <see cref="T:System.TimeSpan" /> to specify a optional timeout.</summary>
43+
/// <param name="timeout">A <see cref="T:System.TimeSpan" /> that represents the number of milliseconds to wait, or a null to wait indefinitely.</param>
44+
/// <returns>
45+
/// <see langword="true" /> if the was checked successful; otherwise, <see langword="false" />.</returns>
46+
public bool Wait(TimeSpan? timeout = null)
47+
{
48+
return timeout.HasValue ? this.waitForCheck.WaitOne(timeout.Value) : this.waitForCheck.WaitOne();
49+
}
50+
51+
private async Task<T> SendCommand<T>(string command, string query = "")
52+
{
53+
#if DEBUG
54+
string baseUri = "http://localhost:8087/api/v1/license";
55+
#else
56+
string baseUri = "https://generator.ky-programming.de/api/v1/license";
57+
#endif
58+
HttpWebRequest request = WebRequest.CreateHttp($"{baseUri}/{command}?{query}");
59+
request.Method = WebRequestMethods.Http.Get;
60+
WebResponse response = request.GetResponse();
61+
using Stream responseStream = response.GetResponseStream();
62+
string responseString = await responseStream.ReadStringAsync();
63+
if (responseString.StartsWith("[") || responseString.StartsWith("{") || responseString.StartsWith("\""))
64+
{
65+
return JsonConvert.DeserializeObject<T>(responseString);
66+
}
67+
Logger.Warning(responseString);
68+
throw new InvalidOperationException("Can not parse the response. No valid json");
69+
}
70+
}
71+
}

Common/Options/GlobalOptions.cs

-41
This file was deleted.

Common/Options/IOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using KY.Generator.Languages;
3+
using KY.Generator.Transfer;
34

45
namespace KY.Generator
56
{
@@ -19,6 +20,10 @@ public interface IOptions
1920
bool AddHeader { get; set; }
2021
bool SkipNamespace { get; set; }
2122
bool OnlySubTypes { get; set; }
23+
string Rename { get; set; }
24+
TypeTransferObject ReturnType { get; set; }
25+
string Formatter { get; set; }
26+
bool ForceOverwrite { get; set; }
2227

2328
// TODO: Should be moved to ITypeScriptOptions
2429
bool Strict { get; set; }

Common/Options/OptionsPart.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using KY.Generator.Languages;
3+
using KY.Generator.Transfer;
44

55
namespace KY.Generator
66
{
@@ -21,5 +21,9 @@ public class OptionsPart
2121
public bool? NoIndex { get; set; }
2222
public Dictionary<string, string> ReplaceName { get; } = new();
2323
public ILanguage Language { get; set; }
24+
public string Rename { get; set; }
25+
public TypeTransferObject ReturnType { get; set; }
26+
public string Formatter { get; set; }
27+
public bool? ForceOverwrite { get; set; }
2428
}
2529
}

Common/Options/OptionsSet.cs

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using KY.Core;
33
using KY.Generator.Languages;
4+
using KY.Generator.Transfer;
45

56
namespace KY.Generator
67
{
@@ -82,6 +83,24 @@ bool IOptions.OnlySubTypes
8283
set => this.Part.OnlySubTypes = value;
8384
}
8485

86+
string IOptions.Rename
87+
{
88+
get => this.Part.Rename;
89+
set => this.Part.Rename = value;
90+
}
91+
92+
TypeTransferObject IOptions.ReturnType
93+
{
94+
get => this.Part.ReturnType;
95+
set => this.Part.ReturnType = value;
96+
}
97+
98+
string IOptions.Formatter
99+
{
100+
get => this.GetValue(x => x?.Formatter);
101+
set => this.Part.Formatter = value;
102+
}
103+
85104
bool IOptions.NoIndex
86105
{
87106
get => this.Part.NoIndex ?? this.Global?.Part.NoIndex ?? false;
@@ -97,6 +116,12 @@ ILanguage IOptions.Language
97116
set => this.Part.Language = value;
98117
}
99118

119+
bool IOptions.ForceOverwrite
120+
{
121+
get => this.GetPrimitive(x => x?.ForceOverwrite);
122+
set => this.Part.ForceOverwrite = value;
123+
}
124+
100125
public OptionsSet(OptionsSet parent, OptionsSet global, OptionsSet caller = null, object target = null)
101126
: base(parent, global, caller, target)
102127
{

0 commit comments

Comments
 (0)