-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileOverwrite.ps1
60 lines (56 loc) · 1.62 KB
/
fileOverwrite.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
## Force overwrite specified configurations from src and dest
## File format of $WIN_SRC_DEST:
## # comments
## src $HOME\dotfiles\.config\nvim
## dest $HOME\AppData\Local
## Result: cp -r -fo $HOME\dotfiles\.config\nvim $HOME\AppData\Local
# debugging: .\fileOverwrite -dry
# or: .\fileOverwrite -dry:$true
param(
[switch] $dry = $false,
[switch] $verbose = $false
)
$CWD_START = $PWD
$DOTFILE_PATH = "$HOME\dotfiles"
$WIN_SRC_DEST = "$HOME\dotfiles\win_src_dest"
function CheckLastExitCode {
if (!$?) {
exit 1
}
return 0
}
if ($verbose) {
if (-Not ($dry)) {
Write-Output $('dry mode, not overwriting paths from ' + $WIN_SRC_DEST)
} else {
Write-Output $('overwriting paths from ' + $WIN_SRC_DEST)
}
}
$content = Get-Content -Path $WIN_SRC_DEST
$SrcPath = ""
$DestPath = ""
if ($verbose) { Write-Output("lines: $($content.Length)") }
for ($i=0; $i -lt $content.Length; $i+=1) {
if (($content[$i] -eq "") -Or ($content[$i].StartsWith("#"))) {
continue
}
if (-Not ($content[$i].StartsWith("src "))) {
Write-Output "'${WIN_SRC_DEST}:$($i+1)': no prefix 'src ', exiting.. "; exit 1;
}
$SrcPath = $content[$i].Substring(4)
if ($verbose) { Write-Output $SrcPath }
$i+=1
if (-Not ($content[$i].StartsWith("dest "))) {
Write-Output "'${WIN_SRC_DEST}:$($i+1)': no prefix 'dest ', exiting.. "; exit 1;
}
$DestPath = $content[$i].Substring(5)
$Cmd = "cp -r -fo $SrcPath $DestPath"
$IexecCmd = '& ' + $Cmd
if ($verbose) { Write-Output $Cmd }
# Write-Output $IexecCmd
# cp -r -fo $SrcPath $DestPath
if (-Not ($dry)) {
Invoke-Expression $IexecCmd
}
CheckLastExitCode
}