Consolidate options to a single file

This commit is contained in:
Pranav K 2017-01-17 11:52:49 -08:00
parent bcd210819a
commit c1ef1e533f
5 changed files with 111 additions and 125 deletions

View File

@ -1,44 +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 Microsoft.Extensions.CommandLineUtils;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
public class CommonOptions
{
public static readonly string ConfigureCompilationTypeTemplate = "--configure-compilation-type";
public static readonly string ContentRootTemplate = "--content-root";
public static readonly string EmbedViewSourceTemplate = "--embed-view-sources";
public CommandArgument ProjectArgument { get; private set; }
public CommandOption ConfigureCompilationType { get; private set; }
public CommandOption ContentRootOption { get; private set; }
public CommandOption EmbedViewSourcesOption { get; private set; }
public void Configure(CommandLineApplication app)
{
ProjectArgument = app.Argument(
"project",
"The path to the project (project folder or project.json) with precompilation.");
ConfigureCompilationType = app.Option(
ConfigureCompilationTypeTemplate,
"Type with Configure method",
CommandOptionType.SingleValue);
ContentRootOption = app.Option(
ContentRootTemplate,
"The application's content root.",
CommandOptionType.SingleValue);
EmbedViewSourcesOption = app.Option(
EmbedViewSourceTemplate,
"Embed view sources as resources in the generated assembly.",
CommandOptionType.NoValue);
}
}
}

View File

@ -0,0 +1,94 @@
// 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 Microsoft.Extensions.CommandLineUtils;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
public class CompilationOptions
{
public static readonly string ConfigureCompilationTypeTemplate = "--configure-compilation-type";
public static readonly string ContentRootTemplate = "--content-root";
public static readonly string EmbedViewSourceTemplate = "--embed-view-sources";
public static readonly string StrongNameKeyPath = "--key-file";
public static readonly string DelaySignTemplate = "--delay-sign";
public static readonly string PublicSignTemplate = "--public-sign";
public static readonly string ApplicationNameTemplate = "--application-name";
public static readonly string OutputPathTemplate = "--output-path";
public CompilationOptions(CommandLineApplication app)
{
OutputPathOption = app.Option(
OutputPathTemplate,
"Path to the emit the precompiled assembly to.",
CommandOptionType.SingleValue);
ApplicationNameOption = app.Option(
ApplicationNameTemplate,
"Name of the application to produce precompiled assembly for.",
CommandOptionType.SingleValue);
ProjectArgument = app.Argument(
"project",
"The path to the project (project folder or project.json) with precompilation.");
ConfigureCompilationType = app.Option(
ConfigureCompilationTypeTemplate,
"Type with Configure method",
CommandOptionType.SingleValue);
ContentRootOption = app.Option(
ContentRootTemplate,
"The application's content root.",
CommandOptionType.SingleValue);
EmbedViewSourcesOption = app.Option(
EmbedViewSourceTemplate,
"Embed view sources as resources in the generated assembly.",
CommandOptionType.NoValue);
KeyFileOption = app.Option(
StrongNameKeyPath,
"Strong name key path",
CommandOptionType.SingleValue);
DelaySignOption = app.Option(
DelaySignTemplate,
"Determines if the precompiled view assembly is to be delay signed.",
CommandOptionType.NoValue);
PublicSignOption = app.Option(
PublicSignTemplate,
"Determines if the precompiled view assembly is to be public signed.",
CommandOptionType.NoValue);
}
public CommandArgument ProjectArgument { get; }
public CommandOption ConfigureCompilationType { get; }
public CommandOption ContentRootOption { get; }
public CommandOption EmbedViewSourcesOption { get; }
public CommandOption KeyFileOption { get; }
public CommandOption DelaySignOption { get; }
public CommandOption PublicSignOption { get; }
public CommandOption OutputPathOption { get; }
public CommandOption ApplicationNameOption { get; }
public string OutputPath => OutputPathOption.Value();
public string ApplicationName => ApplicationNameOption.Value();
public string KeyFile => KeyFileOption.Value();
public bool DelaySign => DelaySignOption.HasValue();
public bool PublicSign => PublicSignOption.HasValue();
}
}

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@ -23,8 +22,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
public class PrecompileRunCommand
{
public static readonly string ApplicationNameTemplate = "--application-name";
public static readonly string OutputPathTemplate = "--output-path";
private static readonly ParallelOptions ParalellOptions = new ParallelOptions
{
MaxDegreeOfParallelism = 4
@ -32,33 +29,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
private CommandLineApplication Application { get; set; }
private CommandOption OutputPathOption { get; set; }
private CommandOption ApplicationNameOption { get; set; }
private MvcServiceProvider MvcServiceProvider { get; set; }
private CommonOptions Options { get; } = new CommonOptions();
private StrongNameOptions StrongNameOptions { get; } = new StrongNameOptions();
private CompilationOptions Options { get; set; }
private string ProjectPath { get; set; }
public void Configure(CommandLineApplication app)
{
Application = app;
Options.Configure(app);
StrongNameOptions.Configure(app);
OutputPathOption = app.Option(
OutputPathTemplate,
"Path to the emit the precompiled assembly to.",
CommandOptionType.SingleValue);
ApplicationNameOption = app.Option(
ApplicationNameTemplate,
"Name of the application to produce precompiled assembly for.",
CommandOptionType.SingleValue);
Options = new CompilationOptions(app);
app.OnExecute(() => Execute());
}
@ -72,7 +52,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
MvcServiceProvider = new MvcServiceProvider(
ProjectPath,
ApplicationNameOption.Value(),
Options.ApplicationNameOption.Value(),
Options.ContentRootOption.Value(),
Options.ConfigureCompilationType.Value());
@ -95,11 +75,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
return 1;
}
var precompileAssemblyName = $"{ApplicationNameOption.Value()}{ViewsFeatureProvider.PrecompiledViewsAssemblySuffix}";
var precompileAssemblyName = $"{Options.ApplicationName}{ViewsFeatureProvider.PrecompiledViewsAssemblySuffix}";
var compilation = CompileViews(results, precompileAssemblyName);
var resources = GetResources(results);
var assemblyPath = Path.Combine(OutputPathOption.Value(), precompileAssemblyName + ".dll");
var assemblyPath = Path.Combine(Options.OutputPath, precompileAssemblyName + ".dll");
var emitResult = EmitAssembly(
compilation,
MvcServiceProvider.Compiler.EmitOptions,
@ -212,9 +192,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
var codeGenerator = new ViewInfoContainerCodeGenerator(compiler, compilation);
codeGenerator.AddViewFactory(results);
var assemblyName = new AssemblyName(ApplicationNameOption.Value());
var assemblyName = new AssemblyName(Options.ApplicationName);
assemblyName = Assembly.Load(assemblyName).GetName();
codeGenerator.AddAssemblyMetadata(assemblyName, StrongNameOptions);
codeGenerator.AddAssemblyMetadata(assemblyName, Options);
return codeGenerator.Compilation;
}
@ -228,21 +208,21 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
return false;
}
if (!OutputPathOption.HasValue())
if (!Options.OutputPathOption.HasValue())
{
Application.Error.WriteLine($"Option {OutputPathTemplate} does not specify a value.");
Application.Error.WriteLine($"Option {CompilationOptions.OutputPathTemplate} does not specify a value.");
return false;
}
if (!ApplicationNameOption.HasValue())
if (!Options.ApplicationNameOption.HasValue())
{
Application.Error.WriteLine($"Option {ApplicationNameTemplate} does not specify a value.");
Application.Error.WriteLine($"Option {CompilationOptions.ApplicationNameTemplate} does not specify a value.");
return false;
}
if (!Options.ContentRootOption.HasValue())
{
Application.Error.WriteLine($"Option {CommonOptions.ContentRootTemplate} does not specify a value.");
Application.Error.WriteLine($"Option {CompilationOptions.ContentRootTemplate} does not specify a value.");
return false;
}

