Ejemplo n.º 1
0
        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
                {
                }
            }

        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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
                {
                }
            }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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));
            }
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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;
            }
        }
Ejemplo n.º 8
0
        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;
            }
        }
Ejemplo n.º 9
0
        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)));
            }
        }
Ejemplo n.º 10
0
 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;
 }
Ejemplo n.º 11
0
 public GuessedHost(string host, RazorRuntime runtime)
 {
     Host    = host;
     Runtime = runtime;
 }
Ejemplo n.º 12
0
        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));
            }
        }
Ejemplo n.º 13
0
 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;
 }
Ejemplo n.º 14
0
        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;
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        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);
        }
Ejemplo n.º 18
0
        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);
        }