From d81cce8a25041fc5e8dde0c6528e1783f5ea1a7e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Fri, 4 Nov 2016 14:20:49 -0700 Subject: [PATCH] Make --framework an optional parameters to the tool Fixes #26 --- .../Internal/PrecompileDispatchCommand.cs | 36 +++++++++++-------- .../PrecompileDispatchCommandTest.cs | 16 --------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools/Internal/PrecompileDispatchCommand.cs b/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools/Internal/PrecompileDispatchCommand.cs index 2707c2d2aa..59ef9b9e05 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools/Internal/PrecompileDispatchCommand.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools/Internal/PrecompileDispatchCommand.cs @@ -3,8 +3,8 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design.Internal; using Microsoft.DotNet.InternalAbstractions; @@ -94,7 +94,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal dispatchArgs.Add(CommonOptions.EmbedViewSourceTemplate); } - var compilerOptions = runtimeContext.ProjectFile.GetCompilerOptions(TargetFramework, Configuration); + var compilerOptions = runtimeContext.ProjectFile.GetCompilerOptions(runtimeContext.TargetFramework, Configuration); if (!string.IsNullOrEmpty(compilerOptions.KeyFile)) { dispatchArgs.Add(StrongNameOptions.StrongNameKeyPath); @@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal var toolName = typeof(Design.Program).GetTypeInfo().Assembly.GetName().Name; var dispatchCommand = DotnetToolDispatcher.CreateDispatchCommand( dispatchArgs, - TargetFramework, + runtimeContext.TargetFramework, Configuration, outputPath: outputPaths.RuntimeOutputPath, buildBasePath: null, @@ -144,12 +144,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal ProjectPath = GetProjectPath(Options.ProjectArgument.Value); Configuration = ConfigurationOption.Value() ?? DotNet.Cli.Utils.Constants.DefaultConfiguration; - if (!FrameworkOption.HasValue()) + if (FrameworkOption.HasValue()) { - Application.Error.WriteLine($"Option {FrameworkOption.Template} does not have a value."); - return false; + TargetFramework = NuGetFramework.Parse(FrameworkOption.Value()); } - TargetFramework = NuGetFramework.Parse(FrameworkOption.Value()); if (!OutputPathOption.HasValue()) { @@ -188,18 +186,28 @@ namespace Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Internal private ProjectContext GetRuntimeContext() { var workspace = new BuildWorkspace(ProjectReaderSettings.ReadFromEnvironment()); - - var projectContext = workspace.GetProjectContext(ProjectPath, TargetFramework); - if (projectContext == null) + var projectContexts = ProjectContext.CreateContextForEachFramework(ProjectPath).ToArray(); + ProjectContext projectContext; + if (TargetFramework != null) { - Debug.Assert(FrameworkOption.HasValue()); - throw new InvalidOperationException($"Project '{ProjectPath}' does not support framework: {FrameworkOption.Value()}"); + projectContext = projectContexts.FirstOrDefault(context => context.TargetFramework == TargetFramework); + if (projectContext == null) + { + throw new InvalidOperationException($"Project '{ProjectPath}' does not support framework: {FrameworkOption.Value()}"); + } + } + else if (projectContexts.Length == 1) + { + projectContext = projectContexts[0]; + } + else + { + throw new InvalidOperationException($"Project '{ProjectPath}' targets multiple frameworks. Specify one using '{FrameworkOption.Template}."); } - var runtimeContext = workspace.GetRuntimeContext( + return workspace.GetRuntimeContext( projectContext, RuntimeEnvironmentRidExtensions.GetAllCandidateRuntimeIdentifiers()); - return runtimeContext; } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools.Test/PrecompileDispatchCommandTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools.Test/PrecompileDispatchCommandTest.cs index 3708ba587f..c90c7e17c6 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools.Test/PrecompileDispatchCommandTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools.Test/PrecompileDispatchCommandTest.cs @@ -69,22 +69,6 @@ Options: result.Error.Split(new[] { Environment.NewLine }, StringSplitOptions.None).First()); } - [Fact] - public void RunPrintsError_IfFrameworkIfNotSpecified() - { - // Arrange - var expected = "Option -f|--framework does not have a value."; - var args = new string[0]; - - // Act - var result = Execute(args); - - // Assert - Assert.Equal(1, result.ExitCode); - Assert.Empty(result.Out); - Assert.Equal(expected, result.Error.Trim()); - } - [Fact] public void RunPrintsError_IfOutputPathIfNotSpecified() {