ååã¯ãLinuxã®ç°å¢å€æ°PATHã«åããããŠããã³ãã³ãããã«ãã¹ã®ãªã¹ããšããŠåŸãããããã«ãããããã¯ãLinuxã®ç°å¢å€æ°PATHãæ¬æ¥æã£ãŠããæ€çŽ¢ã®åªå é äœãåæ ãããããã ã ååŸããã³ãã³ãã®ãã¹ããã«ãã¹ã«å€æŽããããšã§ãã©ãã®ãã¹ã®Linuxã³ãã³ããå®è¡ãããŠããã®ããããããã«ãªã£ããæ¬¡ã®ã¹ã¯ãªãããã該åœãã倿Žéšåã ã
$_linux_command_paths = (
wsl ls -d ($_linux_path[($_linux_path.length-1)..0] -replace "$","/*")
) 2> $null
ã¹ã¯ãªããèªäœã¯çãããPowerShellãå©çšããäžã§éèŠãªããã€ãã®ãã£ããã¹ã䜿ãããŠããã以äžã«ããã®ã¹ã¯ãªããã®ãã€ã³ããããäžåºŠãŸãšããŠãããã
- é åã®äžèº«ãéé ã§äœ¿ã
- é åã®äžèº«ã«å¯ŸããŠåã ã«æåå眮æã宿œ
- æåå眮æãæ£èŠè¡šçŸã§æå®
- ãšã©ãŒã¡ãã»ãŒãžãåé€
- å®è¡çµæã倿°ãªã©ã«ä»£å ¥ããã«ãã®ãŸãŸå©çš
ä»åã¯ãã®ããŒã¿ããããŸã§ã«äœæãã$PROFILEã«é©çšããŠãã³ãã³ãå®è¡ã®åªå
é äœãPowerShellåŽã§å¶åŸ¡ã§ããããã«æžãæããã
ãã«ãã¹ã掻çšããåŠçã«å€æŽãã
ãŸãLinuxã³ãã³ãã颿°ã§ã©ããã³ã°ããããã®ã¹ã¯ãªããããã§ãã¯ããããããŸã§ã¯æ¬¡ã®ããã«ãªã£ãŠããã
ForEach($n in $_linux_command_names) {
if ($n -ne "") {
$_linux_functions += "
function $n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args)
}
else {
wsl $n `$(_path_to_linux `$Args)
}
}"
}
}
å ã«çµæãæ²èŒããŠãããäžèšã次ã®ããã«æžãæããããšã§ãä»åã®ç®çãéæã§ããã
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args)
}
else {
wsl $n `$(_path_to_linux `$Args)
}
}"
}
Remove-Variable n
Remove-Variable _n
ã§ã¯ã现ããèŠãŠãããããŸãããããŸã§ã³ãã³ãåã ãã§ã«ãŒããåããŠããéšåããæ¬¡ã®ããã«ãã«ãã¹ã®ã³ãã³ããªã¹ãã§åãããã«å€æŽãããããã§å€æ°$nã«ã¯ãã«ãã¹ã®ã³ãã³ããä»£å ¥ãããŠããããšã«ãªãã
ForEach($n in $_linux_command_paths) {
ãã ãã颿°åã¯ãã«ãã¹ã§ã¯ãªãã³ãã³ãåã§äœæããã®ã§ããã«ãã¹ããã³ãã³ãåãåãåºãå¿ èŠãããããã®åŠçãè¡ã£ãŠããã®ã次ã®åŠçã ã
$_n = (Split-Path -Leaf $n)
ãã®åŠçã§å€æ°$_nã«ã³ãã³ãåïŒãã¡ã€ã«åïŒãå ¥ãããšã«ãªããã³ãã³ãåã ããåãåºãæ¹æ³ã¯ã»ãã«ããããæååã®å å·¥åŠçãªã©ã䜿ã£ãŠãããã ãããããã§ã¯ãèšè¿°ãç°¡åãªäžèšã®åŠçã䜿ã£ãã
颿°ãå®çŸ©ããŠããéšåããæ¬¡ã®ããã«æžãæããããšã«ãªãã
function $_n {
以äžã®2ã€ã®è¡ã«é¢ããŠã¯ãèšè¿°èªäœã¯ãããŸã§ãšåãã ã
`$Input | wsl $n `$(_path_to_linux `$Args)
wsl $n `$(_path_to_linux `$Args)
ãã ãã倿°$nã«ã¯ã³ãã³ãåã§ã¯ãªããã«ãã¹ãå ¥ã£ãŠãããã€ãŸããwslã«ãã«ãã¹ãæå®ããŠã³ãã³ããå®è¡ããåŠçã«å€ãã£ãŠããããšã«ãªããããã§æžãæãå®äºã ã
次ã®2è¡ã¯ããŸãã ãäžå¿ãããã ãã§ãã䜿ã£ãŠããªã倿°ãªã®ã§ãå¿ èŠæ§ããªããªã£ãæç¹ã§å€æ°ãåé€ããŠããã
Remove-Variable n
Remove-Variable _n
åäœã確èªããŠã¿ãã
æžãæãã$PROFILEã§PowerShellãèµ·åããŠåäœã確ãããŠã¿ãã$_linux_command_pathsãš$_linux_functionsã¯æ¶ããã«æ®ããŠããã®ã§ããã®äžèº«ã確èªããŠã¿ããããŸãã$_linux_command_pathsã確èªãããšã次ã®ããã«ãªã£ãŠããã
åªå é äœã®äœããã¹ã®ã³ãã³ããæåã«è¡šç€ºãããåªå é äœã®é«ããã¹ãæåŸã«è¡šç€ºãããŠããããšããããããããŸã§ã¯ååãšåãã ãæ¬¡ã«$_linux_functionsã®äžèº«ã確èªãããšã次ã®ããã«ãªã£ãŠããããšããããã
wslã§æå®ããŠããã³ãã³ãããããŸã§ã®ã³ãã³ãåã ããããã«ãã¹ã«å€ãã£ãŠããããšãããããå®éã«ã¯å€æŽããåãšåŸã§å®éã«å®è¡ãããã³ãã³ãã«éãã¯ãªãã®ã ããä»åã®æžãå€ãã§PowerShellåŽããå®è¡ããã³ãã³ããå¶åŸ¡ããããç¶æ ã«ã¯ãªã£ããPowerShellã®åäœäŸãšããŠããç°¡åãªããã«å¹æãèŠèŸŒã¿ããããã®ã ãšèšããã ããã
ãããŸã§ã«äœã£ãŠãã$PROFILEã«ãã£ãŠããã®PowerShellã¯LinuxåŽã®ã³ãã³ããããªãã·ãŒã ã¬ã¹ã«å®è¡ã§ããããã«ãªã£ããæ®æ®µLinuxãmacOSã®ã¿ãŒããã«ã§äœæ¥ãããŠããæ¹ã§ããã°ããã®ç°å¢ã¯çµæ§ãªãã¿ãããã®ã§ã¯ãªãããšæãããŸã ããã€ãã®ç¹ã§äžäŸ¿ãªãšãããæ®ã£ãŠããã®ã§ããã®ãããããã¥ãŒãã³ã°ããŠããã°ãPowerShellãšWindowsåŽã®ã³ãã³ãããããŠLinuxã®ã³ãã³ããããã«ã·ãŒã ã¬ã¹ã«å®è¡ã§ããããã«ãªãã¯ãã ã
ä»é²: $PROFILE
æ¬é£èŒæç¹ã§ã®$PROFILEãæ¬¡ã«æ²èŒããŠããã
# Copyright (c) 2020 Daichi GOTO <daichi@ongs.co.jp>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# author: Daichi GOTO (daichi@ongs.co.jp)
# first edition: Mon Jun 22 18:20:36 JST 2020
#========================================================================
# Definition of Linux commands used via wsl
#========================================================================
$_linux_path = (wsl echo '$PATH').Split(":") -NotMatch "/mnt"
$_linux_command_paths = (
wsl ls -d ($_linux_path[($_linux_path.length-1)..0] -replace "$","/*")
) 2> $null
# Generate Linux command functions
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args)
}
else {
wsl $n `$(_path_to_linux `$Args)
}
}"
}
Remove-Variable n
Remove-Variable _n
$_linux_functions += @'
function _path_to_linux {
$linuxpath = @()
# Convert arguments to Linux path style
ForEach($winpath in $Args) {
if ($winpath -eq $null) {
Break
}
# Change drive path to mount path
if ($winpath -match '^[A-Z]:') {
$drive = $winpath.Substring(0,1).ToLower()
$linuxpath += "/mnt/" + $drive + $winpath.Substring(2).Replace('\','/')
}
# Option is not converted
elseif ($winpath -match '^[-+]') {
$linuxpath += $winpath
}
# Other argument is converted
else {
$linuxpath += ([String]$winpath).Replace('\','/')
}
}
$linuxpath
}
'@
# Prepare temporary file path with extension .ps1
$_temp = New-TemporaryFile
$_temp_ps1 = $_temp.FullName + ".ps1"
Remove-Item $_temp
# Write function definition to temporary .ps1 file and parse
$_linux_functions | Out-File $_temp_ps1
. $_temp_ps1
Remove-Item $_temp_ps1
# Delete unnecessary variables
Remove-Variable _temp
Remove-Variable _temp_ps1
#Remove-Variable _linux_path
#Remove-Variable _linux_command_paths
#Remove-Variable _linux_functions
#========================================================================
# Individual Linux command function definitions
#========================================================================
# grep
function grep {
$pattern_exists = $False
$path_exists = $False
$skip = $False
$i = 0
ForEach($a in $Args) {
if ($skip) {
$skip = $False
$i++
continue
}
# Options without argumetn
if ($a -cmatch '^-[abcdDEFGHhIiJLlmnOopqRSsUVvwxZ]') {
}
# Options with argument
elseif ($a -cmatch '^-[ABC]') {
$skip = $True
}
# Pattern file specification option
elseif ($a -ceq '-f') {
$skip = $True
$pattern_exists = $True
$Args[$i+1] = _path_to_linux $Args[$i+1]
}
# Pattern specification option
elseif ($a -ceq '-e') {
$skip = $True
$pattern_exists = $True
}
# Pattern or file path
elseif ($a -cnotmatch '^-') {
if ($pattern_exists) {
$path_exists = $True
}
else {
$pattern_exists = $True
}
}
$i++
}
# Change file path
if ($path_exists) {
$Args[-1] = _path_to_linux $Args[-1]
}
$Input | wsl grep $Args
}
# ls
Get-Alias ls *> $null && Remove-Item alias:ls
function ls { wsl ls --color=auto $Args }
function ll { ls -l }
function la { ls -a }
#========================================================================
# Alias definition
#========================================================================
Set-Alias -Name open -Value explorer
Set-Alias -Name edge -Value "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
Set-Alias -Name chrome -Value "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

