/// <summary> /// Serializes the specified <see cref="SnippetFile"/> to the file. /// </summary> /// <param name="snippetFile">A snippet file to be serialized.</param> /// <exception cref="ArgumentNullException"><paramref name="snippetFile"/> is <c>null</c>.</exception> public static void Serialize(SnippetFile snippetFile) { if (snippetFile == null) { throw new ArgumentNullException(nameof(snippetFile)); } Serialize(snippetFile.FullName, snippetFile.Snippets, new SaveSettings()); }
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(); }
/// <summary> /// Returns enumerable collection of <see cref="Snippet"/> deserialized from a specified snippet file. /// </summary> /// <param name="filePath">The absolute or relative path to the file.</param> /// <returns>An enumerable collection <see cref="Snippet"/> being deserialized.</returns> public static SnippetFile DeserializeFile(string filePath) { using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var file = new SnippetFile(filePath); int index = 0; Debug.WriteLine($"deserializing snippet file '{filePath}'"); foreach (Snippet snippet in Deserialize(stream)) { snippet.FilePath = filePath; snippet.Index = index; file.Snippets.Add(snippet); index++; } return(file); } }