예제 #1
0
        public void ConvertCSharpFolderToVsCodeSnippetJsonFile()
        {
            var visualStudioSnippetFolder =
                @"C:\Users\rstrahl\Documents\Visual Studio 2017\Code Snippets\Visual C#\My Code Snippets";
            var vsCodeSnippetFile =
                @"C:\Users\rstrahl\AppData\Roaming\Code\User\snippets\ww-csharp.code-snippets";

            var snippets = VisualStudioSnippet.ParseSnippetFolder(visualStudioSnippetFolder);

            var jsonRoot = new JObject();

            VsCodeSnippet.ToFile(snippets, vsCodeSnippetFile, true, "ww-");

            Console.WriteLine(File.ReadAllText(vsCodeSnippetFile));

            Assert.IsNotNull(snippets);
            Assert.IsTrue(snippets.Count > 0);
        }
예제 #2
0
        public void ConvertHtmlFolderToVsCodeSnippetJsonFile()
        {
            var visualStudioSnippetFolder =
                @"C:\Users\rstrahl\Documents\Visual Studio 2017\Code Snippets\Visual Web Developer\My HTML Snippets\WebConnection";
            var vsCodeSnippetFile =
                @"C:\Users\rstrahl\AppData\Roaming\Code\User\snippets\wc-WebConnection-VsCode.code-snippets";

            var snippets = VisualStudioSnippet.ParseSnippetFolder(visualStudioSnippetFolder);

            var jsonRoot = new JObject();

            VsCodeSnippet.ToFile(snippets, vsCodeSnippetFile, clearSnippetFile: true);

            Console.WriteLine(File.ReadAllText(vsCodeSnippetFile));

            Assert.IsNotNull(snippets);
            Assert.IsTrue(snippets.Count > 0);
        }
예제 #3
0
        public bool Process()
        {
            var consoleColor = Console.ForegroundColor;

            var version = Assembly.GetEntryAssembly().GetName().Version;
            var ver     = version.Major + "." + version.Minor +
                          (version.Build > 0 ? "." + version.Build : string.Empty);

            var header = $"Visual Studio Snippet Converter v{ver}";

            WriteConsole(header, MessageModes.Information);
            WriteConsole(Utils.Replicate("-", header.Length), MessageModes.Information);

            WriteConsole($"(c) Rick Strahl, West Wind Technologies{Environment.NewLine}", MessageModes.Information);

            Console.WriteLine($"Processing {Parser.SourceFileOrDirectory}...");

            if (Parser.Mode == "vs-vscode")
            {
                if (string.IsNullOrEmpty(Parser.TargetFile))
                {
                    WriteConsole("Error: Please specify an output VS Code snippet file with the `-o` option.",
                                 MessageModes.Error);
                    return(false);
                }

                try
                {
                    List <VisualStudioSnippet> snippets;
                    if (Parser.DirectoryMode)
                    {
                        snippets = VisualStudioSnippet.ParseSnippetFolder(Parser.SourceFileOrDirectory, Parser.Recurse);
                        if (snippets == null || snippets.Count < 1)
                        {
                            WriteConsole("Error: No snippets found in path: " + Parser.SourceFileOrDirectory,
                                         MessageModes.Error);
                            return(false);
                        }
                    }
                    else
                    {
                        var snippet = VisualStudioSnippet.ParseFromFile(Parser.SourceFileOrDirectory);
                        if (snippet == null)
                        {
                            WriteConsole("Error: No snippets found in path: " + Parser.SourceFileOrDirectory, MessageModes.Error);
                            return(false);
                        }
                        snippets = new List <VisualStudioSnippet>()
                        {
                            snippet
                        };
                    }

                    VsCodeSnippet.ToFile(snippets, Parser.TargetFile, false, Parser.SnippetPrefix);

                    WriteConsole($"Processed: {snippets.Count} snippet(s)", MessageModes.Success);
                    WriteConsole($"To File:   {Parser.TargetFile}", MessageModes.Success);
                }
                catch (Exception ex)
                {
                    WriteConsole("Error: Snippet conversion failed for: " + Parser.SourceFileOrDirectory
                                 + "\n" + ex.Message, MessageModes.Error);
                    Console.ForegroundColor = consoleColor;
                    return(false);
                }

                if (Parser.ShowFileInExplorer)
                {
                    Utils.OpenFileInExplorer(Parser.TargetFile);
                }
            }
            else if (Parser.Mode == "vs-rider")
            {
                if (Parser.DirectoryMode)
                {
                    var snippets = VisualStudioSnippet.ParseSnippetFolder(Parser.SourceFileOrDirectory, Parser.Recurse);
                    if (snippets == null || snippets.Count < 1)
                    {
                        var msg = $"Error: No snippets found in path: {Parser.SourceFileOrDirectory}.";

                        if (!Parser.Recurse)
                        {
                            msg += " You can try using the `-r` flag to recurse folder.";
                        }

                        WriteConsole(msg, MessageModes.Error);
                        return(false);
                    }

                    try
                    {
                        JetBrainsLiveTemplate.AddVisualStudioSnippets(snippets, Parser.SnippetPrefix);

                        // Target file is usually empty so get the global config file
                        if (string.IsNullOrEmpty(Parser.TargetFile))
                        {
                            Parser.TargetFile = JetBrainsLiveTemplate.GetRiderConfigurationFile();
                        }

                        WriteConsole($"Processed: {snippets.Count} snippet(s)", MessageModes.Success);
                        WriteConsole($"To File:   {Parser.TargetFile}", MessageModes.Success);
                    }
                    catch (Exception ex)
                    {
                        WriteConsole(
                            "Error: Snippet conversion failed for: " + Parser.SourceFileOrDirectory + "\n" + ex.Message,
                            MessageModes.Error);
                        Console.ForegroundColor = consoleColor;
                        return(false);
                    }
                }
            }

            if (Parser.ShowFileInExplorer)
            {
                Utils.OpenFileInExplorer(Parser.TargetFile);
            }

            Console.ForegroundColor = consoleColor;

            return(true);
        }