Beispiel #1
0
        public List <ProgramError> Compile()
        {
            List <ProgramError> errors = new List <ProgramError>();

            // check for output directory
            if (!Directory.Exists(Path.GetDirectoryName(AssemblyFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(AssemblyFile));
            }

            // dispose assembly and interrupt current task (if any)
            programBlock.IsEnabled = false;


            // clean up old assembly files
            try
            {
                if (File.Exists(this.AssemblyFile))
                {
                    File.Delete(this.AssemblyFile);
                }
                if (File.Exists(this.AssemblyFile + ".mdb"))
                {
                    File.Delete(this.AssemblyFile + ".mdb");
                }
                if (File.Exists(this.AssemblyFile.Replace(".dll", ".mdb")))
                {
                    File.Delete(this.AssemblyFile.Replace(".dll", ".mdb"));
                }
                if (File.Exists(this.AssemblyFile + ".pdb"))
                {
                    File.Delete(this.AssemblyFile + ".pdb");
                }
                if (File.Exists(this.AssemblyFile.Replace(".dll", ".pdb")))
                {
                    File.Delete(this.AssemblyFile.Replace(".dll", ".pdb"));
                }
            }
            catch (Exception ee)
            {
                HomeGenieService.LogError(ee);
            }


            // DO NOT CHANGE THE FOLLOWING LINES OF CODE
            // it is a lil' trick for mono compatibility
            // since it will be caching the assembly when using the same name
            // and use the old one instead of the new one
            string tmpfile = Path.Combine("programs", Guid.NewGuid().ToString() + ".dll");

            System.CodeDom.Compiler.CompilerResults result = new System.CodeDom.Compiler.CompilerResults(null);
            try
            {
                result = CSharpAppFactory.CompileScript(programBlock.ScriptCondition, programBlock.ScriptSource, tmpfile);
            }
            catch (Exception ex)
            {
                // report errors during post-compilation process
                result.Errors.Add(new System.CodeDom.Compiler.CompilerError(programBlock.Name, 0, 0, "-1", ex.Message));
            }

            if (result.Errors.Count > 0)
            {
                int sourceLines = programBlock.ScriptSource.Split('\n').Length;
                foreach (System.CodeDom.Compiler.CompilerError error in result.Errors)
                {
                    int    errorRow  = (error.Line - CSharpAppFactory.PROGRAM_CODE_OFFSET);
                    string blockType = "CR";
                    if (errorRow >= sourceLines + CSharpAppFactory.CONDITION_CODE_OFFSET)
                    {
                        errorRow -= (sourceLines + CSharpAppFactory.CONDITION_CODE_OFFSET);
                        blockType = "TC";
                    }
                    if (!error.IsWarning)
                    {
                        errors.Add(new ProgramError()
                        {
                            Line         = errorRow,
                            Column       = error.Column,
                            ErrorMessage = error.ErrorText,
                            ErrorNumber  = error.ErrorNumber,
                            CodeBlock    = blockType
                        });
                    }
                    else
                    {
                        var warning = String.Format("{0},{1},{2}: {3}", blockType, errorRow, error.Column, error.ErrorText);
                        homegenie.ProgramManager.RaiseProgramModuleEvent(programBlock, Properties.CompilerWarning, warning);
                    }
                }
            }
            if (errors.Count == 0)
            {
                // move/copy new assembly files
                // rename temp file to production file
                appAssembly = result.CompiledAssembly;
                try
                {
                    //string tmpfile = new Uri(value.CodeBase).LocalPath;
                    File.Move(tmpfile, this.AssemblyFile);
                    if (File.Exists(tmpfile + ".mdb"))
                    {
                        File.Move(tmpfile + ".mdb", this.AssemblyFile + ".mdb");
                    }
                    if (File.Exists(tmpfile.Replace(".dll", ".mdb")))
                    {
                        File.Move(tmpfile.Replace(".dll", ".mdb"), this.AssemblyFile.Replace(".dll", ".mdb"));
                    }
                    if (File.Exists(tmpfile + ".pdb"))
                    {
                        File.Move(tmpfile + ".pdb", this.AssemblyFile + ".pdb");
                    }
                    if (File.Exists(tmpfile.Replace(".dll", ".pdb")))
                    {
                        File.Move(tmpfile.Replace(".dll", ".pdb"), this.AssemblyFile.Replace(".dll", ".pdb"));
                    }
                }
                catch (Exception ee)
                {
                    HomeGenieService.LogError(ee);
                }
            }

            return(errors);
        }
Beispiel #2
0
        public override List <ProgramError> Compile()
        {
            var errors = new List <ProgramError>();

            // check for output directory
            if (!Directory.Exists(Path.GetDirectoryName(AssemblyFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(AssemblyFile));
            }

            // dispose assembly and interrupt current task (if any)
            bool wasEnabled = ProgramBlock.IsEnabled;

            ProgramBlock.ScriptErrors = "";
            ProgramBlock.IsEnabled    = false;

            // clean up old assembly files
            try
            {
                // If the file to be deleted does not exist, no exception is thrown.
                File.Delete(AssemblyFile);
                File.Delete(AssemblyFile + ".mdb");
                File.Delete(AssemblyFile.Replace(".dll", ".mdb"));
                File.Delete(AssemblyFile + ".pdb");
                File.Delete(AssemblyFile.Replace(".dll", ".pdb"));
            }
            catch (Exception ex)
            {
                HomeGenieService.LogError(ex);
            }


            // DO NOT CHANGE THE FOLLOWING LINES OF CODE
            // it is a lil' trick for mono compatibility
            // since it will be caching the assembly when using the same name
            // and use the old one instead of the new one
            var tempFile = Path.Combine("programs", Guid.NewGuid().ToString() + ".dll");

#if NETCOREAPP
            EmitResult result = null;
            try
            {
                result = CSharpAppFactory.CompileScript(ProgramBlock.ScriptSetup, ProgramBlock.ScriptSource, tempFile);
            }
            catch (Exception ex)
            {
                // report errors during post-compilation process
                //result.Errors.Add(new System.CodeDom.Compiler.CompilerError(ProgramBlock.Name, 0, 0, "-1", ex.Message));
            }

            if (result != null && !result.Success)
            {
                var sourceLines = ProgramBlock.ScriptSource.Split('\n').Length;
                foreach (var diagnostic in result.Diagnostics)
                {
                    var errorRow  = (diagnostic.Location.GetLineSpan().StartLinePosition.Line - CSharpAppFactory.ProgramCodeOffset) + 1;
                    var errorCol  = diagnostic.Location.GetLineSpan().StartLinePosition.Character + 1;
                    var blockType = CodeBlockEnum.CR;
                    if (diagnostic.Severity == DiagnosticSeverity.Error)
                    {
                        if (errorRow >= sourceLines + CSharpAppFactory.ConditionCodeOffset)
                        {
                            errorRow -= (sourceLines + CSharpAppFactory.ConditionCodeOffset);
                            blockType = CodeBlockEnum.TC;
                        }
                        errors.Add(new ProgramError
                        {
                            Line         = errorRow,
                            Column       = errorCol,
                            ErrorMessage = diagnostic.GetMessage(),
                            ErrorNumber  = diagnostic.Descriptor.Id,
                            CodeBlock    = blockType
                        });
                    }
                    else
                    {
                        var warning = String.Format("{0},{1},{2}: {3}", blockType, errorRow, errorCol,
                                                    diagnostic.GetMessage());
                        HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.CompilerWarning,
                                                                         warning);
                    }
                }
            }
#else
            var result = new System.CodeDom.Compiler.CompilerResults(null);
            try
            {
                result = CSharpAppFactory.CompileScript(ProgramBlock.ScriptSetup, ProgramBlock.ScriptSource, tempFile);
            }
            catch (Exception ex)
            {
                // report errors during post-compilation process
                result.Errors.Add(new System.CodeDom.Compiler.CompilerError(ProgramBlock.Name, 0, 0, "-1", ex.Message));
            }

            if (result.Errors.Count > 0)
            {
                var sourceLines = ProgramBlock.ScriptSource.Split('\n').Length;
                foreach (System.CodeDom.Compiler.CompilerError error in result.Errors)
                {
                    var errorRow  = (error.Line - CSharpAppFactory.ProgramCodeOffset);
                    var blockType = CodeBlockEnum.CR;
                    if (errorRow >= sourceLines + CSharpAppFactory.ConditionCodeOffset)
                    {
                        errorRow -= (sourceLines + CSharpAppFactory.ConditionCodeOffset);
                        blockType = CodeBlockEnum.TC;
                    }
                    if (!error.IsWarning)
                    {
                        errors.Add(new ProgramError
                        {
                            Line         = errorRow,
                            Column       = error.Column,
                            ErrorMessage = error.ErrorText,
                            ErrorNumber  = error.ErrorNumber,
                            CodeBlock    = blockType
                        });
                    }
                    else
                    {
                        var warning = String.Format("{0},{1},{2}: {3}", blockType, errorRow, error.Column,
                                                    error.ErrorText);
                        HomeGenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.CompilerWarning,
                                                                         warning);
                    }
                }
            }
#endif

            if (errors.Count != 0)
            {
                return(errors);
            }

            // move/copy new assembly files
            // rename temp file to production file
#if !NETCOREAPP
            _scriptAssembly = result.CompiledAssembly;
#endif
            try
            {
                //string tmpfile = new Uri(value.CodeBase).LocalPath;
                File.Move(tempFile, this.AssemblyFile);
                if (File.Exists(tempFile + ".mdb"))
                {
                    File.Move(tempFile + ".mdb", this.AssemblyFile + ".mdb");
                }
                if (File.Exists(tempFile.Replace(".dll", ".mdb")))
                {
                    File.Move(tempFile.Replace(".dll", ".mdb"), this.AssemblyFile.Replace(".dll", ".mdb"));
                }
                if (File.Exists(tempFile + ".pdb"))
                {
                    File.Move(tempFile + ".pdb", this.AssemblyFile + ".pdb");
                }
                if (File.Exists(tempFile.Replace(".dll", ".pdb")))
                {
                    File.Move(tempFile.Replace(".dll", ".pdb"), this.AssemblyFile.Replace(".dll", ".pdb"));
                }
            }
            catch (Exception ee)
            {
                HomeGenieService.LogError(ee);
            }

            if (errors.Count == 0 && wasEnabled)
            {
                ProgramBlock.IsEnabled = true;
            }

            return(errors);
        }
