Skip to content

Commit c5300b2

Browse files
mnapoliclaude
andcommitted
Add dependency optimization warnings for heavy dependencies
- Add DependencyAnalyzer helper to detect AWS SDK and Google SDK - Show warnings during deployment for unoptimized dependencies - Check if optimization scripts are already configured - Provide links to official optimization guides - Minimal performance impact during deployment 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d01e2ce commit c5300b2

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

src/Commands/Deploy.php

Lines changed: 14 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,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5758
sprintf("Deploying %s to environment %s", Styles::bold($appName), Styles::bold($environment)),
5859
]);
5960

61+
// Analyze dependencies for optimization warnings
62+
$dependencyAnalysis = DependencyAnalyzer::analyzeComposerDependencies();
63+
if (!empty($dependencyAnalysis['warnings'])) {
64+
IO::writeln(['']);
65+
foreach ($dependencyAnalysis['warnings'] as $warning) {
66+
IO::writeln(Styles::yellow('' . $warning));
67+
}
68+
foreach ($dependencyAnalysis['suggestions'] as $suggestion) {
69+
IO::writeln(Styles::gray(' ' . $suggestion));
70+
}
71+
IO::writeln(['']);
72+
}
73+
6074
IO::spin('creating deployment');
6175

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

src/Helpers/DependencyAnalyzer.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 array{warnings: string[], suggestions: string[]}
12+
*/
13+
public static function analyzeComposerDependencies(string $composerJsonPath = 'composer.json'): array
14+
{
15+
if (!file_exists($composerJsonPath)) {
16+
return ['warnings' => [], 'suggestions' => []];
17+
}
18+
19+
try {
20+
$composerContent = file_get_contents($composerJsonPath);
21+
if ($composerContent === false) {
22+
return ['warnings' => [], 'suggestions' => []];
23+
}
24+
25+
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
26+
if (!is_array($composer)) {
27+
return ['warnings' => [], 'suggestions' => []];
28+
}
29+
30+
$warnings = [];
31+
$suggestions = [];
32+
33+
// Check for AWS SDK
34+
$require = $composer['require'] ?? [];
35+
if (is_array($require) && isset($require['aws/aws-sdk-php'])) {
36+
$isOptimized = self::isAwsSdkOptimized($composerJsonPath);
37+
if (!$isOptimized) {
38+
$warnings[] = 'AWS SDK detected - this can significantly increase deployment size';
39+
$suggestions[] = 'Consider optimizing AWS SDK by including only required services: https://github.com/aws/aws-sdk-php/tree/master/src/Script/Composer';
40+
}
41+
}
42+
43+
// Check for Google SDK
44+
if (is_array($require) && (isset($require['google/apiclient']) || isset($require['google/cloud']))) {
45+
$isOptimized = self::isGoogleSdkOptimized($composerJsonPath);
46+
if (!$isOptimized) {
47+
$warnings[] = 'Google SDK detected - this can significantly increase deployment size';
48+
$suggestions[] = 'Consider optimizing Google SDK by removing unused services: https://github.com/googleapis/google-api-php-client#cleaning-up-unused-services';
49+
}
50+
}
51+
52+
return ['warnings' => $warnings, 'suggestions' => $suggestions];
53+
54+
} catch (JsonException) {
55+
return ['warnings' => [], 'suggestions' => []];
56+
}
57+
}
58+
59+
/**
60+
* Check if AWS SDK is optimized by looking for custom scripts or exclusions
61+
*/
62+
private static function isAwsSdkOptimized(string $composerJsonPath): bool
63+
{
64+
try {
65+
$composerContent = file_get_contents($composerJsonPath);
66+
if ($composerContent === false) {
67+
return false;
68+
}
69+
70+
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
71+
if (!is_array($composer)) {
72+
return false;
73+
}
74+
75+
// Check for AWS SDK optimization script
76+
$scripts = $composer['scripts'] ?? [];
77+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
78+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
79+
foreach ($preAutoloadDump as $script) {
80+
if (is_string($script) && str_contains($script, 'aws-sdk-php') && str_contains($script, 'remove-unused-services')) {
81+
return true;
82+
}
83+
}
84+
}
85+
86+
// Check for custom AWS SDK optimizations in extra section
87+
if (isset($composer['extra']['aws']['includes'])) {
88+
return true;
89+
}
90+
91+
return false;
92+
93+
} catch (Exception) {
94+
return false;
95+
}
96+
}
97+
98+
/**
99+
* Check if Google SDK is optimized by looking for custom exclusions
100+
*/
101+
private static function isGoogleSdkOptimized(string $composerJsonPath): bool
102+
{
103+
try {
104+
$composerContent = file_get_contents($composerJsonPath);
105+
if ($composerContent === false) {
106+
return false;
107+
}
108+
109+
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
110+
if (!is_array($composer)) {
111+
return false;
112+
}
113+
114+
// Check for Google SDK optimization script
115+
$scripts = $composer['scripts'] ?? [];
116+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
117+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
118+
foreach ($preAutoloadDump as $script) {
119+
if (is_string($script) && str_contains($script, 'google') && str_contains($script, 'remove-unused-services')) {
120+
return true;
121+
}
122+
}
123+
}
124+
125+
// Check for custom Google SDK optimizations in extra section
126+
if (isset($composer['extra']['google']['exclude_files'])) {
127+
return true;
128+
}
129+
130+
return false;
131+
132+
} catch (Exception) {
133+
return false;
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)