Beispiel #1
0
        public LocalDataSource(AuditEnvironment host_env, Dictionary <string, object> datasource_options)
        {
            if (!datasource_options.ContainsKey("DirectoryPath"))
            {
                throw new ArgumentException("The datasource options does not contain the DirectoryPath");
            }
            this.DataSourceOptions = datasource_options;
            this.HostEnvironment   = host_env;
            string dir_path = (string)this.DataSourceOptions["DirectoryPath"];

            try
            {
                DirectoryInfo dir = new DirectoryInfo(dir_path);
                if (!dir.Exists)
                {
                    HostEnvironment.Error("The directory {0} does not exist.", dir.FullName);
                    this.DataSourceInitialised = false;
                    return;
                }
                else
                {
                    Directory = dir;
                }
            }
            catch (Exception e)
            {
                this.HostEnvironment.Error(e, "An error occurred attempting to access the directory {0}.", dir_path);
                this.DataSourceInitialised = false;
                return;
            }
        }
Beispiel #2
0
        protected virtual async Task GetAnalyzers()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            // Just in case clear AlternativeCompiler so it is not set to Roslyn or anything else by
            // the CS-Script installed (if any) on the host OS
            CSScript.GlobalSettings.UseAlternativeCompiler = null;
            CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Mono;
            CSScript.CacheEnabled           = false; //Script caching is broken on Mono: https://github.com/oleg-shilo/cs-script/issues/10
            CSScript.KeepCompilingHistory   = true;
            DirectoryInfo analyzers_dir = new DirectoryInfo(Path.Combine(this.DevAuditDirectory, "Analyzers", this.AnalyzerType));

            if (!analyzers_dir.Exists)
            {
                this.AnalyzersInitialized = false;
                return;
            }
            this.AnalyzerScripts = analyzers_dir.GetFiles("*.cs", SearchOption.AllDirectories).ToList();
            foreach (FileInfo f in this.AnalyzerScripts)
            {
                string script = string.Empty;
                using (FileStream fs = f.OpenRead())
                    using (StreamReader r = new StreamReader(fs))
                    {
                        script = await r.ReadToEndAsync();
                    }
                try
                {
                    ByteCodeAnalyzer ba = (ByteCodeAnalyzer)await CSScript.Evaluator.LoadCodeAsync(script, this.HostEnvironment.ScriptEnvironment,
                                                                                                   this.Modules, this.Configuration, this.ApplicationOptions);

                    this.Analyzers.Add(ba);
                    this.HostEnvironment.Info("Loaded {0} analyzer from {1}.", ba.Name, f.FullName);
                }
                catch (csscript.CompilerException ce)
                {
                    HostEnvironment.Error("Compiler error(s) compiling analyzer {0}.", f.FullName);
                    IDictionaryEnumerator en = ce.Data.GetEnumerator();
                    while (en.MoveNext())
                    {
                        List <string> v = (List <string>)en.Value;
                        if (v.Count > 0)
                        {
                            if ((string)en.Key == "Errors")
                            {
                                HostEnvironment.ScriptEnvironment.Error(v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2));
                            }
                            else if ((string)en.Key == "Warnings")
                            {
                                HostEnvironment.ScriptEnvironment.Warning(v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2));
                            }
                            else
                            {
                                HostEnvironment.ScriptEnvironment.Error("{0} : {1}", en.Key, v.Aggregate((s1, s2) => s1 + Environment.NewLine + s2));
                            }
                        }
                    }
                    throw ce;
                }
            }
            sw.Stop();
            if (this.AnalyzerScripts.Count == 0)
            {
                this.HostEnvironment.Info("No {0} analyzers found in {1}.", this.AnalyzerType, analyzers_dir.FullName);
                return;
            }
            else if (this.AnalyzerScripts.Count > 0 && this.Analyzers.Count > 0)
            {
                if (this.Analyzers.Count < this.AnalyzerScripts.Count)
                {
                    this.HostEnvironment.Warning("Failed to load {0} of {1} analyzer(s).", this.AnalyzerScripts.Count - this.Analyzers.Count, this.AnalyzerScripts.Count);
                }
                this.HostEnvironment.Success("Loaded {0} out of {1} analyzer(s) in {2} ms.", this.Analyzers.Count, this.AnalyzerScripts.Count, sw.ElapsedMilliseconds);
                this.AnalyzersInitialized = true;
                return;
            }
            else
            {
                this.HostEnvironment.Error("Failed to load {0} analyzer(s).", this.AnalyzerScripts.Count);
                return;
            }
        }