99 lines
3.2 KiB
C#
99 lines
3.2 KiB
C#
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>());
|
|
}
|
|
|
|
} |