Example #1
0
        public ActionResult Get([FromServices] IProjectGenerator generator, [FromBody] ProjectMetadata metadata)
        {
            if (metadata.Tags != null)
            {
                foreach (var kvp in metadata.Tags.Where(x => x.Value is JsonElement).ToList())
                {
                    var jsonElem = (JsonElement)kvp.Value;
                    if (jsonElem.ValueKind == JsonValueKind.True)
                    {
                        metadata.Tags[kvp.Key] = true;
                    }
                    else if (jsonElem.ValueKind == JsonValueKind.False)
                    {
                        metadata.Tags[kvp.Key] = false;
                    }
                    else
                    {
                        metadata.Tags[kvp.Key] = jsonElem.ToString();
                    }
                }
            }

            try
            {
                byte[] templateBytes = generator.Generate(metadata);
                return(new FileContentResult(templateBytes, "application/zip")
                {
                    FileDownloadName = $"{metadata.ProjectName}.zip"
                });
            }
            catch (TemplateException ex)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Content(ex.Message));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Content(ex.Message));
            }
        }
Example #2
0
        public byte[] Generate(ProjectMetadata metadata)
        {
            var renderer = _renderers.FirstOrDefault(x => string.Compare(x.TemplateType, metadata.TemplateType ?? MustacheRenderer.TEMPLATE_TYPE, true) == 0);

            var templateSource = _templateSources.FirstOrDefault(x => string.Equals(x.SourceType, metadata.TemplateSourceType, StringComparison.InvariantCultureIgnoreCase));

            if (templateSource == null)
            {
                throw new Exception($"Template source '{metadata.TemplateSourceType}' is not found");
            }

            var filesToExclude = string.Join(",", metadata.FilesToExclude, TemplateMetadata.FILE_NAME)
                                 .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
                                 .Trim(',')
                                 .Split(",");

            var files = templateSource
                        .GetFiles(metadata.TemplateSourceUrl, metadata.TemplateSourceDirectory, metadata.TemplateSourceBranch)
                        .Where(x => filesToExclude == null || !MatchFileName(x.Name, filesToExclude))
                        .ToList();

            return(Zip(renderer.Render(files, metadata.Tags, metadata.TagRegexes)));
        }