private static bool HasReferenceFile(string ramlFilePath)
        {
            var refFilePath      = InstallerServices.GetRefFilePath(ramlFilePath);
            var hasReferenceFile = !string.IsNullOrWhiteSpace(refFilePath) && File.Exists(refFilePath);

            return(hasReferenceFile);
        }
        private static ProjectItem AddOrUpdateRamlFile(string modifiedContents, ProjectItem folderItem, string folderPath, string ramlFileName)
        {
            ProjectItem ramlProjItem;
            var         ramlDestFile = Path.Combine(folderPath, ramlFileName);

            if (File.Exists(ramlDestFile))
            {
                var dialogResult = InstallerServices.ShowConfirmationDialog(ramlFileName);

                if (dialogResult == MessageBoxResult.Yes)
                {
                    File.WriteAllText(ramlDestFile, modifiedContents);
                    ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
                }
                else
                {
                    ramlProjItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == ramlFileName);
                    if (ramlProjItem == null)
                    {
                        ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
                    }
                }
            }
            else
            {
                File.WriteAllText(ramlDestFile, modifiedContents);
                ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
            }
            return(ramlProjItem);
        }
        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);
            }
        }
        private static ProjectItem AddOrUpdateRamlFile(string modifiedContents, ProjectItem folderItem, string folderPath, string ramlFileName)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            ProjectItem ramlProjItem;
            var         ramlDestFile = Path.Combine(folderPath, ramlFileName);

            if (File.Exists(ramlDestFile))
            {
                var dialogResult = InstallerServices.ShowConfirmationDialog(ramlFileName);

                if (dialogResult == MessageBoxResult.Yes)
                {
                    File.WriteAllText(ramlDestFile, modifiedContents);
                    ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
                }
                else
                {
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                    ramlProjItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == ramlFileName);
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
                    if (ramlProjItem == null)
                    {
                        ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
                    }
                }
            }
            else
            {
                File.WriteAllText(ramlDestFile, modifiedContents);
                ramlProjItem = folderItem.ProjectItems.AddFromFile(ramlDestFile);
            }
            return(ramlProjItem);
        }
Beispiel #5
0
        private void AddContractFromFile(string ramlFilePath, string targetNamespace, string ramlSource, bool?doNotScaffold, ProjectItem folderItem, string folderPath, string targetFilename)
        {
            InstallerServices.AddRefFile(ramlFilePath, targetNamespace, ramlSource, folderPath, targetFilename);

            var includesFolderPath = folderPath + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName;

            var includesManager = new RamlIncludesManager();
            var result          = includesManager.Manage(ramlSource, includesFolderPath, confirmOverrite: true);

            var includesFolderItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == InstallerServices.IncludesFolderName);

            if (includesFolderItem == null)
            {
                includesFolderItem = folderItem.ProjectItems.AddFolder(InstallerServices.IncludesFolderName);
            }

            foreach (var file in result.IncludedFiles)
            {
                includesFolderItem.ProjectItems.AddFromFile(file);
            }

            //var existingIncludeItems = includesFolderItem.ProjectItems.Cast<ProjectItem>();
            //var oldIncludedFiles = existingIncludeItems.Where(item => !result.IncludedFiles.Contains(item.FileNames[0]));
            //InstallerServices.RemoveSubItemsAndAssociatedFiles(oldIncludedFiles);

            var ramlProjItem = AddOrUpdateRamlFile(result.ModifiedContents, folderItem, folderPath, Path.GetFileName(ramlFilePath));

            InstallerServices.RemoveSubItemsAndAssociatedFiles(ramlProjItem);

            if (doNotScaffold == null || !doNotScaffold.Value)
            {
                Scaffold(ramlSource, targetNamespace, Path.GetFileName(ramlFilePath));
            }
        }
