Change to IncludeSymbols
and Added Json2CSharpCodeGenerator.Console
This commit is contained in:
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<LangVersion>10.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Exe</OutputType>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Infineon.Mesa.Library.Json2CSharpCodeGenerator" Version="2.1.0.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\Json2CSharpCodeGenerator.Tests\Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
99
Json2CSharpCodeGenerator.Console/Program.cs
Normal file
99
Json2CSharpCodeGenerator.Console/Program.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using Json2CSharpCodeGenerator.Lib;
|
||||
using Json2CSharpCodeGenerator.Lib.CodeWriters;
|
||||
using System.Text;
|
||||
|
||||
namespace Json2CSharpCodeGenerator.Console;
|
||||
|
||||
public class Program
|
||||
{
|
||||
|
||||
private static void CSharpCodeWriter(List<string> args)
|
||||
{
|
||||
List<string> matches = new();
|
||||
DirectoryInfo directoryInfo;
|
||||
foreach (string arg in args)
|
||||
{
|
||||
if (arg.Length < 3)
|
||||
continue;
|
||||
if (arg[1] == ':' || (arg[0] == '\\' && arg[1] == '\\'))
|
||||
directoryInfo = new(arg);
|
||||
else
|
||||
directoryInfo = new(Path.Combine(AppContext.BaseDirectory, arg));
|
||||
if (!directoryInfo.Root.Exists)
|
||||
continue;
|
||||
if (directoryInfo.Extension is not ".json" and not ".txt")
|
||||
continue;
|
||||
if (directoryInfo.CreationTime < new DateTime(1800, 1, 1))
|
||||
continue;
|
||||
matches.Add(directoryInfo.FullName);
|
||||
}
|
||||
if (!matches.Any())
|
||||
throw new Exception($"Check input <{string.Join(',', args)}>");
|
||||
JsonClassGenerator jsonClassGenerator = new()
|
||||
{
|
||||
AttributeLibrary = JsonLibrary.SystemTextJson,
|
||||
AttributeUsage = JsonPropertyAttributeUsage.OnlyWhenNecessary,
|
||||
CodeWriter = new CSharpCodeWriter(),
|
||||
CollectionType = OutputCollectionType.MutableList,
|
||||
OutputType = OutputTypes.ImmutableClass,
|
||||
UsePascalCase = true,
|
||||
UseThisKeyWord = false,
|
||||
};
|
||||
string input;
|
||||
string outputFile;
|
||||
string errorMessage;
|
||||
string oldOutputFile;
|
||||
StringBuilder stringBuilder;
|
||||
foreach (string match in matches)
|
||||
{
|
||||
outputFile = Path.ChangeExtension(match, ".cs");
|
||||
if (File.Exists(outputFile))
|
||||
{
|
||||
oldOutputFile = Path.ChangeExtension(match, ".old.cs");
|
||||
if (File.Exists(oldOutputFile))
|
||||
File.Delete(oldOutputFile);
|
||||
File.Move(outputFile, oldOutputFile);
|
||||
}
|
||||
input = File.ReadAllText(match);
|
||||
stringBuilder = jsonClassGenerator.GenerateClasses(input, out errorMessage);
|
||||
if (!string.IsNullOrEmpty(errorMessage))
|
||||
throw new Exception(errorMessage);
|
||||
File.WriteAllText(outputFile, stringBuilder.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public static void Secondary(List<string> args)
|
||||
{
|
||||
int silentIndex = args.IndexOf("s");
|
||||
if (silentIndex > -1)
|
||||
args.RemoveAt(silentIndex);
|
||||
try
|
||||
{
|
||||
CSharpCodeWriter(args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Console.WriteLine(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
|
||||
}
|
||||
// finally
|
||||
// {
|
||||
// Log.CloseAndFlush();
|
||||
// }
|
||||
if (silentIndex > -1)
|
||||
System.Console.WriteLine("Done. Bye");
|
||||
else
|
||||
{
|
||||
System.Console.WriteLine("Done. Press 'Enter' to end");
|
||||
_ = System.Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (args is not null)
|
||||
Secondary(args.ToList());
|
||||
else
|
||||
Secondary(new List<string>());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user