forked from Fallout-build/Fallout
-
Notifications
You must be signed in to change notification settings - Fork 0
Convert the :navigation cluster to command types (#392) #25
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
Open
ChrisonSimtian
wants to merge
1
commit into
cli-cmd-cake
Choose a base branch
from
cli-cmd-navigation
base: cli-cmd-cake
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions
28
src/Fallout.Cli/Commands/Navigation/GetNextDirectoryCommand.cs
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,28 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary><c>fallout :GetNextDirectory</c>: prints (and consumes) the queued next directory.</summary> | ||
| public sealed class GetNextDirectoryCommand : IFalloutCommand | ||
| { | ||
| public string Name => "GetNextDirectory"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| var content = NavigationSession.SessionFile.Existing()?.ReadAllLines(); | ||
| if (content == null || string.IsNullOrWhiteSpace(content[0])) | ||
| { | ||
| Console.WriteLine(EnvironmentInfo.WorkingDirectory); | ||
| return 1; | ||
| } | ||
|
|
||
| var nextDirectory = content[0]; | ||
| content[0] = string.Empty; | ||
| NavigationSession.SessionFile.WriteAllLines(content); | ||
| Console.WriteLine(nextDirectory); | ||
| 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,49 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
| using Fallout.Common.Utilities; | ||
| using static Fallout.Common.Constants; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary> | ||
| /// Shared per-terminal-session state for the directory-navigation commands. The companion shell | ||
| /// functions invoke the commands by name (preserved on conversion): | ||
| /// <code> | ||
| /// function nuke- { nuke :PopDirectory; cd $(nuke :GetNextDirectory) } | ||
| /// function nuke/ { nuke :PushWithChosenRootDirectory; cd $(nuke :GetNextDirectory) } | ||
| /// function nuke. { nuke :PushWithCurrentRootDirectory; cd $(nuke :GetNextDirectory) } | ||
| /// function nuke.. { nuke :PushWithParentRootDirectory; cd $(nuke :GetNextDirectory) } | ||
| /// </code> | ||
| /// </summary> | ||
| internal static class NavigationSession | ||
| { | ||
| public static string SessionId | ||
| => EnvironmentInfo.Platform switch | ||
| { | ||
| PlatformFamily.OSX => EnvironmentInfo.GetVariable("TERM_SESSION_ID").NotNull()[7..], | ||
| PlatformFamily.Windows => EnvironmentInfo.GetVariable("WT_SESSION").NotNull(), | ||
| _ => throw new NotSupportedException($"{EnvironmentInfo.Platform} has no session id selector.") | ||
| }; | ||
|
|
||
| public static AbsolutePath SessionFile => GlobalTemporaryDirectory / $"nuke-{SessionId}.dat"; | ||
|
|
||
| public static int PushAndSetNext(Func<string> directoryProvider) | ||
| { | ||
| try | ||
| { | ||
| var content = SessionFile.Existing()?.ReadAllLines().ToList() ?? new List<string> { null }; | ||
| content[0] = directoryProvider.Invoke(); | ||
| content.Insert(index: 1, EnvironmentInfo.WorkingDirectory); | ||
| SessionFile.WriteAllLines(content); | ||
| return 0; | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| Console.Error.WriteLine(exception.Message); | ||
| return 1; | ||
| } | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Fallout.Cli/Commands/Navigation/PopDirectoryCommand.cs
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,26 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Fallout.Common.IO; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary><c>fallout :PopDirectory</c>: pops the previous directory back to the front of the queue.</summary> | ||
| public sealed class PopDirectoryCommand : IFalloutCommand | ||
| { | ||
| public string Name => "PopDirectory"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| var content = NavigationSession.SessionFile.Existing()?.ReadAllLines().ToList(); | ||
| if (content == null || content.Count <= 1) | ||
| { | ||
| Console.Error.WriteLine("No previous directory"); | ||
| return 1; | ||
| } | ||
|
|
||
| content[0] = content[1]; | ||
| content.RemoveAt(1); | ||
| NavigationSession.SessionFile.WriteAllLines(content); | ||
| return 0; | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Fallout.Cli/Commands/Navigation/PushWithChosenRootDirectoryCommand.cs
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,34 @@ | ||
| using System.Linq; | ||
| using Fallout.Cli.Prompts; | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
| using static Fallout.Common.Constants; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary> | ||
| /// <c>fallout :PushWithChosenRootDirectory</c>: prompts for a discovered root directory and queues it. | ||
| /// </summary> | ||
| public sealed class PushWithChosenRootDirectoryCommand : IFalloutCommand | ||
| { | ||
| private readonly IConsolePrompts _prompts; | ||
|
|
||
| public PushWithChosenRootDirectoryCommand(IConsolePrompts prompts) => _prompts = prompts; | ||
|
|
||
| public string Name => "PushWithChosenRootDirectory"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| return NavigationSession.PushAndSetNext(() => | ||
| { | ||
| var directories = EnvironmentInfo.WorkingDirectory.GlobDirectories($"**/{FalloutDirectoryName}") | ||
| .Concat(EnvironmentInfo.WorkingDirectory.GlobFiles($"**/{FalloutDirectoryName}")) | ||
| .Where(x => !x.Equals(EnvironmentInfo.WorkingDirectory)) | ||
| .Select(x => x.Parent) | ||
| .Select(x => (x, EnvironmentInfo.WorkingDirectory.GetRelativePathTo(x).ToString())) | ||
| .OrderBy(x => x.Item2).ToArray(); | ||
|
|
||
| return _prompts.PromptForChoice("Where to go next?", directories); | ||
| }); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Fallout.Cli/Commands/Navigation/PushWithCurrentRootDirectoryCommand.cs
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,16 @@ | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
| using Fallout.Common.Utilities; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary><c>fallout :PushWithCurrentRootDirectory</c>: queues the current root directory.</summary> | ||
| public sealed class PushWithCurrentRootDirectoryCommand : IFalloutCommand | ||
| { | ||
| public string Name => "PushWithCurrentRootDirectory"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| return NavigationSession.PushAndSetNext(() => rootDirectory.NotNull("No root directory")); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/Fallout.Cli/Commands/Navigation/PushWithParentRootDirectoryCommand.cs
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,19 @@ | ||
| using System.IO; | ||
| using Fallout.Common; | ||
| using Fallout.Common.IO; | ||
| using Fallout.Common.Utilities; | ||
| using static Fallout.Common.Constants; | ||
|
|
||
| namespace Fallout.Cli.Commands.Navigation; | ||
|
|
||
| /// <summary><c>fallout :PushWithParentRootDirectory</c>: queues the parent repository's root directory.</summary> | ||
| public sealed class PushWithParentRootDirectoryCommand : IFalloutCommand | ||
| { | ||
| public string Name => "PushWithParentRootDirectory"; | ||
|
|
||
| public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript) | ||
| { | ||
| return NavigationSession.PushAndSetNext(() => TryGetRootDirectoryFrom(Path.GetDirectoryName(rootDirectory.NotNull("No root directory"))) | ||
| .NotNull("No parent root directory")); | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 They don't have to be
public, do they? If not, keep theminternal, so our IDEs will also flag dead code.