|
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 | + 'publishAssetBundlesTo', |
| 21 | + 'craftEdition', |
| 22 | + ]); |
| 23 | + } |
| 24 | + |
| 25 | + public function actionBuild(): int |
| 26 | + { |
| 27 | + $this->validateEdition($this->craftEdition); |
| 28 | + |
| 29 | + return $this->run('/cloud/asset-bundles/publish', [ |
| 30 | + 'to' => $this->publishAssetBundlesTo, |
| 31 | + ]); |
| 32 | + } |
| 33 | + |
| 34 | + private function validateEdition(string $edition): void |
14 | 35 | { |
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; |
| 36 | + $craftVersion = Craft::$app->getVersion(); |
| 37 | + $editionFromEnv = App::env('CRAFT_EDITION'); |
| 38 | + |
| 39 | + // CRAFT_EDITION is enforced in these versions, so we don't need to validate |
| 40 | + if ($editionFromEnv && Semver::satisfies($craftVersion, '^4.10 || ^5.2')) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $editionFromProjectConfig = Craft::$app->getProjectConfig()->get('system.edition', true); |
| 45 | + |
| 46 | + if (!$editionFromProjectConfig || !$edition) { |
| 47 | + throw new Exception('Unable to determine the Craft CMS edition.'); |
| 48 | + } |
| 49 | + |
| 50 | + if ($edition !== $editionFromProjectConfig) { |
| 51 | + throw new Exception("This Craft Cloud project is only valid for the Craft CMS edition “{$edition}”."); |
| 52 | + } |
36 | 53 | } |
37 | 54 | } |
0 commit comments