Ejemplo n.º 1
0
        void ExecuteScripts()
        {
            if (ScriptFiles.Count == 0)
            {
                Error("No scripts / script files provided");
                throw new CommandLineException();
            }

            foreach (var s in ScriptFiles)
            {
                if (File.Exists(s))
                {
                    Scripts.Add(File.ReadAllText(s));
                    Console.WriteLine("Loaded script: " + s);
                }
                else if (s.IndexOfAny(new[] { ';', ',', '"' }) != -1)
                {
                    Scripts.Add(s);
                }
                else
                {
                    Error("Script file not found: " + s);
                    throw new CommandLineException();
                }
            }

            for (int i = 0; i < Scripts.Count; i++)
            {
                var script = Scripts[i];
                Console.WriteLine("Executing script {0}...", i);

                System.CodeDom.Compiler.CompilerResults result;
                Scripting.ScriptOutputForm.Reset(false);
                var dyn = ScriptEngine.CompileScript(script, out result);
                //nUnit.StartSuite("Script Compilation");
                if (result.Errors.Count > 0)
                {
                    Error("Script compilation errors:");
                    var errIndex = 0;
                    foreach (System.CodeDom.Compiler.CompilerError err in result.Errors)
                    {
                        errIndex++;
                        ErrorX(err.ErrorText, ScriptFiles[i], err.Line, err.Column, err.ErrorNumber);
                        //nUnit.Failure("Script Compilation", $"Compilation Error #{errIndex}", err.ErrorText, $"{scriptFile} line {err.Line}, column {err.Column}");
                    }
                    throw new CommandLineException();
                }
                try
                {
                    Handler.BeginUpdate("script");
                    dyn.Invoke(Handler.Model, null);
                    Handler.EndUpdateAll();
                }
                catch (Exception ex)
                {
                    Error("Script execution error: " + ex.Message);
                    throw new CommandLineException();
                }
                finally
                {
                    Handler.Model.Database.CloseReader();
                }
            }
        }