예제 #1
0
        private string MakeIntermediatePath(RazorProject project, RazorProjectItem item, RazorPageData pageData, HashSet <string> currentPaths)
        {
            if (string.IsNullOrWhiteSpace(project.IntermediateDirectory))
            {
                return(null);
            }

            int    hashCode     = (item.FullPath + "|" + item.ProjectPath + "|" + pageData.SourceChecksum).GetStableHashCode();
            string baseFileName = Path.GetFileNameWithoutExtension(item.ProjectPath);
            string fileName     = $"{baseFileName}.{hashCode:x2}.g.cssql.cs";
            string fullName     = Path.Combine(project.IntermediateDirectory, fileName);

            int n = 0;

            while (currentPaths.Contains(fullName, StringComparer.OrdinalIgnoreCase))
            {
                fileName = $"{baseFileName}.{hashCode:x2}.g{n++}.cssql.cs";

                fullName = Path.Combine(project.IntermediateDirectory, fileName);
            }

            currentPaths.Add(fullName);

            return(fullName);
        }
예제 #2
0
        private IEnumerable <RazorPage> ParsePages(RazorProject project)
        {
            if (project.Items == null || project.Items.Count == 0)
            {
                yield break;
            }

            HashSet <string> intermediatePaths = new HashSet <string>();

            bool hasProjectDirectory = !string.IsNullOrWhiteSpace(project.ProjectDirectory);

            foreach (RazorProjectItem item in project.Items.NotNull())
            {
                bool hasProjectPath = !string.IsNullOrWhiteSpace(item.ProjectPath);

                if (string.IsNullOrWhiteSpace(item.FullPath))
                {
                    throw new InvalidOperationException($"Error parsing file at index {project.Items.IndexOf(item)}. Path cannot be empty.");
                }
                else if (!Path.IsPathRooted(item.FullPath))
                {
                    throw new InvalidOperationException(Error("Path must be rooted."));
                }
                else if (!File.Exists(item.FullPath))
                {
                    throw new FileNotFoundException(Error("File not found."));
                }
                else if (!hasProjectDirectory && !hasProjectPath)
                {
                    throw new InvalidOperationException(Error($"ProjectPath must be specified when no ProjectDirectory is present."));
                }

                string projectPath = hasProjectPath ? item.ProjectPath : null;

                if (hasProjectDirectory)
                {
                    projectPath = PathHelper.MakeRelativePath(project.ProjectDirectory, projectPath ?? item.FullPath);
                }

                if (projectPath == null)
                {
                    throw new InvalidOperationException(Error($"ProjectPath '{item.ProjectPath}' must be relative to to ProjectDirectory '{project.ProjectDirectory}'."));
                }

                RazorPageData pageData = this.Parse(item.FullPath);

                yield return(new RazorPage()
                {
                    Data = pageData,
                    Path = Path.GetFullPath(item.FullPath),
                    ProjectPath = projectPath,
                    IntermediatePath = this.MakeIntermediatePath(project, item, pageData, intermediatePaths),
                });

                string Error(string s) => $"Error parsing file '{item.FullPath}'. {s}";
            }
        }
예제 #3
0
        public IEnumerable <RazorPage> Parse(RazorProject project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            IRazorProjectConvention[] conventions = new IRazorProjectConvention[]
            {
                new RazorNamingConvention(),
                new RazorImportConvention(),
            };

            return(this.Parse(project, conventions));
        }
예제 #4
0
        public IList <RazorPage> Parse(RazorProject project)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            IList <RazorPage> pages = this.ParsePages(project).ToList();

            foreach (IRazorProjectConvention convention in project.Conventions ?? Array.Empty <IRazorProjectConvention>())
            {
                convention.Apply(project, pages);
            }

            return(pages);
        }
예제 #5
0
        private IEnumerable <RazorPage> ParsePages(RazorProject project)
        {
            if (project.Items == null || project.Items.Count == 0)
            {
                yield break;
            }

            foreach (RazorProjectItem item in project.Items.NotNull())
            {
                if (string.IsNullOrEmpty(item.FullPath))
                {
                    throw new InvalidOperationException($"Cannot parse file at index {project.Items.IndexOf(item)}. Path cannot be empty.");
                }
                else if (!File.Exists(item.FullPath))
                {
                    throw new FileNotFoundException($"Cannot parse file at '{item.FullPath}'. File not found.");
                }

                string projectPath = item.ProjectPath;

                if (projectPath != null && Path.IsPathRooted(projectPath))
                {
                    projectPath = null;
                }
                else if (projectPath == null)
                {
                    this.MakeProjectPaths(project.ProjectDirectory, item.FullPath, out string fullPath, out projectPath);
                }
                else if (string.IsNullOrWhiteSpace(projectPath))
                {
                    projectPath = null;
                }

                yield return(new RazorPage()
                {
                    Data = this.Parse(item.FullPath),
                    Path = item.FullPath,
                    ProjectPath = projectPath,
                });
            }
        }
예제 #6
0
        public IList <RazorPage> Parse(RazorProject project, IEnumerable <IRazorProjectConvention> conventions)
        {
            if (project == null)
            {
                throw new ArgumentNullException(nameof(project));
            }

            if (!string.IsNullOrEmpty(project.ProjectDirectory) && !Path.IsPathRooted(project.ProjectDirectory))
            {
                throw new InvalidOperationException("Project directory, if specified, must be a rooted path.");
            }

            List <RazorPage> pages = this.ParsePages(project).ToList();

            foreach (IRazorProjectConvention convention in conventions ?? new IRazorProjectConvention[0])
            {
                convention.Apply(project, pages);
            }

            return(pages);
        }