示例#1
0
        private void OutputError(AddTypeCompilerError error, string[] actualSource)
        {
            // Get the actual line of the file if they
            // used the -FromPath parameter set
            if (String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase) ||
                String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase)
                )
            {
                if (!String.IsNullOrEmpty(error.FileName))
                {
                    actualSource = System.IO.File.ReadAllLines(error.FileName);
                }
            }

            string errorText = StringUtil.Format(AddTypeStrings.CompilationErrorFormat,
                                                 error.FileName, error.Line, error.ErrorText) + Environment.NewLine;

            for (int lineNumber = error.Line - 1; lineNumber < error.Line + 2; lineNumber++)
            {
                if (lineNumber > 0)
                {
                    if (lineNumber > actualSource.Length)
                    {
                        break;
                    }

                    string lineText = "";

                    if (lineNumber == error.Line)
                    {
                        lineText += ">>> ";
                    }

                    lineText += actualSource[lineNumber - 1];

                    errorText += Environment.NewLine + StringUtil.Format(AddTypeStrings.CompilationErrorFormat,
                                                                         error.FileName, lineNumber, lineText) + Environment.NewLine;
                }
            }

            if (error.IsWarning)
            {
                WriteWarning(errorText);
            }
            else
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    new Exception(errorText),
                    "SOURCE_CODE_ERROR",
                    ErrorCategory.InvalidData,
                    error);

                WriteError(errorRecord);
            }
        }
示例#2
0
文件: AddType.cs 项目: 40a/PowerShell
        private void OutputError(AddTypeCompilerError error, string[] actualSource)
        {
            // Get the actual line of the file if they
            // used the -FromPath parameter set
            if (String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase) ||
                String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase)
                )
            {
                if (!String.IsNullOrEmpty(error.FileName))
                {
                    actualSource = System.IO.File.ReadAllText(error.FileName).Split(Utils.Separators.Newline);
                }
            }

            string errorText = StringUtil.Format(AddTypeStrings.CompilationErrorFormat,
                        error.FileName, error.Line, error.ErrorText) + "\n";

            for (int lineNumber = error.Line - 1; lineNumber < error.Line + 2; lineNumber++)
            {
                if (lineNumber > 0)
                {
                    if (lineNumber > actualSource.Length)
                        break;

                    string lineText = "";

                    if (lineNumber == error.Line)
                    {
                        lineText += ">>> ";
                    }

                    lineText += actualSource[lineNumber - 1];

                    errorText += "\n" + StringUtil.Format(AddTypeStrings.CompilationErrorFormat,
                        error.FileName, lineNumber, lineText) + "\n";
                }
            }

            if (error.IsWarning)
            {
                WriteWarning(errorText);
            }
            else
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    new Exception(errorText),
                        "SOURCE_CODE_ERROR",
                        ErrorCategory.InvalidData,
                        error);

                WriteError(errorRecord);
            }
        }
示例#3
0
文件: AddType.cs 项目: 40a/PowerShell
        internal void HandleCompilerErrors(AddTypeCompilerError[] compilerErrors)
        {
            // Get the source code that corresponds to their type in the case of errors
            string[] actualSource = Utils.EmptyArray<string>();

            // Get the source code that corresponds to the
            // error if we generated it
            if ((compilerErrors.Length > 0) &&
                (!String.Equals(ParameterSetName, "FromPath", StringComparison.OrdinalIgnoreCase)) &&
                (!String.Equals(ParameterSetName, "FromLiteralPath", StringComparison.OrdinalIgnoreCase))
                )
            {
                actualSource = sourceCode.Split(Utils.Separators.Newline);
            }

            // Write any errors to the pipeline
            foreach (var error in compilerErrors)
            {
                OutputError(error, actualSource);
            }

            if (compilerErrors.Any(e => !e.IsWarning))
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(AddTypeStrings.CompilerErrors),
                        "COMPILER_ERRORS",
                        ErrorCategory.InvalidData,
                        null);
                ThrowTerminatingError(errorRecord);
            }
        }