From 430841be028b7b762250e5260e965d02c902b15c Mon Sep 17 00:00:00 2001 From: Chris Cheetham Date: Thu, 26 Mar 2020 07:15:14 -0400 Subject: [PATCH] dn not assume a particular profile --- src/Steeltoe.Tooling/Models/ProjectBuilder.cs | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Steeltoe.Tooling/Models/ProjectBuilder.cs b/src/Steeltoe.Tooling/Models/ProjectBuilder.cs index 1164698..084f68a 100644 --- a/src/Steeltoe.Tooling/Models/ProjectBuilder.cs +++ b/src/Steeltoe.Tooling/Models/ProjectBuilder.cs @@ -28,18 +28,12 @@ namespace Steeltoe.Tooling.Models public class ProjectBuilder { private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger(); - private static readonly List DefaultProtocols = new List(); private Context _context; private readonly string _projectFile; private readonly string _launchSettingsFile; private XmlDocument _projectDoc; - static ProjectBuilder() - { - DefaultProtocols.Add(new Protocol("http", 8080)); - } - /// /// Project file. /// @@ -108,35 +102,40 @@ namespace Steeltoe.Tooling.Models private List GetProtocols() { - if (!File.Exists(_launchSettingsFile)) + var protocols = new List(); + if (File.Exists(_launchSettingsFile)) { - return DefaultProtocols; + Logger.LogDebug($"loading launch settings: {_launchSettingsFile}"); + var yaml = new YamlStream(); + using (var reader = new StreamReader(_launchSettingsFile)) + { + yaml.Load(reader); + } + + var root = (YamlMappingNode) yaml.Documents[0].RootNode; + var profiles = (YamlMappingNode) root.Children[new YamlScalarNode("profiles")]; + foreach (var entry in profiles.Children) + { + var profile = (YamlMappingNode) entry.Value; + if (profile.Children.ContainsKey(new YamlScalarNode("applicationUrl"))) + { + var urlSpec = profile.Children[new YamlScalarNode("applicationUrl")]; + var urls = $"{urlSpec}".Split(';'); + foreach (var url in urls) + { + var uri = new Uri(url); + protocols.Add(new Protocol(uri.Scheme, uri.Port)); + } + } + } } - Logger.LogDebug($"loading launch settings: {_launchSettingsFile}"); - var yaml = new YamlStream(); - using (var reader = new StreamReader(_launchSettingsFile)) + // ensure a default protocol + if (protocols.Count == 0) { - yaml.Load(reader); + protocols.Add(new Protocol("http", 8080)); } - var root = (YamlMappingNode) yaml.Documents[0].RootNode; - var profiles = (YamlMappingNode) root.Children[new YamlScalarNode("profiles")]; - var profile = - (YamlMappingNode) profiles.Children[new YamlScalarNode(Path.GetFileNameWithoutExtension(_projectFile))]; - if (!profile.Children.ContainsKey(new YamlScalarNode("applicationUrl"))) - { - return DefaultProtocols; - } - - var urlSpec = profile.Children[new YamlScalarNode("applicationUrl")]; - var urls = $"{urlSpec}".Split(';'); - if (urls.Length == 0) - { - return DefaultProtocols; - } - - var protocols = urls.Select(url => new Uri(url)).Select(uri => new Protocol(uri.Scheme, uri.Port)).ToList(); protocols.Sort(); return protocols; }