Files
git-workshop/module-02-history/setup.ps1
2026-01-04 15:28:05 +01:00

208 lines
5.3 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 02 challenge environment with commit history.
.DESCRIPTION
This script creates a challenge directory with a Git repository that
contains multiple commits for students to explore using git log and git diff.
#>
Write-Host "`n=== Setting up Module 02 Challenge ===" -ForegroundColor Cyan
# Remove existing challenge directory if it exists
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Recurse -Force "challenge"
}
# Create fresh challenge directory
Write-Host "Creating challenge directory..." -ForegroundColor Green
New-Item -ItemType Directory -Path "challenge" | Out-Null
Set-Location "challenge"
# Initialize Git repository
Write-Host "Initializing Git repository..." -ForegroundColor Green
git init | Out-Null
# Configure git for this repository
git config user.name "Workshop Student"
git config user.email "student@example.com"
# Commit 1: Initial project structure
Write-Host "Creating initial project structure..." -ForegroundColor Green
$readmeContent = @"
# My App
This is a simple web application built with Python.
## Features
- User authentication
- Database integration
- User profiles
"@
Set-Content -Path "README.md" -Value $readmeContent
$appContent = @"
# app.py - Main application file
def main():
print("Welcome to My App!")
# Application initialization code here
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Initial project structure" | Out-Null
# Commit 2: Add user authentication
Write-Host "Adding user authentication..." -ForegroundColor Green
$authContent = @"
# auth.py - Authentication module
def login(username, password):
# Authenticate user
print(f"Logging in user: {username}")
return True
def logout(username):
# Log out user
print(f"Logging out user: {username}")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
$appContent = @"
# app.py - Main application file
from auth import login, logout
def main():
print("Welcome to My App!")
# Application initialization code here
login("user", "password")
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add user authentication" | Out-Null
# Commit 3: Add database connection
Write-Host "Adding database connection..." -ForegroundColor Green
$databaseContent = @"
# database.py - Database connection module
def connect():
# Connect to database
print("Connecting to database...")
return True
def disconnect():
# Disconnect from database
print("Disconnecting from database...")
return True
"@
Set-Content -Path "database.py" -Value $databaseContent
$appContent = @"
# app.py - Main application file
from auth import login, logout
from database import connect, disconnect
def main():
print("Welcome to My App!")
connect()
# Application initialization code here
login("user", "password")
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add database connection" | Out-Null
# Commit 4: Fix authentication bug
Write-Host "Fixing authentication bug..." -ForegroundColor Green
$authContent = @"
# auth.py - Authentication module
def login(username, password):
# Authenticate user
if not username or not password:
print("Error: Username and password required")
return False
print(f"Logging in user: {username}")
return True
def logout(username):
# Log out user
print(f"Logging out user: {username}")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
git add .
git commit -m "Fix authentication bug" | Out-Null
# Commit 5: Add user profile feature
Write-Host "Adding user profile feature..." -ForegroundColor Green
$profileContent = @"
# profile.py - User profile module
def get_profile(username):
# Get user profile
print(f"Fetching profile for: {username}")
return {"username": username, "email": f"{username}@example.com"}
def update_profile(username, data):
# Update user profile
print(f"Updating profile for: {username}")
return True
"@
Set-Content -Path "profile.py" -Value $profileContent
$appContent = @"
# app.py - Main application file
from auth import login, logout
from database import connect, disconnect
from profile import get_profile
def main():
print("Welcome to My App!")
connect()
# Application initialization code here
if login("user", "password"):
profile = get_profile("user")
print(f"User profile: {profile}")
pass
if __name__ == "__main__":
main()
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add user profile feature" | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. cd challenge" -ForegroundColor White
Write-Host " 2. Explore the commit history using 'git log'" -ForegroundColor White
Write-Host " 3. Answer the questions in README.md by creating 'answers.txt'" -ForegroundColor White
Write-Host " 4. Run '..\verify.ps1' to check your answers" -ForegroundColor White
Write-Host ""