Skip to content

Commit

Permalink
Ensure any backups created during installation don't get auto-deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricel committed Apr 12, 2021
1 parent 1d40821 commit bd3aa95
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 60 deletions.
5 changes: 1 addition & 4 deletions CliFx/Infrastructure/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ internal class FileSystem : IFileSystem
{
public void Copy(string sourceFileName, string destFileName)
{
if( File.Exists(sourceFileName))
{
File.Copy(sourceFileName, destFileName);
}
File.Copy(sourceFileName, destFileName);
}

public bool Exists(string path)
Expand Down
1 change: 0 additions & 1 deletion CliFx/Infrastructure/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public interface IFileSystem
{
/// <summary>
/// Determines whether the specified file exists.
/// Doesn't work in Linux Subsystem for Windows unless application if the application resides on a windows mount (eg /mnt/c/...)
/// </summary>
bool Exists(string filePath);

Expand Down
111 changes: 56 additions & 55 deletions CliFx/Suggestions/SuggestShellHookInstaller.cs
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());
}
}
}
}

0 comments on commit bd3aa95

Please sign in to comment.