Last active
November 17, 2023 20:48
-
-
Save klkvsk/4e66a68fdaee7c48fb855df9a6d56ffb to your computer and use it in GitHub Desktop.
Rsync wrapper for IDEA to fix rsync deployment from wsl
This file contains hidden or 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
#!/usr/bin/env php | |
<?php | |
/** | |
* Rsync wrapper for IDEA to fix rsync deployment from wsl | |
* In "Rsync settings" set: | |
* - Rsync executable path: "c:\windows\system32\wsl.exe" | |
* - Rsync options: "/path/where/you/saved/wslrsync.php -zar" | |
* - Shell executable path: "ssh" | |
*/ | |
$verbose = true; | |
$cwd = getcwd(); | |
if (str_contains($cwd, 'wsl$')) { | |
// in case you keep your project in WSL, IDEA sets workdir to /mnt/c/wsl$/Ubuntu/home/.. | |
// this is wrong, so "/*/wsl$/<distro_name>" should be removed | |
$realCwd = preg_replace('/^.+wsl\$.[^\/]+/', '', $cwd); | |
} | |
if (str_contains($cwd, ':\\')) { | |
// otherwise, for windows paths we use `wslpath` | |
$realCwd = trim(`wslpath -a $cwd`); | |
} | |
if ($cwd != $realCwd) { | |
if ($verbose) { | |
echo "[fix cwd] $cwd -> $realCwd\n"; | |
} | |
if (file_exists('fileList.txt')) { | |
// for a long list of files, IDEA puts their names in this list file | |
// we need to keep it locally available after changing workdir | |
rename("$cwd/fileList.txt", "$realCwd/fileList.txt"); | |
if ($verbose) { | |
echo "[move fileList.txt] $realCwd/fileList.txt\n"; | |
} | |
} | |
chdir($realCwd); | |
$cwd = $realCwd; | |
} | |
$params = $argv; | |
array_shift($params); // remove this script name | |
foreach ($params as &$param) { | |
// this is mostly for ssh-key path passed in "-e 'ssh -p 22 -i C:\...\id_rsa" | |
// but to be sure, let's convert windows paths found anywhere in params | |
$param = preg_replace_callback( | |
'/[A-Z]:\\\[^ ,\'\"]+/', | |
function ($match) { | |
$winpath = escapeshellarg($match[0]); | |
$wslpath = trim(`wslpath -a $winpath`); | |
if ($verbose) { | |
echo "[fix arg] $winpath -> $wslpath\n"; | |
} | |
return $wslpath; | |
}, | |
$param | |
); | |
$param = escapeshellarg($param); | |
} | |
$cmd = "rsync " . ($verbose ? '-v ' : '') . implode(' ', $params); | |
if ($verbose) { | |
echo "-- running: \n$cmd\n--->\n"; | |
} | |
// output will be passed through | |
system($cmd, $exitCode); | |
if (file_exists("$cwd/fileList.txt")) { | |
@unlink("$cwd/fileList.txt"); | |
} | |
exit($exitCode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment