This commit is contained in:
Chris Cheetham 2020-03-25 17:55:50 -04:00
Родитель bd7755410b
Коммит 19d5d4745e
9 изменённых файлов: 84 добавлений и 72 удалений

Просмотреть файл

@ -13,6 +13,8 @@
// limitations under the License.
using System;
using System.IO;
using System.Reflection;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using Steeltoe.Tooling;
@ -36,11 +38,16 @@ namespace Steeltoe.Cli
try
{
Logger.LogDebug($"working directory: {app.WorkingDirectory}");
var context = new Context(
app.WorkingDirectory,
_console.Out,
new CommandShell()
);
var context = new Context
{
WorkingDirectory = app.WorkingDirectory,
Console = Console.Out,
Shell = new CommandShell(),
Registry = new Registry()
};
context.Registry.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"steeltoe.rc",
"registry"));
GetController().Execute(context);
return 0;
}

Просмотреть файл

@ -32,13 +32,16 @@ See Also:
{
public const string CommandName = "run";
[Option("-g|--generate-only", Description = "Only generate configuration files (don't run in Docker).")]
private bool GenerateOnly { get; }
public RunCommand(IConsole console) : base(console)
{
}
protected override Controller GetController()
{
return new RunController();
return new RunController(!GenerateOnly);
}
}
}

Просмотреть файл

@ -24,29 +24,21 @@ namespace Steeltoe.Tooling
/// <summary>
/// Steeltoe Tooling working directory.
/// </summary>
public string WorkingDirectory { get; }
public string WorkingDirectory { get; set; }
/// <summary>
/// Steeltoe Tooling console. Typically the console is used to display messages to the user.
/// </summary>
public TextWriter Console { get; }
public TextWriter Console { get; set; }
/// <summary>
/// Shell which which to run system commands.
/// </summary>
public Shell Shell { get; }
public Shell Shell { get; set; }
/// <summary>
/// Creates a new Steeltoe Tooling Content.
/// Tooling registry.
/// </summary>
/// <param name="dir">Project directory.</param>
/// <param name="console">User console.</param>
/// <param name="shell">Command shell.</param>
public Context(string dir, TextWriter console, Shell shell)
{
WorkingDirectory = dir;
Console = console;
Shell = shell;
}
public Registry Registry { get; set; }
}
}

Просмотреть файл

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System.ComponentModel.DataAnnotations;
using System.IO;
using Microsoft.Extensions.Logging;
using Steeltoe.Tooling.Models;
@ -26,42 +27,41 @@ namespace Steeltoe.Tooling.Controllers
{
private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger<RunController>();
private readonly bool _runInDocker;
/// <summary>
/// Runs the project in the local Docker environment.
/// </summary>
/// <param name="runInDocker"></param>
public RunController(bool runInDocker)
{
_runInDocker = runInDocker;
}
/// <summary>
/// Runs the project in the local Docker environment.
/// </summary>
protected override void Execute()
{
var project = GetProject();
var images = new Images(project);
string[] files = new string[] {"Dockerfile", "docker-compose.yml"};
if (!Context.Registry.Images.TryGetValue(project.Framework, out var image))
{
throw new ToolingException($"no image for framework: {project.Framework}");
}
var files = new string[] {"Dockerfile", "docker-compose.yml"};
foreach (var file in files)
{
Logger.LogDebug($"writing {file}");
var template = TemplateManager.GetTemplate($"{file}.st");
template.Bind("project", project);
template.Bind("images", images);
template.Bind("image", image);
File.WriteAllText(file, template.Render());
}
if (!_runInDocker) return;
var cli = new Cli("docker-compose", Context.Shell);
cli.Run("up --build", $"running '{project.Name}' in Docker");
}
public class Images
{
public string DotnetSdk { get; }
public Images(Project project)
{
if (project.Framework.Equals("netcoreapp3.1"))
{
DotnetSdk = "mcr.microsoft.com/dotnet/core/sdk:3.1";
}
else
{
throw new ToolingException($"no Docker image available for framework: {project.Framework}");
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Logging;
using Steeltoe.Tooling.Controllers;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NodeDeserializers;
namespace Steeltoe.Tooling
{
/// <summary>
/// Tooling registry.
/// </summary>
public class Registry
{
private static readonly ILogger Logger = Logging.LoggerFactory.CreateLogger<Registry>();
public Dictionary<string, string> Images { get; private set; }
/// <summary>
/// Load the registry from the specified directory.
/// </summary>
/// <param name="directory"></param>
public void Load(string directory)
{
Logger.LogDebug($"loading registry: {directory}");
var deserializer = new DeserializerBuilder().Build();
using (var reader = new StreamReader(Path.Join(directory, "images.yml")))
{
Images = deserializer.Deserialize<Dictionary<string, string>>(reader);
}
}
}
}

Просмотреть файл

@ -0,0 +1 @@
netcoreapp3.1: mcr.microsoft.com/dotnet/core/sdk:3.1

Просмотреть файл

@ -1,4 +1,4 @@
FROM <images.DotnetSdk>
FROM <image>
WORKDIR /<project.Name>
RUN mkdir -p /usr/local/share/dotnet/sdk/NuGetFallbackFolder
CMD ["dotnet", "watch", "run", "--urls", "<project.Services:{svc|<svc.Protocol>://0.0.0.0:<svc.Port>}; separator=";">"]

Просмотреть файл

@ -1,30 +0,0 @@
// Copyright 2020 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Shouldly;
using Xunit;
namespace Steeltoe.Tooling.Test
{
public class ContextTest : ToolingTest
{
[Fact]
public void TestContext()
{
var ctx = new Context(null, Console, Shell);
ctx.Console.ShouldBe(Console);
ctx.Shell.ShouldBe(Shell);
}
}
}

Просмотреть файл

@ -24,7 +24,7 @@ namespace Steeltoe.Tooling.Test
protected readonly MockShell Shell;
protected StringWriter Console;
protected readonly StringWriter Console;
public ToolingTest()
{
@ -32,7 +32,12 @@ namespace Steeltoe.Tooling.Test
Directory.CreateDirectory(path);
Console = new StringWriter();
Shell = new MockShell();
Context = new Context(path, Console, Shell);
Context = new Context
{
WorkingDirectory = path,
Console = Console,
Shell = Shell
};
}
public void Dispose()