Skip to content

Commit 1b84d93

Browse files
authored
Merge pull request #37 from brefphp/feature/optimize-heavy-dependencies-warning
Show a warning when using the AWS or Google SDK to optimize it
2 parents d01e2ce + 9478d6a commit 1b84d93

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/Cli/IO.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public static function error(Throwable $e, bool $logStackTrace = true): void
9898
]);
9999
}
100100

101+
public static function warning(string $message): void
102+
{
103+
self::writeln(Styles::bold(Styles::bgYellow(' ! ')) . ' ' . Styles::yellow($message));
104+
}
105+
101106
public static function enableVerbose(): void
102107
{
103108
if (self::$verboseMode) return;

src/Commands/Deploy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Bref\Cli\Cli\IO;
1616
use Bref\Cli\Cli\Styles;
1717
use Bref\Cli\Components\ServerlessFramework;
18+
use Bref\Cli\Helpers\DependencyAnalyzer;
1819
use Exception;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Input\InputOption;
@@ -57,6 +58,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5758
sprintf("Deploying %s to environment %s", Styles::bold($appName), Styles::bold($environment)),
5859
]);
5960

61+
$dependencyWarnings = DependencyAnalyzer::analyzeComposerDependencies();
62+
if (!empty($dependencyWarnings)) {
63+
IO::writeln('');
64+
foreach ($dependencyWarnings as $warning) {
65+
IO::warning($warning);
66+
}
67+
IO::writeln('');
68+
}
69+
6070
IO::spin('creating deployment');
6171

6272
// Retrieve the current git ref and commit message to serve as a label for the deployment

src/Helpers/DependencyAnalyzer.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bref\Cli\Helpers;
4+
5+
use Exception;
6+
use JsonException;
7+
8+
class DependencyAnalyzer
9+
{
10+
/**
11+
* @return string[]
12+
*/
13+
public static function analyzeComposerDependencies(string $composerJsonPath = 'composer.json'): array
14+
{
15+
if (!file_exists($composerJsonPath)) {
16+
return [];
17+
}
18+
19+
try {
20+
$composerContent = file_get_contents($composerJsonPath);
21+
if ($composerContent === false) {
22+
return [];
23+
}
24+
25+
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
26+
if (!is_array($composer)) {
27+
return [];
28+
}
29+
30+
$warnings = [];
31+
$require = $composer['require'] ?? [];
32+
33+
if (!is_array($require)) {
34+
return [];
35+
}
36+
37+
// Check for AWS SDK in composer.json or vendor directory
38+
$hasAwsSdk = isset($require['aws/aws-sdk-php']) ||
39+
(is_dir('vendor/aws/aws-sdk-php') && file_exists('vendor/aws/aws-sdk-php/composer.json'));
40+
41+
if ($hasAwsSdk) {
42+
$isOptimized = self::isAwsSdkOptimized($composer);
43+
if (!$isOptimized) {
44+
$warnings[] = 'AWS SDK detected - optimize your deployment size: https://github.com/aws/aws-sdk-php/tree/master/src/Script/Composer';
45+
}
46+
}
47+
48+
// Check for Google SDK in composer.json or vendor directory
49+
$hasGoogleSdk = isset($require['google/apiclient']) ||
50+
(is_dir('vendor/google/apiclient') && file_exists('vendor/google/apiclient/composer.json'));
51+
52+
if ($hasGoogleSdk) {
53+
$isOptimized = self::isGoogleSdkOptimized($composer);
54+
if (!$isOptimized) {
55+
$warnings[] = 'Google SDK detected - optimize your deployment size: https://github.com/googleapis/google-api-php-client#cleaning-up-unused-services';
56+
}
57+
}
58+
59+
return $warnings;
60+
61+
} catch (JsonException) {
62+
return [];
63+
}
64+
}
65+
66+
/**
67+
* Check if AWS SDK is optimized by looking for custom scripts or exclusions
68+
* @param array<string, mixed> $composer
69+
*/
70+
private static function isAwsSdkOptimized(array $composer): bool
71+
{
72+
// Check for AWS SDK optimization script
73+
$scripts = $composer['scripts'] ?? [];
74+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
75+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
76+
foreach ($preAutoloadDump as $script) {
77+
if (is_string($script) && str_contains($script, 'Aws\\Script\\Composer\\Composer::removeUnusedServices')) {
78+
return true;
79+
}
80+
}
81+
}
82+
83+
return false;
84+
}
85+
86+
/**
87+
* Check if Google SDK is optimized by looking for custom exclusions
88+
* @param array<string, mixed> $composer
89+
*/
90+
private static function isGoogleSdkOptimized(array $composer): bool
91+
{
92+
// Check for Google SDK optimization script
93+
$scripts = $composer['scripts'] ?? [];
94+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
95+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
96+
foreach ($preAutoloadDump as $script) {
97+
if (is_string($script) && str_contains($script, 'Google\\Task\\Composer::cleanup')) {
98+
return true;
99+
}
100+
}
101+
}
102+
103+
return false;
104+
}
105+
}

0 commit comments

Comments
 (0)