|
2 | 2 |
|
3 | 3 | namespace craft\cloud\cli\controllers; |
4 | 4 |
|
| 5 | +use Composer\Semver\Semver; |
5 | 6 | use Craft; |
6 | 7 | use craft\console\Controller; |
7 | | -use Illuminate\Support\Collection; |
8 | | -use samdark\log\PsrMessage; |
9 | | -use yii\console\ExitCode; |
| 8 | +use craft\helpers\App; |
| 9 | +use yii\console\Exception; |
10 | 10 |
|
11 | 11 | class BuildController extends Controller |
12 | 12 | { |
13 | | - public function actionIndex(string $json): int |
| 13 | + public $defaultAction = 'build'; |
| 14 | + public ?string $publishAssetBundlesTo = null; |
| 15 | + public string $craftEdition = ''; |
| 16 | + |
| 17 | + public function options($actionID): array |
| 18 | + { |
| 19 | + return array_merge(parent::options($actionID), [ |
| 20 | + 'publish-asset-bundles-to', |
| 21 | + 'craft-edition', |
| 22 | + ]); |
| 23 | + } |
| 24 | + |
| 25 | + public function actionBuild(): int |
14 | 26 | { |
15 | | - $options = json_decode($json, true); |
16 | | - $exitCode = ExitCode::OK; |
17 | | - |
18 | | - Collection::make([ |
19 | | - 'cloud/validate/project-type', |
20 | | - 'cloud/asset-bundles/publish', |
21 | | - ])->each(function(string $command) use ($options, &$exitCode) { |
22 | | - $params = $options[$command] ?? []; |
23 | | - $exitCode = $this->run("/$command", $params); |
24 | | - |
25 | | - if ($exitCode !== ExitCode::OK) { |
26 | | - Craft::error(new PsrMessage('Command failed.', [ |
27 | | - 'command' => $command, |
28 | | - 'params' => $params, |
29 | | - ])); |
30 | | - |
31 | | - return false; |
32 | | - } |
33 | | - }); |
34 | | - |
35 | | - return $exitCode; |
| 27 | + if (!$this->isEditionValid($this->craftEdition)) { |
| 28 | + throw new Exception('Invalid Craft CMS edition.'); |
| 29 | + } |
| 30 | + |
| 31 | + return $this->run('/cloud/asset-bundles/publish', [ |
| 32 | + 'to' => $this->publishAssetBundlesTo, |
| 33 | + ]); |
| 34 | + } |
| 35 | + |
| 36 | + private function isEditionValid(string $edition): bool |
| 37 | + { |
| 38 | + $craftVersion = Craft::$app->getInfo()->version; |
| 39 | + |
| 40 | + // CRAFT_EDITION is enforced in these versions, so we don't need to validate |
| 41 | + if (App::env('CRAFT_EDITION') && Semver::satisfies($craftVersion, '^4.10 || ^5.2')) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + $editionFromProjectConfig = Craft::$app->getProjectConfig()->get('system.edition', true); |
| 46 | + |
| 47 | + if (!$editionFromProjectConfig) { |
| 48 | + throw new Exception('Unable to determine the Craft CMS edition.'); |
| 49 | + } |
| 50 | + |
| 51 | + if ($edition !== $editionFromProjectConfig) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + return true; |
36 | 56 | } |
37 | 57 | } |
0 commit comments