VisualBasic:既存プログラムのGit化

公開:2026.06.03(水) 00:56

VisualBasicで既存プロジェクトをGit化する方法

VisualBasic.NETでのGitを使ったソース管理、
既存プログラムに複数のプロジェクトがある場合、1つ1つにGit Initやって・・てするのは面倒なので
Geminiにスクリプトを考えてもらった。

指定した探索ルートのサブフォルダに.vbprojファイルがあれば、そのフォルダに対してGitを構築する。
スクリプト(PowerShell)の先頭にパラメータがあるのでここを修正すればよい。
.gitignoreファイルはどこかに置いておけば、そのパスを指定することでコピーしてくれる。
処理結果はログ(csvファイル)として出力する。

# ==========================================
# 設定エリア(ここを変更してください)
# ==========================================
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


"Visual Basic" "Git" "PowerShell"

0 件のコメント:

その他の記事