Skip to content

Commit

Permalink
Yosahd
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jan 7, 2024
1 parent ec750ec commit 162180b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 169 deletions.
176 changes: 8 additions & 168 deletions src/commands/special/linux_bins.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,7 @@ function global:cd {
}
#cd: usage: cd [-L|[-P [-e]] [-@]] [dir]
if (-not $IsLinuxBin) {
$LinuxBinArguments = @("-L", "-P", "-e", "-@", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Set-Location $RemainingArguments)
}
if ($IsLinuxBin) {
#cd是bash提供的内置命令,没有单独的可执行文件
Expand Down Expand Up @@ -173,27 +153,7 @@ function global:ls {
}
return
}
$LinuxBinArguments = @("-a", "--all", "-A", "--almost-all", "-b", "--escape", "-B", "--ignore-backups", "-c", "--time=ctime", "-C", "--format=vertical", "-d", "--directory", "-D", "--dired", "-f", "--format=across", "-F", "--classify", "-g", "--group-directories-first", "-G", "--no-group", "-h", "--human-readable", "-H", "--si", "-i", "--inode", "-I", "--ignore=", "-k", "--kibibytes", "-l", "--format=long", "-L", "--dereference", "-m", "--format=commas", "-n", "--numeric-uid-gid", "-N", "--literal", "-o", "-1", "--format=single-column", "-p", "--indicator-style=slash", "-q", "--hide-control-chars", "-Q", "--quote-name", "-r", "--reverse", "-R", "--recursive", "-s", "--size", "-S", "--sort=size", "-t", "--sort=time", "-T", "--tabsize=COLS", "-u", "--time=atime", "-U", "--sort=atime", "-v", "--sort=version", "-w", "--width=COLS", "-x", "--format=across", "-X", "--sort=extension", "-Z", "--context", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Get-ChildItem $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的ls.exe
#则调用ls.exe
Expand Down Expand Up @@ -264,27 +224,7 @@ function global:rm {
Remove-Item $Path
return
}
$LinuxBinArguments = @("-f", "--force", "-i", "--interactive", "-I", "--interactive=once", "--one-file-system", "--no-preserve-root", "--preserve-root", "-r", "-R", "--recursive", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Remove-Item $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的rm.exe
#则调用rm.exe
Expand Down Expand Up @@ -365,27 +305,7 @@ function global:mv {
Move-Item $Path -Destination $Destination
return
}
$LinuxBinArguments = @("-b", "--backup", "-f", "--force", "-i", "--interactive", "-n", "--no-clobber", "-u", "--update", "-v", "--verbose", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Move-Item $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的mv.exe
#则调用mv.exe
Expand Down Expand Up @@ -466,27 +386,7 @@ function global:cp {
Copy-Item $Path -Destination $Destination
return
}
$LinuxBinArguments = @("-a", "--archive", "-b", "--backup", "-f", "--force", "-i", "--interactive", "-l", "--link", "-L", "--dereference", "-n", "--no-clobber", "-P", "--no-dereference", "-p", "--preserve", "-R", "-r", "--recursive", "-s", "--symbolic-link", "-S", "--suffix=SUFFIX", "-t", "--target-directory=DIRECTORY", "-T", "--no-target-directory", "-u", "--update", "-v", "--verbose", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Copy-Item $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的cp.exe
#则调用cp.exe
Expand Down Expand Up @@ -548,27 +448,7 @@ function global:mkdir {
New-Item $Path -ItemType Directory
return
}
$LinuxBinArguments = @("-m", "--mode=MODE", "-p", "--parents", "-v", "--verbose", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call New-Item $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的mkdir.exe
#则调用mkdir.exe
Expand Down Expand Up @@ -630,27 +510,7 @@ function global:touch {
New-Item $Path -ItemType File
return
}
$LinuxBinArguments = @("-a", "--time=access", "-c", "--no-create", "-d", "--date=STRING", "-f", "--force", "-h", "--no-dereference", "-m", "--time=modification", "-r", "--reference=FILE", "-t", "--time=WORD", "-v", "--verbose", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call New-Item $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的touch.exe
#则调用touch.exe
Expand Down Expand Up @@ -716,27 +576,7 @@ function global:cat {
Get-Content $Path
return
}
$LinuxBinArguments = @("-A", "--show-all", "-b", "--number-nonblank", "-e", "--show-ends", "-E", "--show-ends", "-n", "--number", "-s", "--squeeze-blank", "-t", "--show-tabs", "-T", "--show-tabs", "-u", "--unbuffered", "-v", "--show-nonprinting", "-w", "--width=COLS", "--help", "--version")
$RemainingArguments | ForEach-Object {
$arg = $_
$LinuxBinArguments | ForEach-Object {
if ($_.Length -eq 2) {
if ($arg.StartsWith($_)) {
$IsLinuxBin = $true
$arg.Substring(2).ToCharArray() | ForEach-Object {
if ($LinuxBinArguments -notcontains "-$_") {
$IsLinuxBin = $false
}
}
}
}
else {
if ($arg -eq $_) {
$IsLinuxBin = $true
}
}
}
}
$IsLinuxBin = !(Test-Call Get-Content $RemainingArguments)
if ($IsLinuxBin) {
#若是linux的cat.exe
#则调用cat.exe
Expand Down
26 changes: 25 additions & 1 deletion src/scripts/TestCommand.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
function global:Test-Command($Command) {
if(!$Command) { return $false }
if (!$Command) { return $false }
[bool]$(Get-Command $Command -ErrorAction Ignore)
}
function global:Test-Call {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidatePattern('^[\w-]+$')]
[string]
$CommandName,

[Parameter(Mandatory = $true)]
[string[]]
$TestArgs
)
$Command = Get-Command $CommandName

$proxyCode = [System.Management.Automation.ProxyCommand]::Create($Command)
$scriptblock = [ScriptBlock]::Create($proxyCode)
Invoke-Expression "function test { $($scriptblock.Ast.ParamBlock.Extent.Text);`$true }"
try {
Invoke-Expression "test $TestArgs"
}
catch {
$false
}
}

0 comments on commit 162180b

Please sign in to comment.