private void AddGeneratedFilesToProject(CodeWriterOutput output)
 {
     foreach (string file in output.CodeFileNames)
     {
         ProjectHelpers.AddFileToActiveProject(file);
     }
 }
예제 #2
0
 private void AddGeneratedFilesToProject(CodeWriterOutput output)
 {
     foreach (string file in output.CodeFileNames)
     {
         currentProject.AddFile(file);
     }
     if (!string.IsNullOrEmpty(output.ConfigurationFile) && File.Exists(output.ConfigurationFile))
     {
         currentProject.AddFile(output.ConfigurationFile);
     }
 }
예제 #3
0
        /// <summary>
        /// This overloaded method handles the generation of client or server code
        /// for a supplied WSDL file
        /// </summary>
        /// <param name="wsdlFile"></param>
        /// <returns></returns>
        private bool ProcessStubGenerationRequest(string wsdlFile)
        {
            try
            {
                // Fist display the UI and get the options.
                WebServiceCodeGenDialogNew dialog = new WebServiceCodeGenDialogNew();
                if (!currentProject.IsWebProject)
                {
                    dialog.DestinationNamespace = currentProject.AssemblyNamespace;
                }
                dialog.DestinationFilename = GetDefaultDestinationFilename(wsdlFile);

                if (!selectedItem.IsProject)
                {
                    //dialog.WsdlLocation = selectedItem.FileName;
                    dialog.WsdlLocation = wsdlFile;
                }

                //if we havent passed the wsdl file in, there is still
                // the option for the user to use the BROWSE button and pick
                // the wsdl.

                if (dialog.ShowDialog() == DialogResult.Cancel)
                {
                    return(false);
                }


                //Get the options set in the dialog and add other things
                // that are specific to VS
                CodeGenerationOptions options = dialog.Options;

                options.Language         = currentProject.ProjectLanguage;
                options.ProjectDirectory = currentProject.ProjectDirectory;
                options.OutputLocation   = GetOutputDirectory();
                // TODO: Infer the config file type according to the project type
                // and merge the generated config file with the existing one.
                options.ConfigurationFile = "output.config";

                outputWindowWriter.Clear();

                CodeGenerator    codeGenerator = new CodeGenerator();
                CodeWriterOutput output        = codeGenerator.GenerateCode(options);

                AddGeneratedFilesToProject(output);

                // Finally add the project references.
                AddAssemblyReferences();

                // add custom assembly references if necessary
                if (options.EnableWsdlEndpoint)
                {
                    AddMetadataExtensionsReference();
                }

                MessageBox.Show("Code generation successfully completed.", "WSCF.Blue", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ClientServiceGenerationException ex)
            {
                AppLog.LogMessage(ex.ToString());

                const string separator = "---------------------------------------------------------------------------------";
                foreach (string message in ex.Messages)
                {
                    outputWindowWriter.WriteMessage(message + "\r\n" + separator + "\r\n");
                }

                MessageBox.Show("Errors were found while importing the contract. Please check the 'WSCF.blue' pane in the Output window for more information.",
                                "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                AppLog.LogMessage(ex.ToString());
                MessageBox.Show(ex.ToString(), "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // TODO: Log the exception.
                //System.Diagnostics.Debugger.Break();
                return(false);
            }
            return(true);
        }
예제 #4
0
        private bool ProcessXsdCodeGenerationRequest()
        {
            try
            {
                VisualStudioProject project = currentProject;
                IEnumerable <VisualStudioSelectedItem> selectedItems = vsInstance.SelectedItems;

                if (selectedItems.Count() == 0)
                {
                    MessageBox.Show(
                        "Cannot generate code for items outside of a project.",
                        "Web Services Contract-First code generation",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);

                    return(true);
                }

                foreach (VisualStudioSelectedItem selectedItem in selectedItems)
                {
                    string extension = Path.GetExtension(selectedItem.FileName).ToLower();
                    if (extension == ".xsd" || extension == ".wsdl")
                    {
                        continue;
                    }

                    MessageBox.Show(
                        "Data Contracts can only be generated for .xsd or .wsdl files.",
                        "Web Services Contract-First code generation",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);

                    return(true);
                }

                string[]         dataContractFiles = selectedItems.Select(i => i.FileName).ToArray();
                XsdCodeGenDialog dialogForm        = new XsdCodeGenDialog(dataContractFiles);
                if (!project.IsWebProject)
                {
                    dialogForm.Namespace = project.AssemblyNamespace;
                }
                dialogForm.TargetFileName = project.GetDefaultDestinationFilename(dataContractFiles[0]);

                if (dialogForm.ShowDialog() == DialogResult.Cancel)
                {
                    return(false);
                }

                CodeGenerationOptions options = new CodeGenerationOptions();
                options.GenerateDataContracts    = true;
                options.DataContractFiles        = dataContractFiles;
                options.GenerateProperties       = dialogForm.PublicProperties;
                options.GenerateCollections      = dialogForm.Collections;
                options.GenerateSeparateFiles    = dialogForm.GenerateMultipleFiles;
                options.OverwriteExistingFiles   = dialogForm.OverwriteFiles;
                options.AdjustCasing             = dialogForm.AdjustCasing;
                options.EnableDataBinding        = dialogForm.DataBinding;
                options.GenerateOrderIdentifiers = dialogForm.OrderIdentifiers;
                options.GenerateTypedLists       = dialogForm.GenericLists;
                options.ClrNamespace             = dialogForm.Namespace;
                options.OutputFileName           = dialogForm.TargetFileName;
                options.OutputLocation           = GetOutputDirectory();
                options.ProjectDirectory         = project.ProjectDirectory;
                options.Language = project.ProjectLanguage;

                CodeGenerator    codeGenerator = new CodeGenerator();
                CodeWriterOutput output        = codeGenerator.GenerateCode(options);

                AddGeneratedFilesToProject(output);

                // Finally add the project references.
                AddAssemblyReferences();

                MessageBox.Show("Code generation successfully completed.", "WSCF.Blue", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                AppLog.LogMessage(ex.ToString());
                MessageBox.Show(ex.ToString(), "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // TODO: Log the exception.
                //System.Diagnostics.Debugger.Break();
            }

            return(true);
        } // End of ProcessXsdCodeGenerationRequestFunction.
        /// <summary>
        /// wsdl生成cs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WsdlCommandInvoke(object sender, EventArgs e)
        {
            var items = ProjectHelpers.GetSelectedItemPaths(_dte);

            if (items.Count() != 1 ||
                !(items.ElementAt(0).ToLower().EndsWith(".wsdl", StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
            var _file           = items.ElementAt(0);
            var project         = ProjectHelpers.GetActiveProject();
            var projectFullName = ProjectHelpers.GetActiveProject().FullName;
            var projectInfo     = new FileInfo(projectFullName);
            var folderproject   = projectInfo.Directory?.FullName;

            try
            {
                // Fist display the UI and get the options.
                WebServiceCodeGenDialogNew dialog = new WebServiceCodeGenDialogNew();
                if (!project.IsWebProject())
                {
                    dialog.DestinationNamespace = project.GetProjectProperty("DefaultNamespace");
                    if (string.IsNullOrEmpty(dialog.DestinationNamespace))
                    {
                        dialog.DestinationNamespace = Path.GetFileNameWithoutExtension(_file);
                    }
                }
                else
                {
                    dialog.DestinationNamespace = Path.GetFileNameWithoutExtension(_file);
                }
                dialog.DestinationFilename = ProjectHelpers.GetDefaultDestinationFilename(_file);
                dialog.WsdlLocation        = _file;

                if (dialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
                var wsdlFile = _file;
                // Try the Rpc2DocumentLiteral translation first.
                // wsdlFile = TryTranslateRpc2DocumentLiteral(wsdlFile);

                CodeGenOptions options = new CodeGenOptions();
                options.MetadataLocation = wsdlFile;
                options.OutputFileName   = dialog.DestinationFilename;
                options.OutputLocation   = folderproject;
                options.ProjectDirectory = project.FullName;
                options.Language         = CodeLanguage.CSharp;
                options.ProjectName      = project.Name;

                options.EnableDataBinding                  = dialog.EnableDataBinding;
                options.EnableSummaryComment               = dialog.Comment;
                options.GenerateCollections                = dialog.Collections;
                options.GenerateTypedLists                 = dialog.TypedList;
                options.EnableLazyLoading                  = dialog.LazyLoading;
                options.GenerateSeparateFiles              = dialog.GenerateMultipleFiles;
                options.OnlyUseDataContractSerializer      = dialog.OnlyUseDataContractSerializer;
                options.GenerateSeparateFilesEachNamespace = dialog.GenerateMultipleFilesEachNamespace;
                options.GenerateSeparateFilesEachXsd       = dialog.GenerateSeparateFilesEachXsd;
                options.AscendingClassByName               = dialog.AscendingClassByName;
                options.OverwriteExistingFiles             = dialog.Overwrite;
                options.ClrNamespace              = dialog.DestinationNamespace;
                options.EnableBaijiSerialization  = dialog.EnableBaijiSerialization;
                options.AddCustomRequestInterface = dialog.AddCustomRequestInterface;
                options.CustomRequestInterface    = dialog.CustomRequestInterface;
                options.ForceElementName          = dialog.ForceElementName;
                options.ForceElementNamespace     = dialog.ForceElementNamespace;
                options.GenerateAsyncOperations   = dialog.GenerateAsyncOperations;

                if (dialog.ServiceCode)
                {
                    options.CodeGeneratorMode = CodeGeneratorMode.Service;
                }
                else if (dialog.ClientCode)
                {
                    options.CodeGeneratorMode = CodeGeneratorMode.Client;
                }
                else
                {
                    options.CodeGeneratorMode = CodeGeneratorMode.ClientForTest;
                }

                options.EnableInitializeFields = true;


                CodeGenerator    codeGenerator = new CodeGenerator();
                CodeWriterOutput output        = codeGenerator.GenerateCode(options);

                AddGeneratedFilesToProject(output);

                // Finally add the project references.
                ProjectHelpers.AddAssemblyReferences(project);

                MessageBox.Show("Code generation successfully completed.", "Ant.SOA.CodeGen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ProjectHelpers.AddError(_package, ex.ToString());
                MessageBox.Show(ex.Message, "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 根据xsd生成model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ModelCommandInvoke(object sender, EventArgs e)
        {
            //可以选多个
            var items = ProjectHelpers.GetSelectedItemPaths(_dte);

            if (items != null && items.Any(r => !r.ToLower().EndsWith(".xsd", StringComparison.OrdinalIgnoreCase)))
            {
                ProjectHelpers.AddError(_package, "select file is not xsd file");
                return;
            }

            var _file           = items.ElementAt(0);
            var project         = ProjectHelpers.GetActiveProject();
            var projectFullName = ProjectHelpers.GetActiveProject().FullName;
            var projectInfo     = new FileInfo(projectFullName);
            var folderproject   = projectInfo.Directory?.FullName;

            try
            {
                string[]         dataContractFiles = items.ToArray();
                XsdCodeGenDialog dialogForm        = new XsdCodeGenDialog(dataContractFiles);
                if (!project.IsWebProject())
                {
                    dialogForm.Namespace = project.GetProjectProperty("DefaultNamespace");
                    if (string.IsNullOrEmpty(dialogForm.Namespace))
                    {
                        dialogForm.Namespace = Path.GetFileNameWithoutExtension(_file);
                    }
                }
                else
                {
                    dialogForm.Namespace = Path.GetFileNameWithoutExtension(_file);
                }
                dialogForm.TargetFileName = ProjectHelpers.GetDefaultDestinationFilename(_file);
                if (dialogForm.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                CodeGenOptions options = new CodeGenOptions();
                options.GenerateDataContractsOnly          = true;
                options.DataContractFiles                  = dataContractFiles;
                options.GenerateSeparateFiles              = dialogForm.GenerateMultipleFiles;
                options.GenerateSeparateFilesEachNamespace = dialogForm.GenerateMultipleFilesEachNamespace;
                options.GenerateSeparateFilesEachXsd       = dialogForm.GenerateSeparateFilesEachXsd;
                options.AscendingClassByName               = dialogForm.AscendingClassByName;
                options.OnlyUseDataContractSerializer      = dialogForm.OnlyUseDataContractSerializer;
                options.OverwriteExistingFiles             = dialogForm.OverwriteFiles;
                options.EnableDataBinding                  = dialogForm.DataBinding;
                options.EnableSummaryComment               = dialogForm.Comment;
                options.GenerateCollections                = dialogForm.Collections;
                options.GenerateTypedLists                 = dialogForm.TypedList;
                options.EnableLazyLoading                  = dialogForm.LazyLoading;
                options.OutputFileName            = dialogForm.TargetFileName;
                options.OutputLocation            = folderproject;
                options.ProjectDirectory          = project.FullName;
                options.Language                  = CodeLanguage.CSharp;
                options.ClrNamespace              = dialogForm.Namespace;
                options.EnableSummaryComment      = dialogForm.Comment;
                options.ProjectName               = project.Name;
                options.EnableBaijiSerialization  = dialogForm.EnableBaijiSerialization;
                options.AddCustomRequestInterface = dialogForm.AddCustomRequestInterface;
                options.CustomRequestInterface    = dialogForm.CustomRequestInterface;
                options.ForceElementName          = dialogForm.ForceElementName;
                options.ForceElementNamespace     = dialogForm.ForceElementNamespace;
                options.GenerateAsyncOperations   = dialogForm.GenerateAsyncOperations;
                options.EnableInitializeFields    = true;



                CodeGenerator    codeGenerator = new CodeGenerator();
                CodeWriterOutput output        = codeGenerator.GenerateCode(options);

                AddGeneratedFilesToProject(output);

                // Finally add the project references.
                ProjectHelpers.AddAssemblyReferences(project);

                MessageBox.Show("Model generation successfully completed.", "Ant.SOA.CodeGen", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ProjectHelpers.AddError(_package, ex.ToString());
                MessageBox.Show(ex.Message, "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #7
0
        private bool ProcessCodeGenerationRequestCore(string wsdlFile)
        {
            try
            {
                VisualStudioProject      project      = this.VisualStudio.SelectedProject;
                VisualStudioSelectedItem selectedItem = this.VisualStudio.SelectedItem;

                // Fist display the UI and get the options.
                WebServiceCodeGenDialogNew dialog = new WebServiceCodeGenDialogNew();
                if (!project.IsWebProject)
                {
                    dialog.DestinationNamespace = project.AssemblyNamespace;
                }
                dialog.DestinationFilename = project.GetDefaultDestinationFilename(wsdlFile);

                if (!selectedItem.IsProject)
                {
                    //dialog.WsdlLocation = selectedItem.FileName;
                    dialog.WsdlLocation = wsdlFile;
                }
                if (dialog.ShowDialog() == DialogResult.Cancel)
                {
                    return(false);
                }

                wsdlFile = dialog.WsdlPath;
                // Try the Rpc2DocumentLiteral translation first.
                // wsdlFile = TryTranslateRpc2DocumentLiteral(wsdlFile);

                CodeGenerationOptions options = new CodeGenerationOptions();
                options.MetadataLocation = wsdlFile;
                options.ClrNamespace     = dialog.DestinationNamespace;
                options.OutputFileName   = dialog.DestinationFilename;
                options.OutputLocation   = GetOutputDirectory();
                options.ProjectDirectory = project.ProjectDirectory;
                options.Language         = project.ProjectLanguage;
                options.ProjectName      = project.ProjectName;
                // TODO: Infer the config file type according to the project type
                // and merge the generated config file with the existing one.
                options.ConfigurationFile         = "output.config";
                options.GenerateService           = dialog.ServiceCode;
                options.GenerateProperties        = dialog.GenerateProperties;
                options.VirtualProperties         = dialog.VirtualProperties;
                options.FormatSoapActions         = dialog.FormatSoapActions;
                options.GenerateCollections       = dialog.Collections;
                options.GenerateTypedLists        = dialog.GenericList;
                options.EnableDataBinding         = dialog.EnableDataBinding;
                options.GenerateOrderIdentifiers  = dialog.OrderIdentifiers;
                options.GenerateAsyncCode         = dialog.AsyncMethods;
                options.GenerateSeparateFiles     = dialog.GenerateMultipleFiles;
                options.AdjustCasing              = dialog.ChangeCasing;
                options.OverwriteExistingFiles    = dialog.Overwrite;
                options.EnableWsdlEndpoint        = dialog.EnabledWsdlEndpoint;
                options.GenerateSvcFile           = dialog.GenerateSvcFile;
                options.ConcurrencyMode           = dialog.ConcurrencyMode;
                options.InstanceContextMode       = dialog.InstanceContextMode;
                options.UseSynchronizationContext = dialog.UseSynchronizationContext;
                options.MethodImplementation      = dialog.MethodImplementation;

                OutputWindowWriter.Clear();

                CodeGenerator    codeGenerator = new CodeGenerator();
                CodeWriterOutput output        = codeGenerator.GenerateCode(options);

                AddGeneratedFilesToProject(output);

                // Finally add the project references.
                AddAssemblyReferences();

                // add custom assembly references if necessary
                if (options.EnableWsdlEndpoint)
                {
                    AddMetadataExtensionsReference();
                }

                MessageBox.Show("Code generation successfully completed.", "WSCF.Blue", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ClientServiceGenerationException ex)
            {
                AppLog.LogMessage(ex.ToString());

                const string separator = "---------------------------------------------------------------------------------";
                foreach (string message in ex.Messages)
                {
                    OutputWindowWriter.WriteMessage(message + "\r\n" + separator + "\r\n");
                }

                MessageBox.Show("Errors were found while importing the contract. Please check the 'WSCF.blue' pane in the Output window for more information.",
                                "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                AppLog.LogMessage(ex.ToString());
                MessageBox.Show(ex.ToString(), "CodeGeneration", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // TODO: Log the exception.
                //System.Diagnostics.Debugger.Break();
            }
            return(true);
        }