Ejemplo n.º 1
0
        private static (IReadOnlyList <RazorInputItem> razorFiles, IReadOnlyList <RazorInputItem> cshtmlFiles) GetRazorInputs(GeneratorExecutionContext context)
        {
            List <RazorInputItem> razorFiles  = null;
            List <RazorInputItem> cshtmlFiles = null;

            foreach (var item in context.AdditionalFiles)
            {
                var path        = item.Path;
                var isComponent = path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase);
                var isRazorView = path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase);

                if (!isComponent && !isRazorView)
                {
                    continue;
                }

                var options = context.AnalyzerConfigOptions.GetOptions(item);
                if (!options.TryGetValue("build_metadata.AdditionalFiles.TargetPath", out var relativePath))
                {
                    throw new InvalidOperationException($"TargetPath is not specified for additional file '{item.Path}.");
                }

                options.TryGetValue("build_metadata.AdditionalFiles.CssScope", out var cssScope);

                string generatedDeclarationPath = null;

                options.TryGetValue("build_metadata.AdditionalFiles.GeneratedOutputFullPath", out var generatedOutputPath);
                if (isComponent)
                {
                    options.TryGetValue("build_metadata.AdditionalFiles.GeneratedDeclarationFullPath", out generatedDeclarationPath);
                }

                var fileKind = isComponent ? FileKinds.GetComponentFileKindFromFilePath(item.Path) : FileKinds.Legacy;

                var inputItem = new RazorInputItem(item, relativePath, fileKind, generatedOutputPath, generatedDeclarationPath, cssScope);

                if (isComponent)
                {
                    razorFiles ??= new();
                    razorFiles.Add(inputItem);
                }
                else
                {
                    cshtmlFiles ??= new();
                    cshtmlFiles.Add(inputItem);
                }
            }

            return(
                (IReadOnlyList <RazorInputItem>)razorFiles ?? Array.Empty <RazorInputItem>(),
                (IReadOnlyList <RazorInputItem>)cshtmlFiles ?? Array.Empty <RazorInputItem>()
                );
        }
Ejemplo n.º 2
0
        private static (IReadOnlyList <RazorInputItem> razorFiles, IReadOnlyList <RazorInputItem> cshtmlFiles) GetRazorInputs(GeneratorExecutionContext context)
        {
            List <RazorInputItem> razorFiles  = new();
            List <RazorInputItem> cshtmlFiles = new();

            foreach (var item in context.AdditionalFiles)
            {
                var path        = item.Path;
                var isComponent = path.EndsWith(".razor", StringComparison.OrdinalIgnoreCase);
                var isRazorView = path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase);

                if (!isComponent && !isRazorView)
                {
                    continue;
                }

                var options = context.AnalyzerConfigOptions.GetOptions(item);
                if (!options.TryGetValue("build_metadata.AdditionalFiles.TargetPath", out var relativePath))
                {
                    context.ReportDiagnostic(Diagnostic.Create(
                                                 RazorDiagnostics.TargetPathNotProvided,
                                                 Location.None,
                                                 item.Path));
                    continue;
                }

                options.TryGetValue("build_metadata.AdditionalFiles.CssScope", out var cssScope);

                if (!options.TryGetValue("build_metadata.AdditionalFiles.GeneratedOutputFullPath", out var generatedOutputPath))
                {
                    context.ReportDiagnostic(Diagnostic.Create(
                                                 RazorDiagnostics.GeneratedOutputFullPathNotProvided,
                                                 Location.None,
                                                 item.Path));
                }

                var fileKind = isComponent ? FileKinds.GetComponentFileKindFromFilePath(item.Path) : FileKinds.Legacy;

                var inputItem = new RazorInputItem(item, relativePath, fileKind, generatedOutputPath, cssScope);

                if (isComponent)
                {
                    razorFiles.Add(inputItem);
                }
                else
                {
                    cshtmlFiles.Add(inputItem);
                }
            }

            return(razorFiles, cshtmlFiles);
        }