-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure any backups created during installation don't get auto-deleted.
- Loading branch information
Showing
3 changed files
with
57 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,55 +1,56 @@ | ||
using CliFx.Infrastructure; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace CliFx.Suggestions | ||
{ | ||
class SuggestShellHookInstaller | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public SuggestShellHookInstaller(IFileSystem fileSystem) | ||
{ | ||
_fileSystem = fileSystem; | ||
} | ||
|
||
public void Install(string commandName) | ||
{ | ||
var detectedEnvironments = new ISuggestEnvironment[] { | ||
new BashEnvironment(), | ||
new WindowsPowershellEnvironment(), | ||
new UnixPowershellEnvironment() } | ||
.Where(env => env.SupportedShellPaths.Any(p => _fileSystem.Exists(p) )); | ||
|
||
foreach (var env in detectedEnvironments) | ||
{ | ||
var path = env.InstallPath; | ||
var pattern = $"### clifx-suggest-begins-here-{Regex.Escape(commandName)}-{env.Version}.*### clifx-suggest-ends-here-{Regex.Escape(commandName)}"; | ||
|
||
string script = ""; | ||
_fileSystem.TryReadText(path, out script); | ||
var match = Regex.Match(script, pattern, RegexOptions.Singleline); | ||
if (match.Success) | ||
{ | ||
continue; | ||
} | ||
|
||
var uninstallPattern = $"### clifx-suggest-begins-here-{Regex.Escape(commandName)}.*### clifx-suggest-ends-here-{Regex.Escape(commandName)}"; | ||
var sb = new StringBuilder(Regex.Replace(script, uninstallPattern, "", RegexOptions.Singleline)); | ||
sb.AppendLine(env.GetInstallCommand(commandName)); | ||
|
||
// backup to temp folder for OS to delete eventually (just in case something really bad happens) | ||
var tempFile = Path.GetFileName(path); | ||
var tempExtension = Path.GetExtension(tempFile) + $".backup_{DateTime.UtcNow.ToFileTime()}"; | ||
tempFile = Path.ChangeExtension(tempFile, tempExtension); | ||
var backupPath = Path.Combine(Path.GetTempPath(), tempFile); | ||
|
||
_fileSystem.Copy(path, backupPath); | ||
_fileSystem.WriteText(path, sb.ToString()); | ||
} | ||
} | ||
} | ||
} | ||
using CliFx.Infrastructure; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace CliFx.Suggestions | ||
{ | ||
class SuggestShellHookInstaller | ||
{ | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public SuggestShellHookInstaller(IFileSystem fileSystem) | ||
{ | ||
_fileSystem = fileSystem; | ||
} | ||
|
||
public void Install(string commandName) | ||
{ | ||
var detectedEnvironments = new ISuggestEnvironment[] { | ||
new BashEnvironment(), | ||
new WindowsPowershellEnvironment(), | ||
new UnixPowershellEnvironment() } | ||
.Where(env => env.SupportedShellPaths.Any(p => _fileSystem.Exists(p) )); | ||
|
||
foreach (var env in detectedEnvironments) | ||
{ | ||
var path = env.InstallPath; | ||
var pattern = $"### clifx-suggest-begins-here-{Regex.Escape(commandName)}-{env.Version}.*### clifx-suggest-ends-here-{Regex.Escape(commandName)}"; | ||
|
||
string script = ""; | ||
_fileSystem.TryReadText(path, out script); | ||
var match = Regex.Match(script, pattern, RegexOptions.Singleline); | ||
if (match.Success) | ||
{ | ||
continue; | ||
} | ||
|
||
var uninstallPattern = $"### clifx-suggest-begins-here-{Regex.Escape(commandName)}.*### clifx-suggest-ends-here-{Regex.Escape(commandName)}"; | ||
var sb = new StringBuilder(Regex.Replace(script, uninstallPattern, "", RegexOptions.Singleline)); | ||
sb.AppendLine(env.GetInstallCommand(commandName)); | ||
|
||
// create backup in case something bad happens | ||
var backupExtension = Path.GetExtension(path) + $".backup_{DateTime.UtcNow.ToFileTime()}"; | ||
var backupPath = Path.ChangeExtension(path, backupExtension); | ||
|
||
if (_fileSystem.Exists(path)) | ||
{ | ||
_fileSystem.Copy(path, backupPath); | ||
} | ||
_fileSystem.WriteText(path, sb.ToString()); | ||
} | ||
} | ||
} | ||
} |