VB.NETの既存プロジェクトに対して一括でgit環境を作成する方法
VB.NETでgit管理する場合で既存のプロジェクトが複数存在するすると、それぞれのフォルダに.gitignoreをコピーして git init、git add、git commit を実行して・・・というのが面倒なのでPowerShellでスクリプトを作成した。(ChatGPTに手伝ってもらった)
・ $RootPathにプロジェクトルートを指定。
・ $GitignoreSourceSourceにコピー元".gitignore"ファイルのフルパスを指定
・ $LogFileに処理結果を記録するログファイル(csv形式)のフルパスを指定
あとば以下のファイルを gitinit.ps1 といったファイル名で保存、
このファイルを右クリックで "PowerShellで実行" にて実行すればよい。
・ $RootPathにプロジェクトルートを指定。
ここで指定したフォルダのサブフォルダに.vbprojファイルがあればgit管理対象とする。
・ $GitignoreSourceSourceにコピー元".gitignore"ファイルのフルパスを指定
ここで指定したファイルをプロジェクトフォルダにコピーする。
・ $CommitMessageにコミット時のメッセージを指定。・ $LogFileに処理結果を記録するログファイル(csv形式)のフルパスを指定
あとば以下のファイルを gitinit.ps1 といったファイル名で保存、
このファイルを右クリックで "PowerShellで実行" にて実行すればよい。
# ==========================================
# 設定エリア(ここを変更してください)
# ==========================================
Param(
[string]$RootPath = "C:\Your\Projects\Path", # 探索ルート
[string]$GitignoreSource = "C:\Common\Path\.gitignore", # コピー元の .gitignore
[string]$CommitMessage = "Initial commit (Automated)", # コミットメッセージ
[string]$LogFile = "C:\Your\Path\git_init_log.csv" # ログ出力先
)
# ==========================================
# 処理開始
# ==========================================
# ログ用のヘッダーを作成(初回のみ)
"Date,Status,Path,Message" | Out-File -FilePath $LogFile -Encoding utf8
# .vbproj が含まれるフォルダを再帰的に取得
$projectFolders = Get-ChildItem -Path $RootPath -Filter "*.vbproj" -Recurse |
Select-Object -ExpandProperty DirectoryName -Unique
Write-Host "--- Git Initialization Task Start ---" -ForegroundColor Cyan
foreach ($folder in $projectFolders) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# 1. すでに .git フォルダがある場合はスキップ
if (Test-Path "$folder\.git") {
$msg = "Already initialized"
Write-Host "Skipping: $folder ($msg)" -ForegroundColor Yellow
"$timestamp,Skipped,$folder,$msg" | Out-File -FilePath $LogFile -Append -Encoding utf8
continue
}
try {
Write-Host "Processing: $folder" -ForegroundColor Cyan
Push-Location $folder
# 2. .gitignore をコピー
Copy-Item -Path $GitignoreSource -Destination "$folder\.gitignore" -Force
# 3. Git コマンドの実行
git init -q
git add .
git commit -m $CommitMessage -q
# 成功ログ
"$timestamp,Success,$folder,Initialized with git" | Out-File -FilePath $LogFile -Append -Encoding utf8
Write-Host "Success!" -ForegroundColor Green
}
catch {
# エラーログ
$err = $_.Exception.Message.Replace(",", " ") # CSV崩れ防止
"$timestamp,Error,$folder,$err" | Out-File -FilePath $LogFile -Append -Encoding utf8
Write-Error "Failed to process $folder"
}
finally {
Pop-Location
}
}
Write-Host "--- All Tasks Completed. Log: $LogFile ---" -ForegroundColor Cyan
0 件のコメント:
コメントを投稿