Пример #1
0
        public PageSectionInfo SectionRemove(PageSectionInfo section)
        {
            var path = Path.Combine(viewsFilepath, section.ControllerID, section.PageID + ".cshtml");
            using (var stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (var reader = new StreamReader(stream))
                {
                    var text = reader.ReadToEnd();
                    var sectionText = GetSectionText(section.ID, text);

                    if (!string.IsNullOrEmpty(sectionText))
                    {
                        text = text
                            .Replace(sectionText + Environment.NewLine, "");

                        stream.Seek(0, SeekOrigin.Begin);
                        stream.SetLength(0);
                        using (var writer = new StreamWriter(stream))
                        {
                            writer.Write(text);
                            writer.Flush();
                        }
                    }
                }
            }
            return section;
        }
Пример #2
0
        public PageSectionInfo SectionAdd(PageSectionInfo section)
        {
            // resolve physical file and text contents
            var path = Path.Combine(viewsFilepath, section.ControllerID, section.PageID + ".cshtml");
            var text = GetTextFromFile(path);

            // generate new section text
            // TODO: this needs to include default modules
            var sectionText = "@section " + section.ID + " {" + Environment.NewLine + "}" + Environment.NewLine;

            // parse existing sections
            var matches = PageSectionRegex.Matches(text).OfType<Match>();

            var sb = new StringBuilder(text);
            // if no sections parsed, insert at head of document
            var insertAt = 0;
            if (matches.Count() == 0)
            {
                // if head of document contains @{...} then insert after closure
                // (convention-based decision, non-conventional code will cause this to misbehave)
                if (text.Contains("@{" + Environment.NewLine))
                {
                    insertAt = text.IndexOf(Environment.NewLine + "}" + Environment.NewLine) + 1 + (Environment.NewLine.Length * 2);
                }
            }
            else
            {
                // otherwise, rewrite lastmost match to contain lastmost match and also new section text
                var last = matches.Last();
                insertAt = last.Index + last.Length + Environment.NewLine.Length;
            }
            sb.Insert(insertAt, sectionText);

            // rewrite file
            WriteTextToPath(sb, path);

            // add missing modules
            if (section.Modules != null)
            {
                foreach (var module in section.Modules)
                {
                    ModuleAdd(module);
                }
            }

            return section;
        }