journal/scripts/script-common.ps1
stan44 069b38071c Added Web WebGateway
Added connector so gateway works.
scripts are much more polished and functional.
2026-02-27 11:03:53 -06:00

241 lines
7.8 KiB
PowerShell

function Clear-JournalProxyEnv {
# Clear proxy/no-index env vars that commonly break package restore.
Remove-Item Env:HTTP_PROXY -ErrorAction SilentlyContinue
Remove-Item Env:HTTPS_PROXY -ErrorAction SilentlyContinue
Remove-Item Env:ALL_PROXY -ErrorAction SilentlyContinue
Remove-Item Env:http_proxy -ErrorAction SilentlyContinue
Remove-Item Env:https_proxy -ErrorAction SilentlyContinue
Remove-Item Env:all_proxy -ErrorAction SilentlyContinue
Remove-Item Env:GIT_HTTP_PROXY -ErrorAction SilentlyContinue
Remove-Item Env:GIT_HTTPS_PROXY -ErrorAction SilentlyContinue
Remove-Item Env:PIP_NO_INDEX -ErrorAction SilentlyContinue
}
function Test-JournalRepoRootCandidate {
param(
[string]$Path
)
if ([string]::IsNullOrWhiteSpace($Path)) {
return $false
}
$markers = @(
"Journal.slnx",
"scripts\dev-shell.ps1",
"Journal.Sidecar\Journal.Sidecar.csproj"
)
foreach ($marker in $markers) {
if (Test-Path (Join-Path $Path $marker)) {
return $true
}
}
return $false
}
function Resolve-JournalRepoRoot {
param(
[string]$StartPath
)
$candidateStarts = @()
if (-not [string]::IsNullOrWhiteSpace($StartPath)) {
$candidateStarts += $StartPath
}
$currentPath = (Get-Location).Path
if (-not [string]::IsNullOrWhiteSpace($currentPath) -and ($candidateStarts -notcontains $currentPath)) {
$candidateStarts += $currentPath
}
$override = $env:JOURNAL_REPO_ROOT
if (-not [string]::IsNullOrWhiteSpace($override)) {
$overridePath = [System.IO.Path]::GetFullPath($override)
if (Test-JournalRepoRootCandidate -Path $overridePath) {
return $overridePath
}
Write-Warning "JOURNAL_REPO_ROOT is set but does not look like this repo: $overridePath"
}
if (Get-Command git -ErrorAction SilentlyContinue) {
foreach ($start in $candidateStarts) {
try {
$gitRoot = & git -C $start rev-parse --show-toplevel 2>$null
if ($? -and -not [string]::IsNullOrWhiteSpace($gitRoot)) {
$gitRootPath = [System.IO.Path]::GetFullPath($gitRoot.Trim())
if (Test-JournalRepoRootCandidate -Path $gitRootPath) {
return $gitRootPath
}
}
}
catch {
}
}
}
foreach ($start in $candidateStarts) {
$cursor = [System.IO.Path]::GetFullPath($start)
while (-not [string]::IsNullOrWhiteSpace($cursor)) {
if (Test-JournalRepoRootCandidate -Path $cursor) {
return $cursor
}
$parent = [System.IO.Directory]::GetParent($cursor)
if ($null -eq $parent) {
break
}
if ($parent.FullName -eq $cursor) {
break
}
$cursor = $parent.FullName
}
}
throw "Could not locate repository root. Set JOURNAL_REPO_ROOT to the repo path."
}
function Initialize-JournalDotnetEnv {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$dotnetCliHome = Join-Path $RepoRoot ".dotnet_home"
$nugetPackages = Join-Path $RepoRoot ".nuget\packages"
$nugetHttpCachePath = Join-Path $RepoRoot ".nuget\http-cache"
$env:DOTNET_CLI_HOME = $dotnetCliHome
$env:NUGET_PACKAGES = $nugetPackages
$env:NUGET_HTTP_CACHE_PATH = $nugetHttpCachePath
$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = "1"
$env:DOTNET_ADD_GLOBAL_TOOLS_TO_PATH = "0"
$env:DOTNET_GENERATE_ASPNET_CERTIFICATE = "0"
$env:DOTNET_CLI_TELEMETRY_OPTOUT = "1"
$env:NUGET_CERT_REVOCATION_MODE = "offline"
New-Item -ItemType Directory -Force -Path $dotnetCliHome, $nugetPackages, $nugetHttpCachePath | Out-Null
}
function Initialize-JournalPipEnv {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$pipCacheDir = Join-Path $RepoRoot ".pip\cache"
$pipTempDir = Join-Path $RepoRoot ".tmp\pip-temp"
$env:PIP_CACHE_DIR = $pipCacheDir
$env:TEMP = $pipTempDir
$env:TMP = $pipTempDir
$env:PIP_DISABLE_PIP_VERSION_CHECK = "1"
$env:PIP_DEFAULT_TIMEOUT = "30"
$env:PIP_RETRIES = "2"
New-Item -ItemType Directory -Force -Path $pipCacheDir, $pipTempDir | Out-Null
}
function Initialize-JournalHuggingFaceEnv {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$hfHome = Join-Path $RepoRoot ".cache\huggingface"
$hfHubCache = Join-Path $hfHome "hub"
$env:HF_HOME = $hfHome
$env:HUGGINGFACE_HUB_CACHE = $hfHubCache
$env:HF_HUB_DISABLE_SYMLINKS_WARNING = "1"
New-Item -ItemType Directory -Force -Path $hfHubCache | Out-Null
}
function Resolve-JournalSidecarProjectPath {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$defaultPath = Join-Path $RepoRoot "Journal.Sidecar\Journal.Sidecar.csproj"
if (Test-Path $defaultPath) {
return (Resolve-Path $defaultPath).Path
}
$exactMatches = @(Get-ChildItem -Path $RepoRoot -Recurse -File -Filter "Journal.Sidecar.csproj" -ErrorAction SilentlyContinue)
if ($exactMatches.Count -eq 1) {
return $exactMatches[0].FullName
}
if ($exactMatches.Count -gt 1) {
$matchList = ($exactMatches | ForEach-Object { $_.FullName }) -join "; "
throw "Found multiple Journal.Sidecar.csproj files: $matchList"
}
$fallbackMatches = @(Get-ChildItem -Path $RepoRoot -Recurse -File -Filter "*.csproj" -ErrorAction SilentlyContinue | Where-Object {
$_.BaseName -match "(?i)sidecar" -or $_.DirectoryName -match "(?i)sidecar"
})
if ($fallbackMatches.Count -eq 1) {
return $fallbackMatches[0].FullName
}
if ($fallbackMatches.Count -gt 1) {
$matchList = ($fallbackMatches | ForEach-Object { $_.FullName }) -join "; "
throw "Found multiple sidecar-like csproj files: $matchList"
}
throw "Could not locate a sidecar project file under: $RepoRoot"
}
function Resolve-JournalAppRoot {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$defaultPath = Join-Path $RepoRoot "Journal.App"
$defaultPackageJson = Join-Path $defaultPath "package.json"
$defaultTauriConfig = Join-Path $defaultPath "src-tauri\tauri.conf.json"
if ((Test-Path $defaultPackageJson) -and (Test-Path $defaultTauriConfig)) {
return (Resolve-Path $defaultPath).Path
}
$packageJsonMatches = @(Get-ChildItem -Path $RepoRoot -Recurse -File -Filter "package.json" -ErrorAction SilentlyContinue | Where-Object {
Test-Path (Join-Path $_.DirectoryName "src-tauri\tauri.conf.json")
})
if ($packageJsonMatches.Count -eq 1) {
return $packageJsonMatches[0].DirectoryName
}
if ($packageJsonMatches.Count -gt 1) {
$matchList = ($packageJsonMatches | ForEach-Object { $_.DirectoryName }) -join "; "
throw "Found multiple Tauri app roots under repo: $matchList"
}
throw "Could not locate Journal.App root under: $RepoRoot"
}
function Resolve-JournalWebGatewayProjectPath {
param(
[Parameter(Mandatory = $true)]
[string]$RepoRoot
)
$defaultPath = Join-Path $RepoRoot "Journal.WebGateway\Journal.WebGateway.csproj"
if (Test-Path $defaultPath) {
return (Resolve-Path $defaultPath).Path
}
$exactMatches = @(Get-ChildItem -Path $RepoRoot -Recurse -File -Filter "Journal.WebGateway.csproj" -ErrorAction SilentlyContinue)
if ($exactMatches.Count -eq 1) {
return $exactMatches[0].FullName
}
if ($exactMatches.Count -gt 1) {
$matchList = ($exactMatches | ForEach-Object { $_.FullName }) -join "; "
throw "Found multiple Journal.WebGateway.csproj files: $matchList"
}
throw "Could not locate Journal.WebGateway.csproj under: $RepoRoot"
}