Skip to content

Commit

Permalink
Fixes namespace and prefix filenames to avoid collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Sep 3, 2021
1 parent 6d469cf commit bf59af5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 42 deletions.
5 changes: 3 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
<IncludeSymbols Condition=" '$(DebugType)' != 'embedded' ">true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<MetadataVersion>10.2.118-preview</MetadataVersion>
<ApiDocsVersion>0.1.4-alpha</ApiDocsVersion>
<MetadataVersion>10.2.179-preview-g98239ddf1d</MetadataVersion>
<DiaMetadataVersion>0.2.185-preview-g7e1e6a442c</DiaMetadataVersion>
<ApiDocsVersion>0.1.5-alpha-g83f1525e8f</ApiDocsVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private enum FriendlyOverloadOf

private bool GroupByModule => string.IsNullOrEmpty(this.options.ClassName);

private string Namespace => this.options.Namespace;
private string Namespace => this.InputAssemblyName;

private string SingleClassName => this.options.ClassName ?? throw new InvalidOperationException("Not in one-class mode.");

Expand Down Expand Up @@ -1000,7 +1000,7 @@ nsContents.Key is object
usingDirectives.Add(UsingDirective(ParseName(GlobalNamespacePrefix + "System.Runtime.Versioning")));
}

usingDirectives.Add(UsingDirective(NameEquals(GlobalWin32NamespaceAlias), ParseName(GlobalNamespacePrefix + this.Namespace)));
usingDirectives.Add(UsingDirective(NameEquals(GlobalWin32NamespaceAlias), ParseName(GlobalNamespacePrefix + CommonNamespace)));

