KLP2 / setup_contributor.ps1
firgiawann's picture
Improve contributor setup with VS Code and pip environment
266752e verified
Raw History Blame Contribute Delete
5.08 kB
param(
[string]$RepoId = "KLP2-PCD/KLP2",
[string]$TargetDir = "",
[switch]$SkipInstall,
[switch]$SkipCodeExtensions
)
$ErrorActionPreference = "Stop"
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host "==> $Message" -ForegroundColor Cyan
}
function Require-Command {
param([string]$Name, [string]$InstallHint)
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
throw "$Name tidak ditemukan. $InstallHint"
}
}
function Get-RepoName {
param([string]$RepoId)
$parts = $RepoId.Split("/")
return $parts[$parts.Length - 1]
}
Write-Step "Cek dependency dasar"
Require-Command "git" "Install Git for Windows: https://git-scm.com/download/win"
Require-Command "python" "Install Python 3.10+ dan aktifkan opsi Add Python to PATH."
$pythonVersion = (& python --version) 2>&1
Write-Host "Python: $pythonVersion"
Write-Host "Git: $(& git --version)"
if (Get-Command "code" -ErrorAction SilentlyContinue) {
Write-Host "VS Code CLI: tersedia"
} else {
Write-Host "VS Code CLI: tidak ditemukan. Install VS Code lalu aktifkan command 'code' dari PATH jika ingin auto-install ekstensi."
}
if ([string]::IsNullOrWhiteSpace($TargetDir)) {
$TargetDir = Join-Path (Get-Location) (Get-RepoName $RepoId)
}
$TargetDir = [System.IO.Path]::GetFullPath($TargetDir)
$repoUrl = "https://huggingface.co/spaces/$RepoId"
Write-Step "Siapkan autentikasi Hugging Face untuk push"
Write-Host "Token harus punya akses write ke repo/organization $RepoId."
Write-Host "Buat token di: https://huggingface.co/settings/tokens"
$hfUsername = Read-Host "Username Hugging Face"
$hfTokenSecure = Read-Host "Token Hugging Face" -AsSecureString
$hfTokenPtr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($hfTokenSecure)
$hfToken = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($hfTokenPtr)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($hfTokenPtr)
if ([string]::IsNullOrWhiteSpace($hfUsername) -or [string]::IsNullOrWhiteSpace($hfToken)) {
throw "Username dan token wajib diisi."
}
$authRepoUrl = "https://$hfUsername`:$hfToken@huggingface.co/spaces/$RepoId"
if (Test-Path $TargetDir) {
Write-Step "Folder repo sudah ada, update dari remote"
Set-Location $TargetDir
if (-not (Test-Path ".git")) {
throw "Folder $TargetDir sudah ada tetapi bukan repository Git."
}
git remote set-url origin $authRepoUrl
git pull origin main
} else {
Write-Step "Clone repo Space"
git clone $authRepoUrl $TargetDir
Set-Location $TargetDir
}
Write-Step "Set identitas Git lokal"
$currentName = (git config user.name) 2>$null
$currentEmail = (git config user.email) 2>$null
if ([string]::IsNullOrWhiteSpace($currentName)) {
$gitName = Read-Host "Nama Git commit"
git config user.name $gitName
}
if ([string]::IsNullOrWhiteSpace($currentEmail)) {
$gitEmail = Read-Host "Email Git commit"
git config user.email $gitEmail
}
Write-Step "Siapkan virtual environment"
if (-not (Test-Path ".venv")) {
python -m venv .venv
}
$venvPython = Join-Path $TargetDir ".venv\Scripts\python.exe"
if (-not (Test-Path $venvPython)) {
throw "Virtual environment gagal dibuat: $venvPython tidak ditemukan."
}
if (-not $SkipInstall) {
Write-Step "Upgrade pip dan install dependency project"
& $venvPython -m pip install --upgrade pip
& $venvPython -m pip install --upgrade setuptools wheel
& $venvPython -m pip install -r requirements.txt
& $venvPython -m pip check
} else {
Write-Host "Skip install dependency karena parameter -SkipInstall dipakai."
}
if ((Get-Command "code" -ErrorAction SilentlyContinue) -and (-not $SkipCodeExtensions)) {
Write-Step "Install ekstensi VS Code yang dibutuhkan"
$extensions = @(
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.jupyter"
)
foreach ($extension in $extensions) {
code --install-extension $extension --force
}
} elseif ($SkipCodeExtensions) {
Write-Host "Skip install ekstensi VS Code karena parameter -SkipCodeExtensions dipakai."
}
Write-Step "Validasi syntax aplikasi"
& $venvPython -m py_compile app.py
Write-Step "Siapkan remote push"
git remote set-url origin $authRepoUrl
git status --short
Write-Host ""
Write-Host "Setup selesai." -ForegroundColor Green
Write-Host "Folder repo: $TargetDir"
Write-Host "Jalankan app lokal:"
Write-Host " .\.venv\Scripts\activate"
Write-Host " python app.py"
Write-Host ""
Write-Host "Dependency Python yang diinstall dari requirements.txt:"
Write-Host " gradio, ultralytics, opencv-python-headless, huggingface_hub, numpy, matplotlib, seaborn"
Write-Host "Ekstensi VS Code yang disiapkan:"
Write-Host " Python, Pylance, Jupyter"
Write-Host ""
Write-Host "Alur push setelah edit:"
Write-Host " git status"
Write-Host " git add app.py README.md CHANGELOG.md requirements.txt"
Write-Host " git commit -m `"update fitur`""
Write-Host " git push origin main"
if (Get-Command "code" -ErrorAction SilentlyContinue) {
Write-Step "Buka VS Code"
code $TargetDir
}