public void TestTransformerType(string testName, RazorRuntime runtime) { string workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { using (var razorGenerator = new HostManager(workingDirectory, loadExtensions: false, defaultRuntime: runtime, assemblyDirectory: Environment.CurrentDirectory)) { string inputFile = SaveInputFile(workingDirectory, testName); var host = razorGenerator.CreateHost(inputFile, testName + ".cshtml", string.Empty); host.DefaultNamespace = GetType().Namespace; host.EnableLinePragmas = false; var output = host.GenerateCode(); AssertOutput(testName, output, runtime); } } finally { try { Directory.Delete(workingDirectory); } catch { } } }
private static bool?IsMvcProject(string projectRoot, out RazorRuntime razorRuntime) { razorRuntime = RazorRuntime.Version1; try { var projectFile = Directory.EnumerateFiles(projectRoot, "*.csproj").FirstOrDefault(); if (projectFile != null) { var content = File.ReadAllText(projectFile); if ((content.IndexOf("System.Web.Mvc, Version=4", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("System.Web.Razor, Version=2", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("Microsoft.AspNet.Mvc.4", StringComparison.OrdinalIgnoreCase) != -1)) { // The project references Razor v2 razorRuntime = RazorRuntime.Version2; } else if ((content.IndexOf("System.Web.Mvc, Version=5", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("System.Web.Razor, Version=3", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("Microsoft.AspNet.Mvc.5", StringComparison.OrdinalIgnoreCase) != -1)) { // The project references Razor v3 razorRuntime = RazorRuntime.Version3; } return(content.IndexOf("System.Web.Mvc", StringComparison.OrdinalIgnoreCase) != -1); } } catch { } return(null); }
private static void AssertOutput(string testName, string output, RazorRuntime runtime) { var expectedContent = GetManifestFileContent(testName, "Output_v" + (int)runtime); output = Regex.Replace(output, @"Runtime Version:[\d.]*", "Runtime Version:N.N.NNNNN.N") .Replace(typeof(HostManager).Assembly.GetName().Version.ToString(), "v.v.v.v"); Assert.Equal(expectedContent, output); }
public IRazorHost CreateHost(string fullPath, string projectRelativePath, CodeDomProvider codeDomProvider, string vsNamespace) { var directives = DirectivesParser.ParseDirectives(_baseDirectory, fullPath); directives["VsNamespace"] = vsNamespace; string guessedHost = null; RazorRuntime runtime = _defaultRuntime; GuessedHost value; if (TryGuessHost(_baseDirectory, projectRelativePath, out value)) { runtime = value.Runtime; guessedHost = value.Host; } string hostName; if (!directives.TryGetValue("Generator", out hostName)) { // Determine the host and runtime from the file \ project hostName = guessedHost; } string razorVersion; if (directives.TryGetValue("RazorVersion", out razorVersion)) { // If the directive explicitly specifies a host, use that. switch (razorVersion) { case "1": runtime = RazorRuntime.Version1; break; case "2": runtime = RazorRuntime.Version2; break; default: runtime = RazorRuntime.Version3; break; } } if (_catalog == null) { _catalog = InitCompositionCatalog(_baseDirectory, _loadExtensions, runtime); } using (var container = new CompositionContainer(_catalog)) { var codeTransformer = GetRazorCodeTransformer(container, projectRelativePath, hostName); var host = container.GetExport <IHostProvider>().Value; return(host.GetRazorHost(projectRelativePath, fullPath, codeTransformer, codeDomProvider, directives)); } }
public void HostManagerHieruisticForMvcHelperInViewsFolder(string path, string expected, RazorRuntime expectedRuntime) { // Act RazorRuntime runtime; var guess = HostManager.GuessHost(Directory.GetCurrentDirectory(), path, out runtime); // Assert Assert.Equal(expected, guess); Assert.Equal(expectedRuntime, runtime); }
public HostManager(string baseDirectory, bool loadExtensions, RazorRuntime defaultRuntime, string assemblyDirectory) { _loadExtensions = loadExtensions; _baseDirectory = baseDirectory; _defaultRuntime = defaultRuntime; _assemblyDirectory = assemblyDirectory; // Repurposing loadExtensions to mean unit-test scenarios. Don't bind to the AssemblyResolve in unit tests if (_loadExtensions) { AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve; } }
private Assembly GetAssembly(RazorRuntime runtime) { int runtimeValue = (int)runtime; // TODO: Check if we can switch to using CodeBase instead of Location // Look for the assembly at vX\RazorGenerator.vX.dll. If not, assume it is at RazorGenerator.vX.dll string runtimeDirectory = Path.Combine(_assemblyDirectory, "v" + runtimeValue); string assemblyName = "RazorGenerator.Core.v" + runtimeValue + ".dll"; string runtimeDirPath = Path.Combine(runtimeDirectory, assemblyName); if (File.Exists(runtimeDirPath)) { Assembly assembly = Assembly.LoadFrom(runtimeDirPath); return(assembly); } else { return(Assembly.LoadFrom(Path.Combine(_assemblyDirectory, assemblyName))); } }
private static bool? IsMvcProject(string projectRoot, out RazorRuntime razorRuntime) { razorRuntime = RazorRuntime.Version1; try { var projectFile = Directory.EnumerateFiles(projectRoot, "*.csproj").FirstOrDefault(); if (projectFile != null) { var content = File.ReadAllText(projectFile); if ((content.IndexOf("System.Web.Mvc, Version=4.0.0.0", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("System.Web.Razor, Version=2.0.0.0", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("Microsoft.AspNet.Mvc.4.0.0", StringComparison.OrdinalIgnoreCase) != -1)) { // The project references Razor v2 razorRuntime = RazorRuntime.Version2; } else if ((content.IndexOf("System.Web.Mvc, Version=5.0.0.0", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("System.Web.Razor, Version=3.0.0.0", StringComparison.OrdinalIgnoreCase) != -1) || (content.IndexOf("Microsoft.AspNet.Mvc.5.0.0", StringComparison.OrdinalIgnoreCase) != -1)) { // The project references Razor v3 razorRuntime = RazorRuntime.Version3; } return content.IndexOf("System.Web.Mvc", StringComparison.OrdinalIgnoreCase) != -1; } } catch { } return null; }
public GuessedHost(string host, RazorRuntime runtime) { Host = host; Runtime = runtime; }
private Assembly GetAssembly(RazorRuntime runtime) { int runtimeValue = (int)runtime; // TODO: Check if we can switch to using CodeBase instead of Location // Look for the assembly at vX\RazorGenerator.vX.dll. If not, assume it is at RazorGenerator.vX.dll string runtimeDirectory = Path.Combine(_assemblyDirectory, "v" + runtimeValue); string assemblyName = "RazorGenerator.Core.v" + runtimeValue + ".dll"; string runtimeDirPath = Path.Combine(runtimeDirectory, assemblyName); if (File.Exists(runtimeDirPath)) { Assembly assembly = Assembly.LoadFrom(runtimeDirPath); return assembly; } else { return Assembly.LoadFrom(Path.Combine(_assemblyDirectory, assemblyName)); } }
internal static string GuessHost(string projectRoot, string projectRelativePath, out RazorRuntime runtime) { bool isMvcProject = IsMvcProject(projectRoot, out runtime) ?? false; if (isMvcProject) { var mvcHelperRegex = new Regex(@"(^|\\)Views(\\.*)+Helpers?", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase); if (mvcHelperRegex.IsMatch(projectRelativePath)) { return "MvcHelper"; } return "MvcView"; } return null; }
private ComposablePartCatalog InitCompositionCatalog(string baseDirectory, bool loadExtensions, RazorRuntime runtime) { // Retrieve available hosts var hostsAssembly = GetAssembly(runtime); var catalog = new AggregateCatalog(new AssemblyCatalog(hostsAssembly)); if (loadExtensions) { // We assume that the baseDirectory points to the project root. Look for the RazorHosts directory under the project root AddCatalogIfHostsDirectoryExists(catalog, baseDirectory); // Look for the Razor Hosts directory up to two directories above the baseDirectory. Hopefully this should cover the solution root. var solutionDirectory = Path.Combine(baseDirectory, @"..\"); AddCatalogIfHostsDirectoryExists(catalog, solutionDirectory); solutionDirectory = Path.Combine(baseDirectory, @"..\..\"); AddCatalogIfHostsDirectoryExists(catalog, solutionDirectory); } return catalog; }
internal static string GuessHost(string projectRoot, string projectRelativePath, out RazorRuntime runtime) { bool isMvcProject = IsMvcProject(projectRoot, out runtime) ?? false; if (isMvcProject) { var mvcHelperRegex = new Regex(@"(^|\\)Views(\\.*)+Helpers?", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase); if (mvcHelperRegex.IsMatch(projectRelativePath)) { return("MvcHelper"); } return("MvcView"); } return(null); }
private ComposablePartCatalog InitCompositionCatalog(string baseDirectory, bool loadExtensions, RazorRuntime runtime) { // Retrieve available hosts var hostsAssembly = GetAssembly(runtime); var catalog = new AggregateCatalog(new AssemblyCatalog(hostsAssembly)); if (loadExtensions) { // We assume that the baseDirectory points to the project root. Look for the RazorHosts directory under the project root AddCatalogIfHostsDirectoryExists(catalog, baseDirectory); // Look for the Razor Hosts directory up to two directories above the baseDirectory. Hopefully this should cover the solution root. var solutionDirectory = Path.Combine(baseDirectory, @"..\"); AddCatalogIfHostsDirectoryExists(catalog, solutionDirectory); solutionDirectory = Path.Combine(baseDirectory, @"..\..\"); AddCatalogIfHostsDirectoryExists(catalog, solutionDirectory); } return(catalog); }