var normalizedResults = new Dictionary<string, CompilationUnitSyntax>(StringComparer.OrdinalIgnoreCase);
results.AsParallel().WithCancellation(cancellationToken).ForAll(kv =>
Expand Down
10 changes: 0 additions & 10 deletions src/Microsoft.Windows.CsWin32/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public record GeneratorOptions
/// <value>The default value is "PInvoke".</value>
public string? ClassName { get; init; } = "PInvoke";

/// <summary>
/// Gets the namespace for generated code.
/// </summary>
/// <value>The default value is "Windows.Win32". Must be non-empty.</value>
public string Namespace { get; init; } = "Windows.Win32";

/// <summary>
/// Gets a value indicating whether to emit a single source file as opposed to types spread across many files.
/// </summary>
Expand Down Expand Up @@ -57,10 +51,6 @@ public record GeneratorOptions
/// <exception cref="InvalidOperationException">Thrown when some setting is invalid.</exception>
public void Validate()
{
if (string.IsNullOrWhiteSpace(this.Namespace))
{
throw new InvalidOperationException("The namespace must be set.");
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Windows.CsWin32/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void Execute(GeneratorExecutionContext context)
.ThenBy(pair => pair.Key, StringComparer.Ordinal);
foreach (var unit in compilationUnits)
{
context.AddSource(unit.Key, unit.Value.ToFullString());
context.AddSource($"{generator.InputAssemblyName}.{unit.Key}", unit.Value.ToFullString());
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/GenerationSandbox.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public void DISPLAYCONFIG_VIDEO_SIGNAL_INFO_Test()
// TODO: write code that sets/gets memory on the inner struct (e.g. videoStandard).
}

[Fact]
public void E_PDB_LIMIT()
{
Assert.Equal(-2140340211, global::Microsoft.Dia.Constants.E_PDB_LIMIT.Value);
}

[Fact]
public void Bool()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="$(MetadataVersion)" GeneratePathProperty="true" PrivateAssets="none" />
<PackageReference Include="Microsoft.Dia.Win32Metadata" Version="$(DiaMetadataVersion)" PrivateAssets="none" />
<PackageReference Include="coverlet.msbuild" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="System.Memory" Version="4.5.4" />
Expand Down
1 change: 1 addition & 0 deletions test/GenerationSandbox.Tests/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ EnumWindows
GetWindowTextLength
GetWindowText
WPARAM
E_PDB_LIMIT
6 changes: 0 additions & 6 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@ public void Validate_Default()
{
new GeneratorOptions().Validate();
}

[Fact]
public void Validate_EmptyNamespace()
{
Assert.Throws<InvalidOperationException>(() => new GeneratorOptions { Namespace = string.Empty }.Validate());
}
}
42 changes: 21 additions & 21 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public async Task TestSimpleStructure()
},
GeneratedSources =
{
(typeof(SourceGenerator), "BOOL.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.BOOL.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -932,7 +932,7 @@ public async Task TestSimpleEnum()
},
GeneratedSources =
{
(typeof(SourceGenerator), "DISPLAYCONFIG_SCANLINE_ORDERING.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.DISPLAYCONFIG_SCANLINE_ORDERING.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -997,7 +997,7 @@ public async Task TestSimpleEnumWithoutDocs()
},
GeneratedSources =
{
(typeof(SourceGenerator), "DISPLAYCONFIG_SCANLINE_ORDERING.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.DISPLAYCONFIG_SCANLINE_ORDERING.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1052,7 +1052,7 @@ public async Task TestFlagsEnum()
},
GeneratedSources =
{
(typeof(SourceGenerator), "FILE_ACCESS_FLAGS.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.FILE_ACCESS_FLAGS.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1128,7 +1128,7 @@ public async Task TestSimpleDelegate()
},
GeneratedSources =
{
(typeof(SourceGenerator), "BOOL.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.BOOL.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1166,7 +1166,7 @@ public static implicit operator bool(BOOL value)
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "Delegates.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.Delegates.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand All @@ -1191,7 +1191,7 @@ namespace UI.WindowsAndMessaging
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "HWND.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.HWND.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1231,7 +1231,7 @@ internal readonly partial struct HWND
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "LPARAM.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.LPARAM.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1294,7 +1294,7 @@ public async Task TestSimpleMethod()
},
GeneratedSources =
{
(typeof(SourceGenerator), "HDC.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.HDC.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1336,7 +1336,7 @@ internal readonly partial struct HDC
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "HWND.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.HWND.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1376,7 +1376,7 @@ internal readonly partial struct HWND
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "PInvoke.User32.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.PInvoke.User32.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1438,7 +1438,7 @@ public async Task TestMethodWithOverloads()
},
GeneratedSources =
{
(typeof(SourceGenerator), "BOOL.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.BOOL.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1476,7 +1476,7 @@ public static implicit operator bool(BOOL value)
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "FILE_ACCESS_FLAGS.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.FILE_ACCESS_FLAGS.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1529,7 +1529,7 @@ internal enum FILE_ACCESS_FLAGS : uint
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "FILE_CREATION_DISPOSITION.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.FILE_CREATION_DISPOSITION.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1560,7 +1560,7 @@ internal enum FILE_CREATION_DISPOSITION : uint
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "FILE_FLAGS_AND_ATTRIBUTES.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.FILE_FLAGS_AND_ATTRIBUTES.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1629,7 +1629,7 @@ internal enum FILE_FLAGS_AND_ATTRIBUTES : uint
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "FILE_SHARE_MODE.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.FILE_SHARE_MODE.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1660,7 +1660,7 @@ internal enum FILE_SHARE_MODE : uint
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "HANDLE.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.HANDLE.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1702,7 +1702,7 @@ internal readonly partial struct HANDLE
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "PCWSTR.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.PCWSTR.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1780,7 +1780,7 @@ internal int Length
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "PInvoke.Kernel32.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.PInvoke.Kernel32.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1886,7 +1886,7 @@ internal static unsafe Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(str
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "PWSTR.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.PWSTR.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down Expand Up @@ -1943,7 +1943,7 @@ internal int Length
}
}
".Replace("\r\n", "\n")),
(typeof(SourceGenerator), "SECURITY_ATTRIBUTES.g.cs", @"// ------------------------------------------------------------------------------
(typeof(SourceGenerator), "Windows.Win32.SECURITY_ATTRIBUTES.g.cs", @"// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
Expand Down

0 comments on commit bf59af5

Please sign in to comment.