コード例 #1
0
        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.");
            }
        }
コード例 #2
0
 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"),
     });
コード例 #3
0
        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
            });
コード例 #4
0
        public static string LanguageAsString(this IPreprocessedRule preprocessedRule)
        {
            switch (preprocessedRule.Language)
            {
            case RuleLanguage.Csharp:
                return("C#");

            case RuleLanguage.Unknown:
                return("Unknown");

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #5
0
        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());
        }
コード例 #6
0
#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);
        }
コード例 #7
0
 internal static string GetRuleCode(this IPreprocessedRule preprocessedRule)
 {
     return(string.Join(Environment.NewLine, preprocessedRule.RuleCode));
 }
コード例 #8
0
 internal static IEnumerable <Assembly> LoadAssemblyReferences(this IPreprocessedRule preprocessedRule)
 {
     return(preprocessedRule.References
            .Select(reference => new AssemblyName(reference))
            .Select(Assembly.Load));
 }
コード例 #9
0
 internal static bool IsSupportedLanguage(this IPreprocessedRule preprocessedRule)
 {
     return(preprocessedRule.Language != RuleLanguage.Unknown);
 }
コード例 #10
0
 internal static bool IsCSharp(this IPreprocessedRule preprocessedRule)
 {
     return(preprocessedRule.Language == RuleLanguage.Csharp);
 }
コード例 #11
0
 internal static bool IsValid(this IPreprocessedRule preprocessedRule)
 {
     return(preprocessedRule.IsSupportedLanguage() && preprocessedRule.RuleCode.Any());
 }
コード例 #12
0
 public ScriptedRuleWrapper(string ruleName, IPreprocessedRule preprocessedRule, IAggregatorLogger logger) : this(ruleName, logger)
 {
     Initialize(preprocessedRule);
 }
コード例 #13
0
 public ScriptedRuleWrapper(string ruleName, IPreprocessedRule preprocessedRule) : this(ruleName, preprocessedRule, new NullLogger())
 {
 }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
0
        public static async Task WriteFile(string ruleFilePath, IPreprocessedRule preprocessedRule, CancellationToken cancellationToken = default)
        {
            var content = Write(preprocessedRule);

            await WriteAllLinesAsync(ruleFilePath, content, cancellationToken);
        }