Change to IncludeSymbols

and Added Json2CSharpCodeGenerator.Console
This commit is contained in:
Mike Phares 2022-08-13 16:20:35 -07:00
parent 8dfc8fe9b8
commit 8b36f3d30a
7 changed files with 151 additions and 16 deletions

15
.vscode/launch.json vendored
View File

@ -5,7 +5,7 @@
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"name": ".NET Core Launch (WinForms)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
@ -17,6 +17,19 @@
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/Json2CSharpCodeGenerator.Console/bin/Debug/net6.0/win-x64/Json2CSharpCodeGenerator.Console.dll",
"args": [
"Test_10_SETTINGS_IMMUTABLE_CLASSES_PHARES_INPUT.txt"
],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": ".NET Core Attach",
"type": "coreclr",

18
.vscode/tasks.json vendored
View File

@ -14,28 +14,16 @@
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Json2CSharpCodeGenerator.WinForms/Json2CSharpCodeGenerator.WinForms.csproj",
"build",
"${workspaceFolder}/Json2CSharpCodeGenerator.Console/Json2CSharpCodeGenerator.Console.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Json2CSharpCodeGenerator.WinForms/Json2CSharpCodeGenerator.WinForms.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

View File

@ -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>

View 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>());
}
}

View File

@ -6,6 +6,15 @@
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.1.0.1</Version>
<Company>Infineon Technologies Americas Corp.</Company>
<Authors>Mike Phares</Authors>
<PackageId>Infineon.Mesa.Library.Json2CSharpCodeGenerator</PackageId>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CG.Pluralization" Version="0.3000.12" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<LangVersion>10.0</LangVersion>
<Nullable>disable</Nullable>
<OutputType>WinExe</OutputType>

View File

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.Wi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.Tests", "Json2CSharpCodeGenerator.Tests\Json2CSharpCodeGenerator.Tests.csproj", "{81FCF430-5425-48A6-9BE7-745937D78D97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.Console", "Json2CSharpCodeGenerator.Console\Json2CSharpCodeGenerator.Console.csproj", "{CFFB74F0-D227-4F6A-A51A-E44837FFD51B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -30,5 +32,9 @@ Global
{81FCF430-5425-48A6-9BE7-745937D78D97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Release|Any CPU.Build.0 = Release|Any CPU
{CFFB74F0-D227-4F6A-A51A-E44837FFD51B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CFFB74F0-D227-4F6A-A51A-E44837FFD51B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CFFB74F0-D227-4F6A-A51A-E44837FFD51B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CFFB74F0-D227-4F6A-A51A-E44837FFD51B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal