Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
/vendor/
# Ignore prefixed build directory
/build/

# Composer
/vendor/
composer.lock

# IDE files
.idea/
.vscode/

# OS generated files
.DS_Store
Thumbs.db

# Test files
/test-prefix.php
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Uncanny Automator Developer Tools

Centralized development tools for Uncanny Owl plugins, with namespaced Codeception to avoid global conflicts.

## Installation

```bash
composer require uncanny-owl/developer-tools
```

## Using the TestDetection utility

The TestDetection utility provides an isolated way to detect when code is running in a test environment, without interfering with other plugins that may also use Codeception.

```php
use UncannyOwl\DevTools\TestDetection;

if (TestDetection::isTestRunning()) {
// Code that should only run during tests
}
```

## How it works

This package uses PHP-Scoper to prefix all Codeception dependencies with the `UncannyOwl\DevTools\Vendor` namespace. This means:

1. Your plugin can detect Codeception tests using our namespaced version
2. Other plugins can still use the global Codeception classes without conflicts
3. The detection is isolated and won't interfere with other plugins

## Release Process

For maintainers, follow these steps to create a new release:

1. Make your code changes on a development branch
2. Update version numbers in composer.json if needed
3. Run the prepare-release script with your version number:

```bash
# Prepare the release
composer run prepare-release
./prepare-release.sh 1.0.0 # Replace with your version

# Follow the instructions printed by the script to push the release
```

The script will:
- Create a release branch
- Build the prefixed version of dependencies
- Include the build directory in the release
- Commit the changes
- Provide instructions for tagging and pushing the release

Once you push the tag, Packagist will automatically update with the new version.
110 changes: 110 additions & 0 deletions build.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Build script to create a prefixed version of the library
*
* Usage: php build.php
*/

echo "Starting build process...\n";

// Step 1: Run php-scoper to prefix the Codeception classes
echo "Running PHP-Scoper...\n";
exec('vendor/bin/php-scoper add-prefix --output-dir=./build --force', $output, $return_var);
if ($return_var !== 0) {
echo "Error running PHP-Scoper\n";
exit(1);
}

// Step 2: Copy our src directory to the build directory
echo "Copying src directory...\n";
if (!file_exists('build/src')) {
mkdir('build/src', 0755, true);
}
exec('cp -R src/* build/src/', $output, $return_var);
if ($return_var !== 0) {
echo "Error copying src directory\n";
exit(1);
}

// Step 3: Create composer.json in the build directory
echo "Creating composer.json in build directory...\n";
$composer_json = [
'name' => 'uncanny-owl/developer-tools',
'description' => 'Centralized development tools for Uncanny Owl plugins (Prefixed version)',
'type' => 'library',
'license' => 'GPL-3.0-or-later',
'authors' => [
[
'name' => 'Uncanny Automator',
'email' => 'support@uncannyautomator.com'
]
],
'autoload' => [
'psr-4' => [
'UncannyOwl\\DevTools\\' => 'src/',
'UncannyOwl\\DevTools\\Vendor\\Codeception\\' => 'codeception/codeception/',
'UncannyOwl\\DevTools\\Vendor\\tad\\WPBrowser\\' => 'lucatume/wp-browser/src/'
]
],
'minimum-stability' => 'dev',
'prefer-stable' => true
];
file_put_contents('build/composer.json', json_encode($composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

// Step 4: Generate autoload files
echo "Generating autoload files...\n";
exec('composer dump-autoload -d build', $output, $return_var);
if ($return_var !== 0) {
echo "Error generating autoload files\n";
exit(1);
}

// Step 5: Create a bootstrap file
echo "Creating bootstrap file...\n";
$bootstrap_content = <<<EOT
<?php
/**
* Bootstrap file for prefixed dependencies
*/

// Define the base directory
define('UNCANNY_DEVTOOLS_PREFIXED_DIR', __DIR__);

// Function to load a prefixed class
function uncanny_devtools_load_prefixed_class(\$class) {
// Only handle our prefixed namespace
if (strpos(\$class, 'UncannyOwl\\\\DevTools\\\\Vendor\\\\') === 0) {
// Remove the prefix
\$relative_class = str_replace('UncannyOwl\\\\DevTools\\\\Vendor\\\\', '', \$class);

// Convert namespace separators to directory separators
\$file = str_replace('\\\\', '/', \$relative_class) . '.php';

// Look in potential directories
\$potential_paths = [
UNCANNY_DEVTOOLS_PREFIXED_DIR . '/codeception/codeception/' . \$file,
UNCANNY_DEVTOOLS_PREFIXED_DIR . '/lucatume/wp-browser/src/' . \$file,
];

foreach (\$potential_paths as \$path) {
if (file_exists(\$path)) {
require_once \$path;
return true;
}
}
}

return false;
}

// Register the autoloader
spl_autoload_register('uncanny_devtools_load_prefixed_class');

// Load the standard autoloader for the UncannyOwl\DevTools namespace
require_once __DIR__ . '/vendor/autoload.php';

EOT;
file_put_contents('build/bootstrap.php', $bootstrap_content);

echo "Build completed successfully!\n";
echo "To use the prefixed version, require 'build/bootstrap.php' in your project.\n";
36 changes: 35 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"vlucas/phpdotenv": "^5.3",
"uocs/uncanny-owl-coding-standards": "dev-main"
},
"require-dev": {
"humbug/php-scoper": "^0.18.3"
},
"config": {
"sort-packages": false,
"optimize-autoloader": true,
Expand All @@ -30,6 +33,11 @@
"phpstan/extension-installer": true
}
},
"autoload": {
"psr-4": {
"UncannyOwl\\DevTools\\": "src/"
}
},
"bin": [
"bin/pr-code-check",
"bin/pr-code-check.bat",
Expand All @@ -51,8 +59,34 @@
"post-update-cmd": [
"@php -r \"file_exists('bin/pr-code-check') && chmod(realpath('bin/pr-code-check'), 0755);\"",
"@php -r \"file_exists('bin/pr-code-check.php') && chmod(realpath('bin/pr-code-check.php'), 0755);\""
],
"prefix-dependencies": "php-scoper add-prefix --output-dir=./build --force",
"build": [
"@prefix-dependencies",
"@composer dump-autoload -d build"
],
"pre-package-release": [
"@build"
],
"release": [
"@composer validate --strict",
"@build",
"php -r \"echo 'Package is ready for release. The prefixed version is in the build directory.\\n';\""
],
"prepare-release": [
"@composer validate --strict",
"@build",
"php -r \"echo 'Run: ./prepare-release.sh VERSION to create a release branch with the prefixed build included.\\n';\""
]
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"extra": {
"php-scoper": {
"prefix": "UncannyOwl\\DevTools\\Vendor",
"exclude-namespaces": [
"UncannyOwl\\DevTools\\*"
]
}
}
}
Loading