private void Initialize(IPreprocessedRule preprocessedRule) { RuleDirectives = preprocessedRule; ImpersonateExecution = RuleDirectives.Impersonate; var references = new HashSet <Assembly>(DefaultAssemblyReferences().Concat(RuleDirectives.LoadAssemblyReferences())); var imports = new HashSet <string>(DefaultImports().Concat(RuleDirectives.Imports)); var scriptOptions = ScriptOptions.Default .WithEmitDebugInformation(true) .WithReferences(references) // Add namespaces .WithImports(imports); if (RuleDirectives.IsCSharp()) { _roslynScript = CSharpScript.Create <string>( code: RuleDirectives.GetRuleCode(), options: scriptOptions, globalsType: typeof(RuleExecutionContext)); } else { _logger.WriteError($"Cannot execute rule: language is not supported."); } }
public static string LanguageAsString(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.Language switch { RuleLanguage.Csharp => "C#", RuleLanguage.Unknown => "Unknown", _ => throw new ArgumentOutOfRangeException($"BUG: {nameof(LanguageAsString)} misses {nameof(RuleLanguage)} enum value"), });
public static string LanguageAsString(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.Language switch { RuleLanguage.Csharp => "C#", RuleLanguage.Unknown => "Unknown", #pragma warning disable S3928 // The parameter name {0} is not declared in the argument list _ => throw new ArgumentOutOfRangeException($"BUG: {nameof(LanguageAsString)} is not a valid {nameof(RuleLanguage)}"), #pragma warning restore S3928 });
public static string LanguageAsString(this IPreprocessedRule preprocessedRule) { switch (preprocessedRule.Language) { case RuleLanguage.Csharp: return("C#"); case RuleLanguage.Unknown: return("Unknown"); default: throw new ArgumentOutOfRangeException(); } }
public static string[] Write(IPreprocessedRule preprocessedRule) { var content = new List <string> { $".language={preprocessedRule.LanguageAsString()}" }; if (preprocessedRule.Impersonate) { content.Add($".impersonate=onBehalfOfInitiator"); } content.AddRange(preprocessedRule.References.Select(reference => $".reference={reference}")); content.AddRange(preprocessedRule.Imports.Select(import => $".import={import}")); content.AddRange(preprocessedRule.RuleCode.Skip(preprocessedRule.FirstCodeLine)); return(content.ToArray()); }
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously private static async Task <IDictionary <string, string> > PackagingFilesAsync(string ruleName, IPreprocessedRule preprocessedRule) #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { var inMemoryFiles = new Dictionary <string, string> { { $"{ruleName}.rule", string.Join(Environment.NewLine, RuleFileParser.Write(preprocessedRule)) } }; // TODO need to find the Assembly with these files, maybe we should move them to shared //var assembly = Assembly.GetExecutingAssembly(); //await inMemoryFiles.AddFunctionDefaultFiles(assembly); return(inMemoryFiles); }
internal static string GetRuleCode(this IPreprocessedRule preprocessedRule) { return(string.Join(Environment.NewLine, preprocessedRule.RuleCode)); }
internal static IEnumerable <Assembly> LoadAssemblyReferences(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.References .Select(reference => new AssemblyName(reference)) .Select(Assembly.Load)); }
internal static bool IsSupportedLanguage(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.Language != RuleLanguage.Unknown); }
internal static bool IsCSharp(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.Language == RuleLanguage.Csharp); }
internal static bool IsValid(this IPreprocessedRule preprocessedRule) { return(preprocessedRule.IsSupportedLanguage() && preprocessedRule.RuleCode.Any()); }
public ScriptedRuleWrapper(string ruleName, IPreprocessedRule preprocessedRule, IAggregatorLogger logger) : this(ruleName, logger) { Initialize(preprocessedRule); }
public ScriptedRuleWrapper(string ruleName, IPreprocessedRule preprocessedRule) : this(ruleName, preprocessedRule, new NullLogger()) { }
private static async Task <IDictionary <string, string> > PackagingFilesAsync(string name, IPreprocessedRule preprocessedRule) { var inMemoryFiles = new Dictionary <string, string> { { $"{name}.rule", string.Join(Environment.NewLine, RuleFileParser.Write(preprocessedRule)) } }; var assembly = Assembly.GetExecutingAssembly(); // TODO we can deserialize a KuduFunctionConfig instead of using a fixed file... using (var stream = assembly.GetManifestResourceStream("aggregator.cli.Rules.function.json")) { using (var reader = new StreamReader(stream)) { var content = await reader.ReadToEndAsync(); inMemoryFiles.Add("function.json", content); } } using (var stream = assembly.GetManifestResourceStream("aggregator.cli.Rules.run.csx")) { using (var reader = new StreamReader(stream)) { var content = await reader.ReadToEndAsync(); inMemoryFiles.Add("run.csx", content); } } return(inMemoryFiles); }
private static async Task <IDictionary <string, string> > PackagingFilesAsync(string ruleName, IPreprocessedRule preprocessedRule) { var inMemoryFiles = new Dictionary <string, string> { { $"{ruleName}.rule", string.Join(Environment.NewLine, RuleFileParser.Write(preprocessedRule)) } }; //var assembly = Assembly.GetExecutingAssembly(); //await inMemoryFiles.AddFunctionDefaultFiles(assembly); return(inMemoryFiles); }
public static async Task WriteFile(string ruleFilePath, IPreprocessedRule preprocessedRule, CancellationToken cancellationToken = default) { var content = Write(preprocessedRule); await WriteAllLinesAsync(ruleFilePath, content, cancellationToken); }