Unified cli tools

- Removed GenerateTool and TagHelperTool
- rzc now handles four commands (server, shutdown, discover and
generate)
- Removed RequestCommand
This commit is contained in:
Ajay Bhargav Baaskaran 2018-01-22 16:14:00 -08:00
parent 4500de5862
commit d9b7ff238f
23 changed files with 57 additions and 770 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
VisualStudioVersion = 15.0.27107.3000
MinimumVisualStudioVersion = 15.0.26730.03
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3C0D6505-79B3-49D0-B4C3-176F0F1836ED}"
ProjectSection(SolutionItems) = preProject
@ -80,12 +80,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.Design.Test", "test\Microsoft.AspNetCore.Razor.Design.Test\Microsoft.AspNetCore.Razor.Design.Test.csproj", "{1D90F276-E1CA-4FDF-A173-EB889E7D3150}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.GenerateTool", "src\Microsoft.AspNetCore.Razor.GenerateTool\Microsoft.AspNetCore.Razor.GenerateTool.csproj", "{8052F088-1204-427B-BB0D-BE3D3BA6811B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.Tasks", "src\Microsoft.AspNetCore.Razor.Tasks\Microsoft.AspNetCore.Razor.Tasks.csproj", "{043B9497-C0BA-4770-9210-4456D2F81CE0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.TagHelperTool", "src\Microsoft.AspNetCore.Razor.TagHelperTool\Microsoft.AspNetCore.Razor.TagHelperTool.csproj", "{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.Test", "test\Microsoft.AspNetCore.Razor.Test\Microsoft.AspNetCore.Razor.Test.csproj", "{323553F0-14AB-4FBD-9CF0-1CC0BE8056F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.Performance", "benchmarks\Microsoft.AspNetCore.Razor.Performance\Microsoft.AspNetCore.Razor.Performance.csproj", "{6205467F-E381-4C42-AEEC-763BD62B3D5E}"
@ -342,14 +338,6 @@ Global
{1D90F276-E1CA-4FDF-A173-EB889E7D3150}.Release|Any CPU.Build.0 = Release|Any CPU
{1D90F276-E1CA-4FDF-A173-EB889E7D3150}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{1D90F276-E1CA-4FDF-A173-EB889E7D3150}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.Release|Any CPU.Build.0 = Release|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{8052F088-1204-427B-BB0D-BE3D3BA6811B}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{043B9497-C0BA-4770-9210-4456D2F81CE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{043B9497-C0BA-4770-9210-4456D2F81CE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{043B9497-C0BA-4770-9210-4456D2F81CE0}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
@ -358,14 +346,6 @@ Global
{043B9497-C0BA-4770-9210-4456D2F81CE0}.Release|Any CPU.Build.0 = Release|Any CPU
{043B9497-C0BA-4770-9210-4456D2F81CE0}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{043B9497-C0BA-4770-9210-4456D2F81CE0}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.Release|Any CPU.Build.0 = Release|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{323553F0-14AB-4FBD-9CF0-1CC0BE8056F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{323553F0-14AB-4FBD-9CF0-1CC0BE8056F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{323553F0-14AB-4FBD-9CF0-1CC0BE8056F8}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
@ -382,14 +362,6 @@ Global
{6205467F-E381-4C42-AEEC-763BD62B3D5E}.Release|Any CPU.Build.0 = Release|Any CPU
{6205467F-E381-4C42-AEEC-763BD62B3D5E}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{6205467F-E381-4C42-AEEC-763BD62B3D5E}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Release|Any CPU.Build.0 = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{933101DA-C4CC-401A-AA01-2784E1025B7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{933101DA-C4CC-401A-AA01-2784E1025B7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{933101DA-C4CC-401A-AA01-2784E1025B7F}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
@ -398,6 +370,14 @@ Global
{933101DA-C4CC-401A-AA01-2784E1025B7F}.Release|Any CPU.Build.0 = Release|Any CPU
{933101DA-C4CC-401A-AA01-2784E1025B7F}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{933101DA-C4CC-401A-AA01-2784E1025B7F}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.DebugNoVSIX|Any CPU.ActiveCfg = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.DebugNoVSIX|Any CPU.Build.0 = Debug|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.Release|Any CPU.Build.0 = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.ReleaseNoVSIX|Any CPU.ActiveCfg = Release|Any CPU
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB}.ReleaseNoVSIX|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -433,13 +413,11 @@ Global
{B8A3E4CA-D54A-441F-A3BF-E00F060CA042} = {92463391-81BE-462B-AC3C-78C6C760741F}
{5257B25D-330A-4DCF-ACED-B4709CFBF916} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
{1D90F276-E1CA-4FDF-A173-EB889E7D3150} = {92463391-81BE-462B-AC3C-78C6C760741F}
{8052F088-1204-427B-BB0D-BE3D3BA6811B} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
{043B9497-C0BA-4770-9210-4456D2F81CE0} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
{AFD77E2F-1A4A-4C2C-9EA9-7E48C8926780} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
{323553F0-14AB-4FBD-9CF0-1CC0BE8056F8} = {92463391-81BE-462B-AC3C-78C6C760741F}
{6205467F-E381-4C42-AEEC-763BD62B3D5E} = {C2C98051-0F39-47F2-80B6-E72B29159F2C}
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
{933101DA-C4CC-401A-AA01-2784E1025B7F} = {92463391-81BE-462B-AC3C-78C6C760741F}
{3E7F2D49-3B45-45A8-9893-F73EC1EEBAAB} = {3C0D6505-79B3-49D0-B4C3-176F0F1836ED}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0035341D-175A-4D05-95E6-F1C2785A1E26}

View File

@ -41,8 +41,6 @@
<!-- These are tools that need to be included in the package. -->
<ItemGroup>
<ToolProject Include="..\Microsoft.AspNetCore.Razor.GenerateTool\Microsoft.AspNetCore.Razor.GenerateTool.csproj" />
<ToolProject Include="..\Microsoft.AspNetCore.Razor.TagHelperTool\Microsoft.AspNetCore.Razor.TagHelperTool.csproj" />
<ToolProject Include="..\Microsoft.AspNetCore.Razor.Tools\Microsoft.AspNetCore.Razor.Tools.csproj" />
</ItemGroup>

View File

@ -13,9 +13,7 @@
<_RazorTagHelperOutputCache>$(IntermediateOutputPath)$(TargetName).TagHelpers.output.cache</_RazorTagHelperOutputCache>
<!-- Used to locate our tools -->
<_RazorGenerateToolAssembly>$(_RazorMSBuildRoot)tools\Microsoft.AspNetCore.Razor.GenerateTool.dll</_RazorGenerateToolAssembly>
<_RazorTagHelperToolAssembly>$(_RazorMSBuildRoot)tools\Microsoft.AspNetCore.Razor.TagHelperTool.dll</_RazorTagHelperToolAssembly>
<_RazorBuildServerAssembly>$(_RazorMSBuildRoot)tools\rzc.dll</_RazorBuildServerAssembly>
<_RazorToolAssembly>$(_RazorMSBuildRoot)tools\rzc.dll</_RazorToolAssembly>
<!-- Used to hash file inputs for RazorGenerate -->
<_RazorGenerateInputsHash></_RazorGenerateInputsHash>
@ -74,9 +72,8 @@
<RazorTagHelper
Debug="$(_RazorDebugTagHelperTask)"
DebugTool="$(_RazorDebugTagHelperTool)"
ToolAssembly="$(_RazorTagHelperToolAssembly)"
ToolAssembly="$(_RazorToolAssembly)"
UseServer="$(UseRazorBuildServer)"
ServerAssembly="$(_RazorBuildServerAssembly)"
Assemblies="@(RazorReferencePath)"
ProjectRoot="$(MSBuildProjectDirectory)"
TagHelperManifest="$(_RazorTagHelperOutputCache)">
@ -109,9 +106,8 @@
<RazorGenerate
Debug="$(_RazorDebugGenerateCodeTask)"
DebugTool="$(_RazorDebugGenerateCodeTool)"
ToolAssembly="$(_RazorGenerateToolAssembly)"
ToolAssembly="$(_RazorToolAssembly)"
UseServer="$(UseRazorBuildServer)"
ServerAssembly="$(_RazorBuildServerAssembly)"
Sources="@(RazorGenerate)"
ProjectRoot="$(MSBuildProjectDirectory)"
TagHelperManifest="$(_RazorTagHelperOutputCache)"

View File

@ -1,85 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.CommandLineUtils;
namespace Microsoft.AspNetCore.Razor.GenerateTool
{
internal class Application : CommandLineApplication
{
public Application()
{
Name = "razor-generatetool";
FullName = "Microsoft ASP.NET Core Razor Generate Tool";
Description = "Generates C# Code from Razor source files.";
ShortVersionGetter = GetInformationalVersion;
HelpOption("-?|-h|--help");
ProjectDirectory = Option("-p", "project root directory", CommandOptionType.SingleValue);
OutputDirectory = Option("-o", "output directory", CommandOptionType.SingleValue);
TagHelperManifest = Option("-t", "tag helper manifest file", CommandOptionType.SingleValue);
Sources = Argument("sources", ".cshtml files to compile", multipleValues: true);
new RunCommand().Configure(this);
}
public CommandArgument Sources { get; }
public CommandOption OutputDirectory { get; }
public CommandOption ProjectDirectory { get; }
public CommandOption TagHelperManifest { get; }
public new int Execute(params string[] args)
{
try
{
return base.Execute(ExpandResponseFiles(args));
}
catch (AggregateException ex) when (ex.InnerException != null)
{
Error.WriteLine(ex.InnerException.Message);
Error.WriteLine(ex.InnerException.StackTrace);
return 1;
}
catch (Exception ex)
{
Error.WriteLine(ex.Message);
Error.WriteLine(ex.StackTrace);
return 1;
}
}
private string GetInformationalVersion()
{
var assembly = typeof(Application).GetTypeInfo().Assembly;
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attribute.InformationalVersion;
}
private static string[] ExpandResponseFiles(string[] args)
{
var expandedArgs = new List<string>();
foreach (var arg in args)
{
if (!arg.StartsWith("@", StringComparison.Ordinal))
{
expandedArgs.Add(arg);
}
else
{
var fileName = arg.Substring(1);
expandedArgs.AddRange(File.ReadLines(fileName));
}
}
return expandedArgs.ToArray();
}
}
}

View File

@ -1,27 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Microsoft.AspNetCore.Razor.GenerateTool
{
internal static class DebugMode
{
public static void HandleDebugSwitch(ref string[] args)
{
if (args.Length > 0 && string.Equals("--debug", args[0], StringComparison.OrdinalIgnoreCase))
{
args = args.Skip(1).ToArray();
while (!Debugger.IsAttached)
{
Console.WriteLine("Waiting for debugger in pid: {0}", Process.GetCurrentProcess().Id);
Thread.Sleep(TimeSpan.FromSeconds(3));
}
}
}
}
}

View File

@ -1,31 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Razor is a markup syntax for adding server-side logic to web pages. This assembly contains infrastructure supporting Razor MSBuild integration.</Description>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputType>Exe</OutputType>
<!-- This is not a package, it is part of Microsoft.AspNetCore.Razor.Design. -->
<IsPackable>false</IsPackable>
<EnableApiCheck>false</EnableApiCheck>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Microsoft.VisualStudio.LanguageServices.Razor\RazorDiagnosticJsonConverter.cs">
<Link>Shared\RazorDiagnosticJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Microsoft.VisualStudio.LanguageServices.Razor\TagHelperDescriptorJsonConverter.cs">
<Link>Shared\TagHelperDescriptorJsonConverter.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils.Sources" Version="$(MicrosoftExtensionsCommandLineUtilsSourcesPackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.AspNetCore.Mvc.Razor.Extensions\Microsoft.AspNetCore.Mvc.Razor.Extensions.csproj" />
<ProjectReference Include="..\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj" />
</ItemGroup>
</Project>

View File

@ -1,16 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Razor.GenerateTool
{
internal static class Program
{
public static int Main(string[] args)
{
DebugMode.HandleDebugSwitch(ref args);
var application = new Application();
return application.Execute(args);
}
}
}

View File

@ -1,192 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.VisualStudio.LanguageServices.Razor;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.Razor.GenerateTool
{
internal class RunCommand
{
public void Configure(Application application)
{
application.OnExecute(() => Execute(application));
}
private int Execute(Application application)
{
if (!ValidateArguments(application))
{
application.ShowHelp();
return 1;
}
return ExecuteCore(
projectDirectory: application.ProjectDirectory.Value() ?? Environment.CurrentDirectory,
outputDirectory: application.OutputDirectory.Value(),
tagHelperManifest: application.TagHelperManifest.Value(),
sources: application.Sources.Values.ToArray());
}
private int ExecuteCore(string projectDirectory, string outputDirectory, string tagHelperManifest, string[] sources)
{
var tagHelpers = GetTagHelpers(tagHelperManifest);
var engine = RazorEngine.Create(b =>
{
RazorExtensions.Register(b);
b.Features.Add(new StaticTagHelperFeature() { TagHelpers = tagHelpers, });
});
var templateEngine = new MvcRazorTemplateEngine(engine, RazorProject.Create(projectDirectory));
var sourceItems = GetRazorFiles(projectDirectory, sources);
var results = GenerateCode(templateEngine, sourceItems);
var success = true;
foreach (var result in results)
{
if (result.CSharpDocument.Diagnostics.Count > 0)
{
success = false;
foreach (var error in result.CSharpDocument.Diagnostics)
{
Console.Error.WriteLine(error.ToString());
}
}
var outputFilePath = Path.Combine(outputDirectory, Path.ChangeExtension(result.ViewFileInfo.ViewEnginePath.Substring(1), ".cs"));
File.WriteAllText(outputFilePath, result.CSharpDocument.GeneratedCode);
}
return success ? 0 : -1;
}
private IReadOnlyList<TagHelperDescriptor> GetTagHelpers(string tagHelperManifest)
{
if (!File.Exists(tagHelperManifest))
{
return Array.Empty<TagHelperDescriptor>();
}
using (var stream = File.OpenRead(tagHelperManifest))
{
var reader = new JsonTextReader(new StreamReader(stream));
var serializer = new JsonSerializer();
serializer.Converters.Add(new RazorDiagnosticJsonConverter());
serializer.Converters.Add(new TagHelperDescriptorJsonConverter());
return serializer.Deserialize<IReadOnlyList<TagHelperDescriptor>>(reader);
}
}
private List<SourceItem> GetRazorFiles(string projectDirectory, string[] sources)
{
var trimLength = projectDirectory.EndsWith("/") ? projectDirectory.Length - 1 : projectDirectory.Length;
var items = new List<SourceItem>(sources.Length);
for (var i = 0; i < sources.Length; i++)
{
var fullPath = Path.Combine(projectDirectory, sources[i]);
if (fullPath.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase))
{
var viewEnginePath = fullPath.Substring(trimLength).Replace('\\', '/');
items.Add(new SourceItem(fullPath, viewEnginePath));
}
}
return items;
}
private OutputItem[] GenerateCode(RazorTemplateEngine templateEngine, IReadOnlyList<SourceItem> sources)
{
var outputs = new OutputItem[sources.Count];
Parallel.For(0, outputs.Length, new ParallelOptions() { MaxDegreeOfParallelism = 4 }, i =>
{
var source = sources[i];
var csharpDocument = templateEngine.GenerateCode(source.ViewEnginePath);
outputs[i] = new OutputItem(source, csharpDocument);
});
return outputs;
}
private bool ValidateArguments(Application application)
{
if (string.IsNullOrEmpty(application.OutputDirectory.Value()))
{
application.Error.WriteLine($"{application.OutputDirectory.ValueName} not specified.");
return false;
}
if (application.Sources.Values.Count == 0)
{
application.Error.WriteLine($"{application.Sources.Name} should have at least one value.");
return false;
}
return true;
}
private struct OutputItem
{
public OutputItem(
SourceItem viewFileInfo,
RazorCSharpDocument cSharpDocument)
{
ViewFileInfo = viewFileInfo;
CSharpDocument = cSharpDocument;
}
public SourceItem ViewFileInfo { get; }
public RazorCSharpDocument CSharpDocument { get; }
}
private struct SourceItem
{
public SourceItem(string fullPath, string viewEnginePath)
{
FullPath = fullPath;
ViewEnginePath = viewEnginePath;
}
public string FullPath { get; }
public string ViewEnginePath { get; }
public Stream CreateReadStream()
{
// We are setting buffer size to 1 to prevent FileStream from allocating it's internal buffer
// 0 causes constructor to throw
var bufferSize = 1;
return new FileStream(
FullPath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite,
bufferSize,
FileOptions.Asynchronous | FileOptions.SequentialScan);
}
}
private class StaticTagHelperFeature : ITagHelperFeature
{
public RazorEngine Engine { get; set; }
public IReadOnlyList<TagHelperDescriptor> TagHelpers { get; set; }
public IReadOnlyList<TagHelperDescriptor> GetDescriptors() => TagHelpers;
}
}
}

View File

@ -1,82 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.CommandLineUtils;
namespace Microsoft.AspNetCore.Razor.TagHelperTool
{
internal class Application : CommandLineApplication
{
public Application()
{
Name = "razor-taghelpertool";
FullName = "Microsoft ASP.NET Core Razor Tag Helper tool";
Description = "Discovers tag helpers in an assembly graph.";
ShortVersionGetter = GetInformationalVersion;
HelpOption("-?|-h|--help");
Assemblies = Argument("assemblies", "assemblies to search for tag helpers", multipleValues: true);
TagHelperManifest = Option("-o", "output file", CommandOptionType.SingleValue);
ProjectRoot = Option("-p", "project root directory", CommandOptionType.SingleValue);
new RunCommand().Configure(this);
}
public CommandArgument Assemblies { get; }
public CommandOption TagHelperManifest { get; }
public CommandOption ProjectRoot { get; }
public new int Execute(params string[] args)
{
try
{
return base.Execute(ExpandResponseFiles(args));
}
catch (AggregateException ex) when (ex.InnerException != null)
{
Error.WriteLine(ex.InnerException.Message);
Error.WriteLine(ex.InnerException.StackTrace);
return 1;
}
catch (Exception ex)
{
Error.WriteLine(ex.Message);
Error.WriteLine(ex.StackTrace);
return 1;
}
}
private string GetInformationalVersion()
{
var assembly = typeof(Application).GetTypeInfo().Assembly;
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attribute.InformationalVersion;
}
private static string[] ExpandResponseFiles(string[] args)
{
var expandedArgs = new List<string>();
foreach (var arg in args)
{
if (!arg.StartsWith("@", StringComparison.Ordinal))
{
expandedArgs.Add(arg);
}
else
{
var fileName = arg.Substring(1);
expandedArgs.AddRange(File.ReadLines(fileName));
}
}
return expandedArgs.ToArray();
}
}
}

View File

@ -1,27 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Microsoft.AspNetCore.Razor.TagHelperTool
{
internal static class DebugMode
{
public static void HandleDebugSwitch(ref string[] args)
{
if (args.Length > 0 && string.Equals("--debug", args[0], StringComparison.OrdinalIgnoreCase))
{
args = args.Skip(1).ToArray();
while (!Debugger.IsAttached)
{
Console.WriteLine("Waiting for debugger in pid: {0}", Process.GetCurrentProcess().Id);
Thread.Sleep(TimeSpan.FromSeconds(3));
}
}
}
}
}

View File

@ -1,31 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Razor is a markup syntax for adding server-side logic to web pages. This assembly contains infrastructure supporting Razor MSBuild integration.</Description>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputType>Exe</OutputType>
<!-- This is not a package, it is part of Microsoft.AspNetCore.Razor.Design. -->
<IsPackable>false</IsPackable>
<EnableApiCheck>false</EnableApiCheck>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Microsoft.VisualStudio.LanguageServices.Razor\RazorDiagnosticJsonConverter.cs">
<Link>Shared\RazorDiagnosticJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Microsoft.VisualStudio.LanguageServices.Razor\TagHelperDescriptorJsonConverter.cs">
<Link>Shared\TagHelperDescriptorJsonConverter.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils.Sources" Version="$(MicrosoftExtensionsCommandLineUtilsSourcesPackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.AspNetCore.Mvc.Razor.Extensions\Microsoft.AspNetCore.Mvc.Razor.Extensions.csproj" />
</ItemGroup>
</Project>

View File

@ -1,16 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Razor.TagHelperTool
{
internal static class Program
{
public static int Main(string[] args)
{
DebugMode.HandleDebugSwitch(ref args);
var application = new Application();
return application.Execute(args);
}
}
}

View File

@ -1,161 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.VisualStudio.LanguageServices.Razor;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.Razor.TagHelperTool
{
internal class RunCommand
{
public void Configure(Application application)
{
application.OnExecute(() => Execute(application));
}
private int Execute(Application application)
{
if (!ValidateArguments(application))
{
application.ShowHelp();
return 1;
}
return ExecuteCore(
projectDirectory: application.ProjectRoot.Value(),
outputFilePath: application.TagHelperManifest.Value(),
assemblies: application.Assemblies.Values.ToArray());
}
private int ExecuteCore(string projectDirectory, string outputFilePath, string[] assemblies)
{
outputFilePath = Path.Combine(projectDirectory, outputFilePath);
var metadataReferences = new MetadataReference[assemblies.Length];
for (var i = 0; i < assemblies.Length; i++)
{
metadataReferences[i] = MetadataReference.CreateFromFile(assemblies[i]);
}
var engine = RazorEngine.Create((b) =>
{
RazorExtensions.Register(b);
b.Features.Add(new DefaultMetadataReferenceFeature() { References = metadataReferences });
b.Features.Add(new CompilationTagHelperFeature());
// TagHelperDescriptorProviders (actually do tag helper discovery)
b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperDescriptorProvider());
b.Features.Add(new ViewComponentTagHelperDescriptorProvider());
});
var feature = engine.Features.OfType<ITagHelperFeature>().Single();
var tagHelpers = feature.GetDescriptors();
using (var stream = new MemoryStream())
{
Serialize(stream, tagHelpers);
stream.Position = 0L;
var newHash = Hash(stream);
var existingHash = Hash(outputFilePath);
if (!HashesEqual(newHash, existingHash))
{
stream.Position = 0;
using (var output = File.OpenWrite(outputFilePath))
{
stream.CopyTo(output);
}
}
}
return 0;
}
private static byte[] Hash(string path)
{
if (!File.Exists(path))
{
return Array.Empty<byte>();
}
using (var stream = File.OpenRead(path))
{
return Hash(stream);
}
}
private static byte[] Hash(Stream stream)
{
using (var sha = SHA256.Create())
{
sha.ComputeHash(stream);
return sha.Hash;
}
}
private bool HashesEqual(byte[] x, byte[] y)
{
if (x.Length != y.Length)
{
return false;
}
for (var i = 0; i < x.Length; i++)
{
if (x[i] != y[i])
{
return false;
}
}
return true;
}
private static void Serialize(Stream stream, IReadOnlyList<TagHelperDescriptor> tagHelpers)
{
using (var writer = new StreamWriter(stream, Encoding.UTF8, bufferSize: 4096, leaveOpen: true))
{
var serializer = new JsonSerializer();
serializer.Converters.Add(new TagHelperDescriptorJsonConverter());
serializer.Converters.Add(new RazorDiagnosticJsonConverter());
serializer.Serialize(writer, tagHelpers);
}
}
private bool ValidateArguments(Application application)
{
if (string.IsNullOrEmpty(application.TagHelperManifest.Value()))
{
application.Error.WriteLine($"{application.TagHelperManifest.ValueName} not specified.");
return false;
}
if (application.Assemblies.Values.Count == 0)
{
application.Error.WriteLine($"{application.Assemblies.Name} should have at least one value.");
return false;
}
if (string.IsNullOrEmpty(application.ProjectRoot.Value()))
{
application.ProjectRoot.Values.Add(Environment.CurrentDirectory);
}
return true;
}
}
}

View File

@ -7,7 +7,6 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.AspNetCore.Razor.Tools;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.CodeAnalysis.CommandLine;
@ -27,9 +26,6 @@ namespace Microsoft.AspNetCore.Razor.Tasks
[Required]
public string ToolAssembly { get; set; }
[Required]
public string ServerAssembly { get; set; }
public bool UseServer { get; set; }
protected override string ToolName => "dotnet";
@ -39,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks
protected override MessageImportance StandardErrorLoggingImportance => MessageImportance.High;
internal abstract RequestCommand Command { get; }
internal abstract string Command { get; }
protected override string GenerateFullPathToTool()
{
@ -83,7 +79,6 @@ namespace Microsoft.AspNetCore.Razor.Tasks
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
{
if (UseServer &&
!string.IsNullOrEmpty(ServerAssembly) &&
TryExecuteOnServer(pathToTool, responseFileCommands, commandLineCommands, out var result))
{
return result;
@ -120,7 +115,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks
CompilerServerLogger.Log($"BuildResponseFile = '{responseFileCommands}'");
// The server contains the tools for discovering tag helpers and generating Razor code.
var clientDir = Path.GetDirectoryName(ServerAssembly);
var clientDir = Path.GetDirectoryName(ToolAssembly);
var workingDir = CurrentDirectoryToUse();
var tempDir = BuildServerConnection.GetTempPath(workingDir);
@ -132,7 +127,6 @@ namespace Microsoft.AspNetCore.Razor.Tasks
tempDir: tempDir);
var responseTask = BuildServerConnection.RunServerCompilation(
Command,
GetArguments(responseFileCommands),
buildPaths,
keepAlive: null,

View File

@ -21,11 +21,14 @@ namespace Microsoft.AspNetCore.Razor.Tasks
[Required]
public string TagHelperManifest { get; set; }
internal override RequestCommand Command => RequestCommand.RazorGenerate;
internal override string Command => "generate";
protected override string GenerateResponseFileCommands()
{
var builder = new StringBuilder();
builder.AppendLine(Command);
for (var i = 0; i < Sources.Length; i++)
{
builder.AppendLine(Sources[i]);

View File

@ -5,7 +5,6 @@
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.CommandLine;
namespace Microsoft.AspNetCore.Razor.Tasks
{
@ -20,7 +19,7 @@ namespace Microsoft.AspNetCore.Razor.Tasks
public string ProjectRoot { get; set; }
internal override RequestCommand Command => RequestCommand.RazorTagHelper;
internal override string Command => "discover";
protected override bool SkipTaskExecution()
{
@ -38,6 +37,9 @@ namespace Microsoft.AspNetCore.Razor.Tasks
protected override string GenerateResponseFileCommands()
{
var builder = new StringBuilder();
builder.AppendLine(Command);
for (var i = 0; i < Assemblies.Length; i++)
{
builder.AppendLine(Assemblies[i]);

View File

@ -23,9 +23,10 @@ namespace Microsoft.AspNetCore.Razor.Tools
HelpOption("-?|-h|--help");
Commands.Add(new DiscoverCommand(this));
Commands.Add(new ServerCommand(this));
Commands.Add(new ShutdownCommand(this));
Commands.Add(new DiscoverCommand(this));
Commands.Add(new GenerateCommand(this));
}
public CancellationToken CancellationToken { get; }

View File

@ -30,18 +30,8 @@ namespace Microsoft.AspNetCore.Razor.Tools
var app = new Application(cancellationToken);
var commandArgs = parsed.args.ToArray();
CommandBase command = null;
if (request.Command == RequestCommand.RazorGenerate)
{
command = new GenerateCommand(app);
}
else if (request.Command == RequestCommand.RazorTagHelper)
{
command = new DiscoverCommand(app);
}
var exitCode = command?.Execute(commandArgs) ?? 0;
var output = command?.Out.ToString() ?? string.Empty;
var exitCode = app.Execute(commandArgs);
var output = app.Out.ToString() ?? string.Empty;
return new CompletedBuildResponse(exitCode, utf8output: false, output: output);
}

View File

@ -62,6 +62,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
projectDirectory: ProjectDirectory.Value(),
outputFilePath: TagHelperManifest.Value(),
assemblies: Assemblies.Values.ToArray());
return Task.FromResult(result);
}
@ -83,7 +84,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
b.Features.Add(new CompilationTagHelperFeature());
// TagHelperDescriptorProviders (actually do tag helper discovery)
b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperDescriptorProvider());
b.Features.Add(new DefaultTagHelperDescriptorProvider());
b.Features.Add(new ViewComponentTagHelperDescriptorProvider());
});

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Tools
protected override Task<int> ExecuteCoreAsync()
{
var result = ExecuteCore(
projectDirectory: ProjectDirectory.Value() ?? Environment.CurrentDirectory,
projectDirectory: ProjectDirectory.Value(),
outputDirectory: OutputDirectory.Value(),
tagHelperManifest: TagHelperManifest.Value(),
sources: Sources.Values.ToArray());
@ -57,6 +57,11 @@ namespace Microsoft.AspNetCore.Razor.Tools
return false;
}
if (string.IsNullOrEmpty(ProjectDirectory.Value()))
{
ProjectDirectory.Values.Add(Environment.CurrentDirectory);
}
return true;
}
@ -92,7 +97,10 @@ namespace Microsoft.AspNetCore.Razor.Tools
}
}
var outputFilePath = Path.Combine(outputDirectory, Path.ChangeExtension(result.ViewFileInfo.ViewEnginePath.Substring(1), ".cs"));
var viewFile = result.ViewFileInfo.ViewEnginePath.Substring(1);
var outputFileName = Path.ChangeExtension(viewFile, ".cs");
var outputFilePath = Path.Combine(outputDirectory, outputFileName);
File.WriteAllText(outputFilePath, result.CSharpDocument.GeneratedCode);
}
@ -114,7 +122,8 @@ namespace Microsoft.AspNetCore.Razor.Tools
serializer.Converters.Add(new RazorDiagnosticJsonConverter());
serializer.Converters.Add(new TagHelperDescriptorJsonConverter());
return serializer.Deserialize<IReadOnlyList<TagHelperDescriptor>>(reader);
var descriptors = serializer.Deserialize<IReadOnlyList<TagHelperDescriptor>>(reader);
return descriptors;
}
}

View File

@ -69,7 +69,6 @@ namespace Microsoft.AspNetCore.Razor.Tools
private CancellationTokenSource _listenCancellationTokenSource;
private List<Task<ConnectionResult>> _connections = new List<Task<ConnectionResult>>();
public DefaultRequestDispatcher(ConnectionHost connectionHost, CompilerHost compilerHost, CancellationToken cancellationToken)
{
_connectionHost = connectionHost;

View File

@ -28,7 +28,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
/// Field Name Type Size (bytes)
/// ----------------------------------------------------
/// Length Integer 4
/// Language RequestLanguage 4
/// Argument Count UInteger 4
/// Arguments Argument[] Variable
///
@ -39,13 +38,11 @@ namespace Microsoft.CodeAnalysis.CommandLine
internal class BuildRequest
{
public readonly uint ProtocolVersion;
public readonly RequestCommand Command;
public readonly ReadOnlyCollection<Argument> Arguments;
public BuildRequest(uint protocolVersion, RequestCommand command, IEnumerable<Argument> arguments)
public BuildRequest(uint protocolVersion, IEnumerable<Argument> arguments)
{
ProtocolVersion = protocolVersion;
Command = command;
Arguments = new ReadOnlyCollection<Argument>(arguments.ToList());
if (Arguments.Count > ushort.MaxValue)
@ -80,7 +77,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
}
public static BuildRequest Create(
RequestCommand command,
string workingDirectory,
string tempDirectory,
IList<string> args,
@ -115,18 +111,22 @@ namespace Microsoft.CodeAnalysis.CommandLine
requestArgs.Add(new Argument(ArgumentId.CommandLineArgument, i, arg));
}
return new BuildRequest(BuildProtocolConstants.ProtocolVersion, command, requestArgs);
return new BuildRequest(BuildProtocolConstants.ProtocolVersion, requestArgs);
}
public static BuildRequest CreateShutdown()
{
var requestArgs = new[] { new Argument(ArgumentId.Shutdown, argumentIndex: 0, value: "") };
return new BuildRequest(BuildProtocolConstants.ProtocolVersion, RequestCommand.None, requestArgs);
var requestArgs = new[]
{
new Argument(ArgumentId.Shutdown, argumentIndex: 0, value: ""),
new Argument(ArgumentId.CommandLineArgument, argumentIndex: 1, value: "shutdown"),
};
return new BuildRequest(BuildProtocolConstants.ProtocolVersion, requestArgs);
}
public bool IsShutdownRequest()
{
return Arguments.Count == 1 && Arguments[0].ArgumentId == ArgumentId.Shutdown;
return Arguments.Count >= 1 && Arguments[0].ArgumentId == ArgumentId.Shutdown;
}
/// <summary>
@ -163,7 +163,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
using (var reader = new BinaryReader(new MemoryStream(requestBuffer), Encoding.Unicode))
{
var protocolVersion = reader.ReadUInt32();
var command = (RequestCommand)reader.ReadUInt32();
uint argumentCount = reader.ReadUInt32();
var argumentsBuilder = new List<Argument>((int)argumentCount);
@ -174,9 +173,7 @@ namespace Microsoft.CodeAnalysis.CommandLine
argumentsBuilder.Add(BuildRequest.Argument.ReadFromBinaryReader(reader));
}
return new BuildRequest(protocolVersion,
command,
argumentsBuilder);
return new BuildRequest(protocolVersion, argumentsBuilder);
}
}
@ -191,7 +188,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
// Format the request.
Log("Formatting request");
writer.Write(ProtocolVersion);
writer.Write((uint)Command);
writer.Write(Arguments.Count);
foreach (Argument arg in Arguments)
{
@ -504,15 +500,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
protected override void AddResponseBody(BinaryWriter writer) { }
}
// The id numbers below are just random. It's useful to use id numbers
// that won't occur accidentally for debugging.
internal enum RequestCommand
{
None = 0x44532621,
RazorTagHelper = 0x44532622,
RazorGenerate = 0x44532623,
}
/// <summary>
/// Constants about the protocol.
/// </summary>

View File

@ -63,7 +63,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
internal const int TimeOutMsNewProcess = 20000;
public static Task<BuildResponse> RunServerCompilation(
RequestCommand command,
List<string> arguments,
BuildPathsAlt buildPaths,
string keepAlive,
@ -72,7 +71,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
var pipeName = PipeName.ComputeDefault();
return RunServerCompilationCore(
command,
arguments,
buildPaths,
pipeName: pipeName,
@ -84,7 +82,6 @@ namespace Microsoft.CodeAnalysis.CommandLine
}
internal static async Task<BuildResponse> RunServerCompilationCore(
RequestCommand language,
List<string> arguments,
BuildPathsAlt buildPaths,
string pipeName,
@ -156,12 +153,12 @@ namespace Microsoft.CodeAnalysis.CommandLine
var pipe = await pipeTask.ConfigureAwait(false);
if (pipe != null)
{
var request = BuildRequest.Create(language,
buildPaths.WorkingDirectory,
buildPaths.TempDirectory,
arguments,
keepAlive,
libEnvVariable);
var request = BuildRequest.Create(
buildPaths.WorkingDirectory,
buildPaths.TempDirectory,
arguments,
keepAlive,
libEnvVariable);
return await TryCompile(pipe, request, cancellationToken).ConfigureAwait(false);
}