public void UpdateRaml(string ramlFilePath)
        {
            var dte  = ServiceProvider.GetService(typeof(SDTE)) as DTE;
            var proj = VisualStudioAutomationHelper.GetActiveProject(dte);
            var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar;

            var refFilePath        = InstallerServices.GetRefFilePath(ramlFilePath);
            var includesFolderPath = contractsFolderPath + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName;
            var ramlSource         = RamlReferenceReader.GetRamlSource(refFilePath);

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

            var includesManager = new RamlIncludesManager();
            var result          = includesManager.Manage(ramlSource, includesFolderPath, contractsFolderPath + Path.DirectorySeparatorChar);

            if (result.IsSuccess)
            {
                File.WriteAllText(ramlFilePath, result.ModifiedContents);
                var parameters = new RamlChooserActionParams(ramlFilePath, ramlFilePath, null, null,
                                                             Path.GetFileName(ramlFilePath).ToLowerInvariant(),
                                                             RamlReferenceReader.GetRamlNamespace(refFilePath), null)
                {
                    UseAsyncMethods = RamlReferenceReader.GetRamlUseAsyncMethods(refFilePath),
                    IncludeApiVersionInRoutePrefix = RamlReferenceReader.GetRamlIncludeApiVersionInRoutePrefix(refFilePath),
                    ModelsFolder = RamlReferenceReader.GetModelsFolder(refFilePath),
                    ImplementationControllersFolder = RamlReferenceReader.GetImplementationControllersFolder(refFilePath),
                    AddGeneratedSuffixToFiles       = RamlReferenceReader.GetAddGeneratedSuffix(refFilePath)
                };
                Scaffold(ramlFilePath, parameters);
            }
        }
Example #2
0
        private void ImplementContractCallback(object sender, EventArgs e)
        {
            ChangeImplementContractCommandStatus(false);

            // Get the file path
            uint         itemid;
            IVsHierarchy hierarchy;

            if (!IsSingleProjectItemSelection(out hierarchy, out itemid))
            {
                return;
            }
            string ramlFilePath;

            ((IVsProject)hierarchy).GetMkDocument(itemid, out ramlFilePath);

            var refFilePath     = Path.Combine(Path.GetDirectoryName(ramlFilePath), Path.GetFileNameWithoutExtension(ramlFilePath) + ".ref");
            var targetNamespace = RamlReferenceReader.GetRamlNamespace(refFilePath);

            try
            {
                var scaffolder = new RamlScaffoldService(new T4Service(ServiceProvider.GlobalProvider),
                                                         ServiceProvider.GlobalProvider);
                scaffolder.Scaffold(ramlFilePath, targetNamespace, Path.GetFileName(ramlFilePath));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

            ChangeImplementContractCommandStatus(true);
        }
        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);
            }
        }
        private Result GenerateCodeUsingTemplate(string wszInputFilePath, RamlInfo ramlInfo, System.IServiceProvider globalProvider,
                                                 string refFilePath)
        {
            var model            = GetGeneratorModel(wszInputFilePath, ramlInfo);
            var vsixPath         = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;
            var templateFileName = Path.Combine(vsixPath, "GeneratedModel.t4");
            var t4Service        = new T4Service(globalProvider);
            var targetNamespace  = RamlReferenceReader.GetRamlNamespace(refFilePath);
            var res = t4Service.TransformText(templateFileName, model, vsixPath, wszInputFilePath, targetNamespace);

            return(res);
        }
        private static Result GenerateCodeUsingTemplate(string wszInputFilePath, RamlInfo ramlInfo, System.IServiceProvider globalProvider,
                                                        string refFilePath, string clientRootClassName, string extensionPath)
        {
            var targetNamespace  = RamlReferenceReader.GetRamlNamespace(refFilePath);
            var model            = GetGeneratorModel(clientRootClassName, ramlInfo, targetNamespace);
            var templateFolder   = GetTemplateFolder(wszInputFilePath);
            var templateFilePath = Path.Combine(templateFolder, ClientT4TemplateName);
            var t4Service        = new T4Service(globalProvider);
            var res = t4Service.TransformText(templateFilePath, model, extensionPath, wszInputFilePath, targetNamespace);

            return(res);
        }
        private static void ScaffoldMainRamlFiles(IEnumerable <string> ramlFiles)
        {
            var globalProvider = ServiceProvider.GlobalProvider;
            var service        = new RamlScaffoldService(new T4Service(globalProvider), ServiceProvider.GlobalProvider);

            foreach (var ramlFile in ramlFiles)
            {
                var refFilePath     = InstallerServices.GetRefFilePath(ramlFile);
                var useAsyncMethods = RamlReferenceReader.GetRamlUseAsyncMethods(refFilePath);
                var targetNamespace = RamlReferenceReader.GetRamlNamespace(refFilePath);
                service.Scaffold(ramlFile, targetNamespace, Path.GetFileName(ramlFile), useAsyncMethods);
            }
        }
        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));
        }
        private static void ScaffoldMainRamlFiles(IEnumerable <string> ramlFiles)
        {
            var service = GetRamlScaffoldService(Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider);

            foreach (var ramlFile in ramlFiles)
            {
                var refFilePath = InstallerServices.GetRefFilePath(ramlFile);
                var includeApiVersionInRoutePrefix = RamlReferenceReader.GetRamlIncludeApiVersionInRoutePrefix(refFilePath);
                var parameters = new RamlChooserActionParams(ramlFile, ramlFile, null, null, Path.GetFileName(ramlFile),
                                                             RamlReferenceReader.GetRamlNamespace(refFilePath), null)
                {
                    UseAsyncMethods = RamlReferenceReader.GetRamlUseAsyncMethods(refFilePath),
                    IncludeApiVersionInRoutePrefix = includeApiVersionInRoutePrefix,
                    ModelsFolder = RamlReferenceReader.GetModelsFolder(refFilePath),
                    ImplementationControllersFolder = RamlReferenceReader.GetImplementationControllersFolder(refFilePath),
                    AddGeneratedSuffixToFiles       = RamlReferenceReader.GetAddGeneratedSuffix(refFilePath)
                };
                service.Scaffold(ramlFile, parameters);
            }
        }
        public void UpdateRaml(string ramlFilePath)
        {
            var dte  = serviceProvider.GetService(typeof(SDTE)) as DTE;
            var proj = VisualStudioAutomationHelper.GetActiveProject(dte);
            var generatedFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar;

            var refFilePath        = InstallerServices.GetRefFilePath(ramlFilePath);
            var includesFolderPath = generatedFolderPath + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName;
            var ramlSource         = RamlReferenceReader.GetRamlSource(refFilePath);
            var includesManager    = new RamlIncludesManager();
            var result             = includesManager.Manage(ramlSource, includesFolderPath, generatedFolderPath + Path.DirectorySeparatorChar);

            if (result.IsSuccess)
            {
                File.WriteAllText(ramlFilePath, result.ModifiedContents);
                var targetNamespace = RamlReferenceReader.GetRamlNamespace(refFilePath);
                var useAsyncMethods = RamlReferenceReader.GetRamlUseAsyncMethods(refFilePath);
                Scaffold(ramlFilePath, targetNamespace, Path.GetFileName(ramlFilePath).ToLowerInvariant(), useAsyncMethods);
            }
        }