Beispiel #3
0
        public List <ProgramError> Compile()
        {
            var errors = new List <ProgramError>();

            // check for output directory
            if (!Directory.Exists(Path.GetDirectoryName(AssemblyFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(AssemblyFile));
            }

            // dispose assembly and interrupt current task (if any)
            ProgramBlock.IsEnabled = false;

            // clean up old assembly files
            try
            {
                // If the file to be deleted does not exist, no exception is thrown.
                File.Delete(AssemblyFile);
                File.Delete(AssemblyFile + ".mdb");
                File.Delete(AssemblyFile.Replace(".dll", ".mdb"));
                File.Delete(AssemblyFile + ".pdb");
                File.Delete(AssemblyFile.Replace(".dll", ".pdb"));
            }
            catch (Exception ex)
            {
                HomeGenieService.LogError(ex);
            }

            // DO NOT CHANGE THE FOLLOWING LINES OF CODE
            // it is a lil' trick for mono compatibility
            // since it will be caching the assembly when using the same name
            // and use the old one instead of the new one
            var tmpFile = Path.Combine(FilePaths.ProgramsFolder, Guid.NewGuid() + ".dll");
            var result  = new CompilerResults(null);

            try
            {
                result = CSharpAppFactory.CompileScript(ProgramBlock.ScriptCondition, ProgramBlock.ScriptSource, tmpFile);
            }
            catch (Exception ex)
            {
                // report errors during post-compilation process
                result.Errors.Add(new CompilerError(ProgramBlock.Name, 0, 0, "-1", ex.Message));
            }

            if (result.Errors.Count > 0)
            {
                var sourceLines = ProgramBlock.ScriptSource.Split('\n').Length;
                foreach (CompilerError error in result.Errors)
                {
                    var errorRow  = (error.Line - CSharpAppFactory.ProgramCodeOffset);
                    var blockType = CodeBlockEnum.CR;
                    if (errorRow >= sourceLines + CSharpAppFactory.ConditionCodeOffset)
                    {
                        errorRow -= (sourceLines + CSharpAppFactory.ConditionCodeOffset);
                        blockType = CodeBlockEnum.TC;
                    }
                    if (!error.IsWarning)
                    {
                        errors.Add(new ProgramError {
                            Line         = errorRow,
                            Column       = error.Column,
                            ErrorMessage = error.ErrorText,
                            ErrorNumber  = error.ErrorNumber,
                            CodeBlock    = blockType
                        });
                    }
                    else
                    {
                        var warning = string.Format("{0},{1},{2}: {3}", blockType, errorRow, error.Column, error.ErrorText);
                        Homegenie.ProgramManager.RaiseProgramModuleEvent(ProgramBlock, Properties.CompilerWarning, warning);
                    }
                }
            }

            if (errors.Count != 0)
            {
                return(errors);
            }

            // move/copy new assembly files
            // rename temp file to production file
            _scriptAssembly = result.CompiledAssembly;
            try
            {
                //string tmpfile = new Uri(value.CodeBase).LocalPath;
                File.Move(tmpFile, AssemblyFile);
                if (File.Exists(tmpFile + ".mdb"))
                {
                    File.Move(tmpFile + ".mdb", AssemblyFile + ".mdb");
                }
                if (File.Exists(tmpFile.Replace(".dll", ".mdb")))
                {
                    File.Move(tmpFile.Replace(".dll", ".mdb"), AssemblyFile.Replace(".dll", ".mdb"));
                }
                if (File.Exists(tmpFile + ".pdb"))
                {
                    File.Move(tmpFile + ".pdb", AssemblyFile + ".pdb");
                }
                if (File.Exists(tmpFile.Replace(".dll", ".pdb")))
                {
                    File.Move(tmpFile.Replace(".dll", ".pdb"), AssemblyFile.Replace(".dll", ".pdb"));
                }
            }
            catch (Exception ee)
            {
                HomeGenieService.LogError(ee);
            }

            return(errors);
        }