Exemplo n.º 1
0
        private void DoEditScript()
        {
            YamlEditorController.ExpandCodeBlock();
            _selectedBlock = YamlHelper.ExtractBlock(YamlEditorController.SelectedText);
            if (_selectedBlock == null)
            {
                return;
            }

            BlockFormat = "(unchanged)";
            YamlEditorController.SetEnabled("YAML Editor is open", false);
            YamlEditorHeight          = new GridLength(50);
            CodeEditorHeight          = new GridLength(100, GridUnitType.Star);
            CodeEditorController.Text = _selectedBlock.Content;
            CodeEditorController.SetEnabled("Code Editor is open", true);
            Dispatcher.BeginInvoke(DispatcherPriority.Background, () => YamlEditorController.Data.ScrollToLine(YamlEditorController.Data.TextArea.Caret.Line));
        }
    static void WriteYaml(string path, YamlBlock firstBlock, List <YamlBlock> blocks)
    {
        using (
            FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write,
                                      FileShare.ReadWrite))
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                foreach (String s in firstBlock.contents)
                {
                    sw.WriteLine(s);
                }
                foreach (YamlBlock item in blocks)
                {
                    foreach (String s in item.contents)
                    {
                        sw.WriteLine(s);
                    }
                }
            }
        }

        Debug.Log(string.Format("{0} Created: {1}", DateTime.Now, path));
    }
    static bool SortYaml(string filePath, out YamlBlock firstBlock, out List <YamlBlock> blocks)
    {
        firstBlock = new YamlBlock();
        blocks     = new List <YamlBlock>();

        if (!File.Exists(filePath))
        {
            return(false);
        }

        YamlBlock block = firstBlock;

        using (
            FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read,
                                      FileShare.ReadWrite))
        {
            using (StreamReader sr = new StreamReader(fs))
            {
                string line = string.Empty;
                do
                {
                    line = sr.ReadLine();
                    if (null == line)
                    {
                        break;
                    }
                    if (line.Trim().StartsWith("---")) // new section
                    {
                        block = new YamlBlock();
                        block.contents.Add(line);
                        blocks.Add(block);
                    }
                    else
                    {
                        block.contents.Add(line);
                    }
                    //Debug.Log(line);
                } while (true);
            }
            blocks.Sort();
        }

        Debug.Log(string.Format("Detected {0} Yaml Blocks", blocks.Count));

        // sort fileID
        string tokenFileID = @"- {fileID: ";

        foreach (YamlBlock item in blocks)
        {
            List <string> newContent   = new List <string>();
            List <string> files        = new List <string>();
            bool          skipFileSort = true;
            foreach (String s in item.contents)
            {
                if (s.Trim().ToLower().Equals("m_Children:"))
                {
                    skipFileSort = false; //temporarily enable sorting
                }
                else if (s.Trim().StartsWith(tokenFileID))
                {
                    if (!skipFileSort)
                    {
                        files.Add(s);
                        continue;
                    }
                }
                else
                {
                    skipFileSort = true; //back to skipping file sorting

                    if (files.Count > 0)
                    {
                        files.Sort();
                        foreach (string file in files)
                        {
                            newContent.Add(file);
                        }
                        files.Clear();
                    }
                }

                newContent.Add(s);
            }
            if (files.Count > 0)
            {
                files.Sort();
                foreach (string file in files)
                {
                    newContent.Add(file);
                }
                files.Clear();
            }
            item.contents = newContent;
        }

        return(true);
    }