private static IEnumerable <string> GetImports(DirectivesParser directives) { var imports = new List <string> { "System", "System.Linq", "System.Collections.Generic", "Microsoft.VisualStudio.Services.WebApi", "aggregator.Engine" }; imports.AddRange(directives.Imports); return(imports.Distinct()); }
public RuleEngine(IAggregatorLogger logger, string[] ruleCode, SaveMode mode, bool dryRun) { State = EngineState.Unknown; this.logger = logger; this.saveMode = mode; this.DryRun = dryRun; var directives = new DirectivesParser(logger, ruleCode); if (!directives.Parse()) { State = EngineState.Error; return; } if (directives.Language == DirectivesParser.Languages.Csharp) { var types = new List <Type>() { typeof(object), typeof(System.Linq.Enumerable), typeof(System.Collections.Generic.CollectionExtensions), typeof(Microsoft.VisualStudio.Services.WebApi.IdentityRef) }; var references = types.ConvertAll(t => t.Assembly).Distinct(); var scriptOptions = ScriptOptions.Default .WithEmitDebugInformation(true) .WithReferences(references) // Add namespaces .WithImports( "System", "System.Linq", "System.Collections.Generic", "Microsoft.VisualStudio.Services.WebApi" ); this.roslynScript = CSharpScript.Create <string>( code: directives.GetRuleCode(), options: scriptOptions, globalsType: typeof(Globals)); } else { logger.WriteError($"Cannot execute rule: language is not supported."); State = EngineState.Error; } }
private static IEnumerable <Assembly> LoadReferences(DirectivesParser directives) { var types = new List <Type>() { typeof(object), typeof(System.Linq.Enumerable), typeof(System.Collections.Generic.CollectionExtensions), typeof(Microsoft.VisualStudio.Services.WebApi.IdentityRef), typeof(WorkItemWrapper) }; var references = types.ConvertAll(t => t.Assembly); // user references foreach (var reference in directives.References) { var name = new AssemblyName(reference); references.Add(Assembly.Load(name)); } return(references.Distinct()); }
public RuleEngine(IAggregatorLogger logger, string[] ruleCode, SaveMode mode, bool dryRun) { State = EngineState.Unknown; this.logger = logger; this.saveMode = mode; this.DryRun = dryRun; var directives = new DirectivesParser(logger, ruleCode); if (!directives.Parse()) { State = EngineState.Error; return; } if (directives.Language == DirectivesParser.Languages.Csharp) { var references = LoadReferences(directives); var imports = GetImports(directives); var scriptOptions = ScriptOptions.Default .WithEmitDebugInformation(true) .WithReferences(references) // Add namespaces .WithImports(imports) ; this.roslynScript = CSharpScript.Create <string>( code: directives.GetRuleCode(), options: scriptOptions, globalsType: typeof(Globals)); } else { logger.WriteError($"Cannot execute rule: language is not supported."); State = EngineState.Error; } }