Beispiel #6
0
        private void AddEmptyContract(string filename, string title, ProjectItem folderItem, string folderPath, string targetNamespace, string targetFilename)
        {
            var newContractFile = Path.Combine(folderPath, filename);
            var contents        = CreateNewRamlContents(title);

            InstallerServices.AddRefFile(newContractFile, targetNamespace, newContractFile, folderPath, targetFilename);

            if (File.Exists(newContractFile))
            {
                var dialogResult = InstallerServices.ShowConfirmationDialog(filename);
                if (dialogResult == MessageBoxResult.Yes)
                {
                    File.WriteAllText(newContractFile, contents);
                    folderItem.ProjectItems.AddFromFile(newContractFile);
                }
                else
                {
                    var item = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == newContractFile);
                    if (item == null)
                    {
                        folderItem.ProjectItems.AddFromFile(newContractFile);
                    }
                }
            }
            else
            {
                File.WriteAllText(newContractFile, contents);
                folderItem.ProjectItems.AddFromFile(newContractFile);
            }
        }
        private void AddFilesToProject(string ramlSourceFile, Project proj, string targetNamespace, string ramlOriginalSource, string targetFileName, string clientRootClassName)
        {
            if (!File.Exists(ramlSourceFile))
            {
                throw new FileNotFoundException("RAML file not found " + ramlSourceFile);
            }

            if (Path.GetInvalidFileNameChars().Any(targetFileName.Contains))
            {
                throw new ArgumentException("Specified filename has invalid chars: " + targetFileName);
            }

            var destFolderName    = Path.GetFileNameWithoutExtension(targetFileName);
            var apiRefsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ApiReferencesFolderName + Path.DirectorySeparatorChar;
            var destFolderPath    = apiRefsFolderPath + destFolderName + Path.DirectorySeparatorChar;
            var apiRefsFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ApiReferencesFolderName);

            var destFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(apiRefsFolderItem, destFolderName, destFolderPath);

            var ramlProjItem = InstallerServices.AddOrUpdateRamlFile(ramlSourceFile, destFolderPath, destFolderItem, targetFileName);
            var refFilePath  = InstallerServices.AddRefFile(ramlSourceFile, targetNamespace, ramlOriginalSource, destFolderPath, targetFileName, null, clientRootClassName);

            ramlProjItem.ProjectItems.AddFromFile(refFilePath);

            ramlProjItem.Properties.Item("CustomTool").Value = string.Empty; // to cause a refresh when file already exists
            ramlProjItem.Properties.Item("CustomTool").Value = "RamlClientTool";
        }
        private static void ShowOrHideCommandRaml(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;
            }

            var refFile = InstallerServices.GetRefFilePath(itemFullPath);

            if (!File.Exists(refFile))
            {
                return;
            }

            ShowAndEnableCommand(menuCommand, true);
        }
        /// <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="e">Event args.</param>
        private async void Execute(object sender, EventArgs e)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            await ChangeCommandStatusAsync(menuCommandID, 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 = InstallerServices.GetRefFilePath(ramlFilePath);


            var editorModel = new RamlPropertiesEditorViewModel();

            editorModel.Load(refFilePath, RAML.Tools.Properties.Settings.Default.ContractsFolderName, RAML.Tools.Properties.Settings.Default.ApiReferencesFolderName);
            AMFToolsPackage.WindowManager.ShowDialog(editorModel);

            //var frm = new RamlPropertiesEditor();
            //frm.Load(refFilePath, RAML.Tools.Properties.Settings.Default.ContractsFolderName, RAML.Tools.Properties.Settings.Default.ApiReferencesFolderName);
            //var result = frm.ShowDialog();
            if (editorModel.WasSaved)
            {
                if (IsServerSide(ramlFilePath))
                {
                    var ramlScaffoldUpdater = RamlScaffoldServiceBase.GetRamlScaffoldService(Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider);
                    ramlScaffoldUpdater.UpdateRaml(ramlFilePath);
                }
                else
                {
                    var templatesManager    = new TemplatesManager();
                    var ramlFolder          = Path.GetDirectoryName(ramlFilePath).TrimEnd(Path.DirectorySeparatorChar);
                    var generatedFolderPath = ramlFolder.Substring(0, ramlFolder.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                    if (!templatesManager.ConfirmWhenIncompatibleClientTemplate(generatedFolderPath))
                    {
                        return;
                    }

                    await RegenerateClientCodeAsync(ramlFilePath);
                }
            }

            await ChangeCommandStatusAsync(menuCommandID, true);
        }
        private static void UpdateRamlAndIncludedFiles(string ramlFilePath, ProjectItem destFolderItem, string ramlSource, string containingFolder)
        {
            var includesFolderItem = destFolderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == InstallerServices.IncludesFolderName);

            InstallerServices.RemoveSubItemsAndAssociatedFiles(includesFolderItem);

            var includeManager = new RamlIncludesManager();
            var result         = includeManager.Manage(ramlSource, containingFolder + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName);

            UpdateRamlFile(ramlFilePath, result.ModifiedContents);

            InstallerServices.AddNewIncludedFiles(result, includesFolderItem, destFolderItem);
        }
        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);
            }
        }
        private void EditRamlPropertiesCallback(object sender, EventArgs e)
        {
            ChangeCommandStatus(editRamlPropertiesCmdId, 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 = InstallerServices.GetRefFilePath(ramlFilePath);


            var editorModel = new RamlPropertiesEditorViewModel();

            editorModel.Load(refFilePath, Settings.Default.ContractsFolderName, Settings.Default.ApiReferencesFolderName);
            windowManager.ShowDialog(editorModel);

            //var frm = new RamlPropertiesEditor();
            //frm.Load(refFilePath, Settings.Default.ContractsFolderName, Settings.Default.ApiReferencesFolderName);
            //var result = frm.ShowDialog();
            if (editorModel.WasSaved)
            {
                if (IsServerSide(ramlFilePath))
                {
                    var ramlScaffoldUpdater = new RamlScaffoldService(new T4Service(ServiceProvider.GlobalProvider), ServiceProvider.GlobalProvider);
                    ramlScaffoldUpdater.UpdateRaml(ramlFilePath);
                }
                else
                {
                    var templatesManager    = new TemplatesManager();
                    var ramlFolder          = Path.GetDirectoryName(ramlFilePath).TrimEnd(Path.DirectorySeparatorChar);
                    var generatedFolderPath = ramlFolder.Substring(0, ramlFolder.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                    if (!templatesManager.ConfirmWhenIncompatibleClientTemplate(generatedFolderPath))
                    {
                        return;
                    }

                    var dte = (DTE2)GetService(typeof(SDTE));
                    dte.ExecuteCommand("Project.RunCustomTool");
                }
            }

            ChangeCommandStatus(editRamlPropertiesCmdId, true);
        }
        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));
        }
