public static CodeRegenerationResult RegenerateCode(string ramlFilePath, string extensionPath)
        {
            var containingFolder = Path.GetDirectoryName(ramlFilePath);
            var refFilePath      = InstallerServices.GetRefFilePath(ramlFilePath);

            var ramlSource = RamlReferenceReader.GetRamlSource(refFilePath);

            if (string.IsNullOrWhiteSpace(ramlSource))
            {
                ramlSource = ramlFilePath;
            }

            var clientRootClassName = RamlReferenceReader.GetClientRootClassName(refFilePath);

            var globalProvider = ServiceProvider.GlobalProvider;
            var destFolderItem = GetDestinationFolderItem(ramlFilePath, globalProvider);
            var result         = UpdateRamlAndIncludedFiles(ramlFilePath, destFolderItem, ramlSource, containingFolder);

            if (!result.IsSuccess)
            {
                return(CodeRegenerationResult.Error("Error when tryng to download " + ramlSource + " - Status Code: " + Enum.GetName(typeof(HttpStatusCode), result.StatusCode)));
            }


            var dte  = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as DTE;
            var proj = VisualStudioAutomationHelper.GetActiveProject(dte);
            var apiRefsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar +
                                    RamlReferenceServiceBase.ApiReferencesFolderName + Path.DirectorySeparatorChar;

            TemplatesManager.CopyClientTemplateToProjectFolder(apiRefsFolderPath);

            var ramlInfo = RamlInfoService.GetRamlInfo(ramlFilePath);

            if (ramlInfo.HasErrors)
            {
                return(CodeRegenerationResult.Error(ramlInfo.ErrorMessage));
            }

            var res = GenerateCodeUsingTemplate(ramlFilePath, ramlInfo, globalProvider, refFilePath, clientRootClassName, extensionPath);

            if (res.HasErrors)
            {
                return(CodeRegenerationResult.Error(res.Errors));
            }


            var content = TemplatesManager.AddClientMetadataHeader(res.Content);

            return(CodeRegenerationResult.Success(content));
        }
Example #2
0
        public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace,
                            IntPtr[] rgbOutputFileContents, out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
        {
            try
            {
                if (bstrInputFileContents == null)
                {
                    throw new ArgumentNullException("bstrInputFileContents");
                }

                var containingFolder    = Path.GetDirectoryName(wszInputFilePath);
                var refFilePath         = InstallerServices.GetRefFilePath(wszInputFilePath);
                var ramlSource          = RamlReferenceReader.GetRamlSource(refFilePath);
                var clientRootClassName = RamlReferenceReader.GetClientRootClassName(refFilePath);

                var globalProvider = ServiceProvider.GlobalProvider;
                var destFolderItem = GetDestinationFolderItem(wszInputFilePath, globalProvider);
                var result         = UpdateRamlAndIncludedFiles(wszInputFilePath, destFolderItem, ramlSource, containingFolder);
                if (!result.IsSuccess)
                {
                    MessageBox.Show("Error when tryng to download " + ramlSource + " - Status Code: " + Enum.GetName(typeof(HttpStatusCode), result.StatusCode));
                    pcbOutput = 0;
                    return(VSConstants.E_ABORT);
                }

                var dte  = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as DTE;
                var proj = VisualStudioAutomationHelper.GetActiveProject(dte);
                var apiRefsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar +
                                        RamlReferenceService.ApiReferencesFolderName + Path.DirectorySeparatorChar;

                templatesManager.CopyClientTemplateToProjectFolder(apiRefsFolderPath);

                var ramlInfo = RamlInfoService.GetRamlInfo(wszInputFilePath);
                if (ramlInfo.HasErrors)
                {
                    MessageBox.Show(ramlInfo.ErrorMessage);
                    pcbOutput = 0;
                    return(VSConstants.E_ABORT);
                }

                var res = GenerateCodeUsingTemplate(wszInputFilePath, ramlInfo, globalProvider, refFilePath, clientRootClassName);

                if (res.HasErrors)
                {
                    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, res.Errors);
                    MessageBox.Show(res.Errors);
                    pcbOutput = 0;
                    return(VSConstants.E_ABORT);
                }


                var content = templatesManager.AddClientMetadataHeader(res.Content);

                var bytes = Encoding.UTF8.GetBytes(content);
                rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(bytes.Length);
                Marshal.Copy(bytes, 0, rgbOutputFileContents[0], bytes.Length);
                pcbOutput = (uint)bytes.Length;
                return(VSConstants.S_OK);
            }
            catch (Exception ex)
            {
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource,
                                     VisualStudioAutomationHelper.GetExceptionInfo(ex));

                var errorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    errorMessage += " - " + ex.InnerException.Message;
                }

                MessageBox.Show(errorMessage);
                pcbOutput = 0;
                return(VSConstants.E_ABORT);
            }
        }