mirror of
https://github.com/amerkoleci/Vortice.Win32.git
synced 2026-01-14 16:16:04 +08:00
Initial setup
This commit is contained in:
164
src/Generator/CodeWriter.cs
Normal file
164
src/Generator/CodeWriter.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
// Copyright © Amer Koleci and Contributors.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
||||
|
||||
namespace Generator;
|
||||
|
||||
public sealed class CodeWriter : IDisposable
|
||||
{
|
||||
private bool _shouldIndent = true;
|
||||
private readonly string[] _indentStrings;
|
||||
private string _indentString = "";
|
||||
private readonly StreamWriter _writer;
|
||||
|
||||
public int IndentLevel { get; private set; }
|
||||
|
||||
public CodeWriter(string fileName, bool enableNullable, params string[] namespaces)
|
||||
{
|
||||
_indentStrings = new string[10];
|
||||
for (int i = 0; i < _indentStrings.Length; i++)
|
||||
{
|
||||
_indentStrings[i] = new string('\t', i);
|
||||
}
|
||||
|
||||
_writer = File.CreateText(fileName);
|
||||
_writer.WriteLine("// ------------------------------------------------------------------------------");
|
||||
_writer.WriteLine("// <auto-generated>");
|
||||
_writer.WriteLine("// This code was generated by a tool.");
|
||||
_writer.WriteLine("//");
|
||||
_writer.WriteLine("// Changes to this file may cause incorrect behavior and will be lost if");
|
||||
_writer.WriteLine("// the code is regenerated.");
|
||||
_writer.WriteLine("// </auto-generated>");
|
||||
_writer.WriteLine("// ------------------------------------------------------------------------------");
|
||||
_writer.WriteLine();
|
||||
|
||||
if (enableNullable)
|
||||
{
|
||||
_writer.WriteLine($"#nullable enable");
|
||||
_writer.WriteLine();
|
||||
}
|
||||
|
||||
foreach (string ns in namespaces)
|
||||
{
|
||||
_writer.WriteLine($"using {ns};");
|
||||
}
|
||||
|
||||
if (namespaces.Length > 0)
|
||||
{
|
||||
_writer.WriteLine();
|
||||
}
|
||||
|
||||
_writer.WriteLine("namespace Vortice.Vulkan;");
|
||||
_writer.WriteLine();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_writer.Dispose();
|
||||
}
|
||||
|
||||
public void Write(char chr)
|
||||
{
|
||||
WriteIndented(chr);
|
||||
}
|
||||
|
||||
public void Write(string @string)
|
||||
{
|
||||
WriteIndented(@string);
|
||||
}
|
||||
|
||||
public void WriteLine()
|
||||
{
|
||||
_writer.WriteLine();
|
||||
_shouldIndent = true;
|
||||
}
|
||||
|
||||
public void WriteLine(string @string)
|
||||
{
|
||||
WriteIndented(@string);
|
||||
_writer.WriteLine();
|
||||
_shouldIndent = true;
|
||||
}
|
||||
|
||||
public void BeginBlock(string content)
|
||||
{
|
||||
WriteLine(content);
|
||||
WriteLine("{");
|
||||
Indent(1);
|
||||
}
|
||||
|
||||
public void EndBlock()
|
||||
{
|
||||
Dedent(1);
|
||||
WriteLine("}");
|
||||
}
|
||||
|
||||
public IDisposable PushBlock(string marker = "{") => new CodeBlock(this, marker);
|
||||
|
||||
public void Indent(int count = 1)
|
||||
{
|
||||
IndentLevel += count;
|
||||
|
||||
if (IndentLevel < _indentStrings.Length)
|
||||
{
|
||||
_indentString = _indentStrings[IndentLevel];
|
||||
}
|
||||
else
|
||||
{
|
||||
_indentString = new string('\t', IndentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dedent(int count = 1)
|
||||
{
|
||||
if (count > IndentLevel)
|
||||
throw new ArgumentException("count out of range.", nameof(count));
|
||||
|
||||
IndentLevel -= count;
|
||||
if (IndentLevel < _indentStrings.Length)
|
||||
{
|
||||
_indentString = _indentStrings[IndentLevel];
|
||||
}
|
||||
else
|
||||
{
|
||||
_indentString = new string('\t', IndentLevel);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteIndented(char chr)
|
||||
{
|
||||
if (_shouldIndent)
|
||||
{
|
||||
_writer.Write(_indentString);
|
||||
_shouldIndent = false;
|
||||
}
|
||||
|
||||
_writer.Write(chr);
|
||||
}
|
||||
|
||||
private void WriteIndented(string @string)
|
||||
{
|
||||
if (_shouldIndent)
|
||||
{
|
||||
_writer.Write(_indentString);
|
||||
_shouldIndent = false;
|
||||
}
|
||||
|
||||
_writer.Write(@string);
|
||||
}
|
||||
|
||||
private class CodeBlock : IDisposable
|
||||
{
|
||||
private readonly CodeWriter _writer;
|
||||
|
||||
public CodeBlock(CodeWriter writer, string content)
|
||||
{
|
||||
_writer = writer;
|
||||
_writer.BeginBlock(content);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_writer.EndBlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/Generator/Generator.csproj
Normal file
19
src/Generator/Generator.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="vulkan/*.*">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="vk_video/*.*">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
28
src/Generator/Program.cs
Normal file
28
src/Generator/Program.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright © Amer Koleci and Contributors.
|
||||
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
|
||||
|
||||
namespace Generator;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
string outputPath = AppContext.BaseDirectory;
|
||||
if (args.Length > 0)
|
||||
{
|
||||
outputPath = args[0];
|
||||
}
|
||||
|
||||
if (!Path.IsPathRooted(outputPath))
|
||||
{
|
||||
outputPath = Path.Combine(AppContext.BaseDirectory, outputPath);
|
||||
}
|
||||
|
||||
if (!Directory.Exists(outputPath))
|
||||
{
|
||||
Directory.CreateDirectory(outputPath);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user