Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df68167d04 | ||
|
|
9a79e6f649 | ||
|
|
dfae1cb572 | ||
|
|
752f3ba433 | ||
|
|
72a5e289f6 | ||
|
|
01401cb1cd | ||
|
|
d3cb76dc32 | ||
|
|
61b1eda37d |
No files matched your search
@@ -4,6 +4,9 @@ on:
|
|||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- 'backend/v*'
|
- 'backend/v*'
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- published
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
publish:
|
publish:
|
||||||
@@ -32,8 +35,13 @@ jobs:
|
|||||||
|
|
||||||
- name: Package Go module
|
- name: Package Go module
|
||||||
env:
|
env:
|
||||||
RELEASE_TAG: ${{ github.ref_name }}
|
RELEASE_TAG: ${{ github.event_name == 'release' && github.event.release.tag_name || github.ref_name }}
|
||||||
run: bash scripts/package-go-module.sh "$RELEASE_TAG" "$RUNNER_TEMP/gitea-go-package"
|
run: |
|
||||||
|
package_directory='gitea-go-package'
|
||||||
|
rm -rf "$package_directory"
|
||||||
|
mkdir -p "$package_directory"
|
||||||
|
bash scripts/package-go-module.sh "$RELEASE_TAG" "$package_directory"
|
||||||
|
find "$package_directory" -maxdepth 1 -type f -name '*.zip' -print -quit | grep -q .
|
||||||
|
|
||||||
- name: Publish to Gitea Package Registry
|
- name: Publish to Gitea Package Registry
|
||||||
env:
|
env:
|
||||||
@@ -46,8 +54,25 @@ jobs:
|
|||||||
echo 'PACKAGE_REGISTRY_USERNAME and PACKAGE_REGISTRY_TOKEN secrets are required.' >&2
|
echo 'PACKAGE_REGISTRY_USERNAME and PACKAGE_REGISTRY_TOKEN secrets are required.' >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
package_file="$(find "$RUNNER_TEMP/gitea-go-package" -maxdepth 1 -type f -name '*.zip' -print -quit)"
|
package_file="$(find gitea-go-package -maxdepth 1 -type f -name '*.zip' -print -quit)"
|
||||||
test -n "$package_file"
|
test -n "$package_file"
|
||||||
curl --fail-with-body --user "$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN" \
|
response_file="$(mktemp)"
|
||||||
|
http_status="$(curl --silent --show-error --fail-with-body \
|
||||||
|
--user "$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN" \
|
||||||
|
--output "$response_file" \
|
||||||
|
--write-out '%{http_code}' \
|
||||||
--upload-file "$package_file" \
|
--upload-file "$package_file" \
|
||||||
"$GITEA_GO_REGISTRY"
|
"$GITEA_GO_REGISTRY" || true)"
|
||||||
|
case "$http_status" in
|
||||||
|
201)
|
||||||
|
echo 'Go package published.'
|
||||||
|
;;
|
||||||
|
409)
|
||||||
|
echo 'Go package version already exists; leaving the immutable package unchanged.'
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
cat "$response_file" >&2
|
||||||
|
echo "Gitea Go package upload failed with HTTP status $http_status." >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
name: Publish npm packages
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- published
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Publish shared npm packages
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
steps:
|
||||||
|
- name: Checkout source
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 22
|
||||||
|
cache: npm
|
||||||
|
|
||||||
|
- name: Install and build packages
|
||||||
|
run: |
|
||||||
|
npm ci
|
||||||
|
npm run build
|
||||||
|
|
||||||
|
- name: Configure Gitea npm registry
|
||||||
|
env:
|
||||||
|
PACKAGE_REGISTRY_USERNAME: ${{ secrets.PACKAGE_REGISTRY_USERNAME }}
|
||||||
|
PACKAGE_REGISTRY_TOKEN: ${{ secrets.PACKAGE_REGISTRY_TOKEN }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [ -z "$PACKAGE_REGISTRY_USERNAME" ] || [ -z "$PACKAGE_REGISTRY_TOKEN" ]; then
|
||||||
|
echo 'PACKAGE_REGISTRY_USERNAME and PACKAGE_REGISTRY_TOKEN secrets are required.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
npmrc_path="$RUNNER_TEMP/gitea-npmrc"
|
||||||
|
umask 077
|
||||||
|
{
|
||||||
|
printf '@gabriel20xx:registry=https://repos.gfranz.ch/api/packages/Gabriel20xx/npm/\n'
|
||||||
|
printf '//repos.gfranz.ch/api/packages/Gabriel20xx/npm/:_authToken=%s\n' "$PACKAGE_REGISTRY_TOKEN"
|
||||||
|
printf 'always-auth=true\n'
|
||||||
|
} > "$npmrc_path"
|
||||||
|
printf 'NPM_CONFIG_USERCONFIG=%s\n' "$npmrc_path" >> "$GITHUB_ENV"
|
||||||
|
|
||||||
|
- name: Publish npm packages
|
||||||
|
env:
|
||||||
|
NPM_REGISTRY: https://repos.gfranz.ch/api/packages/Gabriel20xx/npm/
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
for package in \
|
||||||
|
@gabriel20xx/design-tokens \
|
||||||
|
@gabriel20xx/web-ui \
|
||||||
|
@gabriel20xx/node-backend; do
|
||||||
|
version="$(npm pkg get version --workspace="$package" | tr -d '"[:space:]')"
|
||||||
|
if npm view "$package@$version" version --registry="$NPM_REGISTRY" >/dev/null 2>&1; then
|
||||||
|
echo "$package@$version is already published; skipping."
|
||||||
|
else
|
||||||
|
npm publish --workspace="$package" --registry="$NPM_REGISTRY"
|
||||||
|
fi
|
||||||
|
done
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
name: Publish Python package
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- published
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Publish shared Python package
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
steps:
|
||||||
|
- name: Checkout source
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v5
|
||||||
|
with:
|
||||||
|
python-version: '3.12'
|
||||||
|
|
||||||
|
- name: Build and publish package
|
||||||
|
env:
|
||||||
|
PACKAGE_REGISTRY_USERNAME: ${{ secrets.PACKAGE_REGISTRY_USERNAME }}
|
||||||
|
PACKAGE_REGISTRY_TOKEN: ${{ secrets.PACKAGE_REGISTRY_TOKEN }}
|
||||||
|
TWINE_REPOSITORY_URL: https://repos.gfranz.ch/api/packages/Gabriel20xx/pypi
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if [ -z "$PACKAGE_REGISTRY_USERNAME" ] || [ -z "$PACKAGE_REGISTRY_TOKEN" ]; then
|
||||||
|
echo 'PACKAGE_REGISTRY_USERNAME and PACKAGE_REGISTRY_TOKEN secrets are required.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
python -m pip install --disable-pip-version-check --no-cache-dir build twine
|
||||||
|
package_directory="$RUNNER_TEMP/gitea-pypi-package"
|
||||||
|
python -m build packages/python-backend --outdir "$package_directory"
|
||||||
|
package_index_url="${TWINE_REPOSITORY_URL%/}/simple/gabriel20xx-shared-backend/"
|
||||||
|
package_index_file="$(mktemp)"
|
||||||
|
package_index_status="$(curl --silent --show-error --fail-with-body \
|
||||||
|
--user "$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN" \
|
||||||
|
--output "$package_index_file" \
|
||||||
|
--write-out '%{http_code}' \
|
||||||
|
"$package_index_url" || true)"
|
||||||
|
package_files=("$package_directory"/*)
|
||||||
|
files_to_upload=()
|
||||||
|
case "$package_index_status" in
|
||||||
|
200)
|
||||||
|
for package_file in "${package_files[@]}"; do
|
||||||
|
if ! grep -Fq "$(basename "$package_file")" "$package_index_file"; then
|
||||||
|
files_to_upload+=("$package_file")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
;;
|
||||||
|
404)
|
||||||
|
files_to_upload=("${package_files[@]}")
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
cat "$package_index_file" >&2
|
||||||
|
echo "Gitea PyPI package lookup failed with HTTP status $package_index_status." >&2
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
if [ "${#files_to_upload[@]}" -eq 0 ]; then
|
||||||
|
echo 'Python package version already exists with all build artifacts; leaving it unchanged.'
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
TWINE_USERNAME="$PACKAGE_REGISTRY_USERNAME" \
|
||||||
|
TWINE_PASSWORD="$PACKAGE_REGISTRY_TOKEN" \
|
||||||
|
python -m twine upload --non-interactive \
|
||||||
|
--repository-url "$TWINE_REPOSITORY_URL" \
|
||||||
|
"${files_to_upload[@]}"
|
||||||
Executable
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
repo_root="$(git rev-parse --show-toplevel)"
|
||||||
|
sync_script="$repo_root/scripts/sync-consumers.ps1"
|
||||||
|
|
||||||
|
if command -v pwsh >/dev/null 2>&1; then
|
||||||
|
pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$sync_script" -Commit
|
||||||
|
elif command -v powershell.exe >/dev/null 2>&1; then
|
||||||
|
powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$sync_script" -Commit
|
||||||
|
else
|
||||||
|
echo "SharedCode synchronization requires pwsh or powershell.exe." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@@ -59,31 +59,29 @@ SharedCode version has been validated.
|
|||||||
|
|
||||||
## Publish the individual packages
|
## Publish the individual packages
|
||||||
|
|
||||||
The package names use the `@gabriel20xx` npm scope. That scope must belong to the npm account used to publish. If a different scope is preferred, replace the scope consistently in both package manifests and imports before publishing.
|
The package names use the `@gabriel20xx` npm scope. The `publish-npm.yml` and
|
||||||
|
`publish-python.yml` Gitea workflows publish the packages when a `v*` tag is pushed or a
|
||||||
|
matching Gitea release is published. Configure `PACKAGE_REGISTRY_USERNAME` and
|
||||||
|
`PACKAGE_REGISTRY_TOKEN` as user Actions secrets before publishing.
|
||||||
|
|
||||||
Publish the tokens first because `web-ui` depends on them:
|
The npm workflow publishes `design-tokens` first because `web-ui` depends on it. It also
|
||||||
|
skips an npm version that is already present, so retrying a partially completed release is safe.
|
||||||
```bash
|
The Python workflow uses Twine's `--skip-existing` option for the same reason.
|
||||||
npm login
|
|
||||||
npm publish --workspace=@gabriel20xx/design-tokens --access public
|
|
||||||
npm publish --workspace=@gabriel20xx/web-ui --access public
|
|
||||||
npm publish --workspace=@gabriel20xx/node-backend --access public
|
|
||||||
```
|
|
||||||
|
|
||||||
For a private registry, configure that registry and omit `--access public` if the registry does not use npm's public scoped-package rules.
|
|
||||||
|
|
||||||
## Install individual packages from a registry
|
## Install individual packages from a registry
|
||||||
|
|
||||||
From a project's frontend directory, install the published UI package:
|
From a project's frontend directory, install the published UI package from Gitea:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @gabriel20xx/web-ui@^0.1.0
|
npm config set @gabriel20xx:registry https://repos.gfranz.ch/api/packages/Gabriel20xx/npm/
|
||||||
|
npm config set //repos.gfranz.ch/api/packages/Gabriel20xx/npm/:_authToken "$PACKAGE_REGISTRY_TOKEN"
|
||||||
|
npm install @gabriel20xx/web-ui@0.1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
The design-token package is installed transitively. Install it directly only when an application needs to reference its theme file itself:
|
The design-token package is installed transitively. Install it directly only when an application needs to reference its theme file itself:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @gabriel20xx/design-tokens@^0.1.0
|
npm install @gabriel20xx/design-tokens@0.1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
Import the shared stylesheet once from the application's global stylesheet, before project-specific overrides:
|
Import the shared stylesheet once from the application's global stylesheet, before project-specific overrides:
|
||||||
@@ -143,20 +141,20 @@ Use `auth.SchemaOptions{SingleUser: false, APITokens: true}` for a multi-user se
|
|||||||
|
|
||||||
For embedded SQL migrations, use `migrate.Up(ctx, databaseURL, embeddedMigrations, "migrations")`. The package does not contain application tables, so migrations remain versioned with the service that owns those tables.
|
For embedded SQL migrations, use `migrate.Up(ctx, databaseURL, embeddedMigrations, "migrations")`. The package does not contain application tables, so migrations remain versioned with the service that owns those tables.
|
||||||
|
|
||||||
The Go module lives under `backend`, so its release tag must use the submodule prefix (`backend/v0.1.0`). The consuming `go.mod` uses the normal module version (`v0.1.0`). To publish a release, push a tag such as `backend/v0.1.0`; the Gitea workflow packages the module and uploads it to the private registry. Never reuse an existing package version.
|
The Go module lives under `backend`, so its release tag must use the submodule prefix (`backend/v0.1.0`). The consuming `go.mod` uses the normal module version (`v0.1.0`). To publish a release, push a tag such as `backend/v0.1.0`; the Gitea workflow packages the module and uploads it to the private registry. The workflow also listens for Gitea's `release.published` event, so a release can be published from the Gitea Releases page to manually retry a failed upload for an existing tag. Never reuse a package version that was already uploaded successfully.
|
||||||
|
|
||||||
For Node services, install the shared package after publishing it:
|
For Node services, install the shared package after publishing it:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @gabriel20xx/node-backend@^0.1.0
|
npm install @gabriel20xx/node-backend@0.1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
For Python services, build and publish the wheel to the Python package index used by the projects, then install it normally:
|
For Python services, configure the Gitea index and install the published wheel:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python -m pip install build
|
python -m pip install \
|
||||||
python -m build packages/python-backend
|
--index-url "https://$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN@repos.gfranz.ch/api/packages/Gabriel20xx/pypi/simple" \
|
||||||
python -m pip install gabriel20xx-shared-backend==0.1.0
|
gabriel20xx-shared-backend==0.1.0
|
||||||
```
|
```
|
||||||
|
|
||||||
The Node and Python packages intentionally expose the same infrastructure concepts but use their native database drivers. This is the safe way to share behavior across the polyglot services; a Go package cannot be imported by the Python or Node applications.
|
The Node and Python packages intentionally expose the same infrastructure concepts but use their native database drivers. This is the safe way to share behavior across the polyglot services; a Go package cannot be imported by the Python or Node applications.
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "gabriel20xx-shared-backend"
|
name = "gabriel20xx-shared-backend"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
description = "Shared PostgreSQL and authentication primitives for Python services."
|
description = "Shared PostgreSQL and authentication primitives for Python services."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
$repositoryRoot = (& git -C $PSScriptRoot rev-parse --show-toplevel).Trim()
|
||||||
|
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($repositoryRoot)) {
|
||||||
|
throw 'Could not locate the shared Git repository.'
|
||||||
|
}
|
||||||
|
|
||||||
|
& git -C $repositoryRoot config core.hooksPath .githooks
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw 'Could not configure the shared Git hooks path.'
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Installed shared post-commit synchronization hook in $repositoryRoot."
|
||||||
@@ -33,8 +33,13 @@ git archive --format=tar "$release_tag" "$module_directory" \
|
|||||||
|
|
||||||
test -f "$module_root/go.mod"
|
test -f "$module_root/go.mod"
|
||||||
grep -Fxq "module $module_path" "$module_root/go.mod"
|
grep -Fxq "module $module_path" "$module_root/go.mod"
|
||||||
mkdir -p "$repository_root/$output_directory"
|
if [[ "$output_directory" = /* ]]; then
|
||||||
zip_file="$repository_root/$output_directory/${module_path//\//_}@${version}.zip"
|
output_path="$output_directory"
|
||||||
|
else
|
||||||
|
output_path="$repository_root/$output_directory"
|
||||||
|
fi
|
||||||
|
mkdir -p "$output_path"
|
||||||
|
zip_file="$output_path/${module_path//\//_}@${version}.zip"
|
||||||
rm -f "$zip_file"
|
rm -f "$zip_file"
|
||||||
|
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -0,0 +1,324 @@
|
|||||||
|
[CmdletBinding()]
|
||||||
|
param(
|
||||||
|
[string]$SourceRepository,
|
||||||
|
[switch]$Force,
|
||||||
|
[switch]$Commit,
|
||||||
|
[switch]$Push
|
||||||
|
)
|
||||||
|
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
if ($Push -and -not $Commit) {
|
||||||
|
throw 'The -Push switch requires -Commit so that only generated synchronization changes are pushed.'
|
||||||
|
}
|
||||||
|
|
||||||
|
$consumerProjects = @(
|
||||||
|
'Original/SimpleCollection',
|
||||||
|
'Original/CollabTable',
|
||||||
|
'Original/GameStream',
|
||||||
|
'Original/SyncServer',
|
||||||
|
'Original/UnraidMonitor',
|
||||||
|
'Original/PubliSafe',
|
||||||
|
'Original/MediaCollection',
|
||||||
|
'Forks/Bitmagnet',
|
||||||
|
'Forks/Heretic',
|
||||||
|
'Original/InfiniteVideo',
|
||||||
|
'Forks/MegaBasterd',
|
||||||
|
'Forks/HordeWorkerReGen',
|
||||||
|
'Original/OutdoorGames'
|
||||||
|
)
|
||||||
|
|
||||||
|
function Invoke-GitText {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$WorkingDirectory,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string[]]$Arguments
|
||||||
|
)
|
||||||
|
|
||||||
|
$output = & git -C $WorkingDirectory @Arguments 2>&1
|
||||||
|
$exitCode = $LASTEXITCODE
|
||||||
|
if ($exitCode -ne 0) {
|
||||||
|
$diagnostics = @($output | ForEach-Object { [string]$_ } | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
|
||||||
|
$blockingDiagnostics = @($diagnostics | Where-Object { $_ -notmatch '^\s*(warning|hint):' })
|
||||||
|
if ($blockingDiagnostics.Count -eq 0) {
|
||||||
|
$diagnostics | ForEach-Object { Write-Warning $_ }
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$details = $diagnostics -join [Environment]::NewLine
|
||||||
|
throw "git $($Arguments -join ' ') failed in '$WorkingDirectory'. $details"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return @($output | ForEach-Object { [string]$_ })
|
||||||
|
}
|
||||||
|
|
||||||
|
function Resolve-FullPath {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$Path
|
||||||
|
)
|
||||||
|
|
||||||
|
return (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-RelativePath {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$BasePath,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$TargetPath
|
||||||
|
)
|
||||||
|
|
||||||
|
$base = (Resolve-FullPath $BasePath).TrimEnd('\') + '\'
|
||||||
|
$target = Resolve-FullPath $TargetPath
|
||||||
|
if (-not $target.StartsWith($base, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||||
|
throw "'$TargetPath' is not inside '$BasePath'."
|
||||||
|
}
|
||||||
|
|
||||||
|
return $target.Substring($base.Length).Replace('\', '/')
|
||||||
|
}
|
||||||
|
|
||||||
|
function Write-Utf8NoBom {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$Path,
|
||||||
|
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$Content
|
||||||
|
)
|
||||||
|
|
||||||
|
$encoding = New-Object System.Text.UTF8Encoding($false)
|
||||||
|
$normalizedContent = $Content.Replace("`r`n", "`n").Replace("`r", "`n")
|
||||||
|
[System.IO.File]::WriteAllText($Path, $normalizedContent, $encoding)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Convert-ToConsumerWebUiPackage {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$PackagePath
|
||||||
|
)
|
||||||
|
|
||||||
|
$manifest = Get-Content -LiteralPath $PackagePath -Raw | ConvertFrom-Json
|
||||||
|
$manifest.files = @('src', 'README.md')
|
||||||
|
$manifest.dependencies = [pscustomobject]@{}
|
||||||
|
$manifest.svelte = './src/index.ts'
|
||||||
|
$manifest.types = './src/index.ts'
|
||||||
|
|
||||||
|
$rootExport = $manifest.exports.PSObject.Properties | Where-Object { $_.Name -eq '.' }
|
||||||
|
if ($null -ne $rootExport) {
|
||||||
|
$rootExport.Value.types = './src/index.ts'
|
||||||
|
$rootExport.Value.svelte = './src/index.ts'
|
||||||
|
$rootExport.Value.import = './src/index.ts'
|
||||||
|
}
|
||||||
|
|
||||||
|
$stylesExport = $manifest.exports.PSObject.Properties | Where-Object { $_.Name -eq './styles.css' }
|
||||||
|
if ($null -ne $stylesExport) {
|
||||||
|
$stylesExport.Value = './src/lib/styles.css'
|
||||||
|
}
|
||||||
|
|
||||||
|
$json = $manifest | ConvertTo-Json -Depth 20
|
||||||
|
Write-Utf8NoBom -Path $PackagePath -Content ($json + [Environment]::NewLine)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Convert-ToConsumerWebUiSources {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
[string]$WebUiRoot
|
||||||
|
)
|
||||||
|
|
||||||
|
$stylesPath = Join-Path $WebUiRoot 'src/lib/styles.css'
|
||||||
|
if (Test-Path -LiteralPath $stylesPath) {
|
||||||
|
$styles = Get-Content -LiteralPath $stylesPath -Raw
|
||||||
|
$styles = $styles.Replace("@import '@gabriel20xx/design-tokens/theme.css';", "@import '../../../design-tokens/theme.css';")
|
||||||
|
Write-Utf8NoBom -Path $stylesPath -Content $styles
|
||||||
|
}
|
||||||
|
|
||||||
|
$svelteConfigPath = Join-Path $WebUiRoot 'svelte.config.js'
|
||||||
|
if (Test-Path -LiteralPath $svelteConfigPath) {
|
||||||
|
Write-Utf8NoBom -Path $svelteConfigPath -Content "export default {};`n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Test-LocalSharedDependency {
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory = $true)]
|
||||||
|
$PackageManifest
|
||||||
|
)
|
||||||
|
|
||||||
|
$localPackageNames = @(
|
||||||
|
'@gabriel20xx/web-ui',
|
||||||
|
'@gabriel20xx/node-backend'
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach ($sectionName in @('dependencies', 'devDependencies', 'optionalDependencies')) {
|
||||||
|
$sectionProperty = $PackageManifest.PSObject.Properties[$sectionName]
|
||||||
|
if ($null -eq $sectionProperty -or $null -eq $sectionProperty.Value) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($dependency in $sectionProperty.Value.PSObject.Properties) {
|
||||||
|
if ($dependency.Name -in $localPackageNames -and ([string]$dependency.Value).StartsWith('file:')) {
|
||||||
|
return $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([string]::IsNullOrWhiteSpace($SourceRepository)) {
|
||||||
|
$scriptParent = Resolve-FullPath (Join-Path $PSScriptRoot '..')
|
||||||
|
if ((Split-Path -Leaf $scriptParent) -ieq 'shared') {
|
||||||
|
$SourceRepository = $scriptParent
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$consumerRoot = Resolve-FullPath (Join-Path $PSScriptRoot '..\..')
|
||||||
|
$SourceRepository = Join-Path $consumerRoot '..\..\shared'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$sourceRoot = Resolve-FullPath $SourceRepository
|
||||||
|
$sourceRepositoryRoot = (Invoke-GitText -WorkingDirectory $sourceRoot -Arguments @('rev-parse', '--show-toplevel') | Select-Object -First 1).Trim()
|
||||||
|
$sourceRoot = Resolve-FullPath $sourceRepositoryRoot
|
||||||
|
$workspaceRoot = Split-Path -Parent $sourceRoot
|
||||||
|
$sourceCommit = (Invoke-GitText -WorkingDirectory $sourceRoot -Arguments @('rev-parse', '--short', 'HEAD') | Select-Object -First 1).Trim()
|
||||||
|
$sourceFiles = @(Invoke-GitText -WorkingDirectory $sourceRoot -Arguments @('ls-files') | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
|
||||||
|
|
||||||
|
$sourceFileSet = @{}
|
||||||
|
foreach ($sourceFile in $sourceFiles) {
|
||||||
|
$sourceFileSet[$sourceFile.Replace('\', '/')] = $true
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "Synchronizing $($sourceFiles.Count) tracked SharedCode files from $sourceCommit."
|
||||||
|
|
||||||
|
$failures = New-Object 'System.Collections.Generic.List[string]'
|
||||||
|
|
||||||
|
foreach ($relativeProject in $consumerProjects) {
|
||||||
|
$projectRoot = Join-Path $workspaceRoot ($relativeProject -replace '/', '\')
|
||||||
|
if (-not (Test-Path -LiteralPath $projectRoot -PathType Container)) {
|
||||||
|
$failures.Add("${relativeProject}: project directory not found")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$projectRoot = Resolve-FullPath $projectRoot
|
||||||
|
$destinationRoot = Join-Path $projectRoot 'SharedCode'
|
||||||
|
$dirtySharedCode = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('status', '--porcelain', '--', 'SharedCode'))
|
||||||
|
if ($dirtySharedCode.Count -gt 0 -and -not $Force) {
|
||||||
|
throw "SharedCode has local changes. Commit them or rerun with -Force."
|
||||||
|
}
|
||||||
|
|
||||||
|
New-Item -ItemType Directory -Force -Path $destinationRoot | Out-Null
|
||||||
|
|
||||||
|
$existingSharedFiles = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('ls-files', '--', 'SharedCode') | Where-Object { $_ -like 'SharedCode/*' })
|
||||||
|
foreach ($existingSharedFile in $existingSharedFiles) {
|
||||||
|
$relativeSharedFile = $existingSharedFile.Substring('SharedCode/'.Length).Replace('\', '/')
|
||||||
|
if (-not $sourceFileSet.ContainsKey($relativeSharedFile)) {
|
||||||
|
$stalePath = Join-Path $destinationRoot ($relativeSharedFile -replace '/', '\')
|
||||||
|
if (Test-Path -LiteralPath $stalePath -PathType Leaf) {
|
||||||
|
Remove-Item -LiteralPath $stalePath -Force
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($sourceFile in $sourceFiles) {
|
||||||
|
$sourcePath = Join-Path $sourceRoot ($sourceFile -replace '/', '\')
|
||||||
|
$destinationPath = Join-Path $destinationRoot ($sourceFile -replace '/', '\')
|
||||||
|
$destinationDirectory = Split-Path -Parent $destinationPath
|
||||||
|
New-Item -ItemType Directory -Force -Path $destinationDirectory | Out-Null
|
||||||
|
Copy-Item -LiteralPath $sourcePath -Destination $destinationPath -Force
|
||||||
|
}
|
||||||
|
|
||||||
|
$webUiPackagePath = Join-Path $destinationRoot 'packages/web-ui/package.json'
|
||||||
|
if (Test-Path -LiteralPath $webUiPackagePath) {
|
||||||
|
Convert-ToConsumerWebUiPackage -PackagePath $webUiPackagePath
|
||||||
|
Convert-ToConsumerWebUiSources -WebUiRoot (Join-Path $destinationRoot 'packages/web-ui')
|
||||||
|
}
|
||||||
|
|
||||||
|
$lockFiles = @()
|
||||||
|
$sharedCodePrefix = ((Resolve-FullPath $destinationRoot).TrimEnd('\') + '\')
|
||||||
|
$packageFiles = @(Get-ChildItem -LiteralPath $projectRoot -Recurse -File -Filter 'package.json' -Force |
|
||||||
|
Where-Object {
|
||||||
|
$_.FullName -notlike "$sharedCodePrefix*" -and
|
||||||
|
$_.FullName -notmatch '\\node_modules\\'
|
||||||
|
})
|
||||||
|
|
||||||
|
foreach ($packageFile in $packageFiles) {
|
||||||
|
$packageManifest = Get-Content -LiteralPath $packageFile.FullName -Raw | ConvertFrom-Json
|
||||||
|
if (-not (Test-LocalSharedDependency -PackageManifest $packageManifest)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
$lockPath = Join-Path $packageFile.DirectoryName 'package-lock.json'
|
||||||
|
if (-not (Test-Path -LiteralPath $lockPath -PathType Leaf)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
$relativeLockPath = Get-RelativePath -BasePath $projectRoot -TargetPath $lockPath
|
||||||
|
$trackedLock = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('ls-files', '--', $relativeLockPath))
|
||||||
|
$shouldStageLock = $trackedLock.Count -gt 0
|
||||||
|
Write-Host " Updating $relativeProject/$(Get-RelativePath -BasePath $projectRoot -TargetPath $lockPath)"
|
||||||
|
Push-Location -LiteralPath $packageFile.DirectoryName
|
||||||
|
try {
|
||||||
|
& npm install --package-lock-only --ignore-scripts --prefer-offline
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
throw "npm install --package-lock-only failed with exit code $LASTEXITCODE."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
Pop-Location
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($shouldStageLock -and $lockFiles -notcontains $relativeLockPath) {
|
||||||
|
$lockFiles += $relativeLockPath
|
||||||
|
}
|
||||||
|
elseif (-not $shouldStageLock) {
|
||||||
|
Write-Host " Leaving untracked/ignored lockfile out of the generated commit: $relativeLockPath"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$null = Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('add', '--', 'SharedCode')
|
||||||
|
foreach ($lockFile in $lockFiles) {
|
||||||
|
$null = Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('add', '--', $lockFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetPaths = @('SharedCode') + $lockFiles
|
||||||
|
$stagedChanges = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments (@('diff', '--cached', '--name-only', '--') + $targetPaths))
|
||||||
|
if ($Commit -and $stagedChanges.Count -gt 0) {
|
||||||
|
$commitMessage = "Sync SharedCode snapshot from $sourceCommit"
|
||||||
|
$commitArguments = @('commit', '--only', '-m', $commitMessage, '--') + $targetPaths
|
||||||
|
$commitOutput = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments $commitArguments)
|
||||||
|
$commitOutput | ForEach-Object { Write-Host " $_" }
|
||||||
|
$remainingStagedChanges = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments (@('diff', '--cached', '--name-only', '--') + $targetPaths))
|
||||||
|
if ($remainingStagedChanges.Count -gt 0) {
|
||||||
|
throw 'The generated synchronization changes remain staged after the commit.'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Push) {
|
||||||
|
$pushOutput = @(Invoke-GitText -WorkingDirectory $projectRoot -Arguments @('push', 'origin', 'main'))
|
||||||
|
$pushOutput | ForEach-Object { Write-Host " $_" }
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($stagedChanges.Count -gt 0) {
|
||||||
|
Write-Host "Synced $relativeProject."
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Write-Host "$relativeProject is already synchronized."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$message = "${relativeProject}: $($_.Exception.Message)"
|
||||||
|
$failures.Add($message)
|
||||||
|
Write-Warning $message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($failures.Count -gt 0) {
|
||||||
|
throw "SharedCode synchronization failed for $($failures.Count) project(s): $($failures -join '; ')"
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host 'SharedCode synchronization completed.'
|
||||||
Reference in new issue
Block a user