public override void AddContract(RamlChooserActionParams parameters) { var dte = ServiceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); InstallNugetDependencies(proj, newtonsoftJsonForCorePackageVersion); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ContractsFolderName); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar; var targetFolderPath = GetTargetFolderPath(contractsFolderPath, parameters.TargetFileName); if (!Directory.Exists(targetFolderPath)) { Directory.CreateDirectory(targetFolderPath); } if (string.IsNullOrWhiteSpace(parameters.RamlSource) && !string.IsNullOrWhiteSpace(parameters.RamlTitle)) { AddEmptyContract(folderItem, contractsFolderPath, parameters); } else { AddContractFromFile(folderItem, contractsFolderPath, parameters); } }
public void ScaffoldToProject(RamlChooserActionParams parameters, Project proj) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); var data = parameters.Data; if (data == null || data.RamlDocument == null) { return; } var model = new WebApiGeneratorService(data.RamlDocument, parameters.ControllersNamespace, parameters.ModelsNamespace).BuildModel(); var unitTestsFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, UnitTestsFolderName); var unitTestsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + UnitTestsFolderName + Path.DirectorySeparatorChar; var templates = new[] { UnitTestsControllerTemplateName, UnitTestsControllerImplementationTemplateName, ModelTemplateName, EnumTemplateName }; if (!templatesManager.ConfirmWhenIncompatibleServerTemplate(unitTestsFolderPath, templates)) { return; } var extensionPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar; AddOrUpdateUnitTestsControllerBase(parameters, unitTestsFolderPath, model, unitTestsFolderItem, extensionPath); AddOrUpdateUnitTestsControllerImplementations(parameters, unitTestsFolderPath, proj, model, unitTestsFolderItem, extensionPath); }
private void AddOrUpdateControllerImplementations(RamlChooserActionParams parameters, string contractsFolderPath, Project proj, WebApiGeneratorModel model, ProjectItem folderItem, string extensionPath) { templatesManager.CopyServerTemplateToProjectFolder(contractsFolderPath, ControllerImplementationTemplateName, Settings.Default.ControllerImplementationTemplateTitle, TemplateSubFolder); var controllersFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, "Controllers"); var templatesFolder = Path.Combine(contractsFolderPath, "Templates"); var controllerImplementationTemplateParams = new TemplateParams <ControllerObject>( Path.Combine(templatesFolder, ControllerImplementationTemplateName), controllersFolderItem, "controllerObject", model.Controllers, contractsFolderPath, folderItem, extensionPath, parameters.TargetNamespace, "Controller", false, GetVersionPrefix(parameters.IncludeApiVersionInRoutePrefix, model.ApiVersion)) { TargetFolder = TargetFolderResolver.GetImplementationControllersFolderPath(proj, parameters.ImplementationControllersFolder), RelativeFolder = parameters.ImplementationControllersFolder, Title = Settings.Default.ControllerImplementationTemplateTitle, IncludeHasModels = true, HasModels = model.Objects.Any(o => o.IsScalar == false) || model.Enums.Any(), UseAsyncMethods = parameters.UseAsyncMethods, IncludeApiVersionInRoutePrefix = parameters.IncludeApiVersionInRoutePrefix, ApiVersion = model.ApiVersion }; codeGenerator.GenerateCodeFromTemplate(controllerImplementationTemplateParams); }
public void AddContract(RamlChooserActionParams parameters) { var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); InstallNugetDependencies(proj); if (VisualStudioAutomationHelper.IsAVisualStudio2015Project(proj)) { AddXmlFormatterInWebApiConfig(proj); } var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, contractsFolderName); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + contractsFolderName + Path.DirectorySeparatorChar; var targetFolderPath = GetTargetFolderPath(contractsFolderPath, parameters.TargetFileName, proj); if (!Directory.Exists(targetFolderPath)) { Directory.CreateDirectory(targetFolderPath); } if (string.IsNullOrWhiteSpace(parameters.RamlSource) && !string.IsNullOrWhiteSpace(parameters.RamlTitle)) { AddEmptyContract(folderItem, contractsFolderPath, parameters); } else { AddContractFromFile(folderItem, contractsFolderPath, parameters); } }
public override void AddContract(RamlChooserActionParams parameters) { Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); Tracking.Track("Asp.Net WebApi Scaffold"); var dte = ServiceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); InstallDependencies(proj, newtonsoftJsonPackageVersion); AddXmlFormatterInWebApiConfig(proj); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ContractsFolderName); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar; var targetFolderPath = GetTargetFolderPath(contractsFolderPath, parameters.TargetFileName); if (!Directory.Exists(targetFolderPath)) { Directory.CreateDirectory(targetFolderPath); } if (string.IsNullOrWhiteSpace(parameters.RamlSource) && !string.IsNullOrWhiteSpace(parameters.RamlTitle)) { AddEmptyContract(folderItem, contractsFolderPath, parameters); } else { AddContractFromFile(folderItem, contractsFolderPath, parameters); } }
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"; }
public void Scaffold(string ramlSource, RamlChooserActionParams parameters) { var data = RamlScaffolderHelper.GetRamlData(ramlSource, parameters.TargetNamespace); if (data == null || data.Model == null) { return; } var model = data.Model; var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); var contractsFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, contractsFolderName); var ramlItem = contractsFolderItem.ProjectItems.Cast <ProjectItem>() .First(i => i.Name.ToLowerInvariant() == parameters.TargetFileName.ToLowerInvariant()); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + contractsFolderName + Path.DirectorySeparatorChar; if (VisualStudioAutomationHelper.IsAVisualStudio2015Project(proj)) { templateSubFolder = "AspNet5"; } else { templateSubFolder = "RAMLWebApi2Scaffolder"; } var templates = new[] { ControllerBaseTemplateName, ControllerInterfaceTemplateName, ControllerImplementationTemplateName, ModelTemplateName, EnumTemplateName }; if (!templatesManager.ConfirmWhenIncompatibleServerTemplate(contractsFolderPath, templates)) { return; } var extensionPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar; AddOrUpdateModels(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateEnums(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerBase(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerInterfaces(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerImplementations(parameters, contractsFolderPath, proj, model, contractsFolderItem, extensionPath); }
private static ProjectItem CreateFolderItem(ProjectItem projItem, IReadOnlyList <string> folders, int index) { var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(projItem, folders[index]); index++; if (index >= folders.Count) { return(folderItem); } return(CreateFolderItem(folderItem, folders, index)); }
public void AddContract(RamlChooserActionParams parameters) { var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, folderName); var generatedFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + folderName + Path.DirectorySeparatorChar; if (string.IsNullOrWhiteSpace(parameters.RamlSource) && !string.IsNullOrWhiteSpace(parameters.RamlTitle)) { AddEmptyContract(parameters.TargetFileName, parameters.RamlTitle, folderItem, generatedFolderPath, parameters.TargetNamespace, parameters.TargetFileName); } else { AddContractFromFile(parameters.RamlFilePath, parameters.TargetNamespace, parameters.RamlSource, parameters.DoNotScaffold, folderItem, generatedFolderPath, parameters.TargetFileName); } }
private static ProjectItem CreateFolderItem(Project proj, string path, string relativeFolder) { if (ContainsSubFolders(relativeFolder)) { var folders = relativeFolder.Split(PossibleDirectorySeparatorChars); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, folders[0]); return(CreateFolderItem(folderItem, folders, 1)); } if (VisualStudioAutomationHelper.IsAVisualStudio2015Project(proj)) { return(null); } var folderName = path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1); return(VisualStudioAutomationHelper.AddFolderIfNotExists(proj, folderName)); }
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"; }
public void Scaffold(string ramlSource, string targetNamespace, string ramlFileName) { var data = RamlScaffolderHelper.GetRamlData(ramlSource, targetNamespace); if (data == null || data.Model == null) { return; } var model = data.Model; var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, folderName); var ramlItem = folderItem.ProjectItems.Cast <ProjectItem>().First(i => i.Name == ramlFileName); var generatedFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + folderName + Path.DirectorySeparatorChar; var vsixPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar; var templatesPath = vsixPath + "Templates" + Path.DirectorySeparatorChar + "RAMLWebApi2Scaffolder" + Path.DirectorySeparatorChar; // Add / Update model objects ReplaceTemplateValues(templatesPath, vsixPath, ObjectTemplateName + ".t4", targetNamespace); GenerateCodeFromTemplate(Path.Combine(templatesPath, ObjectTemplateName + ".cs.t4"), ramlItem, "apiObject", model.Objects.Values, generatedFolderPath, folderItem); // Add / Update controllers definition ReplaceTemplateValues(templatesPath, vsixPath, ControllerDeclarationTemplateName + ".t4", targetNamespace); GenerateCodeFromTemplate(Path.Combine(templatesPath, ControllerDeclarationTemplateName + ".cs.t4"), ramlItem, "controllerObject", model.Controllers, generatedFolderPath, folderItem, "Controller"); // Add / Update controllers interface ReplaceTemplateValues(templatesPath, vsixPath, ControllerInterfaceTemplateName + ".t4", targetNamespace); GenerateCodeFromTemplate(Path.Combine(templatesPath, ControllerInterfaceTemplateName + ".cs.t4"), ramlItem, "controllerObject", model.Controllers, generatedFolderPath, folderItem, "Controller", true, "I"); // Add controllers implementation var controllersFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, "Controllers"); var controllersFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + "Controllers" + Path.DirectorySeparatorChar; ReplaceTemplateValues(templatesPath, vsixPath, ControllerImplementationTemplateName + ".t4", targetNamespace); GenerateCodeFromTemplate(Path.Combine(templatesPath, ControllerImplementationTemplateName + ".cs.t4"), controllersFolderItem, "controllerObject", model.Controllers, controllersFolderPath, folderItem, "Controller", false); }
private void AddOrUpdateControllerImplementations(string targetNamespace, string generatedFolderPath, Project proj, WebApiGeneratorModel model, ProjectItem folderItem, string extensionPath, bool useAsyncMethods) { templatesManager.CopyServerTemplateToProjectFolder(generatedFolderPath, ControllerImplementationTemplateName, Settings.Default.ControllerImplementationTemplateTitle); var controllersFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, "Controllers"); var controllersFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + "Controllers" + Path.DirectorySeparatorChar; var templatesFolder = Path.Combine(generatedFolderPath, "Templates"); var controllerImplementationTemplateParams = new TemplateParams <ControllerObject>(Path.Combine(templatesFolder, ControllerImplementationTemplateName), controllersFolderItem, "controllerObject", model.Controllers, controllersFolderPath, folderItem, extensionPath, targetNamespace, "Controller", false); controllerImplementationTemplateParams.Title = Settings.Default.ControllerImplementationTemplateTitle; controllerImplementationTemplateParams.IncludeHasModels = true; controllerImplementationTemplateParams.HasModels = model.Objects.Any() || model.Enums.Any(); controllerImplementationTemplateParams.UseAsyncMethods = useAsyncMethods; GenerateCodeFromTemplate(controllerImplementationTemplateParams); }
public override void AddTests(RamlChooserActionParams parameters) { Tracking.Track("Unit Tests Scaffold (Asp.Net Core)"); var dte = ServiceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); InstallDependencies(proj); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, UnitTestsFolderName); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + UnitTestsFolderName + Path.DirectorySeparatorChar; var targetFolderPath = GetTargetFolderPath(contractsFolderPath, parameters.TargetFileName); if (!Directory.Exists(targetFolderPath)) { Directory.CreateDirectory(targetFolderPath); } AddUnitTests(folderItem, contractsFolderPath, parameters); }
public void AddContract(RamlChooserActionParams parameters) { var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); InstallNugetDependencies(proj); AddXmlFormatterInWebApiConfig(proj); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ContractsFolderName); var generatedFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar; if (string.IsNullOrWhiteSpace(parameters.RamlSource) && !string.IsNullOrWhiteSpace(parameters.RamlTitle)) { AddEmptyContract(parameters.TargetFileName, parameters.RamlTitle, folderItem, generatedFolderPath, parameters.TargetNamespace, parameters.TargetFileName, parameters.UseAsyncMethods); } else { AddContractFromFile(parameters.RamlFilePath, parameters.TargetNamespace, parameters.RamlSource, folderItem, generatedFolderPath, parameters.TargetFileName, parameters.UseAsyncMethods); } }
public void Scaffold(string ramlSource, string targetNamespace, string ramlFileName, bool useAsyncMethods) { var data = RamlScaffolderHelper.GetRamlData(ramlSource, targetNamespace); if (data == null || data.Model == null) { return; } var model = data.Model; var dte = serviceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); var folderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ContractsFolderName); var ramlItem = folderItem.ProjectItems.Cast <ProjectItem>().First(i => i.Name.ToLowerInvariant() == ramlFileName.ToLowerInvariant()); var generatedFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar; if (!templatesManager.ConfirmWhenIncompatibleServerTemplate(generatedFolderPath, new[] { ControllerBaseTemplateName, ControllerInterfaceTemplateName, ControllerImplementationTemplateName, ModelTemplateName, EnumTemplateName })) { return; } var extensionPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar; AddOrUpdateModels(targetNamespace, generatedFolderPath, ramlItem, model, folderItem, extensionPath); AddOrUpdateEnums(targetNamespace, generatedFolderPath, ramlItem, model, folderItem, extensionPath); AddOrUpdateControllerBase(targetNamespace, generatedFolderPath, ramlItem, model, folderItem, extensionPath, useAsyncMethods); AddOrUpdateControllerInterfaces(targetNamespace, generatedFolderPath, ramlItem, model, folderItem, extensionPath, useAsyncMethods); AddOrUpdateControllerImplementations(targetNamespace, generatedFolderPath, proj, model, folderItem, extensionPath, useAsyncMethods); }
public void Scaffold(string ramlSource, RamlChooserActionParams parameters) { var data = parameters.Data; if (data == null || data.RamlDocument == null) { return; } var model = new WebApiGeneratorService(data.RamlDocument, parameters.ControllersNamespace, parameters.ModelsNamespace).BuildModel(); var dte = ServiceProvider.GetService(typeof(SDTE)) as DTE; var proj = VisualStudioAutomationHelper.GetActiveProject(dte); var contractsFolderItem = VisualStudioAutomationHelper.AddFolderIfNotExists(proj, ContractsFolderName); var ramlItem = contractsFolderItem.ProjectItems.Cast <ProjectItem>() .First(i => i.Name.ToLowerInvariant() == parameters.TargetFileName.ToLowerInvariant()); var contractsFolderPath = Path.GetDirectoryName(proj.FullName) + Path.DirectorySeparatorChar + ContractsFolderName + Path.DirectorySeparatorChar; var templates = new[] { ControllerBaseTemplateName, ControllerInterfaceTemplateName, ControllerImplementationTemplateName, ModelTemplateName, EnumTemplateName }; if (!templatesManager.ConfirmWhenIncompatibleServerTemplate(contractsFolderPath, templates)) { return; } var extensionPath = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar; AddOrUpdateModels(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateEnums(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerBase(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerInterfaces(parameters, contractsFolderPath, ramlItem, model, contractsFolderItem, extensionPath); AddOrUpdateControllerImplementations(parameters, contractsFolderPath, proj, model, contractsFolderItem, extensionPath); AddJsonSchemaParsingErrors(model.Warnings, contractsFolderPath, contractsFolderItem, ramlItem); if (parameters.GenerateUnitTests.HasValue && parameters.GenerateUnitTests.Value) { var testsProj = VisualStudioAutomationHelper.GetProject(dte, parameters.TestsProjectName); parameters.TestsNamespace = VisualStudioAutomationHelper.GetDefaultNamespace(testsProj) + "." + NetNamingMapper.GetObjectName(Path.GetFileNameWithoutExtension(parameters.RamlFilePath)); var unitTestsScaffoldService = UnitTestsScaffoldServiceBase.GetScaffoldService(Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider); unitTestsScaffoldService.InstallDependencies(testsProj); unitTestsScaffoldService.ScaffoldToProject(parameters, testsProj); } }