namespace StockSharp.Algo.Compilation; using Ecng.Compilation; /// /// Extension class. /// public static class CodeExtensions { /// /// Microsoft Core Library. /// public const string MsCorLib = "mscorlib"; /// /// Default references. /// public static IEnumerable DefaultReferences => CreateAssemblyReferences( [ MsCorLib, "netstandard", "System", "System.Core", "System.Configuration", "System.Xml", "System.Runtime", "System.Linq", "System.Private.CoreLib", "System.ObjectModel", "System.ComponentModel.Primitives", "System.ComponentModel.TypeConverter", "System.ComponentModel.Annotations", "System.Collections", "System.Drawing.Primitives", "System.Runtime.InteropServices", #if NET10_0_OR_GREATER "System.Linq.AsyncEnumerable", #else "Ecng.Linq", #endif "Ecng.Common", "Ecng.Collections", "Ecng.ComponentModel", "Ecng.Configuration", "Ecng.Serialization", "Ecng.Reflection", "Ecng.Drawing", "Ecng.Logging", "Ecng.IO", "StockSharp.Algo", "StockSharp.Algo.Indicators", "StockSharp.Algo.Strategies", "StockSharp.Messages", "StockSharp.BusinessEntities", "StockSharp.Localization", "StockSharp.Diagram.Core", "StockSharp.Charting.Interfaces", "StockSharp.Alerts.Interfaces", "StockSharp.Reporting", ]); /// /// F# references. /// public static IEnumerable FSharpReferences => CreateAssemblyReferences( [ "FSharp.Core", "FSharp.Control.TaskSeq", "System.Runtime.Numerics", "System.Numerics", "System.Net.Requests", "System.Net.WebClient", "System.Private.Uri", "System.Threading", ]); /// /// Create assembly references. /// /// Assembly names. /// List of . public static IEnumerable CreateAssemblyReferences(this IEnumerable names) => names.Select(r => new AssemblyReference { FileName = $"{r}.dll" }); private static readonly CachedSynchronizedDictionary _projectReferences = new(StringComparer.InvariantCultureIgnoreCase); /// /// Project references. /// public static IEnumerable ProjectReferences => _projectReferences.CachedValues; /// /// Add project reference. /// /// public static void AddProjectReference(ICodeReference reference) { if (reference is null) throw new ArgumentNullException(nameof(reference)); _projectReferences.Add(reference.Id, reference); } /// /// Try get project reference. /// /// /// /// Check result. public static bool TryGetProjectReference(string id, out ICodeReference reference) { if (id.IsEmpty()) throw new ArgumentNullException(nameof(id)); return _projectReferences.TryGetValue(id, out reference); } /// /// Remove project reference. /// /// /// Check result. public static bool RemoveProjectReference(string id) => _projectReferences.Remove(id); /// /// Add assembly reference. /// /// /// Assembly path. public static void AddAsmRef(this CodeInfo ci, string asmFile) => ci.CheckOnNull(nameof(ci)).AssemblyReferences.Add(new() { FileName = asmFile }); /// /// Default language. /// public const string DefaultLanguage = FileExts.CSharp; /// /// Get C# compiler. /// /// public static ICompiler GetCSharpCompiler() => TryGetCSharpCompiler() ?? throw new InvalidOperationException($"No compiler for {DefaultLanguage}."); /// /// Try get C# compiler. /// /// public static ICompiler TryGetCSharpCompiler() => TryGetCompiler(DefaultLanguage); /// /// Try get compiler for the specified file extension. /// /// File extension. /// public static ICompiler GetCompiler(this string fileExt) => TryGetCompiler(fileExt) ?? throw new InvalidOperationException($"No compiler for {fileExt}."); /// /// Try get compiler for the specified language. /// /// File extension. /// public static ICompiler TryGetCompiler(this string fileExt) { if (fileExt.IsEmpty()) throw new ArgumentNullException(nameof(fileExt)); var provider = ServicesRegistry.TryCompilerProvider; ICompiler compiler; if (provider is not null) { if (!provider.TryGetValue(fileExt, out compiler)) return null; } else { compiler = ServicesRegistry.TryCompiler; if (compiler?.Extension.EqualsIgnoreCase(fileExt) != true) return null; } return compiler; } /// /// Determine whether the specified file extension is a code file. /// /// File extension. /// Check result. public static bool IsCodeExtension(this string fileExt) => TryGetCompiler(fileExt) is not null; /// /// Determine whether the specified code supports references. /// /// /// Check result. public static bool IsReferencesSupported(this CodeInfo code) => code.CheckOnNull(nameof(code)).Language.IsReferencesSupported(); /// /// Determines whether the specified file extension supports references. /// /// /// Check result. public static bool IsReferencesSupported(this string langExt) => TryGetCompiler(langExt)?.IsReferencesSupported == true; }