Ejemplo n.º 1
0
        public static void SaveSnippet(Snippet snippet, string filePath, bool onlyIfChanged = true)
        {
            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            SaveSettings settings = CreateSaveSettings();

            if (!onlyIfChanged ||
                !File.Exists(filePath) ||
                !string.Equals(
                    File.ReadAllText(filePath, Encoding.UTF8),
                    SnippetSerializer.CreateXml(snippet, settings),
                    StringComparison.Ordinal))
            {
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    Console.WriteLine($"saving {filePath}");
                    SnippetSerializer.Serialize(fileStream, snippet, settings);
                }

                Console.WriteLine();
            }
        }
Ejemplo n.º 2
0
        public static void SaveSnippet(Snippet snippet, string filePath, bool onlyIfChanged = true)
        {
            if (snippet == null)
            {
                throw new ArgumentNullException(nameof(snippet));
            }

            var settings = new SaveSettings()
            {
                OmitXmlDeclaration      = true,
                OmitCodeSnippetsElement = true,
                IndentChars             = "  ",
                Comment = "Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0."
            };

            if (!onlyIfChanged ||
                !File.Exists(filePath) ||
                !string.Equals(
                    File.ReadAllText(filePath, Encoding.UTF8),
                    SnippetSerializer.CreateXml(snippet, settings),
                    StringComparison.Ordinal))
            {
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    Console.WriteLine($"saving {filePath}");
                    SnippetSerializer.Serialize(fileStream, snippet, settings);
                }

                Console.WriteLine();
            }
        }
Ejemplo n.º 3
0
        public static void SaveSnippetsToSingleFile(IEnumerable <Snippet> snippets, string filePath)
        {
            if (snippets == null)
            {
                throw new ArgumentNullException(nameof(snippets));
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                Console.WriteLine($"saving {filePath}");
                SnippetSerializer.Serialize(fileStream, snippets, CreateSaveSettings());
            }

            Console.WriteLine();
        }
Ejemplo n.º 4
0
        private static void Main(string[] args)
        {
#if DEBUG
            string dirPath = @"..\..\..\..\SnippetEssentials\SnippetEssentials.CSharp";
#else
            string dirPath = @"..\SnippetEssentials\SnippetEssentials.CSharp";
#endif
            if (!Directory.Exists(dirPath))
            {
                Console.WriteLine($"Directory not found: {dirPath}");
                Console.ReadKey();
                return;
            }

            var snippets = new List <Snippet>(LoadSnippets(dirPath).OrderBy(f => f.FilePath));

            var snippetFile = new SnippetFile(Path.Combine(dirPath, "snippets.xml"));

            if (File.Exists(snippetFile.FullName))
            {
                Console.WriteLine($"File already exists: {snippetFile.FullName}");
                Console.ReadKey();
                return;
            }

            foreach (Snippet snippet in snippets)
            {
                string category = Path.GetDirectoryName(snippet.FilePath)
                                  .Replace(dirPath, string.Empty)
                                  .TrimStart(Path.DirectorySeparatorChar)
                                  .Replace(Path.DirectorySeparatorChar, '.');

                snippet.Keywords.Add("Category:" + category);
                snippet.Keywords.Add("FullyQualifiedName:" + category + "." + Path.GetFileNameWithoutExtension(snippet.FilePath));

                snippetFile.Snippets.Add(snippet);

                string relativePath = snippet.FilePath.Replace(dirPath, string.Empty);
                Console.WriteLine(relativePath);
            }

            var saveSettings = new SaveSettings();

            SnippetSerializer.Serialize(snippetFile, saveSettings);

            Console.WriteLine("FINISHED");
            Console.ReadKey();
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Serializes the current instance to the specified <see cref="Stream"/>, optionally specifying serialization process.
 /// </summary>
 /// <param name="stream">The stream to output this <see cref="Snippet"/> to.</param>
 /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
 public void Save(Stream stream, SaveSettings settings)
 {
     SnippetSerializer.Serialize(stream, this, settings);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Serializes the current instance to the specified file.
 /// </summary>
 /// <param name="filePath">The absolute or relative path to the file.</param>
 /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param>
 public void Save(string filePath, SaveSettings settings)
 {
     SnippetSerializer.Serialize(filePath, this, settings);
 }