Skip to content

Commit e1caae5

Browse files
committed
Add maml init command
1 parent 674407d commit e1caae5

6 files changed

Lines changed: 50 additions & 60 deletions

File tree

src/Command/CommandCommon.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
use Deployer\Deployer;
1414
use Deployer\Support\Reporter;
15+
use function Deployer\Support\ci_name;
1516

1617
trait CommandCommon
1718
{
1819
/**
19-
* Collecting anonymous stat helps Deployer team improve developer experience.
20-
* If you are not comfortable with this, you will always be able to disable this
21-
* by setting DO_NOT_TRACK environment variable to `1`.
2220
* @codeCoverageIgnore
2321
*/
2422
protected function telemetry(array $data = []): void
@@ -33,6 +31,7 @@ protected function telemetry(array $data = []): void
3331
'deployer_phar' => Deployer::isPharArchive(),
3432
'php_version' => phpversion(),
3533
'os' => defined('PHP_OS_FAMILY') ? PHP_OS_FAMILY : (stristr(PHP_OS, 'DAR') ? 'OSX' : (stristr(PHP_OS, 'WIN') ? 'WIN' : (stristr(PHP_OS, 'LINUX') ? 'LINUX' : PHP_OS))),
34+
'ci' => ci_name(),
3635
], $data));
3736
} catch (\Throwable $e) {
3837
return;

src/Command/InitCommand.php

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Deployer\Command;
1212

13+
use Maml\Maml;
1314
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Input\InputOption;
@@ -72,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7273
$io = new SymfonyStyle($input, $output);
7374
$recipePath = $input->getOption('path');
7475

75-
$language = $io->choice('Select recipe language', ['php', 'yaml'], 'php');
76+
$language = $io->choice('Select recipe language', ['php', 'maml'], 'php');
7677
if (empty($recipePath)) {
7778
$recipePath = "deploy.$language";
7879
}
@@ -148,7 +149,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
148149
$hosts = [];
149150
}
150151

151-
file_put_contents($recipePath, $this->$language($template, $project, $repository, $hosts));
152+
153+
$code = match ($language) {
154+
'php' => $this->php($template, $project, $repository, $hosts),
155+
'maml' => $this->maml($template, $project, $repository, $hosts),
156+
default => $default,
157+
};
158+
159+
file_put_contents($recipePath, $code);
152160

153161
$this->telemetry();
154162
$output->writeln(sprintf(
@@ -191,51 +199,33 @@ private function php(string $template, string $project, string $repository, arra
191199
PHP;
192200
}
193201

194-
private function yaml(string $template, string $project, string $repository, array $hosts): string
202+
private function maml(string $template, string $project, string $repository, array $hosts): string
195203
{
196-
$h = "";
197-
foreach ($hosts as $host) {
198-
$h .= " $host:\n"
199-
. " remote_user: deployer\n"
200-
. " deploy_path: '~/{$project}'\n";
201-
}
202-
203-
$additionalConfigs = $this->getAdditionalConfigs($template);
204-
205-
return <<<YAML
206-
import:
207-
- recipe/$template.php
204+
$recipe = [
205+
"import" => [
206+
"recipe/$template.php",
207+
],
208+
"config" => [
209+
"repository" => "$repository",
210+
],
211+
"hosts" => [],
212+
"tasks" => [
213+
"example" => [
214+
[
215+
"run" => "date",
216+
],
217+
],
218+
],
219+
];
208220

209-
config:
210-
repository: '$repository'
211-
$additionalConfigs
212-
hosts:
213-
$h
214-
tasks:
215-
build:
216-
- run: uptime
217-
218-
after:
219-
deploy:failed: deploy:unlock
220-
221-
YAML;
222-
}
223-
224-
private function getAdditionalConfigs(string $template): string
225-
{
226-
if ($template !== 'common') {
227-
return '';
221+
foreach ($hosts as $host) {
222+
$recipe['hosts'][$host] = [
223+
"remote_user" => "deployer",
224+
"deploy_path" => "~/$project",
225+
];
228226
}
229227

230-
return <<<YAML
231-
shared_files:
232-
- .env
233-
shared_dirs:
234-
- uploads
235-
writable_dirs:
236-
- uploads
237-
238-
YAML;
228+
return Maml::stringify($recipe);
239229
}
240230

241231
private function recipes(): array

src/Logger/Logger.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212

1313
use Deployer\Exception\Exception;
1414
use Deployer\Exception\RunException;
15-
use Deployer\Exception\SchemaException;
1615
use Deployer\Host\Host;
1716
use Deployer\Logger\Handler\HandlerInterface;
1817
use Deployer\Task\Task;
19-
use Symfony\Component\Console\Input\InputInterface;
2018
use Symfony\Component\Console\Output\OutputInterface;
2119
use Throwable;
20+
use function Deployer\Support\ci_name;
2221

2322
class Logger
2423
{
@@ -62,9 +61,10 @@ public function print(Host $host, string $buffer, bool $force = false): void
6261
public function startTask(Task $task): void
6362
{
6463
$this->startTime = round(microtime(true) * 1000);
65-
if (getenv('GITHUB_WORKFLOW')) {
64+
$ci = ci_name();
65+
if ($ci === 'github') {
6666
$this->output->writeln("::group::task {$task->getName()}");
67-
} elseif (getenv('GITLAB_CI')) {
67+
} elseif ($ci === 'gitlab') {
6868
$sectionId = md5($task->getName());
6969
$start = round($this->startTime / 1000);
7070
$this->output->writeln("\e[0Ksection_start:{$start}:{$sectionId}\r\e[0K{$task->getName()}");
@@ -86,9 +86,10 @@ public function endTask(Task $task): void
8686
$millis = $millis - $seconds * 1000;
8787
$taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms";
8888

89-
if (getenv('GITHUB_WORKFLOW')) {
89+
$ci = ci_name();
90+
if ($ci === 'github') {
9091
$this->output->writeln("::endgroup::");
91-
} elseif (getenv('GITLAB_CI')) {
92+
} elseif ($ci === 'gitlab') {
9293
$sectionId = md5($task->getName());
9394
$endTime = round($endTime / 1000);
9495
$this->output->writeln("\e[0Ksection_end:{$endTime}:{$sectionId}\r\e[0K");

src/Support/Reporter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace Deployer\Support;
1212

13-
use Deployer\Utility\Httpie;
1413
use Symfony\Component\Process\PhpProcess;
1514

1615
/**

src/Support/helpers.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,15 @@ function colorize_host(string $alias): string
210210
return "<$tag>$alias</>";
211211
}
212212

213-
function escape_shell_argument(string $argument): string
213+
function ci_name(): ?string
214214
{
215-
return "'" . str_replace("'", "'\\''", $argument) . "'";
215+
if (getenv('GITHUB_WORKFLOW')) {
216+
return 'github';
217+
}
218+
if (getenv('GITLAB_CI')) {
219+
return 'gitlab';
220+
}
221+
return null;
216222
}
217223

218224
function deployer_root(): string

tests/src/Support/HelpersTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,4 @@ public function testParseHomeDir()
5959
$this->assertStringStartsWith('~', parse_home_dir('~path'));
6060
$this->assertStringEndsWith('~', parse_home_dir('path~'));
6161
}
62-
63-
public function testEscapeShellArgument()
64-
{
65-
$this->assertEquals('\'{"foobar":"Lorem ipsum\'\\\'\'s dolor"}\'', escape_shell_argument(json_encode(['foobar' => 'Lorem ipsum\'s dolor'])));
66-
}
6762
}

0 commit comments

Comments
 (0)