-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-lua.ps1
More file actions
234 lines (197 loc) · 8.07 KB
/
install-lua.ps1
File metadata and controls
234 lines (197 loc) · 8.07 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
Param(
[switch]$Local,
[switch]$FirstLaunchOnly,
[switch]$SkipFirstLaunch,
[switch]$NoBackup,
[string]$Repo = "magic-alt/nvim-cpp-ide",
[string]$SourcePath
)
$ErrorActionPreference = "Stop"
$ProgressPreference = 'SilentlyContinue'
$destNvim = "$HOME\AppData\Local\nvim"
$initLuaPath = "$destNvim\init.lua"
$scriptRoot = $PSScriptRoot
if (-not $scriptRoot -and $MyInvocation.MyCommand.Path) {
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
}
function Write-Header {
Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ Neovim C/C++ IDE Installer (Lua Edition, Neovim 0.11+) ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
}
function Ensure-Git {
if ($script:GitExe) { return $script:GitExe }
$gitPaths = @(
"C:\\Program Files\\Git\\cmd\\git.exe",
"C:\\Program Files (x86)\\Git\\cmd\\git.exe",
"$env:LOCALAPPDATA\\Programs\\Git\\cmd\\git.exe",
"git"
)
foreach ($gitCandidate in $gitPaths) {
try {
$testGit = if ($gitCandidate -eq "git") { "git" } else { $gitCandidate }
$null = & $testGit --version 2>&1
if ($LASTEXITCODE -eq 0) {
$script:GitExe = $testGit
if ($gitCandidate -ne "git") {
Write-Host " Using Git at: $gitCandidate" -ForegroundColor DarkGray
}
return $script:GitExe
}
} catch {
continue
}
}
throw "Git not found. Install it first: winget install Git.Git"
}
function Backup-NvimConfig {
param([string]$Path)
if (-not (Test-Path $Path)) { return }
$backupName = "$Path.bak.$([DateTime]::Now.ToString('yyyyMMdd-HHmmss'))"
Write-Host " Backing up existing config to:" -ForegroundColor Yellow
Write-Host " $backupName" -ForegroundColor DarkYellow
Rename-Item $Path $backupName
Write-Host " ✓ Backup created" -ForegroundColor Green
}
function Install-FromLocal {
param([string]$SourceRoot, [string]$Destination)
if (-not $SourceRoot) {
throw "Local mode requires -SourcePath or running from a saved script file."
}
$initSource = Join-Path $SourceRoot "init.lua"
if (-not (Test-Path $initSource)) {
throw "init.lua not found at: $SourceRoot"
}
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
Copy-Item -Force $initSource (Join-Path $Destination "init.lua")
Write-Host " ✓ Copied init.lua from $SourceRoot" -ForegroundColor Green
}
function Install-FromRemote {
param([string]$RepoName, [string]$Destination, [string]$GitExe)
$tmp = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString())
Write-Host "[2/3] Clone repo..." -ForegroundColor Cyan
try {
Write-Host " Cloning https://github.com/$RepoName.git" -ForegroundColor DarkGray
$cloneArgs = @("clone", "--depth", "1", "https://github.com/$RepoName.git", $tmp)
if ($GitExe -eq "git") {
& git $cloneArgs 2>&1 | Out-Null
} else {
& $GitExe $cloneArgs 2>&1 | Out-Null
}
if ($LASTEXITCODE -ne 0) {
throw "Git clone failed (exit code: $LASTEXITCODE)"
}
$initSource = Join-Path $tmp "init.lua"
if (-not (Test-Path $initSource)) {
throw "init.lua not found in repository!"
}
New-Item -ItemType Directory -Force -Path $Destination | Out-Null
Copy-Item -Force $initSource (Join-Path $Destination "init.lua")
Write-Host " ✓ Installed to: $Destination\init.lua" -ForegroundColor Green
} finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}
}
function Invoke-FirstLaunch {
param([string]$InitPath, [string]$GitExe)
Write-Host "[First Launch] Preparing environment..." -ForegroundColor Cyan
if (-not (Test-Path $InitPath)) {
throw "init.lua not found at: $InitPath"
}
Write-Host " ✓ Found init.lua" -ForegroundColor Green
$nvimCmd = Get-Command nvim -ErrorAction SilentlyContinue
if (-not $nvimCmd) {
throw "Neovim (nvim) not found in PATH. Install Neovim 0.11+ and rerun with -FirstLaunchOnly."
}
$nvimData = "$env:LOCALAPPDATA\nvim-data"
$lazyPath = "$nvimData\lazy\lazy.nvim"
if (-not (Test-Path $lazyPath)) {
Write-Host " Installing lazy.nvim plugin manager..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path (Split-Path $lazyPath) | Out-Null
$cloneArgs = @("clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", "--branch=stable", $lazyPath)
try {
if ($GitExe -eq "git") {
& git $cloneArgs 2>&1 | Out-Null
} else {
& $GitExe $cloneArgs 2>&1 | Out-Null
}
if ($LASTEXITCODE -ne 0) {
throw "lazy.nvim clone failed (exit code: $LASTEXITCODE)"
}
Write-Host " ✓ lazy.nvim installed" -ForegroundColor Green
} catch {
throw "无法安装 lazy.nvim:$_"
}
} else {
Write-Host " ✓ lazy.nvim already present" -ForegroundColor Green
}
Write-Host " Synchronizing plugins (Lazy! sync)..." -ForegroundColor Cyan
try {
$syncOutput = & $nvimCmd.Source --headless "+Lazy! sync" +qa 2>&1
if ($LASTEXITCODE -eq 0 -or $LASTEXITCODE -eq $null) {
Write-Host " ✓ Plugin sync completed" -ForegroundColor Green
} else {
Write-Host " ⚠️ Sync completed with warnings (exit code: $LASTEXITCODE)" -ForegroundColor Yellow
if ($syncOutput) {
Write-Host $syncOutput -ForegroundColor DarkYellow
}
}
} catch {
Write-Host " ⚠️ Failed to run Lazy sync" -ForegroundColor Yellow
Write-Host $_.Exception.Message -ForegroundColor DarkYellow
}
$pluginRoot = "$nvimData\lazy"
$pluginDirs = Get-ChildItem -Path $pluginRoot -Directory -ErrorAction SilentlyContinue
if ($pluginDirs) {
Write-Host " ✓ Detected $($pluginDirs.Count) plugins" -ForegroundColor Green
$pluginDirs | Select-Object -First 10 | ForEach-Object {
Write-Host " - $($_.Name)" -ForegroundColor DarkGray
}
if ($pluginDirs.Count -gt 10) {
Write-Host " ... and $($pluginDirs.Count - 10) more" -ForegroundColor DarkGray
}
} else {
Write-Host " ⚠️ No plugins detected under $pluginRoot" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Launch Neovim: nvim" -ForegroundColor White
Write-Host " 2. Install recommended LSP servers (in Neovim):" -ForegroundColor White
Write-Host " :MasonInstall clangd lua-language-server clang-format stylua" -ForegroundColor DarkGray
Write-Host " 3. Run health checks: :checkhealth" -ForegroundColor White
Write-Host " 4. Explore keybindings: press <Space> (which-key)" -ForegroundColor White
}
if ($FirstLaunchOnly -and $SkipFirstLaunch) {
throw "-FirstLaunchOnly cannot be combined with -SkipFirstLaunch"
}
Write-Header
if (-not $FirstLaunchOnly) {
Write-Host "[1/3] Backup old configs..." -ForegroundColor Cyan
if (-not $NoBackup) {
Backup-NvimConfig -Path $destNvim
} elseif (-not (Test-Path $destNvim)) {
Write-Host " No existing config found" -ForegroundColor DarkGray
} else {
Write-Host " Skipping backup as requested" -ForegroundColor Yellow
Remove-Item -Recurse -Force $destNvim
}
if ($Local) {
Write-Host "[2/3] Install init.lua from local path..." -ForegroundColor Cyan
$resolvedSource = if ($SourcePath) { $SourcePath } else { $scriptRoot }
Install-FromLocal -SourceRoot $resolvedSource -Destination $destNvim
} else {
$gitExe = Ensure-Git
Install-FromRemote -RepoName $Repo -Destination $destNvim -GitExe $gitExe
}
Write-Host "[3/3] Installation complete" -ForegroundColor Green
Write-Host ""
}
if ($SkipFirstLaunch) {
Write-Host "Skipping first-launch bootstrap as requested (-SkipFirstLaunch)." -ForegroundColor Yellow
} else {
$gitExeForBootstrap = Ensure-Git
Invoke-FirstLaunch -InitPath $initLuaPath -GitExe $gitExeForBootstrap
}
Write-Host ""
Write-Host "All done!" -ForegroundColor Green