Skip to content

Commit

Permalink
Merge pull request #13665 from nextcloud/bugfix/noid/migrate-to-php-s…
Browse files Browse the repository at this point in the history
…coper

fix(dependencies): Migrate to PHP scoper
  • Loading branch information
nickvergessen authored Nov 4, 2024
2 parents d6e1f55 + 3ddfa91 commit f38ca37
Show file tree
Hide file tree
Showing 7 changed files with 850 additions and 376 deletions.
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ updates:
directories:
- "/tests/integration"
- "/vendor-bin/csfixer"
- "/vendor-bin/mozart"
- "/vendor-bin/openapi-extractor"
- "/vendor-bin/php-scoper"
- "/vendor-bin/phpunit"
- "/vendor-bin/psalm"
- "/vendor-bin/rector"
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ appstore:
--exclude=jest.config.js \
--exclude=jest.global.setup.js \
--exclude=.l10nignore \
--exclude=lib-vendor-organizer.php \
--exclude=mkdocs.yml \
--exclude=Makefile \
--exclude=node_modules \
Expand All @@ -100,6 +101,7 @@ appstore:
--exclude=.readthedocs.yaml \
--exclude=/recording \
--exclude=/redocly.yaml \
--exclude=/scoper.inc.php \
--exclude=/site \
--exclude=/src \
--exclude=.stylelintignore \
Expand Down
24 changes: 8 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
"psalm:fix": "psalm --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"post-install-cmd": [
"@composer bin all install --ansi",
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
"vendor/bin/php-scoper add-prefix --force # Scope our dependencies",
"@php lib-vendor-organizer.php lib/Vendor/ OCA\\\\Talk\\\\Vendor",
"composer dump-autoload -o"
],
"post-update-cmd": [
"@composer bin all update --ansi",
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
"vendor/bin/php-scoper add-prefix --force # Scope our dependencies",
"rm -Rf lib/Vendor && mv build lib/Vendor",
"find lib/Vendor/ -maxdepth 1 -mindepth 1 -type d | cut -d '/' -f3 | xargs -I {} rm -Rf vendor/{} # Remove origins",
"@php lib-vendor-organizer.php lib/Vendor/ OCA\\\\Talk\\\\Vendor",
"composer dump-autoload -o"
],
"test:unit": "vendor/bin/phpunit -c tests/php/phpunit.xml --colors=always --fail-on-warning --fail-on-risky"
},
Expand All @@ -47,17 +51,5 @@
"bamarni/composer-bin-plugin": "^1.8",
"cuyz/valinor": "^1.14",
"firebase/php-jwt": "^6.10"
},
"extra": {
"mozart": {
"dep_namespace": "OCA\\Talk\\Vendor\\",
"dep_directory": "/lib/Vendor/",
"classmap_directory": "/lib/autoload/",
"classmap_prefix": "NEXTCLOUDTALK_",
"packages": [
"firebase/php-jwt",
"cuyz/valinor"
]
}
}
}
83 changes: 83 additions & 0 deletions lib-vendor-organizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env php
<?php
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

$sourceDirectory = $argv[1];
$sourceDirectory = rtrim($sourceDirectory, '/') . '/';

if (!str_starts_with('/', $sourceDirectory)) {
$sourceDirectory = getcwd() . '/' . $sourceDirectory;
}

$stripNamespacePrefix = $argv[2] ?? '';
if ($stripNamespacePrefix) {
printf("Namespace Prefix to strip from destination dir is %s%s", $stripNamespacePrefix, PHP_EOL);
}

if (!file_exists($sourceDirectory) || !is_dir($sourceDirectory)) {
print("Directory not found");
exit(1);
}
$organizationList = [];
foreach(scandir($sourceDirectory) as $file) {
if (!is_dir($sourceDirectory . $file) || $file === '.' || $file === '..') {
continue;
}
$organizationList[] = $sourceDirectory . $file . '/';
}

$projectList = [];
foreach($organizationList as $organizationDir) {
foreach(scandir($organizationDir) as $file) {
if (!is_dir($organizationDir . $file) || $file === '.' || $file === '..') {
continue;
}
$projectList[] = $organizationDir . $file . '/';
}
}

foreach ($projectList as $projectDir) {
if (!file_exists($projectDir . 'composer.json')) {
continue;
}
$projectInfo = json_decode(file_get_contents($projectDir . 'composer.json'), true);
if (!isset($projectInfo['autoload']['psr-4'])) {
printf("No supported autoload configuration in %s" . PHP_EOL, $projectDir);
exit(2);
}
foreach ($projectInfo['autoload']['psr-4'] as $namespace => $codeDir) {
if ($stripNamespacePrefix !== '' && strpos($namespace, $stripNamespacePrefix) === 0) {
$namespace = str_replace($stripNamespacePrefix, '', $namespace);
}
$destination = $sourceDirectory . str_replace('\\', '/', $namespace);
if (file_exists($destination)) {
rmdir_recursive($destination);
}
mkdir($destination, 0777, true);
if (!rename($projectDir . $codeDir, $destination)) {
printf("Failed to move %s to %s" . PHP_EOL, $projectDir . $codeDir, $destination);
exit(3);
}
}
}

foreach($organizationList as $organizationDir) {
rmdir_recursive($organizationDir);
}

function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) {
continue;
}
if (is_dir("$dir/$file")) {
rmdir_recursive("$dir/$file");
} else {
unlink("$dir/$file");
}
}
rmdir($dir);
}
50 changes: 50 additions & 0 deletions scoper.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

use Isolated\Symfony\Component\Finder\Finder;

// You can do your own things here, e.g. collecting symbols to expose dynamically
// or files to exclude.
// However, beware that this file is executed by PHP-Scoper, hence if you are using
// the PHAR it will be loaded by the PHAR. So it is highly recommended to avoid
// to autoload any code here: it can result in a conflict or even corrupt
// the PHP-Scoper analysis.

return [
// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#prefix
'prefix' => 'OCA\\Talk\\Vendor',
'output-dir' => 'lib/Vendor',

// For more see: https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#finders-and-paths
'finders' => [
Finder::create()->files()
->exclude([
'test',
'composer',
'bin',
])
->notName('autoload.php')
->in('vendor/cuyz'),
Finder::create()->files()
->exclude([
'test',
'composer',
'bin',
])
->notName('autoload.php')
->in('vendor/psr/simple-cache'),
Finder::create()->files()
->exclude([
'test',
'composer',
'bin',
])
->notName('autoload.php')
->in('vendor/firebase'),
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"sort-packages": true
},
"require": {
"coenjacobs/mozart": "^0.7.1"
"humbug/php-scoper": "^0.18.7"
}
}
Loading

0 comments on commit f38ca37

Please sign in to comment.