View File

@ -1,44 +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 Microsoft.Extensions.CommandLineUtils;
namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal
{
public class StrongNameOptions
{
public static readonly string StrongNameKeyPath = "--key-file";
public static readonly string DelaySignTemplate = "--delay-sign";
public static readonly string PublicSignTemplate = "--public-sign";
public CommandOption KeyFileOption { get; set; }
public CommandOption DelaySignOption { get; private set; }
public CommandOption PublicSignOption { get; private set; }
public void Configure(CommandLineApplication app)
{
KeyFileOption = app.Option(
StrongNameKeyPath,
"Strong name key path",
CommandOptionType.SingleValue);
DelaySignOption = app.Option(
DelaySignTemplate,
"Determines if the precompiled view assembly is to be delay signed.",
CommandOptionType.NoValue);
PublicSignOption = app.Option(
PublicSignTemplate,
"Determines if the precompiled view assembly is to be public signed.",
CommandOptionType.NoValue);
}
public string KeyFile => KeyFileOption.Value();
public bool DelaySign => DelaySignOption.HasValue();
public bool PublicSign => PublicSignOption.HasValue();
}
}

View File

@ -58,14 +58,14 @@ namespace {ViewsFeatureProvider.ViewInfoContainerNamespace}
public void AddAssemblyMetadata(
AssemblyName applicationAssemblyName,
StrongNameOptions strongNameOptions)
CompilationOptions compilationOptions)
{
if (!string.IsNullOrEmpty(strongNameOptions.KeyFile))
if (!string.IsNullOrEmpty(compilationOptions.KeyFile))
{
var updatedOptions = Compilation.Options.WithStrongNameProvider(new DesktopStrongNameProvider());
var keyFilePath = Path.GetFullPath(strongNameOptions.KeyFile);
var keyFilePath = Path.GetFullPath(compilationOptions.KeyFile);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || strongNameOptions.PublicSign)
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || compilationOptions.PublicSign)
{
updatedOptions = updatedOptions.WithCryptoPublicKey(
SnkUtils.ExtractPublicKey(File.ReadAllBytes(keyFilePath)));
@ -73,7 +73,7 @@ namespace {ViewsFeatureProvider.ViewInfoContainerNamespace}
else
{
updatedOptions = updatedOptions.WithCryptoKeyFile(keyFilePath)
.WithDelaySign(strongNameOptions.DelaySign);
.WithDelaySign(compilationOptions.DelaySign);
}
Compilation = Compilation.WithOptions(updatedOptions);