/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; namespace Opc.Ua.SourceGeneration { internal static class Extensions { /// /// Get options from file options /// /// /// public static NodesetFileOptions ToNodeSetOptions(this AnalyzerConfigOptions options) { if (options == null) { return new NodesetFileOptions(); } return new NodesetFileOptions { Ignore = options.GetBool(nameof(NodesetFileOptions.Ignore), false), Prefix = options.GetString(nameof(NodesetFileOptions.Prefix), false), Version = options.GetString(nameof(NodesetFileOptions.Version), false), Name = options.GetString(nameof(NodesetFileOptions.Name), false), ModelUri = options.GetString(nameof(NodesetFileOptions.ModelUri), false) }; } /// /// Create collection /// public static NodesetFileCollection ToNodeSetFileCollection( this ImmutableArray<(AdditionalText, NodesetFileOptions)> nodeset2Files, IFileSystem fileSystem, ITelemetryContext telemetry) { return new NodesetFileCollection( [.. nodeset2Files.Select(f => (f.Item1.Path, f.Item2))], fileSystem, telemetry); } /// /// Design files end in xml but are not nodeset2.xml files. /// /// /// public static bool IsDesignOrNodeset2File(this AdditionalText text) { return text.HasFileExtension("xml"); } /// /// Identifer files are csv files /// public static bool IsIdentifierFile(this AdditionalText text) { return text.HasFileExtension("csv"); } /// /// Has file extension check /// public static bool HasFileExtension(this AdditionalText text, string extension) { return text.Path.EndsWith("." + extension, StringComparison.OrdinalIgnoreCase); } /// /// Get value /// /// public static T GetValue( this AnalyzerConfigOptions options, string propertyName, Func converter, bool buildProperty = true) { string prefix = buildProperty ? "build_property" : "build_metadata.AdditionalFiles"; options.TryGetValue( $"{prefix}.{SourceGenerator.Name}{propertyName}".ToLowerInvariant(), out string value); return converter(value); } /// /// Get integer value from build properties /// public static int GetInteger( this AnalyzerConfigOptions config, string propertyName, bool buildProperty = true) { return config.GetValue(propertyName, s => s != null && int.TryParse(s, out int integer) ? integer : 0, buildProperty); } /// /// Get boolean option from options /// public static bool GetBool( this AnalyzerConfigOptions config, string propertyName, bool buildProperty = true) { return config.GetValue(propertyName, s => string.Equals(s, "true", StringComparison.OrdinalIgnoreCase), buildProperty); } /// /// Get string option from options /// public static string GetString( this AnalyzerConfigOptions config, string propertyName, bool buildProperty = true) { return config.GetValue(propertyName, s => s, buildProperty) ?? string.Empty; } /// /// Get string option from options /// public static List GetStrings( this AnalyzerConfigOptions config, string propertyName, bool buildProperty = true) { return config.GetValue(propertyName, Split, buildProperty); static List Split(string s) { return s == null ? [] : [.. s .Split(';', ',', '+') .Select(e => e.Trim()) .Where(s => !string.IsNullOrEmpty(s))]; } } /// /// Get namespace information /// public static string GetFullNamespace(this INamedTypeSymbol symbol) { var parts = new List(); INamespaceSymbol ns = symbol.ContainingNamespace; while (ns != null && !ns.IsGlobalNamespace) { parts.Insert(0, ns.Name); ns = ns.ContainingNamespace; } return string.Join(".", parts); } /// /// Get fully qualified type name string with global:: /// prefix and no global namespace /// public static string GetFullyQualifiedTypeName(this ITypeSymbol type) { return "global::" + type.ToDisplayString( SymbolDisplayFormat.FullyQualifiedFormat .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)); } /// /// Check whether the type implements an interface with the /// given name, regardless of namespace. This is used to check for /// specific interfaces without needing the full namespace. /// public static bool ImplementsInterface( this ITypeSymbol type, string interfaceName) { return type.AllInterfaces.Any(i => i.Name == interfaceName); } /// /// Check if a type symbol has a specific attribute by name. /// public static bool HasAttribute( this ITypeSymbol type, string attributeName) { return type.GetAttributes().Any(a => a.AttributeClass?.Name == attributeName); } /// /// Get a named argument from the attribute /// public static string GetValue( this AttributeData attr, string name) { if (attr == null) { return null; } foreach (KeyValuePair kvp in attr.NamedArguments) { if (kvp.Key == name && kvp.Value.Value is string s) { return s; } } return null; } /// /// Get a named argument from the attribute /// public static int GetInteger( this AttributeData attr, string name, int defaultValue = default) { if (attr == null) { return defaultValue; } foreach (KeyValuePair kvp in attr.NamedArguments) { if (kvp.Key == name && kvp.Value.Value is int i) { return i; } } return defaultValue; } } }