diff --git a/activate.ps1 b/activate.ps1 index 2fce1e4b..630a155c 100644 --- a/activate.ps1 +++ b/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) { diff --git a/src/Microsoft.Tye.Core/ConfigFileFinder.cs b/src/Microsoft.Tye.Core/ConfigFileFinder.cs index bc5a2d31..0f408d96 100644 --- a/src/Microsoft.Tye.Core/ConfigFileFinder.cs +++ b/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); diff --git a/src/tye/InitHost.cs b/src/tye/InitHost.cs index 24e89a28..e0daeb37 100644 --- a/src/tye/InitHost.cs +++ b/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 diff --git a/test/E2ETest/TyeInitTests.cs b/test/E2ETest/TyeInitTests.cs index bb592c9e..f6059f23 100644 --- a/test/E2ETest/TyeInitTests.cs +++ b/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); + } } }