Skip to content

Commit

Permalink
>
Browse files Browse the repository at this point in the history
  • Loading branch information
asklar committed May 11, 2022
1 parent e946259 commit af23f44
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
17 changes: 17 additions & 0 deletions src/Authoring/cswinmd/CsWinMD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Authors>Microsoft Corporation</Authors>
<Company>Microsoft Corporation</Company>
<Product>C#/WinRT</Product>
<PackageId>CsWinMD</PackageId>

<FileVersion>$(VersionNumber)</FileVersion>
<Version>$(VersionNumber)</Version>
<AssemblyVersion>$(VersionNumber)</AssemblyVersion>
<InformationalVersion>$(VersionNumber)</InformationalVersion>
<NeutralLanguage>en</NeutralLanguage>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>

<Description>C# to WinMD authoring tool Preview $(VersionString)</Description>
<AssemblyTitle>C# to WinMD authoring tool Preview v$(VersionString)</AssemblyTitle>
<Copyright>Copyright (c) Microsoft Corporation. All rights reserved.</Copyright>

</PropertyGroup>

<ItemGroup>
Expand Down
89 changes: 52 additions & 37 deletions src/Authoring/cswinmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,50 @@ class Program
/// <param name="i">Input WinMD path</param>
/// <param name="o">Output directory</param>
/// <param name="sdkVersion">Optional sdk version</param>
/// <param name="verbose">Verbose logging</param>
/// <param name="nologo">Don't print logo</param>
/// Uses System.CommandLine.Dragonfruit
public static void Main(string[] i, string o, string? sdkVersion)
public static void Main(string[] i, string o, string? sdkVersion, bool? verbose, bool? nologo)
{
if (i.Length == 0)
if (!nologo.HasValue || !nologo.Value)
{
Console.Error.WriteLine("No C# source files specified");
return;
Console.WriteLine($"CSWinMD {Assembly.GetExecutingAssembly().GetName().Version}");
}
var outFolder = string.IsNullOrEmpty(o) ? Environment.GetEnvironmentVariable("TEMP") : o;
try
{
if (i.Length == 0)
{
Console.Error.WriteLine("No C# source files specified");
return;
}

string inputFile = i[0];
string inputFile = i[0];

Console.Write($"Compiling {inputFile}");
Console.Write($"Compiling {inputFile}");

string inputText = File.ReadAllText(inputFile);
string inputText = File.ReadAllText(inputFile);

string componentName = Path.GetFileNameWithoutExtension(inputFile);
string componentName = Path.GetFileNameWithoutExtension(inputFile);

var assemblyName = componentName;
var assemblyName = componentName;

var windows_winmd = GetWindowsWinMdPath(sdkVersion);
var compilation = CSharpCompilation.Create(
assemblyName: componentName,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(inputText, new CSharpParseOptions(LanguageVersion.Preview), inputFile) },
references: new[] { MetadataReference.CreateFromFile(windows_winmd),
var windows_winmd = GetWindowsWinMdPath(sdkVersion);
var compilation = CSharpCompilation.Create(
assemblyName: componentName,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(inputText, new CSharpParseOptions(LanguageVersion.Preview), inputFile) },
references: new[] { MetadataReference.CreateFromFile(windows_winmd),
MetadataReference.CreateFromFile(typeof(Binder).Assembly.Location)
},
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
},
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);

var g = new SourceGenerator();
var outFolder = string.IsNullOrEmpty(o) ? Environment.GetEnvironmentVariable("TEMP") : o;
if (!Directory.Exists(outFolder))
{
Directory.CreateDirectory(outFolder);
}
try
{
var g = new SourceGenerator();

if (!Directory.Exists(outFolder))
{
Directory.CreateDirectory(outFolder);
}
var cp = new ConfigProvider();
var config = cp.GlobalOptions as ConfigOptions;
config.Values["build_property.AssemblyName"] = assemblyName;
Expand Down Expand Up @@ -109,7 +116,17 @@ public static void Main(string[] i, string o, string? sdkVersion)
}
catch (Exception e)
{
Console.WriteLine(File.ReadAllText(Path.Join(outFolder, "log.txt")));
Console.WriteLine();
Console.Error.WriteLine(e);
if (verbose.HasValue && verbose.Value)
{
var log_txt = Path.Join(outFolder, "log.txt");
try
{
Console.Error.WriteLine(File.ReadAllText(log_txt));
}
catch { }
}
}
}

Expand All @@ -119,19 +136,17 @@ private static bool IsVersion(string v)
}
private static string GetWindowsWinMdPath(string? sdkVersion)
{
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
using (var roots = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows Kits\Installed Roots"))
using var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
using var roots = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows Kits\Installed Roots");
var kitsRoot10 = (string)roots.GetValue("KitsRoot10");
var unionMetadata = Path.Combine(kitsRoot10, "UnionMetadata");
if (sdkVersion == null)
{
var kitsRoot10 = (string)roots.GetValue("KitsRoot10");
var unionMetadata = Path.Combine(kitsRoot10, "UnionMetadata");
if (sdkVersion == null) {
var dirs = Directory.EnumerateDirectories(unionMetadata);
sdkVersion = Path.GetFileName(dirs.Where(IsVersion).Last());
}
var path = Path.Combine(kitsRoot10, "UnionMetadata", sdkVersion, "Windows.winmd");
return path;
var dirs = Directory.EnumerateDirectories(unionMetadata);
sdkVersion = Path.GetFileName(dirs.Where(IsVersion).Last());
}
throw new ArgumentException("Could not determine Windows.winmd path in the Windows SDK");
var path = Path.Combine(kitsRoot10, "UnionMetadata", sdkVersion, "Windows.winmd");
return path;
}
}
}

0 comments on commit af23f44

Please sign in to comment.