Skip to content

Commit 355beca

Browse files
committed
make the strauss version a configurable option during both testing and packing
1 parent ef33ab6 commit 355beca

File tree

8 files changed

+119
-14
lines changed

8 files changed

+119
-14
lines changed

.github/config/packager.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Strauss version for packaging and tests
2+
STRAUSS_VERSION="0.19.4"
3+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
# Load Strauss version for packaging/tests.
3+
# Usage: source .github/scripts/read-packager-config.sh
4+
5+
set -euo pipefail
6+
7+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
9+
CONFIG_FILE="${PROJECT_ROOT}/.github/config/packager.conf"
10+
11+
if [ ! -f "${CONFIG_FILE}" ]; then
12+
echo "❌ packager config not found: ${CONFIG_FILE}" >&2
13+
exit 1
14+
fi
15+
16+
# shellcheck source=/dev/null
17+
source "${CONFIG_FILE}"
18+
19+
if [ -z "${STRAUSS_VERSION:-}" ]; then
20+
echo "❌ STRAUSS_VERSION is not set in ${CONFIG_FILE}" >&2
21+
exit 1
22+
fi
23+
24+
# Strip leading "v" if present
25+
STRAUSS_VERSION="${STRAUSS_VERSION#v}"
26+
27+
export STRAUSS_VERSION
28+
export SHIELD_STRAUSS_VERSION="${SHIELD_STRAUSS_VERSION:-${STRAUSS_VERSION}}"
29+
30+
echo "Using STRAUSS_VERSION=${STRAUSS_VERSION}"
31+

.github/workflows/docker-tests.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ jobs:
192192
npm run build
193193
fi
194194
195+
- name: Load Strauss version
196+
run: |
197+
source .github/scripts/read-packager-config.sh
198+
echo "SHIELD_STRAUSS_VERSION=${SHIELD_STRAUSS_VERSION}" >> $GITHUB_ENV
199+
echo "STRAUSS_VERSION=${STRAUSS_VERSION}" >> $GITHUB_ENV
200+
195201
- name: Build plugin package
196202
run: |
197203
PACKAGE_DIR="${{ github.workspace }}/shield-package"
@@ -222,6 +228,7 @@ jobs:
222228
echo "WP_VERSION_PREVIOUS=${{ needs.detect-wp-versions.outputs.previous }}"
223229
echo "PLUGIN_SOURCE=${{ env.SHIELD_PACKAGE_PATH }}"
224230
echo "SHIELD_PACKAGE_PATH=${{ env.SHIELD_PACKAGE_PATH }}"
231+
echo "SHIELD_STRAUSS_VERSION=${SHIELD_STRAUSS_VERSION}"
225232
echo "SHIELD_TEST_IMAGE=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"
226233
echo "SHIELD_TEST_IMAGE_LATEST=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"
227234
echo "SHIELD_TEST_IMAGE_PREVIOUS=shield-test-runner:php${PHP_VERSION}-wp${WP_VERSION}"

.github/workflows/tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ jobs:
6565
- name: Build assets
6666
run: npm run build
6767

68+
- name: Load Strauss version
69+
run: |
70+
source .github/scripts/read-packager-config.sh
71+
echo "SHIELD_STRAUSS_VERSION=${SHIELD_STRAUSS_VERSION}" >> $GITHUB_ENV
72+
echo "STRAUSS_VERSION=${STRAUSS_VERSION}" >> $GITHUB_ENV
73+
6874
- name: Build plugin package
6975
run: |
7076
PACKAGE_DIR="${{ github.workspace }}/shield-package"