Beispiel #14
0
        private void AddFilesToProject(string ramlSourceFile, Project proj, string targetNamespace, string ramlOriginalSource, string targetFileName, string clientRootClassName)
        {
            if (!File.Exists(ramlSourceFile))
            {
                throw new FileNotFoundException("RAML file not found " + ramlSourceFile);
            }

            if (Path.GetInvalidFileNameChars().Any(targetFileName.Contains))
            {
                throw new ArgumentException("Specified filename has invalid chars: " + targetFileName);
            }

            var destFolderName    = Path.GetFileNameWithoutExtension(targetFileName);
            var apiRefsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ApiReferencesFolderName + Path.DirectorySeparatorChar;
            var destFolderPath    = apiRefsFolderPath + destFolderName + Path.DirectorySeparatorChar;
            var apiRefsFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ApiReferencesFolderName);

            var destFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(apiRefsFolderItem, destFolderName, destFolderPath);

            var includesManager = new RamlIncludesManager();
            var result          = includesManager.Manage(ramlOriginalSource, destFolderPath, ramlSourceFile);

            var ramlDestFile = Path.Combine(destFolderPath, targetFileName);

            if (File.Exists(ramlDestFile))
            {
                new FileInfo(ramlDestFile).IsReadOnly = false;
            }
            File.WriteAllText(ramlDestFile, result.ModifiedContents);

            var ramlProjItem = InstallerServices.AddOrUpdateRamlFile(ramlDestFile, destFolderPath, destFolderItem, targetFileName);
            var props        = new RamlProperties
            {
                ClientName = clientRootClassName,
                Source     = ramlOriginalSource,
                Namespace  = targetNamespace
            };
            var refFilePath = InstallerServices.AddRefFile(ramlSourceFile, destFolderPath, targetFileName, props);

            ramlProjItem.ProjectItems.AddFromFile(refFilePath);

            ramlProjItem.Properties.Item("CustomTool").Value = string.Empty; // to cause a refresh when file already exists
            ramlProjItem.Properties.Item("CustomTool").Value = "RamlClientTool";
        }
        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);
            }
        }
        protected void AddContractFromFile(ProjectItem folderItem, string folderPath, RamlChooserActionParams parameters)
        {
            var includesFolderPath = folderPath + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName;

            var includesManager = new RamlIncludesManager();
            var result          = includesManager.Manage(parameters.RamlSource, includesFolderPath, confirmOverrite: true, rootRamlPath: folderPath + Path.DirectorySeparatorChar);

            ManageIncludes(folderItem, result);

            var ramlProjItem = AddOrUpdateRamlFile(result.ModifiedContents, folderItem, folderPath, parameters.TargetFileName);

            InstallerServices.RemoveSubItemsAndAssociatedFiles(ramlProjItem);

            var targetFolderPath = GetTargetFolderPath(folderPath, parameters.TargetFileName);

            RamlProperties props       = Map(parameters);
            var            refFilePath = InstallerServices.AddRefFile(parameters.RamlFilePath, targetFolderPath, parameters.TargetFileName, props);

            ramlProjItem.ProjectItems.AddFromFile(refFilePath);

            Scaffold(ramlProjItem.FileNames[0], parameters);
        }
