Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Last active December 27, 2016 15:03
Show Gist options
  • Save natemcmaster/ced86a82f5faeca2d4f81fad2fdc7c04 to your computer and use it in GitHub Desktop.
Save natemcmaster/ced86a82f5faeca2d4f81fad2fdc7c04 to your computer and use it in GitHub Desktop.
dotnet-names
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<RootNamespace>DotnetNames.Tool</RootNamespace>
<PackageType>DotnetCliTool</PackageType>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
<None Include="$(ProjectRuntimeConfigFilePath)" Pack="true" PackagePath="lib\$(TargetFramework)" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
namespace DotnetNames.Tool
{
class Program
{
public static int Main()
{
var projectFile = Directory.EnumerateFiles(
Directory.GetCurrentDirectory(),
"*.*proj")
.Where(f => !f.EndsWith(".xproj")) // filter xproj files, which are MSBuild meta-projects
.FirstOrDefault();
var targetFileName = Path.GetFileName(projectFile) + ".dotnet-names.targets";
var projectExtPath = Path.Combine(Path.GetDirectoryName(projectFile), "obj");
var targetFile = Path.Combine(projectExtPath, targetFileName);
File.WriteAllText(targetFile,
@"<Project>
<Target Name=""_GetDotNetNames"">
<ItemGroup>
<_DotNetNamesOutput Include=""AssemblyName: $(AssemblyName)"" />
<_DotNetNamesOutput Include=""RootNamespace: $(RootNamespace)"" />
<_DotNetNamesOutput Include=""TargetFramework: $(TargetFramework)"" />
<_DotNetNamesOutput Include=""TargetFrameworks: $(TargetFrameworks)"" />
</ItemGroup>
<WriteLinesToFile File=""$(_DotNetNamesFile)"" Lines=""@(_DotNetNamesOutput)"" Overwrite=""true"" />
</Target>
</Project>");
var tmpFile = Path.GetTempFileName();
var psi = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"msbuild \"{projectFile}\" /t:_GetDotNetNames /nologo \"/p:_DotNetNamesFile={tmpFile}\""
};
var process = Process.Start(psi);
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.Error.WriteLine("Invoking MSBuild target failed");
return 1;
}
var lines = File.ReadAllLines(tmpFile);
File.Delete(tmpFile); // cleanup
var properties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var line in lines)
{
var idx = line.IndexOf(':');
if (idx <= 0) continue;
var name = line.Substring(0, idx)?.Trim();
var value = line.Substring(idx + 1)?.Trim();
properties.Add(name, value);
}
Console.WriteLine($"Found {properties.Count} properties");
Console.WriteLine($"Assembly name = { properties["AssemblyName"] }");
Console.WriteLine($"Root namespace = { properties["RootNamespace"] }");
if (properties.TryGetValue("TargetFramework", out var framework)
&& !string.IsNullOrEmpty(framework))
{
Console.WriteLine($"Target framework = {framework}");
}
if (properties.TryGetValue("TargetFrameworks", out var tfms)
&& !string.IsNullOrEmpty(tfms))
{
Console.WriteLine("Target frameworks = ");
foreach (var tfm in tfms.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
Console.WriteLine($" - {tfm}");
}
}
return process.ExitCode;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment