Esempio n. 1
0
        public IActionResult GetHelp()
        {
            var help = new List <string>();

            if (!(_options?.Logo is null))
            {
                try
                {
                    help.Add(string.Empty);
                    var logoPath = _options.Logo;
                    System.IO.File.ReadAllLines(logoPath).ToList().ForEach(l => help.Add(l));
                    help.Add(string.Empty);
                }
                catch (Exception e)
                {
                    help.Add($"!!! failed to load logo: {e.Message}");
                    help.Add(string.Empty);
                }
            }

            help.Add(" :: Steeltoe Initializr ::  https://start.steeltoe.io");
            help.Add(string.Empty);
            var metadata = _configService.GetInitializrConfig().ProjectMetadata;

            help.Add("This service generates quickstart projects that can be easily customized.");
            help.Add("Possible customizations include a project's dependencies and .NET target framework.");
            help.Add(string.Empty);
            help.Add("The URI templates take a set of parameters to customize the result of a request.");
            var table = new List <List <string> >
            {
                new List <string> {
                    "Parameter", "Description", "Default value"
                },
                new List <string> {
                    "name", "project name", metadata.Name.Default
                },
                new List <string> {
                    "applicationName", "application name", metadata.ApplicationName.Default
                },
                new List <string> {
                    "namespace", "namespace", metadata.Namespace.Default
                },
                new List <string> {
                    "description", "project description", metadata.Description.Default
                },
                new List <string> {
                    "steeltoeVersion", "Steeltoe version", metadata.SteeltoeVersion.Default
                },
                new List <string> {
                    "dotNetFramework", "target .NET framework", metadata.DotNetFramework.Default
                },
                new List <string> {
                    "dotNetTemplate", ".NET template", metadata.DotNetTemplate.Default
                },
                new List <string> {
                    "language", "programming language", metadata.Language.Default
                },
                new List <string> {
                    "packaging", "project packaging", metadata.Packaging.Default
                },
            };

            help.AddRange(ToTable(table));
            help.Add(string.Empty);
            help.Add("The following section has a list of supported identifiers for the comma-separated");
            help.Add("list of \"dependencies\".");
            table = new List <List <string> >
            {
                new List <string> {
                    "Id", "Description", "Required Steeltoe version"
                },
            };
            foreach (var group in metadata.Dependencies.Values)
            {
                foreach (var dependency in group.Values)
                {
                    table.Add(new List <string>
                    {
                        dependency.Id,
                        dependency.Description,
                        new ReleaseRange(dependency.SteeltoeVersionRange).ToPrettyString(),
                    });
                }
            }

            help.AddRange(ToTable(table));
            help.Add(string.Empty);
            help.Add("Examples:");
            help.Add(string.Empty);
            help.Add("To create a default project:");
            help.Add("\t$ http https://start.steeltoe.io/api/project -d");
            help.Add(string.Empty);
            help.Add("To create a project targeting Steeltoe 2.5.1 and netcoreapp2.1:");
            help.Add(
                "\t$ http https://start.steeltoe.io/api/project steeltoeVersion==2.5.1 dotNetFramework==netcoreapp2.1 -d");
            help.Add(string.Empty);
            help.Add("To create a project with a actuator endpoints and a Redis backend:");
            help.Add("\t$ http https://start.steeltoe.io/api/project dependencies==actuators,redis -d");

            return(Ok(string.Join(NewLine, help)));
        }
Esempio n. 2
0
 public IActionResult GetInitializrConfiguration()
 {
     return(Ok(_configService.GetInitializrConfig()));
 }
        public ActionResult GetProjectArchive([FromQuery] ProjectSpec spec)
        {
            var config         = _configService.GetInitializrConfig();
            var defaults       = config.ProjectMetadata;
            var normalizedSpec = new ProjectSpec()
            {
                Name            = spec.Name ?? defaults?.Name?.Default,
                Description     = spec.Description ?? defaults?.Description?.Default,
                Namespace       = spec.Namespace ?? defaults?.Namespace?.Default,
                SteeltoeVersion = spec.SteeltoeVersion ?? defaults?.SteeltoeVersion?.Default,
                DotNetFramework = spec.DotNetFramework ?? defaults?.DotNetFramework?.Default,
                DotNetTemplate  = spec.DotNetTemplate ?? defaults?.DotNetTemplate?.Default,
                Language        = spec.Language ?? defaults?.Language?.Default,
                Packaging       = spec.Packaging ?? defaults?.Packaging?.Default,
                Dependencies    = spec.Dependencies ?? defaults?.Dependencies?.Default,
            };

            if (normalizedSpec.Packaging is null)
            {
                return(StatusCode(
                           StatusCodes.Status500InternalServerError,
                           "Default packaging not configured."));
            }

            var archiver = _archiverRegistry.Lookup(normalizedSpec.Packaging);

            if (archiver is null)
            {
                return(NotFound($"Packaging '{normalizedSpec.Packaging}' not found."));
            }

            if (normalizedSpec.Dependencies != null)
            {
                var caseSensitiveDeps = new List <string>();
                if (config.ProjectMetadata?.Dependencies?.Values != null)
                {
                    foreach (var group in config.ProjectMetadata.Dependencies.Values)
                    {
                        foreach (var dep in group.Values)
                        {
                            caseSensitiveDeps.Add(dep.Id);
                        }
                    }
                }

                var deps = normalizedSpec.Dependencies.Split(',');
                for (int i = 0; i < deps.Length; ++i)
                {
                    var found = false;
                    foreach (var caseSensitiveDep in caseSensitiveDeps)
                    {
                        if (caseSensitiveDep.Equals(deps[i], StringComparison.OrdinalIgnoreCase))
                        {
                            deps[i] = caseSensitiveDep;
                            found   = true;
                        }
                    }

                    if (!found)
                    {
                        return(NotFound($"Dependency '{deps[i]}' not found."));
                    }
                }

                normalizedSpec.Dependencies = string.Join(',', deps);
            }

            Logger.LogDebug("Project specification: {ProjectSpec}", normalizedSpec);
            var project = _projectGenerator.GenerateProject(normalizedSpec);

            if (project is null)
            {
                return(NotFound($"No project template for spec: {normalizedSpec}"));
            }

            var archiveBytes = archiver.ToBytes(project.FileEntries);

            return(File(
                       archiveBytes,
                       $"application/{normalizedSpec.Packaging}",
                       $"{normalizedSpec.Name}{archiver.GetFileExtension()}"));
        }