Beispiel #1
0
        public void ConvertFolderToVsCodeSnippetJsonList()
        {
            var snippets = VisualStudioSnippet.ParseSnippetFolder(
                "C:\\Users\\rstrahl\\Documents\\Visual Studio 2017\\Code Snippets\\Visual Web Developer\\My HTML Snippets\\WebConnection");

            var jsonRoot = new JObject();

            VsCodeSnippet.FromVisualStudioCodeSnippetList(snippets, jsonRoot);

            Console.WriteLine(jsonRoot);

            Assert.IsNotNull(snippets);
            Assert.IsTrue(snippets.Count > 0);
        }
Beispiel #2
0
        public void CreateVsCodeSnippetfromVisualStudioSnippetTest()
        {
            var snippet = VisualStudioSnippet.ParseFromFile(
                "C:\\Users\\rstrahl\\Documents\\Visual Studio 2017\\Code Snippets\\Visual Web Developer\\My HTML Snippets\\WebConnection\\ww-fv.snippet");

            Assert.IsNotNull(snippet);
            Assert.IsTrue(snippet.Shortcut == "ww-fv");


            var vsCodeSnippet = VsCodeSnippet.FromVisualStudioCodeSnippet(snippet);

            Console.WriteLine(vsCodeSnippet.ToJson());

            Assert.IsNotNull(vsCodeSnippet);
            Assert.AreEqual(snippet.Shortcut, vsCodeSnippet.Prefix);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public override void Parse()
        {
            Mode = ParseStringParameterSwitch("-m", null);
            if (string.IsNullOrEmpty(Mode))
            {
                Mode = ParseStringParameterSwitch("--mode", "vs-vscode");
            }
            Mode = Mode.ToLower();

            SourceFileOrDirectory = Args[0];
            SourceFileOrDirectory = Environment.ExpandEnvironmentVariables(SourceFileOrDirectory);

            TargetFile = ParseStringParameterSwitch("-o", null);
            if (string.IsNullOrEmpty(TargetFile))
            {
                TargetFile = ParseStringParameterSwitch("--output", null);
            }

            if (Mode == "vs-vscode")
            {
                if (string.IsNullOrEmpty(TargetFile))
                {
                    TargetFile = "~\\visualstudio-exported.code-snippets";
                }
                TargetFile = VsCodeSnippet.FixupSnippetPath(TargetFile);
            }


            Verbose = ParseParameterSwitch("-v");
            Recurse = ParseParameterSwitch("-r");

            ShowFileInExplorer = ParseParameterSwitch("-d");



            SnippetPrefix = ParseStringParameterSwitch("-p", null);
            if (string.IsNullOrEmpty(SnippetPrefix))
            {
                SnippetPrefix = ParseStringParameterSwitch("--prefix", null);
            }


            // Fix up for Visual Studio Snippet Path
            if (Mode.StartsWith("vs-"))
            {
                SourceFileOrDirectory = VisualStudioSnippet.FixupSnippetPath(SourceFileOrDirectory);
            }

            if (!string.IsNullOrEmpty(SourceFileOrDirectory) &&
                Directory.Exists(SourceFileOrDirectory))
            {
                DirectoryMode = true;
            }

            if (string.IsNullOrEmpty(SourceFileOrDirectory))
            {
                throw new ArgumentException("Source File or Directory shouldn't be empty.");
            }


            if (!DirectoryMode && !File.Exists(SourceFileOrDirectory))
            {
                throw new ArgumentException("Source file or directory does not exist.");
            }
        }
Beispiel #6
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);
        }