Example #1
0
        private void AddGeneratedMethod(Resource resource, string url, string objectName, Method method, ICollection <string> methodsNames,
                                        ICollection <ClientGeneratorMethod> generatorMethods, IDictionary <string, Parameter> parentUriParameters)
        {
            var generatedMethod = BuildClassMethod(url, method, resource);

            if (generatedMethod.ReturnType != "string")
            {
                var returnType = CollectionTypeHelper.GetBaseType(generatedMethod.ReturnType);

                var returnTypeObject = schemaObjects.Values.Any(o => o.Name == returnType)
                    ? schemaObjects.Values.First(o => o.Name == returnType)
                    : schemaResponseObjects.Values.FirstOrDefault(o => o.Name == returnType);

                if (returnTypeObject != null)
                {
                    generatedMethod.ReturnTypeObject = returnTypeObject;
                    generatedMethod.OkReturnType     = GetOkReturnType(generatedMethod);
                }
            }
            uriParametersGenerator.Generate(resource, url, generatedMethod, uriParameterObjects, parentUriParameters);

            if (!IsVerbForMethod(method))
            {
                return;
            }

            if (methodsNames.Contains(generatedMethod.Name))
            {
                generatedMethod.Name = GetUniqueName(methodsNames, generatedMethod.Name, resource.RelativeUri);
            }

            GetQueryParameters(objectName, method, generatedMethod);

            GetHeaders(objectName, method, generatedMethod);

            GetResponseHeaders(objectName, generatedMethod, method);

            generatorMethods.Add(generatedMethod);
            methodsNames.Add(generatedMethod.Name);
        }
        private void AddOrUpdateModels(RamlChooserActionParams parameters, string contractsFolderPath, ProjectItem ramlItem, WebApiGeneratorModel model, ProjectItem contractsFolderItem, string extensionPath)
        {
            templatesManager.CopyServerTemplateToProjectFolder(contractsFolderPath, ModelTemplateName,
                                                               Settings.Default.ModelsTemplateTitle, TemplateSubFolder);
            var templatesFolder = Path.Combine(contractsFolderPath, "Templates");

            var models = model.Objects;

            // when is an XML model, skip empty objects
            if (model.Objects.Any(o => !string.IsNullOrWhiteSpace(o.GeneratedCode)))
            {
                models = model.Objects.Where(o => o.Properties.Any() || !string.IsNullOrWhiteSpace(o.GeneratedCode));
            }

            // when array has no properties, set it collection on base type
            foreach (var arrayModel in models.Where(o => o.IsArray && o.Properties.Count == 0 && o.Type != null &&
                                                    CollectionTypeHelper.IsCollection(o.Type) && !NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(o.Type))))
            {
                arrayModel.BaseClass = arrayModel.Type.Substring(1); // remove the initil "I" to make it a concrete class
            }
            // skip array of primitives
            models = models.Where(o => o.Type == null || !(CollectionTypeHelper.IsCollection(o.Type) &&
                                                           NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(o.Type))));
            models = models.Where(o => !o.IsScalar); // skip scalar types

            var targetFolderPath = GetTargetFolderPath(contractsFolderPath, ramlItem.FileNames[0]);

            var apiObjectTemplateParams = new TemplateParams <ApiObject>(
                Path.Combine(templatesFolder, ModelTemplateName), ramlItem, "apiObject", models,
                contractsFolderPath, contractsFolderItem, extensionPath, parameters.ControllersNamespace,
                GetVersionPrefix(parameters.IncludeApiVersionInRoutePrefix, model.ApiVersion) +
                (parameters.AddGeneratedSuffixToFiles ? ".generated" : string.Empty))
            {
                Title          = Settings.Default.ModelsTemplateTitle,
                RelativeFolder = parameters.ModelsFolder,
                TargetFolder   = TargetFolderResolver.GetModelsTargetFolder(ramlItem.ContainingProject,
                                                                            targetFolderPath, parameters.ModelsFolder),
                ModelsNamespace = parameters.ModelsNamespace
            };

            codeGenerator.GenerateCodeFromTemplate(apiObjectTemplateParams);
        }
Example #3
0
        public void GenerateCode(RamlInfo data, string targetNamespace, string clientRootClassName, string ramlDestFile, string destFolderPath,
                                 string destFolderName, ProjectItem ramlProjItem)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            //var ramlInfo = await RamlInfoService.GetRamlInfo(ramlDestFile);
            //if (ramlInfo.HasErrors)
            //{
            //    ActivityLog.LogError(VisualStudioAutomationHelper.RamlVsToolsActivityLogSource, ramlInfo.ErrorMessage);
            //    MessageBox.Show(ramlInfo.ErrorMessage);
            //    return;
            //}

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

            var templateFilePath = Path.Combine(templateFolder, ClientT4TemplateName);
            var extensionPath    = Path.GetDirectoryName(GetType().Assembly.Location) + Path.DirectorySeparatorChar;

            // when array has no properties, set it collection on base type
            foreach (var arrayModel in model.Objects.Where(o => o.IsArray && o.Properties.Count == 0 && o.Type != null &&
                                                           CollectionTypeHelper.IsCollection(o.Type) && !NewNetTypeMapper.IsPrimitiveType(CollectionTypeHelper.GetBaseType(o.Type))))
            {
                arrayModel.BaseClass = arrayModel.Type.Substring(1); // remove the initil "I" to make it a concrete class
            }

            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);
            ramlProjItem.ProjectItems.AddFromFile(csTargetFile);
        }