public virtual void Install(bool dryRun) { commandOutputProvider.Information($"Installing scripts in {ProfileLocation}"); var tempOutput = new StringBuilder(); if (fileSystem.FileExists(ProfileLocation)) { var profileText = fileSystem.ReadAllText(ProfileLocation); if (!dryRun) { if (profileText.Contains(AllShellsPrefix) || profileText.Contains(AllShellsSuffix) || profileText.Contains(ProfileScript)) { if (!profileText.Contains(ProfileScript)) { var message = $"Looks like command line completion is already installed, but points to a different executable.{Environment.NewLine}" + $"Please manually edit the file {ProfileLocation} to remove the existing auto complete script and then re-install."; throw new CommandException(message); } commandOutputProvider.Information("Looks like command line completion is already installed. Nothing to do."); return; } var backupPath = ProfileLocation + ".orig"; commandOutputProvider.Information($"Backing up the existing profile to {backupPath}"); fileSystem.CopyFile(ProfileLocation, backupPath); } commandOutputProvider.Information($"Updating profile at {ProfileLocation}"); tempOutput.AppendLine(profileText); } else { commandOutputProvider.Information($"Creating profile at {ProfileLocation}"); fileSystem.EnsureDirectoryExists(Path.GetDirectoryName(ProfileLocation)); } tempOutput.AppendLine(AllShellsPrefix); tempOutput.AppendLine(ProfileScript); tempOutput.AppendLine(AllShellsSuffix); if (dryRun) { commandOutputProvider.Warning("Preview of script changes: "); commandOutputProvider.Information(tempOutput.ToString()); commandOutputProvider.Warning("Preview of script changes finished. "); } else { fileSystem.OverwriteFile(ProfileLocation, tempOutput.ToString()); commandOutputProvider.Warning("All Done! Please reload your shell or dot source your profile to get started! Use the <tab> key to autocomplete."); } }
private string Copy(string source, string baseDirectory, string destinationDirectory) { var relativePath = fileSystem.GetPathRelativeTo(source, baseDirectory); var destination = Path.Combine(destinationDirectory, relativePath); LogMessage("Copy file: " + source, importance: MessageImportance.Normal); var relativeDirectory = Path.GetDirectoryName(destination); fileSystem.EnsureDirectoryExists(relativeDirectory); fileSystem.CopyFile(source, destination); return(destination); }
private void Copy(IEnumerable <string> sourceFiles, string baseDirectory, string destinationDirectory) { foreach (var source in sourceFiles) { var relativePath = fileSystem.GetPathRelativeTo(source, baseDirectory); var destination = Path.Combine(destinationDirectory, relativePath); LogMessage("Copy file: " + source, importance: MessageImportance.Normal); var relativeDirectory = Path.GetDirectoryName(destination); fileSystem.EnsureDirectoryExists(relativeDirectory); fileSystem.CopyFile(source, destination); } }