Exemple #1
0
        public static void CopySnippetsToVisualStudioProject(string projectDirPath, IEnumerable <SnippetDirectory> snippetDirectories)
        {
            string projectName = Path.GetFileName(projectDirPath);

            string csprojPath = Path.Combine(projectDirPath, $"{projectName}.{ProjectDocument.CSharpProjectExtension}");

            var document = new ProjectDocument(csprojPath);

            document.RemoveSnippetFiles();

            XElement newItemGroup = document.AddItemGroup();

            foreach (SnippetDirectory snippetDirectory in snippetDirectories)
            {
                string directoryPath = Path.Combine(projectDirPath, snippetDirectory.DirectoryName);

                Directory.CreateDirectory(directoryPath);

                Snippet[] snippets = snippetDirectory.EnumerateSnippets().ToArray();

                foreach (IGrouping <string, Snippet> grouping in snippets
                         .GroupBy(f => Path.GetFileNameWithoutExtension(f.FilePath))
                         .Where(f => f.Count() > 1))
                {
                    throw new Exception($"multiple files with same name '{grouping.Key}'");
                }

                IOUtility.SaveSnippets(snippets, directoryPath);

                document.AddSnippetFiles(snippets.Select(f => f.FilePath), newItemGroup);
            }

            document.Save();
        }
Exemple #2
0
        public static void CopySnippetsToVisualStudioProject(string projectDirPath, IEnumerable <SnippetDirectory> snippetDirectories)
        {
            string projectName = Path.GetFileName(projectDirPath);

            string csprojPath = Path.Combine(projectDirPath, $"{projectName}.{ProjectDocument.CSharpProjectExtension}");

            var document = new ProjectDocument(csprojPath);

            document.RemoveSnippetFiles();

#if !DEBUG
            var allSnippets = new List <Snippet>();
#endif

            XElement newItemGroup = document.AddItemGroup();

            foreach (SnippetDirectory snippetDirectory in snippetDirectories)
            {
                string directoryPath = Path.Combine(projectDirPath, snippetDirectory.DirectoryName);

                Directory.CreateDirectory(directoryPath);

                Snippet[] snippets = snippetDirectory.EnumerateSnippets().ToArray();

                foreach (IGrouping <string, Snippet> grouping in snippets
                         .GroupBy(f => Path.GetFileNameWithoutExtension(f.FilePath))
                         .Where(f => f.Count() > 1))
                {
                    throw new Exception($"multiple files with same name '{grouping.Key}'");
                }

                IOUtility.SaveSnippets(snippets, directoryPath);

                document.AddSnippetFiles(snippets.Select(f => f.FilePath), newItemGroup);

#if !DEBUG
                allSnippets.AddRange(snippets);
#endif
            }

            document.Save();

#if !DEBUG
            foreach (Snippet snippet in allSnippets)
            {
                string submenuShortcut = snippet.GetSubmenuShortcut();

                snippet.RemoveShortcutFromTitle();

                snippet.RemoveMetaKeywords();
                snippet.Keywords.Add($"{KnownTags.MetaTagPrefix}Name:{snippet.FileNameWithoutExtension()}");

                if (!string.IsNullOrEmpty(submenuShortcut))
                {
                    snippet.Keywords.Add($"{KnownTags.MetaTagPrefix}SubmenuShortcut:{submenuShortcut}");
                }
            }

            IOUtility.SaveSnippetsToSingleFile(
                allSnippets
                .Where(f => !f.HasTag(KnownTags.ExcludeFromReadme))
                .OrderBy(f => f.Language.ToString())
                .ThenBy(f => f.FileNameWithoutExtension()),
                Path.Combine(projectDirPath, "snippets.xml"));
#endif
        }