Esempio n. 1
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();
            }
        }
Esempio n. 2
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();
            }
        }
 private static IEnumerable <Snippet> DeserializeSnippets(IEnumerable <string> dirPaths)
 {
     foreach (string dirPath in dirPaths)
     {
         foreach (Snippet snippet in SnippetSerializer.Deserialize(dirPath, SearchOption.AllDirectories))
         {
             yield return(snippet);
         }
     }
 }
Esempio n. 4
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();
        }
Esempio n. 5
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();
        }
Esempio n. 6
0
        private static IEnumerable <Snippet> LoadSnippets(string dirPath)
        {
            foreach (string dirPath2 in Directory.EnumerateDirectories(dirPath, "*", SearchOption.TopDirectoryOnly))
            {
                if (Path.GetFileName(dirPath2).StartsWith("_"))
                {
                    continue;
                }

                foreach (SnippetFile snippetFile in SnippetSerializer.DeserializeFiles(dirPath2, SearchOption.AllDirectories))
                {
                    foreach (Snippet snippet in snippetFile.Snippets)
                    {
                        yield return(snippet);
                    }
                }
            }
        }
Esempio n. 7
0
 public IEnumerable <Snippet> EnumerateSnippets()
 {
     return(SnippetSerializer.Deserialize(Path, SearchOption.AllDirectories));
 }
Esempio n. 8
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);
 }
Esempio n. 9
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);
 }
Esempio n. 10
0
        static void Main(string[] args)
        {
            var dirPaths = new List <string>();

            if (args?.Length > 0)
            {
                dirPaths.AddRange(args);
            }
            else
            {
                dirPaths.Add(Environment.CurrentDirectory);
            }

            Console.WriteLine();
            Console.WriteLine("directories:");

            for (int i = 0; i < dirPaths.Count; i++)
            {
                Console.WriteLine($"  {dirPaths[i]}");

                if (!Assert(Directory.Exists(dirPaths[i]), "directory not found"))
                {
                    return;
                }
            }

            var snippets = new List <Snippet>(SnippetSerializer.DeserializeFiles(dirPaths[0], SearchOption.AllDirectories).SelectMany(f => f.Snippets));

            Console.WriteLine();
            Console.WriteLine($"number of snippets: {snippets.Count}");

            foreach (SnippetValidationResult result in SnippetChecker.Validate(snippets))
            {
                Console.WriteLine();
                Console.WriteLine($"{result.Importance}: \"{result.Description}\" in \"{result.Snippet.FilePath}\"");
            }

            foreach (IGrouping <string, Snippet> snippet in snippets
                     .Where(f => f.Keywords.Contains(NonUniqueShortcutKeyword))
                     .GroupBy(f => f.Shortcut)
                     .Where(f => f.Count() == 1))
            {
                Console.WriteLine();
                Console.WriteLine($"unused {NonUniqueShortcutKeyword} in \"{snippet.First().FilePath}\"");
            }

            foreach (ShortcutInfo shortcutInfo in SnippetChecker.FindDuplicateShortcuts(dirPaths, NonUniqueShortcutKeyword))
            {
                Console.WriteLine();
                Console.WriteLine($"shortcut duplicate: {shortcutInfo.Shortcut}");

                foreach (Snippet item in shortcutInfo.Snippets)
                {
                    Console.WriteLine($"  {item.FilePath}");
                }
            }
            ;

            SaveSnippets(snippets);

            Assert(false);
        }