Skip to content

Commit

Permalink
Fix tye init --force (dotnet#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkotalik authored Jan 15, 2021
1 parent 4bfccef commit 4c52a21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion activate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.Tye.Core/ConfigFileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class ConfigFileFinder
{
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);

Expand Down
17 changes: 17 additions & 0 deletions src/tye/InitHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ public static (string, string) CreateTyeFileContent(FileInfo? path, bool force)
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
Expand Down
15 changes: 15 additions & 0 deletions test/E2ETest/TyeInitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,20 @@ public void Console_Normalization_Service_Name()

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);
}
}
}

0 comments on commit 4c52a21

Please sign in to comment.