Exemple #1
0
        static void outputJSONDiagnostics(IEnumerable <Diagnostic> diagnostics, string mainCode, ReCTAttachment[] attachments)
        {
            List <ReCTError> errors = new List <ReCTError>();

            foreach (var diagnostic in diagnostics.Where(d => d.Location.Text == null))
            {
                ReCTError newError = new ReCTError()
                {
                    ErrorMessage = diagnostic.Message
                };
                errors.Add(newError);
            }

            foreach (var diagnostic in diagnostics.Where(d => d.Location.Text != null)
                     .OrderBy(d => d.Location.FileName)
                     .ThenBy(d => d.Location.Span.Start)
                     .ThenBy(d => d.Location.Span.Length))
            {
                var text           = diagnostic.Location.Text;
                var startLine      = diagnostic.Location.StartLine + 1;
                var startCharacter = diagnostic.Location.StartCharacter + 1;
                var endLine        = diagnostic.Location.EndLine + 1;
                var endCharacter   = diagnostic.Location.EndCharacter + 1;

                var lineDifference = endLine - startLine;

                var span      = diagnostic.Location.Span;
                var lineIndex = text.GetLineIndex(span.Start);
                var line      = text.Lines[lineIndex];


                var            attachment       = "";
                ReCTAttachment attachmentObject = null;

                foreach (var att in attachments)
                {
                    if (span.Start >= att.startingIndex && span.End < att.startingIndex + att.Length)
                    {
                        attachment       = att.Name;
                        startLine        = att.Code.Substring(0, span.Start - att.startingIndex).Split('\n').Length;
                        endLine          = startLine + lineDifference;
                        attachmentObject = att;
                        break;
                    }
                }

                // if we arent inside of an attachment -> adjust line count
                if (attachment == "")
                {
                    int goBackBy = 0;
                    int attLines = 0;
                    foreach (var att in attachments)
                    {
                        if (att.startingIndex < span.Start)
                        {
                            goBackBy += att.Lines;
                            attLines++;
                        }
                    }

                    startLine = startLine - goBackBy + attLines;
                    endLine   = startLine + lineDifference;
                }

                // figure out start and end index
                String code = mainCode;
                if (attachmentObject != null)
                {
                    code = attachmentObject.Code;
                }

                int startingIndex = -1;
                int endingIndex   = -1;

                int lineNum = 1;
                int charNum = 0;

                //Console.WriteLine("-[ " + diagnostic.Message + " ]--------");

                for (int i = 0; i < code.Length; i++)
                {
                    charNum++;

                    if (code[i] == '\n')
                    {
                        // in case the end is in some unreachable location but still this line
                        if (lineNum == endLine)
                        {
                            endingIndex = i - 1;
                        }

                        lineNum++;
                        charNum = 1;
                    }

                    if (lineNum == startLine && charNum == startCharacter)
                    {
                        startingIndex = i;
                    }

                    if (lineNum == endLine && charNum == endCharacter)
                    {
                        endingIndex = i;
                    }

                    //Console.WriteLine("LINE: " + lineNum + "; ENDLINE: " + endLine + "; ENDCHAR: " + endCharacter + "; CHAR: " + i);
                }

                if (startingIndex != -1 && endingIndex == -1)
                {
                    endingIndex = startingIndex + 1;
                }

                ReCTError newError = new ReCTError()
                {
                    ErrorMessage = diagnostic.Message,
                    Attachment   = attachment,

                    startLine = startLine,
                    startChar = startCharacter,

                    endLine = endLine,
                    endChar = endCharacter,

                    startIndex = startingIndex,
                    endIndex   = endingIndex
                };
                errors.Add(newError);
            }

            ReCTErrorData errorData = new ReCTErrorData();

            errorData.Errors = errors.ToArray();

            // jsonizing
            Console.WriteLine("\nERRDATA");
            Console.WriteLine(JsonSerializer.Serialize(errorData));
        }
Exemple #2
0
        static List <ReCTAttachment> evaluateFlags(ref string code, ref List <string> filesToCopy, ref List <string> foldersToCopy, string inPath)
        {
            Console.WriteLine("Evaluating Flags...");

            List <ReCTAttachment> attachments = new List <ReCTAttachment>();

            var lookingforfile = "";

            try
            {
                while (code.Contains("#attach"))
                {
                    string neededFile = "";
                    var    matches    = Regex.Matches(code, @"(?<=#attach\(\" + "\"" + @")(.*)(?=\" + "\"" + @"\))");

                    neededFile     = matches[0].Value;
                    lookingforfile = neededFile;

                    // new attachment object
                    ReCTAttachment attachment = new ReCTAttachment();

                    // set name of attachment
                    attachment.Name = Path.GetFileName(lookingforfile);

                    // figure out starting character
                    attachment.startingIndex = matches[0].Index - 9;


                    if (!Path.IsPathRooted(lookingforfile))
                    {
                        lookingforfile = Path.GetDirectoryName(inPath) + "/" + neededFile;
                    }

                    var codeFromFile = File.ReadAllText(lookingforfile);

                    // set length of attatchment
                    attachment.Code   = codeFromFile;
                    attachment.Length = codeFromFile.Length;
                    attachment.Lines  = codeFromFile.Split("\n").Length;

                    code = code.Replace($"#attach(\"{neededFile}\")", codeFromFile);

                    // add attachment to the pile
                    attachments.Add(attachment);
                }
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"[L: ?, C: ?] Could not find attachment file '{lookingforfile}'!");
                Console.ForegroundColor = ConsoleColor.White;
                return(attachments);
            }

            Console.ForegroundColor = ConsoleColor.Yellow;

            if (code.Contains("#closeConsole"))
            {
                Console.WriteLine("Flag '#closeConsole' is not supported in Standalone mode, will be ignored.");
            }

            if (code.Contains("#noConsole"))
            {
                Console.WriteLine("Flag '#noConsole' is not supported in Standalone mode, will be ignored.");
            }

            Console.ForegroundColor = ConsoleColor.White;

            while (code.Contains("#copyFolder"))
            {
                var matches = Regex.Matches(code, @"(?<=#copyFolder\(\" + "\"" + @")(.*)(?=\" + "\"" + @"\))");

                for (int i = 0; i < matches.Count; i++)
                {
                    foldersToCopy.Add(matches[i].Value);
                    code = code.Replace($"#copyFolder(\"{matches[i].Value}\")", "");
                }
            }

            while (code.Contains("#copy"))
            {
                var matches = Regex.Matches(code, @"(?<=#copy\(\" + "\"" + @")(.*)(?=\" + "\"" + @"\))");

                for (int i = 0; i < matches.Count; i++)
                {
                    filesToCopy.Add(matches[i].Value);
                    code = code.Replace($"#copy(\"{matches[i].Value}\")", "");
                }
            }

            return(attachments);
        }