bin/package-plugin.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
$options = getopt( '', [
1010
'output::',
11+
'strauss-version::',
1112
'skip-root-composer',
1213
'skip-lib-composer',
1314
'skip-npm-install',
@@ -21,6 +22,16 @@
2122
if ( is_string( $outputDir ) ) {
2223
$outputDir = trim( $outputDir, " \t\n\r\0\x0B\"'" );
2324
}
25+
26+
$straussVersion = $options[ 'strauss-version' ] ?? null;
27+
if ( is_string( $straussVersion ) ) {
28+
$straussVersion = trim( $straussVersion );
29+
}
30+
$envStrauss = getenv( 'SHIELD_STRAUSS_VERSION' );
31+
$resolvedStrauss = $straussVersion !== null && $straussVersion !== ''
32+
? $straussVersion
33+
: ( is_string( $envStrauss ) ? $envStrauss : null );
34+
2435
$packagerOptions = [];
2536

2637
if ( isset( $options[ 'skip-root-composer' ] ) ) {
@@ -47,6 +58,10 @@
4758
$packagerOptions[ 'skip_copy' ] = true;
4859
}
4960

61+
if ( is_string( $resolvedStrauss ) && $resolvedStrauss !== '' ) {
62+
$packagerOptions[ 'strauss_version' ] = $resolvedStrauss;
63+
}
64+
5065
try {
5166
$path = ( new PluginPackager() )->package( is_string( $outputDir ) ? $outputDir : null, $packagerOptions );
5267
echo '✅ Shield plugin package created at: '.$path.PHP_EOL;

bin/run-docker-tests.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3535
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
3636
cd "$PROJECT_ROOT"
3737

38+
# Set a predictable Compose project name to avoid generic "docker" container names
39+
export COMPOSE_PROJECT_NAME="shield-tests"
40+
3841
# Source matrix configuration (single source of truth)
3942
if [ -f "$PROJECT_ROOT/.github/config/matrix.conf" ]; then
4043
source "$PROJECT_ROOT/.github/config/matrix.conf"
@@ -43,6 +46,14 @@ else
4346
DEFAULT_PHP="8.2"
4447
fi
4548

49+
# Source packager config for Strauss version
50+
if [ -f "$PROJECT_ROOT/.github/scripts/read-packager-config.sh" ]; then
51+
# shellcheck source=/dev/null
52+
source "$PROJECT_ROOT/.github/scripts/read-packager-config.sh"
53+
else
54+
echo "⚠️ Warning: Packager config loader not found, STRAUSS_VERSION not set"
55+
fi
56+
4657
# PHP version to test (can be overridden via environment variable)
4758
PHP_VERSION=${PHP_VERSION:-"$DEFAULT_PHP"}
4859
echo "🐘 PHP Version: $PHP_VERSION"
@@ -118,6 +129,13 @@ export PLUGIN_SOURCE="$PACKAGE_DIR"
118129
# We pass relative path to avoid absolute path issues across host/container
119130
PACKAGE_DIR_RELATIVE="tmp/shield-package-local"
120131

132+
# Ensure clean environment now that env vars are set
133+
echo "🧹 Cleaning up any existing test containers/volumes..."
134+
docker compose -f tests/docker/docker-compose.yml \
135+
-f tests/docker/docker-compose.package.yml \
136+
down -v --remove-orphans || true
137+
echo " ✅ Clean start ensured"
138+
121139
# Start MySQL containers early in background for parallel initialization
122140
# Based on testing, MySQL takes ~38 seconds to fully initialize
123141
echo "🗄️ Starting MySQL databases in background for parallel initialization..."
@@ -273,6 +291,7 @@ docker run --rm --name shield-composer-package \
273291
-v "$PROJECT_ROOT:/app" \
274292
-w /app \
275293
-e COMPOSER_PROCESS_TIMEOUT=900 \
294+
-e SHIELD_STRAUSS_VERSION="$SHIELD_STRAUSS_VERSION" \
276295
$COMPOSER_IMAGE \
277296
composer package-plugin -- --output="$PACKAGE_DIR_RELATIVE" \
278297
--skip-root-composer --skip-lib-composer \
@@ -338,6 +357,7 @@ run_parallel_tests() {
338357

339358
# Prepare output directories
340359
mkdir -p /tmp
360+
rm -f /tmp/shield-test-latest.log /tmp/shield-test-previous.log /tmp/shield-test-latest.exit /tmp/shield-test-previous.exit
341361

342362
# Set up environment variables for both WordPress versions
343363
cat > tests/docker/.env << EOF
@@ -347,6 +367,7 @@ WP_VERSION_PREVIOUS=$PREVIOUS_VERSION
347367
TEST_PHP_VERSION=$PHP_VERSION
348368
PLUGIN_SOURCE=$PACKAGE_DIR
349369
SHIELD_PACKAGE_PATH=$PACKAGE_DIR
370+
SHIELD_STRAUSS_VERSION=$SHIELD_STRAUSS_VERSION
350371
SHIELD_TEST_IMAGE_LATEST=shield-test-runner:php$PHP_VERSION-wp$LATEST_VERSION
351372
SHIELD_TEST_IMAGE_PREVIOUS=shield-test-runner:php$PHP_VERSION-wp$PREVIOUS_VERSION
352373
EOF

infrastructure/src/Tooling/PluginPackager.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class PluginPackager {
1111

1212
private string $projectRoot;
13+
private string $straussVersion = '';
1314

1415
/** @var callable */
1516
private $logger;
@@ -30,6 +31,7 @@ public function __construct( ?string $projectRoot = null, ?callable $logger = nu
3031
*/
3132
public function package( ?string $outputDir = null, array $options = [] ) :string {
3233
$options = $this->resolveOptions( $options );
34+
$this->straussVersion = $this->resolveStraussVersion( $options );
3335
$targetDir = $this->resolveOutputDirectory( $outputDir );
3436
$this->log( sprintf( 'Packaging Shield plugin to: %s', $targetDir ) );
3537

@@ -46,7 +48,7 @@ public function package( ?string $outputDir = null, array $options = [] ) :strin
4648
'install',
4749
'--no-interaction',
4850
'--prefer-dist',
49-
'--optimize-autoloader'
51+
// '--optimize-autoloader'
5052
] ),
5153
$this->projectRoot
5254
);
@@ -58,7 +60,7 @@ public function package( ?string $outputDir = null, array $options = [] ) :strin
5860
'install',
5961
'--no-interaction',
6062
'--prefer-dist',
61-
'--optimize-autoloader'
63+
// '--optimize-autoloader'
6264
] ),
6365
$this->projectRoot.'/src/lib'
6466
);
@@ -245,7 +247,7 @@ private function log( string $message ) :void {
245247
}
246248

247249
/**
248-
* @return array<string,bool>
250+
* @return array<string,mixed>
249251
*/
250252
private function resolveOptions( array $options ) :array {
251253
$defaults = [
@@ -255,11 +257,26 @@ private function resolveOptions( array $options ) :array {
255257
'npm_build' => true,
256258
'directory_clean' => true,
257259
'skip_copy' => false,
260+
'strauss_version' => null,
258261
];
259262

260263
return array_replace( $defaults, array_intersect_key( $options, $defaults ) );
261264
}
262265

266+
private function resolveStraussVersion( array $options ) :string {
267+
$fromOptions = $options[ 'strauss_version' ] ?? null;
268+
if ( is_string( $fromOptions ) && $fromOptions !== '' ) {
269+
return ltrim( $fromOptions, "v \t\n\r\0\x0B" );
270+
}
271+
272+
$env = getenv( 'SHIELD_STRAUSS_VERSION' );
273+
if ( is_string( $env ) && $env !== '' ) {
274+
return ltrim( $env, "v \t\n\r\0\x0B" );
275+
}
276+
277+
return self::FALLBACK_STRAUSS_VERSION;
278+
}
279+
263280
/**
264281
* Parse .gitattributes and return list of export-ignore patterns
265282
* This makes .gitattributes the single source of truth for package contents
@@ -548,7 +565,7 @@ private function installComposerDependencies( string $targetDir ) :void {
548565
'--no-dev',
549566
'--no-interaction',
550567
'--prefer-dist',
551-
'--optimize-autoloader',
568+
// '--optimize-autoloader',
552569
'--quiet'
553570
] ),
554571
$libDir
@@ -558,18 +575,21 @@ private function installComposerDependencies( string $targetDir ) :void {
558575
$this->log( '' );
559576
}
560577

561-
private const STRAUSS_VERSION = '0.19.4';
562-
private const STRAUSS_DOWNLOAD_URL = 'https://github.com/BrianHenryIE/strauss/releases/download/0.19.4/strauss.phar';
578+
private const FALLBACK_STRAUSS_VERSION = '0.19.4';
563579

564580
/**
565581
* Download Strauss phar file for namespace prefixing
566582
* @throws RuntimeException if download fails with detailed error message
567583
*/
568584
private function downloadStraussPhar( string $targetDir ) :void {
569-
$this->log( sprintf( 'Downloading Strauss v%s...', self::STRAUSS_VERSION ) );
585+
$this->log( sprintf( 'Downloading Strauss v%s...', $this->straussVersion ) );
570586

571587
$libDir = Path::join( $targetDir, 'src/lib' );
572588
$targetPath = Path::join( $libDir, 'strauss.phar' );
589+
$downloadUrl = sprintf(
590+
'https://github.com/BrianHenryIE/strauss/releases/download/%s/strauss.phar',
591+
$this->straussVersion
592+
);
573593

574594
// Check if allow_url_fopen is enabled
575595
if ( !ini_get( 'allow_url_fopen' ) ) {
@@ -579,7 +599,7 @@ private function downloadStraussPhar( string $targetDir ) :void {
579599
'WHY: The PHP setting "allow_url_fopen" is disabled, which prevents PHP from downloading files from URLs. '.
580600
'HOW TO FIX: Enable allow_url_fopen in your php.ini file by setting "allow_url_fopen = On", '.
581601
'or contact your system administrator to enable this setting. '.
582-
'Alternatively, you can manually download strauss.phar from '.self::STRAUSS_DOWNLOAD_URL.' '.
602+
'Alternatively, you can manually download strauss.phar from '.$downloadUrl.' '.
583603
'and place it in the package src/lib directory.'
584604
);
585605
}
@@ -602,7 +622,7 @@ private function downloadStraussPhar( string $targetDir ) :void {
602622
error_clear_last();
603623

604624
// Attempt to download the file
605-
$content = @file_get_contents( self::STRAUSS_DOWNLOAD_URL, false, $context );
625+
$content = @file_get_contents( $downloadUrl, false, $context );
606626

607627
if ( $content === false ) {
608628
$lastError = error_get_last();
@@ -617,7 +637,7 @@ private function downloadStraussPhar( string $targetDir ) :void {
617637
'(3) Firewall or proxy blocking the connection, (4) SSL certificate verification failure. '.
618638
'HOW TO FIX: Check your internet connection and try again. If the problem persists, '.
619639
'you can manually download strauss.phar from the URL above and place it at: %s',
620-
self::STRAUSS_DOWNLOAD_URL,
640+
$downloadUrl,
621641
$errorMessage,
622642
$targetPath
623643
)
@@ -632,8 +652,8 @@ private function downloadStraussPhar( string $targetDir ) :void {
632652
'WHY: This usually indicates a server-side issue or the release file was not found. '.
633653
'HOW TO FIX: Verify that Strauss version %s exists at the download URL. '.
634654
'You can manually download strauss.phar and place it at: %s',
635-
self::STRAUSS_DOWNLOAD_URL,
636-
self::STRAUSS_VERSION,
655+
$downloadUrl,
656+
$this->straussVersion,
637657
$targetPath
638658
)
639659
);
@@ -690,7 +710,7 @@ private function downloadStraussPhar( string $targetDir ) :void {
690710
'HOW TO FIX: Delete the empty file and try running the packaging again. '.
691711
'If the problem persists, manually download from: %s',
692712
$targetPath,
693-
self::STRAUSS_DOWNLOAD_URL
713+
$downloadUrl
694714
)
695715
);
696716
}

tests/docker/docker-compose.package.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ services:
77
- ${PLUGIN_SOURCE}:/package
88
environment:
99
SHIELD_PACKAGE_PATH: /package
10+
SHIELD_STRAUSS_VERSION: ${SHIELD_STRAUSS_VERSION}
1011

1112
test-runner-previous:
1213
volumes:
1314
- ../../:/app
1415
- ${PLUGIN_SOURCE}:/package
1516
environment:
16-
SHIELD_PACKAGE_PATH: /package
17+
SHIELD_PACKAGE_PATH: /package
18+
SHIELD_STRAUSS_VERSION: ${SHIELD_STRAUSS_VERSION}

0 commit comments

Comments
 (0)