-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathInstallCommand.cs
More file actions
99 lines (82 loc) · 4.09 KB
/
InstallCommand.cs
File metadata and controls
99 lines (82 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.CommandLine;
using DeveloperCli.Installation;
using Spectre.Console;
namespace DeveloperCli.Commands;
public class InstallCommand : Command
{
private static readonly string Intro =
$"""
[green]Welcome to:[/]
To get the full benefit of PlatformPlatform, allow this tool to register an alias on your machine.
This will allow you to run PlatformPlatform commands from anywhere on your machine by typing [green]{Configuration.AliasName}[/].
[green]The CLI can be used to:[/]
* Start all PlatformPlatform services locally in one command
* Be guided through setting up secure passwordless continuous deployments between GitHub and Azure
* Run static code analysis on your codebase to ensure it does not fail when running in GitHub Workflows
* Run tests and show code coverage reports locally
* Much more is coming soon!
[green]Best of all, you can easily create your own commands to automate your own workflows![/]
The CLI will automatically detect any changes and recompile itself whenever your team does a [bold][grey]git pull[/][/].
It's a great way to automate workflows and share them with your team.
[green]Is this secure?[/]
Yes. But, like any code you copy from the internet, you should always review it before you run it.
Just open the project in your IDE and review the code.
[green]How does it work?[/]
The CLI has several commands that you can run from anywhere on your machine.
Each command is one C# class that can be customized to automate your own workflows.
Each command check for its prerequisites (e.g., Docker, Node, Aspire, Azure CLI, etc.)
To remove the alias, just run [green]{Configuration.AliasName} uninstall[/].
""";
public InstallCommand() : base(
"install",
$"This will register the alias {Configuration.AliasName} so it will be available everywhere"
)
{
var forceOption = new Option<bool>("--force", "-f") { Description = "Force reinstall even if already installed" };
Options.Add(forceOption);
SetAction(parseResult => Execute(parseResult.GetValue(forceOption)));
}
private static void Execute(bool force)
{
Prerequisite.Ensure(Prerequisite.Dotnet);
if (!force && IsAliasRegistered())
{
var installedAliasPath = Configuration.GetConfigurationSetting().CliSourceCodeFolder!;
AnsiConsole.MarkupLine(Environment.ProcessPath!.StartsWith(installedAliasPath)
? $"[yellow]The CLI is already installed please run {Configuration.AliasName} to use it. Use --force to reinstall.[/]"
: $"[yellow]There is already a CLI with the alias '{Configuration.AliasName}' installed in {installedAliasPath}. Use --force to reinstall.[/]"
);
Environment.Exit(0);
}
AnsiConsole.Write(new Markup(Intro));
AnsiConsole.WriteLine();
if (AnsiConsole.Confirm($"This will register the alias '[green]{Configuration.AliasName}[/]', so it will be available everywhere."))
{
AnsiConsole.WriteLine();
RegisterAlias();
GitHooksSync.Sync(true);
}
AnsiConsole.MarkupLine(
Configuration.IsWindows
? "Please restart your terminal to update your PATH."
: $"Please restart your terminal to update your PATH (or run [green]source ~/{Configuration.MacOs.GetShellInfo().ProfileName}[/])."
);
}
private static bool IsAliasRegistered()
{
return Configuration.IsWindows
? Configuration.Windows.IsFolderInPath(Configuration.PublishFolder)
: Configuration.MacOs.IsAliasRegisteredMacOs();
}
private static void RegisterAlias()
{
if (Configuration.IsWindows)
{
Configuration.Windows.AddFolderToPath(Configuration.PublishFolder);
}
else if (Configuration.IsMacOs || Configuration.IsLinux)
{
Configuration.MacOs.RegisterAliasMacOs();
}
}
}