Beispiel #18
0
        private void AddContractFromFile(ProjectItem folderItem, string folderPath, RamlChooserActionParams parameters)
        {
            var includesFolderPath = folderPath + Path.DirectorySeparatorChar + InstallerServices.IncludesFolderName;

            var includesManager = new RamlIncludesManager();
            var result          = includesManager.Manage(parameters.RamlSource, includesFolderPath, confirmOverrite: true, rootRamlPath: folderPath + Path.DirectorySeparatorChar);

            var includesFolderItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == InstallerServices.IncludesFolderName);

            if (includesFolderItem == null && !VisualStudioAutomationHelper.IsAVisualStudio2015Project(folderItem.ContainingProject))
            {
                includesFolderItem = folderItem.ProjectItems.AddFolder(InstallerServices.IncludesFolderName);
            }

            foreach (var file in result.IncludedFiles)
            {
                if (!VisualStudioAutomationHelper.IsAVisualStudio2015Project(folderItem.ContainingProject) || !File.Exists(file))
                {
                    includesFolderItem.ProjectItems.AddFromFile(file);
                }
            }

            //var existingIncludeItems = includesFolderItem.ProjectItems.Cast<ProjectItem>();
            //var oldIncludedFiles = existingIncludeItems.Where(item => !result.IncludedFiles.Contains(item.FileNames[0]));
            //InstallerServices.RemoveSubItemsAndAssociatedFiles(oldIncludedFiles);

            var ramlProjItem = AddOrUpdateRamlFile(result.ModifiedContents, folderItem, folderPath, parameters.TargetFileName);

            InstallerServices.RemoveSubItemsAndAssociatedFiles(ramlProjItem);

            var targetFolderPath = GetTargetFolderPath(folderPath, parameters.TargetFileName, folderItem.ContainingProject);

            RamlProperties props       = Map(parameters);
            var            refFilePath = InstallerServices.AddRefFile(parameters.RamlFilePath, targetFolderPath, parameters.TargetFileName, props);

            ramlProjItem.ProjectItems.AddFromFile(refFilePath);

            Scaffold(ramlProjItem.FileNames[0], parameters);
        }
Beispiel #19
0
        public void Install(ProjectWidePackage pwPkg)
        {
            if (pwPkg == null)
            {
                throw new ArgumentNullException("pwPkg");
            }

            pwPkg.OnPackagePreparing();

            var targetProj = pwPkg.TargetProject;
            var pkgId      = pwPkg.PackageId;
            var pkgVer     = pwPkg.PackageVersion;

            if (!InstallerServices.IsPackageInstalledEx(targetProj, pkgId, pkgVer))
            {
                if (InstallerServices.IsPackageInstalled(targetProj, pkgId))
                {
                    Uninstaller.UninstallPackage(targetProj, pkgId, false);
                }
                InstallPackage(pwPkg, targetProj, pkgId, pkgVer);
            }
        }
        protected void AddEmptyContract(ProjectItem folderItem, string folderPath, RamlChooserActionParams parameters)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            var newContractFile = Path.Combine(folderPath, parameters.TargetFileName);
            var contents        = CreateNewRamlContents(parameters.RamlTitle);

            ProjectItem ramlProjItem;

            if (File.Exists(newContractFile))
            {
                var dialogResult = InstallerServices.ShowConfirmationDialog(parameters.TargetFileName);
                if (dialogResult == MessageBoxResult.Yes)
                {
                    File.WriteAllText(newContractFile, contents);
                    ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
                }
                else
                {
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
                    ramlProjItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == newContractFile);
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
                    if (ramlProjItem == null)
                    {
                        ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
                    }
                }
            }
            else
            {
                File.WriteAllText(newContractFile, contents);
                ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
            }

            var props            = Map(parameters);
            var targetFolderPath = GetTargetFolderPath(folderPath, parameters.TargetFileName);
            var refFilePath      = InstallerServices.AddRefFile(newContractFile, targetFolderPath, parameters.TargetFileName, props);
            ramlProjItem.ProjectItems.AddFromFile(refFilePath);
        }
        protected void AddEmptyContract(ProjectItem folderItem, string folderPath, RamlChooserActionParams parameters)
        {
            var newContractFile = Path.Combine(folderPath, parameters.TargetFileName);
            var contents        = CreateNewRamlContents(parameters.RamlTitle);

            ProjectItem ramlProjItem;

            if (File.Exists(newContractFile))
            {
                var dialogResult = InstallerServices.ShowConfirmationDialog(parameters.TargetFileName);
                if (dialogResult == MessageBoxResult.Yes)
                {
                    File.WriteAllText(newContractFile, contents);
                    ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
                }
                else
                {
                    ramlProjItem = folderItem.ProjectItems.Cast <ProjectItem>().FirstOrDefault(i => i.Name == newContractFile);
                    if (ramlProjItem == null)
                    {
                        ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
                    }
                }
            }
            else
            {
                File.WriteAllText(newContractFile, contents);
                ramlProjItem = folderItem.ProjectItems.AddFromFile(newContractFile);
            }

            var props            = Map(parameters);
            var targetFolderPath = GetTargetFolderPath(folderPath, parameters.TargetFileName);
            var refFilePath      = InstallerServices.AddRefFile(newContractFile, targetFolderPath, parameters.TargetFileName, props);

            ramlProjItem.ProjectItems.AddFromFile(refFilePath);
        }
Beispiel #22
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 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);
        }
        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);
        }