forked from leafsphp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployCommand.php
More file actions
104 lines (87 loc) · 3.04 KB
/
DeployCommand.php
File metadata and controls
104 lines (87 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace Leaf\Console;
use Leaf\Console\Utils\Package;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class DeployCommand extends Command
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('deploy')
->setAliases(['publish'])
->setDescription('Deploy your leaf project')
->addOption('to', null, InputOption::VALUE_OPTIONAL, 'Provider to deploy to eg: git, heroku', 'git')
->addOption('project', null, InputOption::VALUE_OPTIONAL, 'Project name for provider', '')
->addOption('branch', null, InputOption::VALUE_OPTIONAL, 'Branch to deploy', 'main')
->addOption('switch-to-main', null, InputOption::VALUE_NONE, 'Switch default branch to main')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces deploy');
}
/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$needsUpdate = Package::updateAvailable();
if ($needsUpdate) {
$output->writeln('<comment>Update found, updating to the latest stable version...</comment>');
$updateProcess = Process::fromShellCommandline('php ' . dirname(__DIR__) . '/bin/leaf update');
$updateProcess->run();
if ($updateProcess->isSuccessful()) {
$output->writeln("<info>Leaf CLI updated successfully, building your app...</info>\n");
}
}
$vendors = [
'git' => ['git push'],
'heroku' => [
"heroku git:remote -a {$input->getOption('project')}",
$input->getOption('branch') !== 'main' ? "git checkout {$input->getOption('branch')}" : 'echo ""',
$input->getOption('switch-to-main') ? 'git checkout -b main && git branch -D master && git push heroku main' : 'echo ""',
"git push heroku {$input->getOption('branch')}"
],
];
$output->writeln('<comment>Deploying ' . basename(getcwd()) . '...</comment>');
if ($input->getOption('to') === 'heroku' && !Utils\Core::commandExists('heroku')) {
$output->writeln('<info>Heroku not installed, attempting to install heroku CLI...</info>');
$installHerokuProcess = Process::fromShellCommandline(
'brew tap heroku/brew && brew install heroku',
null,
null,
null,
null
);
$installHerokuProcess->run(function ($type, $line) use ($output) {
$output->write($line);
});
if (!$installHerokuProcess->isSuccessful()) {
$output->writeln('<error>Could not install heroku CLI. Manually install it and try again.</error>');
return 1;
}
}
$process = Process::fromShellCommandline(
str_replace('&& &&', '&&', implode(' && ', $vendors[$input->getOption('to')])),
null,
null,
null,
null
);
$process->run(function ($type, $line) use ($output) {
$output->write($line);
});
if (!$process->isSuccessful()) return 1;
return 0;
}
}