scripts/modules/initialization/setup-initialization.ps1
# strangeloop Setup - Project Initialization Module # Version: 1.0.0 param( [string]${loop-name}, [string]${app-name}, [string]$Platform, [switch]${requires-wsl} ) # Import shared modules $SharedPath = Split-Path $PSScriptRoot -Parent | Join-Path -ChildPath "shared" Import-Module "$SharedPath\write-functions.ps1" -Force -DisableNameChecking Import-Module "$SharedPath\test-functions.ps1" -Force -DisableNameChecking Import-Module "$SharedPath\path-functions.ps1" -Force -DisableNameChecking function initialize-project { param( [string]${loop-name}, [string]${app-name}, [string]$Platform, [switch]${requires-wsl} ) Write-Step "Initializing strangeloop Project..." try { # Get application name if not provided if (-not $AppName) { # Generate default name based on loop name $defaultName = if ($LoopName) { "Test-$LoopName-app" } else { "Test-strangeloop-app" } if ($Force) { $AppName = "$defaultName-$(Get-Date -Format 'yyyyMMdd-HHmmss')" Write-Info "Auto-generated app name: $AppName" } else { $userInput = Read-UserPrompt -Prompt "Enter application name" -DefaultValue $defaultName if ([string]::IsNullOrWhiteSpace($userInput)) { $AppName = $defaultName Write-Info "Using default app name: $AppName" } else { $AppName = $userInput.Trim() } } } # Validate app name if ($AppName -notmatch "^[a-zA-Z0-9-_]+$") { Write-Error "Invalid application name. Use only letters, numbers, hyphens, and underscores." return $false } # Validate loop name if (-not $LoopName) { Write-Error "No loop selected for initialization" return $false } # Determine target environment and project path if ($RequiresWSL -and $Platform -eq "WSL") { Write-Info "Project will be created in WSL environment" # Check if WSL is available if (-not (Get-Command wsl -ErrorAction SilentlyContinue)) { Write-Error "WSL is required but not available" return $false } # Create project in WSL home directory try { $wslUser = & wsl -- whoami 2>$null if ($wslUser) { $wslProjectPath = "/home/$($wslUser.Trim())/AdsSnR_Containers/services/$AppName" } else { $wslProjectPath = "/home/\$USER/AdsSnR_Containers/services/$AppName" } } catch { $wslProjectPath = "/home/\$USER/AdsSnR_Containers/services/$AppName" } # Check if project already exists in WSL $existsResult = wsl -- test -d "$wslProjectPath" if ($LASTEXITCODE -eq 0) { # Directory exists, check if it has strangeloop project $isstrangeloopProject = wsl -- test -d "$wslProjectPath/strangeloop" if ($LASTEXITCODE -eq 0) { # It's a strangeloop project if ($Force) { Write-Info "Removing existing strangeloop project in WSL (forced)..." $removeResult = wsl -- rm -rf "$wslProjectPath" if ($LASTEXITCODE -ne 0) { Write-Error "Failed to remove existing project: $removeResult" return $false } } else { Write-Warning "A strangeloop project '$AppName' already exists in WSL" $response = Read-UserPrompt -Prompt "Do you want to overwrite it?" -ValidValues @("y","n") if (-not (Test-YesResponse $response)) { Write-Info "Project creation cancelled by user" return $false } Write-Info "Removing existing project..." $removeResult = wsl -- rm -rf "$wslProjectPath" if ($LASTEXITCODE -ne 0) { Write-Error "Failed to remove existing project: $removeResult" return $false } } } else { # Directory exists but not a strangeloop project Write-Warning "Directory '$AppName' already exists in WSL but is not a strangeloop project" if (-not $Force) { $response = Read-UserPrompt -Prompt "Do you want to use this directory?" -ValidValues @("y","n") if (-not (Test-YesResponse $response)) { Write-Info "Project creation cancelled by user" return $false } } } } Write-Progress "Creating project directory in WSL..." # Create directory in WSL (if it doesn't exist) $createDirResult = wsl -- mkdir -p "$wslProjectPath" 2>&1 if ($LASTEXITCODE -ne 0) { Write-Error "Failed to create WSL project directory: $createDirResult" return $false } # Initialize strangeloop project in WSL Write-Progress "Initializing strangeloop project in WSL..." $initResult = wsl -- bash -c "cd '$wslProjectPath' && strangeloop init --loop $LoopName" 2>&1 if ($LASTEXITCODE -ne 0) { Write-Warning "strangeloop initialization in WSL encountered issues: $initResult" # Check if strangeloop directory was created despite the error $checkResult = wsl -- test -d "$wslProjectPath/strangeloop" if ($LASTEXITCODE -ne 0) { Write-Error "strangeloop initialization failed - no project structure created in WSL" return $false } Write-Info "Continuing with setup despite initialization warnings..." } else { Write-Success "strangeloop project initialized with loop: $LoopName (in WSL)" } # Update settings in WSL $updateSettingsResult = wsl -- bash -c "cd '$wslProjectPath' && if [ -f strangeloop/settings.yaml ]; then sed -i 's/^name:.*/name: $AppName/' strangeloop/settings.yaml; fi" 2>&1 if ($LASTEXITCODE -eq 0) { Write-Success "Project settings updated with name: $AppName (in WSL)" } # Skip VS Code workspace file creation - not needed for basic functionality # Apply configuration in WSL Write-Progress "Applying project configuration in WSL..." $recurseResult = wsl -- bash -c "cd '$wslProjectPath' && strangeloop recurse" 2>&1 if ($LASTEXITCODE -eq 0) { Write-Success "Project configuration applied successfully (in WSL)" } else { Write-Warning "Project configuration completed with warnings: $recurseResult" } $projectLocation = "WSL: $wslProjectPath" } else { # Windows environment initialization Write-Info "Project will be created in Windows environment" # Create project directory $projectDir = Join-Path (Get-Location) $AppName if (Test-Path $projectDir) { $isstrangeloopProject = Test-Path "$projectDir\strangeloop" if ($isstrangeloopProject) { # It's a strangeloop project if ($Force) { Write-Info "Removing existing strangeloop project (forced)..." Remove-Item $projectDir -Recurse -Force } else { Write-Warning "A strangeloop project '$AppName' already exists at $projectDir" $response = Read-UserPrompt -Prompt "Do you want to overwrite it?" -ValidValues @("y","n") if (-not (Test-YesResponse $response)) { Write-Info "Project creation cancelled by user" return $false } Write-Info "Removing existing project..." Remove-Item $projectDir -Recurse -Force } } else { # Directory exists but not a strangeloop project Write-Warning "Directory '$AppName' already exists but is not a strangeloop project" if (-not $Force) { $response = Read-UserPrompt -Prompt "Do you want to use this directory?" -ValidValues @("y","n") if (-not (Test-YesResponse $response)) { Write-Info "Project creation cancelled by user" return $false } } } } Write-Progress "Creating project directory..." if (-not (Test-Path $projectDir)) { New-Item -ItemType Directory -Path $projectDir -Force | Out-Null } # Change to project directory Set-Location $projectDir # Initialize strangeloop project Write-Progress "Initializing strangeloop project..." try { strangeloop init --loop $LoopName Write-Success "strangeloop project initialized with loop: $LoopName" } catch { Write-Warning "strangeloop initialization encountered issues: $($_.Exception.Message)" # Check if strangeloop directory was created despite the error if (-not (Test-Path ".\strangeloop")) { Write-Error "strangeloop initialization failed - no project structure created" return $false } Write-Info "Continuing with setup despite initialization warnings..." } # Update settings.yaml if it exists $settingsPath = ".\strangeloop\settings.yaml" if (Test-Path $settingsPath) { Write-Progress "Updating project settings..." try { $settingsContent = Get-Content $settingsPath -Raw $updatedSettings = $settingsContent -replace '(name:\s*)[^\r\n]*', "`$1$AppName" Set-Content $settingsPath -Value $updatedSettings -NoNewline Write-Success "Project settings updated with name: $AppName" } catch { Write-Warning "Could not update settings.yaml: $($_.Exception.Message)" } } # Apply configuration Write-Progress "Applying project configuration..." try { strangeloop recurse Write-Success "Project configuration applied successfully" } catch { Write-Warning "Project configuration completed with warnings: $($_.Exception.Message)" } $projectLocation = $projectDir } Write-Success "Project initialization completed successfully" Write-Info "Project location: $projectLocation" return $true } catch { Write-Error "Project initialization failed: $($_.Exception.Message)" return $false } } # Main execution if ($MyInvocation.InvocationName -ne '.') { $result = initialize-project -LoopName $LoopName -AppName $AppName -Platform $Platform -RequiresWSL:$RequiresWSL -Force:$Force if ($result) { Write-Success "Project initialization completed successfully" return @{ Success = $true; Phase = "Initialization"; Message = "Project initialization completed successfully" } } else { Write-Error "Project initialization failed" return @{ Success = $false; Phase = "Initialization"; Message = "Project initialization failed" } } } # Export functions for module usage # Export-ModuleMember -Function @( # 'initialize-project' # ) |