forked from Tyrrrz/CliFx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tyrrrz#13 - Add installation code for suggest mode.
- Loading branch information
Showing
9 changed files
with
280 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.IO; | ||
|
||
namespace CliFx.Infrastructure | ||
{ | ||
class FileSystem : IFileSystem | ||
{ | ||
public void Copy(string sourceFileName, string destFileName) | ||
{ | ||
File.Copy(sourceFileName, destFileName); | ||
} | ||
|
||
public bool Exists(string path) | ||
{ | ||
return File.Exists(path); | ||
} | ||
|
||
public string ReadAllText(string path) | ||
{ | ||
return File.ReadAllText(path); | ||
} | ||
|
||
public void WriteAllText(string path, string content) | ||
{ | ||
File.WriteAllText(path, content); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace CliFx.Infrastructure | ||
{ | ||
/// <summary> | ||
/// Abstraction for the file system | ||
/// </summary> | ||
public interface IFileSystem | ||
{ | ||
/// <summary> | ||
/// Determines whether the specified file exists. | ||
/// </summary> | ||
bool Exists(string filePath); | ||
|
||
/// <summary> | ||
/// Opens a text file, reads all the text in the file, and then closes the file. | ||
/// </summary> | ||
string ReadAllText(string filePath); | ||
|
||
/// <summary> | ||
/// Creates a new file, writes the specified string to the file, and then closes | ||
/// the file. If the target file already exists, it is overwritten. | ||
/// </summary> | ||
void WriteAllText(string filePath, string content); | ||
|
||
/// <summary> | ||
/// Copies an existing file to a new file. Overwriting a file of the same name is | ||
/// not allowed. | ||
/// </summary> | ||
void Copy(string path, string backupPath); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace CliFx.Suggestions | ||
{ | ||
class BashSuggestEnvironment : ISuggestEnvironment | ||
{ | ||
public string Version => "V1"; | ||
|
||
public bool ShouldInstall() | ||
{ | ||
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) | ||
{ | ||
return File.Exists(GetInstallPath()); | ||
} | ||
return false; | ||
} | ||
|
||
public string GetInstallPath() | ||
{ | ||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".bashrc"); | ||
} | ||
|
||
public string GetInstallCommand(string commandName) | ||
{ | ||
var safeName = commandName.Replace(" ", "_"); | ||
return $@" | ||
### clifx-suggest-begins-here-{safeName}-{Version} | ||
# this block provides auto-complete for the {commandName} command | ||
# and assumes that {commandName} is on the path | ||
_{safeName}_complete() | ||
{{ | ||
local word=${{COMP_WORDS[COMP_CWORD]}} | ||
# generate unique environment variable | ||
CLIFX_CMD_CACHE=""clifx-suggest-$(uuidgen)"" | ||
# replace hyphens with underscores to make it valid | ||
CLIFX_CMD_CACHE=${{CLIFX_CMD_CACHE//\-/_}} | ||
export $CLIFX_CMD_CACHE=${{COMP_LINE}} | ||
local completions | ||
completions=""$({commandName} ""[suggest]"" --cursor ""${{COMP_POINT}}"" --envvar $CLIFX_CMD_CACHE 2>/dev/null)"" | ||
if [ $? -ne 0]; then | ||
completions="""" | ||
fi | ||
unset $CLIFX_CMD_CACHE | ||
COMPREPLY=( $(compgen -W ""$completions"" -- ""$word"") ) | ||
}} | ||
complete -f -F _{safeName}_complete ""{commandName}"" | ||
### clifx-suggest-ends-here-{safeName}"; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace CliFx.Suggestions | ||
{ | ||
interface ISuggestEnvironment | ||
{ | ||
bool ShouldInstall(); | ||
|
||
string Version { get; } | ||
|
||
string GetInstallPath(); | ||
|
||
string GetInstallCommand(string command); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace CliFx.Suggestions | ||
{ | ||
class PowershellSuggestEnvironment : ISuggestEnvironment | ||
{ | ||
public string Version => "V1"; | ||
|
||
public bool ShouldInstall() | ||
{ | ||
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) | ||
{ | ||
return File.Exists("/usr/bin/pwsh"); | ||
|
||
} | ||
return true; | ||
} | ||
|
||
public string GetInstallPath() | ||
{ | ||
var baseDir = ""; | ||
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) | ||
{ | ||
baseDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", ".powershell"); | ||
} | ||
else | ||
{ | ||
var myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.DoNotVerify); | ||
baseDir = Path.Combine(myDocuments, "WindowsPowerShell"); | ||
} | ||
|
||
return Path.Combine(baseDir, "Microsoft.PowerShell_profile.ps1"); | ||
} | ||
|
||
public string GetInstallCommand(string commandName) | ||
{ | ||
var safeName = commandName.Replace(" ", "_"); | ||
return $@" | ||
### clifx-suggest-begins-here-{safeName}-{Version} | ||
# this block provides auto-complete for the {commandName} command | ||
# and assumes that {commandName} is on the path | ||
$scriptblock = {{ | ||
param($wordToComplete, $commandAst, $cursorPosition) | ||
$command = ""{commandName}"" | ||
$commandCacheId = ""clifx-suggest-"" + (new-guid).ToString() | ||
Set-Content -path ""ENV:\$commandCacheId"" -value $commandAst | ||
$result = &$command `[suggest`] --envvar $commandCacheId --cursor $cursorPosition | ForEach-Object {{ | ||
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) | ||
}} | ||
Remove-Item -Path ""ENV:\$commandCacheId"" | ||
$result | ||
}} | ||
Register-ArgumentCompleter -Native -CommandName ""{commandName}"" -ScriptBlock $scriptblock | ||
### clifx-suggest-ends-here-{safeName}"; | ||
} | ||
} | ||
} |
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