// Internal for testing internal static bool TryGetLanguageVersion( IImmutableDictionary <string, IProjectRuleSnapshot> state, out RazorLanguageVersion languageVersion) { if (!state.TryGetValue(Rules.RazorGeneral.SchemaName, out var rule)) { languageVersion = null; return(false); } if (!rule.Properties.TryGetValue(Rules.RazorGeneral.RazorLangVersionProperty, out var languageVersionValue)) { languageVersion = null; return(false); } if (string.IsNullOrEmpty(languageVersionValue)) { languageVersion = null; return(false); } if (!RazorLanguageVersion.TryParse(languageVersionValue, out languageVersion)) { languageVersion = RazorLanguageVersion.Latest; } return(true); }
public static RazorEngine CreateInstance(string baseType) { var configuration = RazorConfiguration.Default; var razorProjectEngine = RazorProjectEngine.Create(configuration, new NullRazorProjectFileSystem(), builder => { TkInjectDirective.Register(builder); TkModelDirective.Register(builder); if (!RazorLanguageVersion.TryParse("3.0", out var razorLanguageVersion) || configuration.LanguageVersion.CompareTo(razorLanguageVersion) < 0) { NamespaceDirective.Register(builder); FunctionsDirective.Register(builder); InheritsDirective.Register(builder); } SectionDirective.Register(builder); builder.Features.Add(new ModelExpressionPass()); builder.Features.Add(new RazorTemplateDocumentClassifierPass(baseType)); builder.Features.Add(new RazorAssemblyAttributeInjectionPass()); #if NETSTANDARD2_0 builder.Features.Add(new InstrumentationPass()); #endif builder.AddTargetExtension(new TemplateTargetExtension() { TemplateTypeName = "global::YJC.Toolkit.Razor.TkRazorHelperResult", }); OverrideRuntimeNodeWriterTemplateTypeNamePhase.Register(builder); }); return(razorProjectEngine.Engine); }
protected override bool ValidateArguments() { if (string.IsNullOrEmpty(TagHelperManifest.Value())) { Error.WriteLine($"{TagHelperManifest.ValueName} must be specified."); return(false); } if (Assemblies.Values.Count == 0) { Error.WriteLine($"{Assemblies.Name} must have at least one value."); return(false); } if (string.IsNullOrEmpty(ProjectDirectory.Value())) { ProjectDirectory.Values.Add(Environment.CurrentDirectory); } if (string.IsNullOrEmpty(Version.Value())) { Error.WriteLine($"{Version.ValueName} must be specified."); return(false); } else if (!RazorLanguageVersion.TryParse(Version.Value(), out _)) { Error.WriteLine($"{Version.ValueName} is not a valid language version."); return(false); } if (string.IsNullOrEmpty(Configuration.Value())) { Error.WriteLine($"{Configuration.ValueName} must be specified."); return(false); } if (ExtensionNames.Values.Count != ExtensionFilePaths.Values.Count) { Error.WriteLine($"{ExtensionNames.ValueName} and {ExtensionFilePaths.ValueName} should have the same number of values."); } foreach (var filePath in ExtensionFilePaths.Values) { if (!Path.IsPathRooted(filePath)) { Error.WriteLine($"Extension file paths must be fully-qualified, absolute paths."); return(false); } } if (!Parent.Checker.Check(ExtensionFilePaths.Values)) { Error.WriteLine($"Extenions could not be loaded. See output for details."); return(false); } return(true); }
protected override bool ValidateArguments() { if (Sources.Values.Count == 0) { Error.WriteLine($"{Sources.Description} should have at least one value."); return(false); } if (Outputs.Values.Count != Sources.Values.Count) { Error.WriteLine($"{Sources.Description} has {Sources.Values.Count}, but {Outputs.Description} has {Outputs.Values.Count} values."); } if (RelativePaths.Values.Count != Sources.Values.Count) { Error.WriteLine($"{Sources.Description} has {Sources.Values.Count}, but {RelativePaths.Description} has {RelativePaths.Values.Count} values."); } if (string.IsNullOrEmpty(ProjectDirectory.Value())) { ProjectDirectory.Values.Add(Environment.CurrentDirectory); } if (string.IsNullOrEmpty(Version.Value())) { Error.WriteLine($"{Version.Description} must be specified."); return(false); } else if (!RazorLanguageVersion.TryParse(Version.Value(), out _)) { Error.WriteLine($"Invalid option {Version.Value()} for Razor language version --version; must be Latest or a valid version in range {RazorLanguageVersion.Version_1_0} to {RazorLanguageVersion.Latest}."); return(false); } if (string.IsNullOrEmpty(Configuration.Value())) { Error.WriteLine($"{Configuration.Description} must be specified."); return(false); } if (ExtensionNames.Values.Count != ExtensionFilePaths.Values.Count) { Error.WriteLine($"{ExtensionNames.Description} and {ExtensionFilePaths.Description} should have the same number of values."); } foreach (var filePath in ExtensionFilePaths.Values) { if (!Path.IsPathRooted(filePath)) { Error.WriteLine($"Extension file paths must be fully-qualified, absolute paths."); return(false); } } return(true); }
public static RazorSourceGenerationContext Create(GeneratorExecutionContext context) { var globalOptions = context.AnalyzerConfigOptions.GlobalOptions; if (!globalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace)) { rootNamespace = "ASP"; } globalOptions.TryGetValue("build_property.DesignTimeBuild", out var designTimeBuild); if (!globalOptions.TryGetValue("build_property._RazorReferenceAssemblyTagHelpersOutputPath", out var refsTagHelperOutputCachePath)) { throw new InvalidOperationException("_RazorReferenceAssemblyTagHelpersOutputPath is not specified."); } if (!globalOptions.TryGetValue("build_property.RazorLangVersion", out var razorLanguageVersionString) || !RazorLanguageVersion.TryParse(razorLanguageVersionString, out var razorLanguageVersion)) { context.ReportDiagnostic(Diagnostic.Create( RazorDiagnostics.InvalidRazorLangVersionDescriptor, Location.None, razorLanguageVersionString)); return(null); } if (!globalOptions.TryGetValue("build_property.RazorConfiguration", out var configurationName)) { configurationName = "default"; } globalOptions.TryGetValue("build_property._RazorSourceGeneratorDebug", out var waitForDebugger); globalOptions.TryGetValue("build_property._RazorSourceGeneratorWriteGeneratedOutput", out var writeOutput); globalOptions.TryGetValue("build_property._RazorSourceGeneratorLog", out var enableLogging); var razorConfiguration = RazorConfiguration.Create(razorLanguageVersion, configurationName, Enumerable.Empty <RazorExtension>()); var(razorFiles, cshtmlFiles) = GetRazorInputs(context); var fileSystem = GetVirtualFileSystem(razorFiles, cshtmlFiles); return(new RazorSourceGenerationContext { RootNamespace = rootNamespace, Configuration = razorConfiguration, FileSystem = fileSystem, RazorFiles = razorFiles, CshtmlFiles = cshtmlFiles, DesignTimeBuild = designTimeBuild == "true", RefsTagHelperOutputCachePath = refsTagHelperOutputCachePath, WaitForDebugger = waitForDebugger == "true", WriteGeneratedContent = writeOutput == "true", EnableLogging = enableLogging == "true" }); }
public void TryParseInvalid() { // Arrange var value = "not-version"; // Act var result = RazorLanguageVersion.TryParse(value, out var _); // Assert Assert.False(result); }
// Internal for testing internal async Task OnProjectChanged(IProjectVersionedValue <IProjectSubscriptionUpdate> update) { if (IsDisposing || IsDisposed) { return; } await CommonServices.TasksService.LoadedProjectAsync(async() => { await ExecuteWithLock(async() => { var languageVersion = update.Value.CurrentState[Rules.RazorGeneral.SchemaName].Properties[Rules.RazorGeneral.RazorLangVersionProperty]; var defaultConfiguration = update.Value.CurrentState[Rules.RazorGeneral.SchemaName].Properties[Rules.RazorGeneral.RazorDefaultConfigurationProperty]; RazorConfiguration configuration = null; if (!string.IsNullOrEmpty(languageVersion) && !string.IsNullOrEmpty(defaultConfiguration)) { if (!RazorLanguageVersion.TryParse(languageVersion, out var parsedVersion)) { parsedVersion = RazorLanguageVersion.Latest; } var extensions = update.Value.CurrentState[Rules.RazorExtension.PrimaryDataSourceItemType].Items.Select(e => { return(new ProjectSystemRazorExtension(e.Key)); }).ToArray(); var configurations = update.Value.CurrentState[Rules.RazorConfiguration.PrimaryDataSourceItemType].Items.Select(c => { var includedExtensions = c.Value[Rules.RazorConfiguration.ExtensionsProperty] .Split(';') .Select(name => extensions.Where(e => e.ExtensionName == name).FirstOrDefault()) .Where(e => e != null) .ToArray(); return(new ProjectSystemRazorConfiguration(parsedVersion, c.Key, includedExtensions)); }).ToArray(); configuration = configurations.Where(c => c.ConfigurationName == defaultConfiguration).FirstOrDefault(); } if (configuration == null) { // Ok we can't find a language version. Let's assume this project isn't using Razor then. await UpdateProjectUnsafeAsync(null).ConfigureAwait(false); return; } var hostProject = new HostProject(CommonServices.UnconfiguredProject.FullPath, configuration); await UpdateProjectUnsafeAsync(hostProject).ConfigureAwait(false); }); }, registerFaultHandler : true); }
public void TryParse50() { // Arrange var value = "5.0"; // Act var result = RazorLanguageVersion.TryParse(value, out var version); // Assert Assert.True(result); Assert.Same(RazorLanguageVersion.Version_5_0, version); }
public void TryParseExperimental() { // Arrange var value = "experimental"; // Act var result = RazorLanguageVersion.TryParse(value, out var version); // Assert Assert.True(result); Assert.Same(RazorLanguageVersion.Experimental, version); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartObject) { return(null); } string configurationName = null; string languageVersionValue = null; IReadOnlyList <RazorExtension> extensions = null; reader.ReadProperties(propertyName => { switch (propertyName) { case nameof(RazorConfiguration.ConfigurationName): if (reader.Read()) { configurationName = (string)reader.Value; } break; case nameof(RazorConfiguration.LanguageVersion): if (reader.Read()) { languageVersionValue = reader.Value as string ?? RazorLanguageVersionObjectJsonConverter.Instance.ReadJson( reader, objectType: null, existingValue: null, serializer) as string; } break; case nameof(RazorConfiguration.Extensions): if (reader.Read()) { extensions = serializer.Deserialize <RazorExtension[]>(reader); } break; } }); if (!RazorLanguageVersion.TryParse(languageVersionValue, out var languageVersion)) { languageVersion = RazorLanguageVersion.Version_2_1; } return(RazorConfiguration.Create(languageVersion, configurationName, extensions)); }
// Internal for testing internal static bool TryGetLanguageVersion(IMSBuildEvaluatedPropertyCollection projectProperties, out RazorLanguageVersion languageVersion) { var languageVersionValue = projectProperties.GetValue(RazorLangVersionProperty); if (string.IsNullOrEmpty(languageVersionValue)) { languageVersion = null; return false; } if (!RazorLanguageVersion.TryParse(languageVersionValue, out languageVersion)) { languageVersion = RazorLanguageVersion.Latest; } return true; }
// Internal for testing internal static bool TryGetLanguageVersion(ProjectInstance projectInstance, out RazorLanguageVersion languageVersion) { var languageVersionValue = projectInstance.GetPropertyValue(RazorLangVersionProperty); if (string.IsNullOrEmpty(languageVersionValue)) { languageVersion = null; return(false); } if (!RazorLanguageVersion.TryParse(languageVersionValue, out languageVersion)) { languageVersion = RazorLanguageVersion.Latest; } return(true); }
public RazorSourceGenerationContext(GeneratorExecutionContext context) { var globalOptions = context.AnalyzerConfigOptions.GlobalOptions; if (!globalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace)) { rootNamespace = "ASP"; } var razorLanguageVersion = RazorLanguageVersion.Latest; if (!globalOptions.TryGetValue("build_property.RazorLangVersion", out var razorLanguageVersionString) || !RazorLanguageVersion.TryParse(razorLanguageVersionString, out razorLanguageVersion)) { context.ReportDiagnostic(Diagnostic.Create( RazorDiagnostics.InvalidRazorLangVersionDescriptor, Location.None, razorLanguageVersionString)); } if (!globalOptions.TryGetValue("build_property.RazorConfiguration", out var configurationName)) { configurationName = "default"; } globalOptions.TryGetValue("build_property._RazorSourceGeneratorDebug", out var waitForDebugger); globalOptions.TryGetValue("build_property.SuppressRazorSourceGenerator", out var suppressRazorSourceGenerator); globalOptions.TryGetValue("build_property.GenerateRazorMetadataSourceChecksumAttributes", out var generateMetadataSourceChecksumAttributes); var razorConfiguration = RazorConfiguration.Create(razorLanguageVersion, configurationName, Enumerable.Empty <RazorExtension>(), true); var(razorFiles, cshtmlFiles) = GetRazorInputs(context); var fileSystem = GetVirtualFileSystem(context, razorFiles, cshtmlFiles); RootNamespace = rootNamespace; Configuration = razorConfiguration; FileSystem = fileSystem; RazorFiles = razorFiles; CshtmlFiles = cshtmlFiles; WaitForDebugger = waitForDebugger == "true"; SuppressRazorSourceGenerator = suppressRazorSourceGenerator == "true"; GenerateMetadataSourceChecksumAttributes = generateMetadataSourceChecksumAttributes == "true"; }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartObject) { return(null); } var configurationName = reader.ReadNextStringProperty(nameof(RazorConfiguration.ConfigurationName)); var languageVersionValue = reader.ReadNextStringProperty(nameof(RazorConfiguration.LanguageVersion)); var extensions = reader.ReadPropertyArray <RazorExtension>(serializer, nameof(RazorConfiguration.Extensions)); if (!RazorLanguageVersion.TryParse(languageVersionValue, out var languageVersion)) { languageVersion = RazorLanguageVersion.Version_2_1; } return(RazorConfiguration.Create(languageVersion, configurationName, extensions)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartObject) { return(null); } var obj = JObject.Load(reader); var configurationName = obj[nameof(RazorConfiguration.ConfigurationName)].Value <string>(); var languageVersionValue = obj[nameof(RazorConfiguration.LanguageVersion)].Value <string>(); var extensions = obj[nameof(RazorConfiguration.Extensions)].ToObject <RazorExtension[]>(serializer); if (!RazorLanguageVersion.TryParse(languageVersionValue, out var languageVersion)) { languageVersion = RazorLanguageVersion.Version_2_1; } return(RazorConfiguration.Create(languageVersion, configurationName, extensions)); }
protected override bool ValidateArguments() { if (Sources.Values.Count == 0) { Error.WriteLine($"{Sources.Description} should have at least one value."); return(false); } if (Outputs.Values.Count != Sources.Values.Count) { Error.WriteLine($"{Sources.Description} has {Sources.Values.Count}, but {Outputs.Description} has {Outputs.Values.Count} values."); return(false); } if (RelativePaths.Values.Count != Sources.Values.Count) { Error.WriteLine($"{Sources.Description} has {Sources.Values.Count}, but {RelativePaths.Description} has {RelativePaths.Values.Count} values."); return(false); } if (FileKinds.Values.Count != 0 && FileKinds.Values.Count != Sources.Values.Count) { // 2.x tasks do not specify FileKinds - in which case, no values will be present. If a kind for one file is specified, we expect as many kind entries // as sources. Error.WriteLine($"{Sources.Description} has {Sources.Values.Count}, but {FileKinds.Description} has {FileKinds.Values.Count} values."); return(false); } if (CssScopeSources.Values.Count != CssScopeValues.Values.Count) { // CssScopeSources and CssScopeValues arguments must appear as matched pairs Error.WriteLine($"{CssScopeSources.Description} has {CssScopeSources.Values.Count}, but {CssScopeValues.Description} has {CssScopeValues.Values.Count} values."); return(false); } if (string.IsNullOrEmpty(ProjectDirectory.Value())) { ProjectDirectory.Values.Add(Environment.CurrentDirectory); } if (string.IsNullOrEmpty(Version.Value())) { Error.WriteLine($"{Version.Description} must be specified."); return(false); } else if (!RazorLanguageVersion.TryParse(Version.Value(), out _)) { Error.WriteLine($"Invalid option {Version.Value()} for Razor language version --version; must be Latest or a valid version in range {RazorLanguageVersion.Version_1_0} to {RazorLanguageVersion.Latest}."); return(false); } if (string.IsNullOrEmpty(Configuration.Value())) { Error.WriteLine($"{Configuration.Description} must be specified."); return(false); } if (ExtensionNames.Values.Count != ExtensionFilePaths.Values.Count) { Error.WriteLine($"{ExtensionNames.Description} and {ExtensionFilePaths.Description} should have the same number of values."); } foreach (var filePath in ExtensionFilePaths.Values) { if (!Path.IsPathRooted(filePath)) { Error.WriteLine($"Extension file paths must be fully-qualified, absolute paths."); return(false); } } return(true); }