|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Deployee\Plugins\GenerateDeploy\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Deployee\Components\Config\ConfigInterface; |
|
6
|
|
|
use Deployee\Components\Environment\Environment; |
|
7
|
|
|
use Deployee\Plugins\Deploy\Definitions\Deploy\AbstractDeployDefinition; |
|
8
|
|
|
use Nette\PhpGenerator\ClassType; |
|
9
|
|
|
use Symfony\Component\Console\Command\Command; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
class GenerateDeployCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ConfigInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Environment |
|
23
|
|
|
*/ |
|
24
|
|
|
private $env; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Environment $env |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setEnv(Environment $env) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->env = $env; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ConfigInterface $config |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setConfig(ConfigInterface $config) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->config = $config; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function configure() |
|
46
|
|
|
{ |
|
47
|
|
|
parent::configure(); |
|
48
|
|
|
$this |
|
49
|
|
|
->setName('deploy:generate') |
|
50
|
|
|
->addArgument("name", InputArgument::OPTIONAL, '', uniqid('', false) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param InputInterface $input |
|
56
|
|
|
* @param OutputInterface $output |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
|
|
$class = sprintf('Deploy_%d_%s', time(), $input->getArgument('name')); |
|
|
|
|
|
|
61
|
|
|
$class = str_replace(['-', ' '], '_', $class); |
|
62
|
|
|
$filePath = $this->config->get('deploy_definition_path') . "/{$class}.php"; |
|
63
|
|
|
$filePath = strpos($filePath, '/') !== 0 && strpos($filePath, ':') !== 1 |
|
64
|
|
|
? $this->env->getWorkDir() . DIRECTORY_SEPARATOR . $filePath |
|
65
|
|
|
: $filePath; |
|
66
|
|
|
|
|
67
|
|
|
$fileContents = $this->generateFileContents($class); |
|
68
|
|
|
if(!file_put_contents($filePath, $fileContents)){ |
|
69
|
|
|
throw new \RuntimeException('File could not be generated!'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$output->writeln(sprintf('Generated file %s', $filePath)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $class |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
private function generateFileContents(string $class): string |
|
80
|
|
|
{ |
|
81
|
|
|
$generator = new ClassType($class); |
|
82
|
|
|
$generator->addExtend(AbstractDeployDefinition::class); |
|
83
|
|
|
if(class_exists('Deployee\Plugins\IdeSupport\IdeSupportPlugin')){ |
|
84
|
|
|
$generator->setComment('@mixin \deployee_ide_helper'); |
|
85
|
|
|
} |
|
86
|
|
|
$method = $generator->addMethod('define'); |
|
87
|
|
|
$method->setComment('@return void'); |
|
88
|
|
|
$method->setBody('// Start writing awesome deployments'); |
|
89
|
|
|
|
|
90
|
|
|
return <<<EOL |
|
91
|
|
|
<?php |
|
92
|
|
|
|
|
93
|
|
|
{$generator} |
|
94
|
|
|
EOL; |
|
95
|
|
|
} |
|
96
|
|
|
} |