-
-
Notifications
You must be signed in to change notification settings - Fork 16
Convert :cake-convert and :cake-clean to command types (#392) #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Linq; | ||
| using Fallout.Cli.Prompts; | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
|
|
||
| namespace Fallout.Cli.Commands; | ||
|
|
||
| /// <summary> | ||
| /// <c>fallout :cake-clean</c>: lists and optionally deletes the <c>*.cake</c> scripts. | ||
| /// </summary> | ||
| public sealed class CakeCleanCommand : IFalloutCommand | ||
| { | ||
| private readonly IConsolePrompts _prompts; | ||
|
|
||
| public CakeCleanCommand(IConsolePrompts prompts) => _prompts = prompts; | ||
|
|
||
| public string Name => "cake-clean"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| var cakeFiles = Program.GetCakeFiles().ToList(); | ||
| Host.Information("Found .cake files:"); | ||
| cakeFiles.ForEach(x => Host.Debug($" - {x}")); | ||
|
|
||
| if (_prompts.PromptForConfirmation("Delete?")) | ||
| cakeFiles.ForEach(x => x.DeleteFile()); | ||
|
|
||
| return 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Fallout.Cli.Prompts; | ||
| using Fallout.Common; | ||
| using Fallout.Common.Execution; | ||
| using Fallout.Common.IO; | ||
| using Fallout.Solutions; | ||
| using Fallout.Common.Utilities; | ||
| using static Fallout.Common.EnvironmentInfo; | ||
|
|
||
| namespace Fallout.Cli.Commands; | ||
|
|
||
| /// <summary> | ||
| /// <c>fallout :cake-convert</c>: best-effort conversion of <c>*.cake</c> scripts to Fallout C#. | ||
| /// </summary> | ||
| public sealed class CakeConvertCommand : IFalloutCommand | ||
| { | ||
| private readonly IConsolePrompts _prompts; | ||
|
|
||
| public CakeConvertCommand(IConsolePrompts prompts) => _prompts = prompts; | ||
|
|
||
| public string Name => "cake-convert"; | ||
|
|
||
| // The .cake syntax-rewriting helpers (GetCakeFiles/GetCakeConvertedContent/GetCakePackages) and | ||
| // the shared GetConfiguration/AddOrReplacePackage helpers remain on Program until the #392 | ||
| // collapse PR; GetCakeConvertedContent/GetCakePackages are also exercised directly by tests. | ||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| Program.PrintInfo(); | ||
| Logging.Configure(); | ||
| Telemetry.ConvertCake(); | ||
| ProjectModelTasks.Initialize(); | ||
|
|
||
| Host.Warning( | ||
| new[] | ||
| { | ||
| "Converting .cake files is a best effort approach using syntax rewriting.", | ||
| "Compile errors are to be expected, however, the following elements are currently covered:", | ||
| " - Target definitions", | ||
| " - Default targets", | ||
| " - Parameter declarations", | ||
| " - Absolute paths", | ||
| " - Globbing patterns", | ||
| " - Tool invocations (dotnet CLI, SignTool)", | ||
| " - Addin and tool references", | ||
| }.JoinNewLine()); | ||
|
|
||
| Host.Debug(); | ||
| if (!_prompts.PromptForConfirmation("Continue?")) | ||
| return 0; | ||
| Host.Debug(); | ||
|
|
||
| if (buildScript == null && | ||
| _prompts.PromptForConfirmation("Should a NUKE project be created for better results?")) | ||
| { | ||
| Program.Setup(args, rootDirectory: null, buildScript: null); | ||
| } | ||
|
|
||
| var buildScriptFile = WorkingDirectory / Program.CurrentBuildScriptName; | ||
| var buildProjectFile = buildScriptFile.Exists() | ||
| ? Program.GetConfiguration(buildScriptFile, evaluate: true) | ||
| .GetValueOrDefault(Program.BUILD_PROJECT_FILE, defaultValue: null) | ||
| : null; | ||
|
|
||
| foreach (var cakeFile in Program.GetCakeFiles()) | ||
| { | ||
| var outputFile = cakeFile.Parent / cakeFile.NameWithoutExtension.Capitalize() + ".cs"; | ||
| var content = Program.GetCakeConvertedContent(cakeFile.ReadAllText()); | ||
| outputFile.WriteAllText(content); | ||
| } | ||
|
|
||
| if (buildProjectFile != null) | ||
| { | ||
| var packages = Program.GetCakeFiles().SelectMany(x => Program.GetCakePackages(x.ReadAllText())); | ||
| foreach (var package in packages) | ||
| Program.AddOrReplacePackage(package.Id, package.Version, package.Type, buildProjectFile); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using Fallout.Cli.Commands; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Fallout.Cli.Tests.Commands; | ||
|
|
||
| public class CakeCleanCommandTests | ||
| { | ||
| [Fact] | ||
| public void Name_IsCakeClean() | ||
| => new CakeCleanCommand(new FakeConsolePrompts()).Name.Should().Be("cake-clean"); | ||
|
|
||
| [Fact] | ||
| public void Execute_WhenDeletionDeclined_ReturnsZeroAndDeletesNothing() | ||
| { | ||
| // ConfirmationResult = false → the "Delete?" prompt is declined, so no .cake files are removed. | ||
| var prompts = new FakeConsolePrompts { ConfirmationResult = false }; | ||
|
|
||
| new CakeCleanCommand(prompts).Execute([], rootDirectory: null, buildScript: null).Should().Be(0); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.