Files
Rules/init-cursor.ps1
T

174 lines
5.9 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
Инициализация Cursor в текущем проекте; на новом ПК сначала настраивает git/SSH.
.DESCRIPTION
Один вызов init-cursor:
- если машина не настроена: SSH, Gitea keys, clone Rules в $HOME\CursorRules
- затем копирует rules в текущий репозиторий и настраивает remotes
Первый запуск (приватный repo — через git clone, см. README):
git clone --depth 1 ssh://git@git.papatramp.ru:2222/PapaTramp/Rules.git $HOME\CursorRules
& $HOME\CursorRules\install.ps1
.EXAMPLE
cd C:\Users\papat\Projects\MyRepo
init-cursor
init-cursor -Update
#>
function Get-CursorRulesDest {
return (Join-Path $HOME 'CursorRules')
}
function Get-CursorBootstrapRoot {
param([string]$RulesDest)
$scriptRoot = $PSScriptRoot
if (Test-Path (Join-Path $scriptRoot 'scripts\Invoke-MachineBootstrap.ps1')) {
return $scriptRoot
}
if (Test-Path (Join-Path $RulesDest 'local\git-ssh-hosts.conf')) {
return $RulesDest
}
if (Test-Path (Join-Path $scriptRoot 'local\git-ssh-hosts.conf')) {
return $scriptRoot
}
return $null
}
function Initialize-CursorRulesEnvironment {
[CmdletBinding()]
param(
[switch]$Update
)
$rulesDest = Get-CursorRulesDest
$bootstrapRoot = Get-CursorBootstrapRoot -RulesDest $rulesDest
if (-not $bootstrapRoot) {
Write-Host 'Error: CursorRules not found. Run install.ps1 from Gitea first:' -ForegroundColor Red
Write-Host ' git clone --depth 1 ssh://git@git.papatramp.ru:2222/PapaTramp/Rules.git $HOME\CursorRules' -ForegroundColor Yellow
Write-Host ' & $HOME\CursorRules\install.ps1' -ForegroundColor Yellow
return $false
}
$bootstrapScript = Join-Path $bootstrapRoot 'scripts\Invoke-MachineBootstrap.ps1'
if (-not (Test-Path -LiteralPath $bootstrapScript)) {
Write-Host "Error: bootstrap script not found: $bootstrapScript" -ForegroundColor Red
return $false
}
$repoReady = Test-Path (Join-Path $rulesDest '.git')
$skipClone = $repoReady
& $bootstrapScript -Root $bootstrapRoot -RulesDest $rulesDest -SkipClone:$skipClone
if ($Update -and $repoReady) {
Write-Host '[*] Updating CursorRules from remote...' -ForegroundColor Cyan
git -C $rulesDest pull --ff-only
if ($LASTEXITCODE -ne 0) {
Write-Host '[!] git pull failed in CursorRules' -ForegroundColor Yellow
}
else {
Write-Host '[+] CursorRules updated' -ForegroundColor Green
}
}
return $true
}
function Install-CursorInCurrentProject {
[CmdletBinding()]
param(
[switch]$SkipGitRemotes
)
$source = Get-CursorRulesDest
$rulesSrc = Join-Path $source '.cursor\rules'
$scripts = Join-Path $source 'scripts'
$cursorIgnore = Join-Path $source '.cursorignore'
if (Test-Path $cursorIgnore) {
Copy-Item $cursorIgnore . -Force
Write-Host '[+] .cursorignore copied' -ForegroundColor Green
}
if (Test-Path $rulesSrc) {
New-Item -ItemType Directory -Path '.\.cursor\rules' -Force | Out-Null
Copy-Item (Join-Path $rulesSrc '*.mdc') '.\.cursor\rules\' -Force
Write-Host '[+] .cursor/rules (*.mdc) copied' -ForegroundColor Green
}
$gitIgnorePath = Join-Path (Get-Location) '.gitignore'
$ignoreLines = @('.cursor/', '.cursorignore')
if (Test-Path (Join-Path (Get-Location) '.git')) {
if (-not (Test-Path $gitIgnorePath)) {
Set-Content -Path $gitIgnorePath -Value ($ignoreLines -join "`n") -Encoding UTF8
Write-Host '[+] .gitignore created (.cursor/, .cursorignore)' -ForegroundColor Green
}
else {
$existing = Get-Content $gitIgnorePath -Raw
$added = @()
foreach ($line in $ignoreLines) {
if ($existing -notmatch "(?m)^$([regex]::Escape($line))\s*$") {
Add-Content -Path $gitIgnorePath -Value $line
$added += $line
}
}
if ($added.Count -gt 0) {
Write-Host "[+] .gitignore updated: $($added -join ', ')" -ForegroundColor Green
}
}
}
if ($SkipGitRemotes) {
Write-Host '[!] Git remotes skipped (-SkipGitRemotes)' -ForegroundColor Yellow
return
}
git rev-parse --is-inside-work-tree 2>$null | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host '[!] Not a git repo - remotes skipped' -ForegroundColor Yellow
return
}
$setRemotes = Join-Path $scripts 'Set-GitRemotes.ps1'
if (Test-Path $setRemotes) {
Write-Host '[*] Configuring git remotes...' -ForegroundColor Cyan
& powershell -NoProfile -ExecutionPolicy Bypass -File $setRemotes
}
$ensureRepo = Join-Path $scripts 'Ensure-GiteaHomeRepo.ps1'
if (Test-Path $ensureRepo) {
$repoName = Split-Path -Leaf (git rev-parse --show-toplevel)
Write-Host "[*] Ensuring home Gitea repo: $repoName" -ForegroundColor Cyan
try {
& powershell -NoProfile -ExecutionPolicy Bypass -File $ensureRepo -RepoName $repoName
}
catch {
Write-Host "[!] home Gitea ensure failed: $($_.Exception.Message)" -ForegroundColor Yellow
}
}
}
function init-cursor {
[CmdletBinding()]
param(
[switch]$SkipGitRemotes,
[switch]$Update
)
if (-not (Initialize-CursorRulesEnvironment -Update:$Update)) {
return
}
Install-CursorInCurrentProject -SkipGitRemotes:$SkipGitRemotes
Write-Host '[i] Remotes policy: kalinamall=truth; home=Push-HomeMirror; github=Publish-GithubOrphan' -ForegroundColor DarkCyan
Write-Host '[+] init-cursor complete' -ForegroundColor Green
}
if ($MyInvocation.InvocationName -eq '.') {
Write-Host '[+] init-cursor loaded - run: init-cursor' -ForegroundColor DarkCyan
}