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