Browse Source

Fix tye init --force (#896)

pull/906/head
Justin Kotalik 5 years ago
committed by GitHub
parent
commit
4c52a2181f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      activate.ps1
  2. 5
      src/Microsoft.Tye.Core/ConfigFileFinder.cs
  3. 17
      src/tye/InitHost.cs
  4. 15
      test/E2ETest/TyeInitTests.cs

2
activate.ps1

@ -13,7 +13,7 @@ if ($MyInvocation.CommandOrigin -eq 'runspace') {
function dtye() {
$ProjectPath = Join-Path (Join-Path $PSScriptRoot "src") "tye"
dotnet run --project "$ProjectPath" @args
dotnet run --project "$ProjectPath" -- @args
}
function deactivate ([switch]$init) {

5
src/Microsoft.Tye.Core/ConfigFileFinder.cs

@ -10,9 +10,10 @@ namespace Microsoft.Tye
{
private static readonly string[] FileFormats = { "tye.yaml", "tye.yml", "docker-compose.yaml", "docker-compose.yml", "*.csproj", "*.fsproj", "*.sln" };
public static bool TryFindSupportedFile(string directoryPath, out string? filePath, out string? errorMessage)
public static bool TryFindSupportedFile(string directoryPath, out string? filePath, out string? errorMessage, string[]? fileFormats = null)
{
foreach (var format in FileFormats)
fileFormats ??= FileFormats;
foreach (var format in fileFormats)
{
var files = Directory.GetFiles(directoryPath, format);

17
src/tye/InitHost.cs

@ -23,6 +23,23 @@ namespace Microsoft.Tye
ThrowIfTyeFilePresent(path, "tye.yaml");
}
if (force)
{
// Don't use existing tye.yaml if we are force creating it again.
// path prior is pointing to the tye.yaml file still, so refind another file that isn't the tye.yaml
var hasViableFileType = ConfigFileFinder.TryFindSupportedFile(path?.DirectoryName ?? ".",
out var filePath,
out var errorMessage,
new string[] { "*.csproj", "*.fsproj", "*.sln" });
if (!hasViableFileType)
{
throw new CommandException(errorMessage!);
}
path = new FileInfo(filePath);
}
var template = @"
# tye application configuration file
# read all about it at https://github.com/dotnet/tye

15
test/E2ETest/TyeInitTests.cs

@ -89,5 +89,20 @@ namespace E2ETest
YamlAssert.Equals(expectedContent, content);
}
[Fact]
public void Tye_Init_Force_Works()
{
using var projectDirectory = CopyTestProjectDirectory("single-project");
var tyePath = Path.Combine(projectDirectory.DirectoryPath, "tye.yaml");
var text = File.ReadAllText(tyePath);
File.WriteAllText(tyePath, text + "thisisatest");
var projectFile = new FileInfo(tyePath);
var (content, _) = InitHost.CreateTyeFileContent(projectFile, force: true);
Assert.DoesNotContain("thisisatest", content);
}
}
}

Loading…
Cancel
Save