Ejemplo n.º 1
0
        private void GenerateCode(string targetNamespace, string clientRootClassName, string ramlDestFile, string destFolderPath,
                                  string destFolderName)
        {
            var ramlInfo = RamlInfoService.GetRamlInfo(ramlDestFile);

            if (ramlInfo.HasErrors)
            {
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, ramlInfo.ErrorMessage);
                MessageBox.Show(ramlInfo.ErrorMessage);
                return;
            }

            var model          = new ClientGeneratorService(ramlInfo.RamlDocument, clientRootClassName, targetNamespace).BuildModel();
            var directoryName  = Path.GetDirectoryName(ramlDestFile).TrimEnd(Path.DirectorySeparatorChar);
            var templateFolder = directoryName.Substring(0, directoryName.LastIndexOf(Path.DirectorySeparatorChar)) +
                                 Path.DirectorySeparatorChar + "Templates";

            var templateFilePath = Path.Combine(templateFolder, ClientT4TemplateName);
            var extensionPath    = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;
            var t4Service        = new T4Service(ServiceProvider);
            var res = t4Service.TransformText(templateFilePath, model, extensionPath, ramlDestFile, targetNamespace);

            if (res.HasErrors)
            {
                ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, res.Errors);
                MessageBox.Show(res.Errors);
                return;
            }

            var content      = TemplatesManager.AddClientMetadataHeader(res.Content);
            var csTargetFile = Path.Combine(destFolderPath, destFolderName + ".cs");

            File.WriteAllText(csTargetFile, content);
        }
Ejemplo n.º 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      = GetRefFilePath(wszInputFilePath, containingFolder);
                var ramlSource       = RamlReferenceReader.GetRamlSource(refFilePath);

                var globalProvider = Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider;
                var destFolderItem = GetDestinationFolderItem(wszInputFilePath, globalProvider);
                UpdateRamlAndIncludedFiles(wszInputFilePath, destFolderItem, ramlSource, containingFolder);

                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);

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

                var bytes = Encoding.UTF8.GetBytes(res.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);
            }
        }
Ejemplo n.º 3
0
        private async Task RegenerateClientCodeAsync(string ramlFilePath)
        {
            var ramlInfo = await RamlInfoService.GetRamlInfo(ramlFilePath);

            //TODO:
            //var result = RamlReferenceServiceBase.GetRamlReferenceService().GenerateCode(ramlInfo, proj,, GetExtensionPath());
            //if (!result.IsSuccess)
            //{
            //    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, result.ErrorMessage);
            //    MessageBox.Show(result.ErrorMessage);
            //}
        }
        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));
        }
Ejemplo n.º 5
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);
            }
        }