Example #10
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);
            }
        }
        private static void ShowOrHideUpdateRamlCommandContract(object sender)
        {
            // get the menu that fired the event
            var menuCommand = sender as OleMenuCommand;

            if (menuCommand == null)
            {
                return;
            }

            ShowAndEnableCommand(menuCommand, false);

            IVsHierarchy hierarchy;
            uint         itemid;

            if (!IsSingleProjectItemSelection(out hierarchy, out itemid))
            {
                return;
            }
            // Get the file path
            string itemFullPath;

            ((IVsProject)hierarchy).GetMkDocument(itemid, out itemFullPath);

            FileInfo transformFileInfo;

            try
            {
                transformFileInfo = new FileInfo(itemFullPath);
            }
            catch (Exception)
            {
                return;
            }


            var endsWithExtension = transformFileInfo.Name.EndsWith(".raml");

            // if not leave the menu hidden
            if (!endsWithExtension)
            {
                return;
            }

            if (itemFullPath.Contains(RamlReferenceService.ApiReferencesFolderName))
            {
                return;
            }

            var folder = Path.GetDirectoryName(itemFullPath);

            if (folder.EndsWith(InstallerServices.IncludesFolderName))
            {
                return;
            }

            if (!IsAspNet5OrWebApiCoreInstalled())
            {
                return;
            }

            var refFile = InstallerServices.GetRefFilePath(itemFullPath);
            var source  = RamlReferenceReader.GetRamlSource(refFile);

            if (string.IsNullOrWhiteSpace(source))
            {
                return;
            }

            ShowAndEnableCommand(menuCommand, true);
        }
        private static void ShowOrHideUpdateRamlRefCommand(object sender, string containingFolderName)
        {
            // get the menu that fired the event
            var menuCommand = sender as OleMenuCommand;

            if (menuCommand == null)
            {
                return;
            }

            ShowAndEnableCommand(menuCommand, false);

            var dte  = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as DTE;
            var proj = VisualStudioAutomationHelper.GetActiveProject(dte);

            if (VisualStudioAutomationHelper.IsAVisualStudio2015Project(proj))
            {
                return;
            }

            IVsHierarchy hierarchy;
            uint         itemid;

            if (!IsSingleProjectItemSelection(out hierarchy, out itemid))
            {
                return;
            }
            // Get the file path
            string itemFullPath;

            ((IVsProject)hierarchy).GetMkDocument(itemid, out itemFullPath);

            FileInfo transformFileInfo;

            try
            {
                transformFileInfo = new FileInfo(itemFullPath);
            }
            catch (Exception)
            {
                return;
            }

            var endsWithExtension = transformFileInfo.Name.EndsWith(".raml");

            // if not leave the menu hidden
            if (!endsWithExtension)
            {
                return;
            }

            if (!itemFullPath.Contains(containingFolderName))
            {
                return;
            }

            var folder = Path.GetDirectoryName(itemFullPath);

            if (folder.EndsWith(InstallerServices.IncludesFolderName))
            {
                return;
            }

            var refFile = InstallerServices.GetRefFilePath(itemFullPath);
            var source  = RamlReferenceReader.GetRamlSource(refFile);

            if (string.IsNullOrWhiteSpace(source))
            {
                return;
            }

            ShowAndEnableCommand(menuCommand, true);
        }