-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
305 lines (230 loc) · 7.97 KB
/
Copy pathinstall.ps1
File metadata and controls
305 lines (230 loc) · 7.97 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<#
.SYNOPSIS
vimrcファイルをインストールする
既存のvimrcファイルが存在する場合、 ファイル名_backup_日時 というファイルで同一ディレクトリ下にバックアップする
また、既存のvimrcファイルが本スクリプトで生成したファイルであった場合でも、同様にバックアップする。
.PARAMETER Help
本スクリプトの説明を標準エラー出力する
.PARAMETER Uninstall
本スクリプトで生成したファイルを削除し、本スクリプトで変更したバックアップファイルからvimrcファイルに戻す
なお、このコマンドだけでは本リポジトリそのものの削除はしない
.EXAMPLE
PS> ./install.ps1 [options]
#>
Param(
[Switch]$Help,
[Switch]$Uninstall
)
$HELP_MESSAGE = @"
This is installer for vimrc.
If an vimrc file exists, backup it under the same directory to rename the file name: \${file_name}_backup_\${date}.
Even if the existing vimrc file is a file generated by this script, it will be backup as well.
Arguments:
Option
-h, -Help: Show help message.
-u, -Uninstall:
Remove a file generated by this script, and restore backup file to vimrc.
But this script is not removed this repository.
Examples:
./install.ps1 [option]
"@
$VIMRC_DESCRIPTION = "Morichan's vimrc file"
<#
.SYNOPSIS
vimrcファイルが存在するディレクトリの絶対パスを返す
#>
function Get-VimrcDirectoryPath {
return (Get-Command vim.exe).Path | Split-Path -Parent
}
<#
.SYNOPSIS
vimrcファイルの絶対パスを返す
#>
function Get-VimrcFilePath {
return "$(Get-VimrcDirectoryPath)\vimrc"
}
<#
.SYNOPSIS
本スクリプト実行前に存在するvimrcファイルをバックアップする
.PARAMETER FilePath
バックアップ対象のvimrcファイル絶対パス
#>
function Backup-VimrcFile {
Param(
[Parameter(Mandatory)][String]$FilePath
)
if (-not (Test-Path $FilePath)) {
Write-Host "File is not found, so don't backup: $FilePath"
return
}
$Date = "$(Get-Date -Format 'yyyyMMddHHmmss')"
Write-Host "+ Rename-Item $FilePath ${FilePath}_backup_$Date"
Rename-Item "$FilePath" "${FilePath}_backup_$Date"
}
<#
.SYNOPSIS
vimrcファイルを生成する
動的生成するための変数が多いため、sampleファイルからのコピーはせずにOut-Fileコマンドで生成する
.PARAMETER FilePath
出力先のvimrcファイル絶対パス
.PARAMETER Description
出力先のvimrcファイルの先頭に記載する説明文、これを元に Is-GeneratedByThisScript の実行を判定する
#>
function Generate-VimrcFile {
Param(
[Parameter(Mandatory)][String]$FilePath,
[Parameter(Mandatory)][String]$Description
)
$RepositoryPath = $PSScriptRoot
$ReadmePath = "$RepositoryPath\README.md"
$RcPath = "$RepositoryPath\rc"
if (Test-Path $FilePath) {
Write-Host "File already exists, so don't generate: $FilePath"
return
}
Write-Host "+ @"" ... ""@ | Out-File -FilePath $FilePath"
@"
" $Description
" This is custom settins, for more details: $ReadmePath
set runtimepath+=$RcPath
let g:VIM_DOTFILES_ROOT_DIR='$RepositoryPath'
runtime! init.vim
"@ | Out-File -FilePath $FilePath -Encoding utf8
}
<#
.SYNOPSIS
対象ファイルが本スクリプトで生成したファイルの場合は正常終了する
2番目の引数の文字列が1行目に記載されている場合に、本スクリプトで生成したファイルとして判断する
.PARAMETER FilePath
対象ファイルの絶対パス
.PARAMETER Description
本スクリプトで生成したファイルであると判断するための検索文字列
.OUTPUTS
System.True.
対象ファイルは本スクリプトで生成したファイルであることを表す
正確にはTrulyな値を返す
.OUTPUTS
System.False.
対象ファイルは本スクリプトで生成したファイルでないことを表す
正確にはFalsyな値を返す
#>
function Is-GeneratedByThisScript {
Param(
[Parameter(Mandatory)][String]$FilePath,
[Parameter(Mandatory)][String]$Description
)
return (Test-Path $FilePath) -and ((Get-Content -path $FilePath -TotalCount 1) | Select-String -pattern $Description)
}
<#
.SYNOPSIS
バックアップファイルの絶対パスを返す
.OUTPUTS
System.String.
バックアップファイルの絶対パス
複数存在する場合、Get-ChildItemによる出力結果の1番目のファイルの絶対パスを返す
存在しない場合、空文字を返す
#>
function Get-VimrcBackupFilePath {
$FileName = (Get-ChildItem -Path "$(Get-VimrcDirectoryPath)" -Filter "vimrc_backup_*" | Select-Object -First 1)
$DirPath = "$(Get-VimrcDirectoryPath)"
if ("$FileName" -eq "") {
return ""
}
return "$DirPath\$FileName"
}
<#
.SYNOPSIS
本スクリプトで生成したvimrcファイルを削除する
本スクリプトで生成したか否かの判定は Is-GeneratedByThisScript 関数に依存する
#>
function Remove-VimrcFile {
$FilePath = "$(Get-VimrcFilePath)"
if (-not (Test-Path $FilePath)) {
Write-Host "File is not found, so don't remove: $FilePath"
return
} elseif (-not (Is-GeneratedByThisScript $FilePath "$VIMRC_DESCRIPTION")) {
Write-Host "File is not generated by this script, so don't remove: $FilePath"
return
}
Write-Host "+ Remove-Item $FilePath"
Remove-Item $FilePath
}
<#
.SYNOPSIS
バックアップファイルをvimrcファイルにリストアする
リストア対象のバックアップファイルの選定は Is-GeneratedByThisScript 関数に依存する
#>
function Restore-VimrcFile {
$FilePath = "$(Get-VimrcBackupFilePath)"
if ("$FilePath" -eq "") {
Write-Host "File is not found, so don't restore."
return
} elseif (-not (Test-Path $FilePath)) {
Write-Host "File is not found, so don't restore: $FilePath"
return
}
Write-Host "+ Rename-Item $FilePath $(Get-VimrcFilePath)"
Rename-Item "$FilePath" "$(Get-VimrcFilePath)"
}
<#
.SYNOPSIS
本リポジトリの .vim ディレクトリ配下にdeinをインストールする
インストール済みの場合、deinをアップデートする
#>
function Install-Dein {
$Path = "$PSScriptRoot\.vim\dein\repos\github.com\Shougo"
$DeinPath = "$Path\dein.vim"
if (Test-Path $DeinPath) {
$CurrentDirectory = "$(Convert-Path .)"
Set-Location $DeinPath
git pull
Set-Location $CurrentDirectory
return
}
Write-Host "+ New-Item $Path -ItemType Directory -ErrorAction SilentlyContinue"
New-Item $Path -ItemType Directory -ErrorAction SilentlyContinue
Write-Host "+ git clone https://github.com/Shougo/dein.vim.git $DeinPath"
git clone https://github.com/Shougo/dein.vim.git $DeinPath
}
<#
.SYNOPSIS
本リポジトリの .vim ディレクトリ配下にundoディレクトリを作成する
undoディレクトリはvimのundo機能利用時に呼出す
#>
function Make-UndoDirectory {
$Path = "$PSScriptRoot\.vim\undo"
Write-Host "+ New-Item $Path -ItemType Directory -ErrorAction SilentlyContinue"
New-Item $Path -ItemType Directory -ErrorAction SilentlyContinue
}
<#
.SYNOPSIS
引数を確認する
#>
function Check-Properties {
if ($Help) {
Write-Host $HELP_MESSAGE
exit 1
} elseif ($Uninstall) {
Remove-VimrcFile
Restore-VimrcFile
exit 1
}
}
<#
.SYNOPSIS
メイン関数
#>
function Main {
Check-Properties
$VimrcFilePath = "$(Get-VimrcFilePath)"
$VimrcDescription = $VIMRC_DESCRIPTION
if (Is-GeneratedByThisScript $VimrcFilePath $VimrcDescription) {
Write-Host "Info: vimrc is generated by this script: $VimrcFilePath"
}
Backup-VimrcFile $VimrcFilePath
Generate-VimrcFile $VimrcFilePath $VimrcDescription
Install-Dein
Make-UndoDirectory
Write-Host "Succeeded!"
}
Main