Example #1
0
        public static void SaveSnippetsToSingleFile(
            IEnumerable <Snippet> snippets,
            string filePath,
            bool onlyIfChanged = true)
        {
            if (snippets == null)
            {
                throw new ArgumentNullException(nameof(snippets));
            }

            SaveSettings settings = CreateSaveSettings();

            string content = SnippetSerializer.CreateXml(snippets, settings);

            if (!ShouldSave(filePath, content, Encoding.UTF8, onlyIfChanged))
            {
                return;
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                Console.WriteLine($"saving file {filePath}");
                SnippetSerializer.Serialize(fileStream, snippets, settings);
            }
        }
Example #2
0
        private static IEnumerable <Snippet> EnumerateSnippets(string directoryPath)
        {
            foreach (string path in Directory.EnumerateDirectories(directoryPath, "*", SearchOption.TopDirectoryOnly))
            {
                string name = Path.GetFileName(path);

                if (name == KnownNames.Dev)
                {
                    continue;
                }

                if (name == KnownNames.AutoGeneration)
                {
                    continue;
                }

                foreach (Snippet snippet in SnippetSerializer.Deserialize(path, SearchOption.AllDirectories))
                {
                    yield return(snippet);
                }
            }

            foreach (string filePath in Directory.EnumerateFiles(directoryPath, SnippetFileSearcher.Pattern, SearchOption.TopDirectoryOnly))
            {
                foreach (Snippet snippet in SnippetSerializer.DeserializeFile(filePath).Snippets)
                {
                    yield return(snippet);
                }
            }
        }
        public void GenerateSnippets(string sourceDirectoryPath, string destinationDirectoryPath)
        {
            Snippet[] snippets = SnippetSerializer.Deserialize(sourceDirectoryPath, SearchOption.AllDirectories)
                                 .SelectMany(snippet => GenerateSnippets(snippet))
                                 .ToArray();

            IOUtility.SaveSnippets(snippets, destinationDirectoryPath);
        }
Example #4
0
 public static IEnumerable <Snippet> GenerateSnippets(string sourceDirectoryPath)
 {
     foreach (Snippet snippet in GenerateSnippets(SnippetSerializer.Deserialize(sourceDirectoryPath, SearchOption.AllDirectories)))
     {
         snippet.AddTag(KnownTags.AutoGenerated);
         snippet.SortCollections();
         yield return(snippet);
     }
 }
Example #5
0
        private static bool ShouldSave(Snippet snippet, string filePath, SaveSettings settings, bool onlyIfChanged)
        {
            if (!onlyIfChanged)
            {
                return(true);
            }

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

            string s1 = File.ReadAllText(filePath, Encoding.UTF8);
            string s2 = SnippetSerializer.CreateXml(snippet, settings);

            return(!string.Equals(s1, s2, StringComparison.Ordinal));
        }
Example #6
0
        public static void SaveSnippet(Snippet snippet, string filePath, bool onlyIfChanged = true)
        {
            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            SaveSettings settings = CreateSaveSettings();

            if (!ShouldSave(snippet, filePath, settings, onlyIfChanged))
            {
                return;
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                Console.WriteLine($"saving file {filePath}");
                SnippetSerializer.Serialize(fileStream, snippet, settings);
            }
        }
Example #7
0
        public void Snippet_Deserialize()
        {
            const string filePath = @"..\..\..\Snippet.snippet";

            Snippet snippet = null;

            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                snippet = SnippetSerializer.Deserialize(stream).FirstOrDefault();

            var settings = new SaveSettings()
            {
                IndentChars             = "  ",
                OmitCodeSnippetsElement = true,
                OmitXmlDeclaration      = true,
                Comment = " comment "
            };

            string oldValue = File.ReadAllText(filePath, Encoding.UTF8);

            string newValue = SnippetSerializer.CreateXml(snippet, settings);

            Assert.Equal(oldValue, newValue);
        }
Example #8
0
 public IEnumerable <Snippet> EnumerateSnippets(SearchOption searchOption = SearchOption.AllDirectories)
 {
     return(SnippetSerializer.Deserialize(Path, searchOption));
 }
Example #9
0
 public IEnumerable <Snippet> GenerateSnippets(string sourceDirectoryPath, SearchOption searchOption = SearchOption.AllDirectories)
 {
     return(SnippetSerializer.Deserialize(sourceDirectoryPath, searchOption)
            .SelectMany(snippet => GenerateSnippets(snippet)));
 }