コード例 #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="args">Event args.</param>
        private void Execute(object sender, EventArgs args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            object          service         = ServiceProvider.GetService(typeof(SVsTextManager));
            IVsTextManager2 textManager     = service as IVsTextManager2;
            string          highlightedText = EditorUtilities.GetHighlightedText(textManager);

            GenerateTypeWindow generateTypeWindow = new GenerateTypeWindow();
            DialogResult       result             = generateTypeWindow.ShowDialog();

            if (result != DialogResult.OK)
            {
                return;
            }

            ButlerCode bCode = ButlerCodeFactory.Create();

            bCode.Namespace  = generateTypeWindow.TypeNamespace;
            bCode.ClassName  = generateTypeWindow.TypeName;
            bCode.SourceJson = highlightedText;

            string generated = bCode.Generate();

            Clipboard.SetText(generated);

            _package.DoAlert("Generated code contents copied to clipboard.");
        }
コード例 #2
0
        public static void Generate(string sourceJson)
        {
            ButlerCode bCode = ButlerCodeFactory.Create();

            bCode.Namespace  = "JsonButler.Cli.Payloads";
            bCode.ClassName  = "MyPayload";
            bCode.SourceJson = sourceJson;
            string generatedCode = bCode.Generate();

            ButlerWriterService.SetClipboardText(generatedCode);
        }
コード例 #3
0
        public static void Generate(string sourceJson, string outputFile)
        {
            ButlerCode bCode = ButlerCodeFactory.Create();

            string[] filePathSegments = outputFile.Split('/');
            string   fullFileName     = filePathSegments[filePathSegments.Length - 1];

            string[] fileNameSegments = fullFileName.Split('.');
            string   shortFileName    = fileNameSegments[0];

            bCode.Namespace  = "JsonButler.Cli.Payloads";
            bCode.ClassName  = shortFileName.ToPascalCase();
            bCode.SourceJson = sourceJson;
            string generatedCode = bCode.Generate();

            ButlerWriterService.WriteAllText(outputFile, generatedCode);
        }
コード例 #4
0
        public void GenerateCodeFile_ComplexData_CodeGenerated()
        {
            // Valid JSON text. This can be from a file, or from a database/server response.
            string input = Resources.ButlerJson0;

            // Generates C# code (i.e. contents of potential C# file).
            ButlerCode bCode = ButlerCodeFactory.Create();

            bCode.Namespace  = "JsonButler.Tests.Results";
            bCode.ClassName  = "ComplexDataPayload";
            bCode.SourceJson = input;
            string generatedCsCode = bCode.Generate();  // Generate

            Console.WriteLine(generatedCsCode);
            string expected = Resources.ButlerCs0;

            Tuple <string, string> diff = TestUtilities.PeekAtFirstDiff(expected, generatedCsCode);

            Assert.AreEqual(expected, generatedCsCode, $"\n{diff.Item1}\n{diff.Item2}");
        }