Skip to content

Commit 2e178b6

Browse files
committed
WIP CI platform detection
1 parent f735b32 commit 2e178b6

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

src/Commands/DockerCiCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands;
4+
5+
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform;
6+
use BlameButton\LaravelDockerBuilder\Detector\CiPlatformDetector;
7+
8+
class DockerCiCommand extends BaseCommand
9+
{
10+
protected $name = 'docker:ci';
11+
12+
protected $description = '';
13+
14+
public function handle(): int
15+
{
16+
$detected = app(CiPlatformDetector::class)->detect();
17+
18+
if (CiPlatform::GITLAB_CI === $detected) {
19+
$output = base_path('.gitlab-ci.yml');
20+
21+
if (file_exists($output)) {
22+
return self::SUCCESS;
23+
}
24+
25+
copy(package_path('resources/templates/.gitlab-ci.yml'), $output);
26+
}
27+
28+
return self::SUCCESS;
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices;
4+
5+
class CiPlatform
6+
{
7+
public const GITLAB_CI = 'gitlab';
8+
9+
public const GITHUB_ACTIONS = 'github';
10+
}

src/Detector/CiPlatformDetector.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Detector;
4+
5+
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\Choices\CiPlatform;
6+
use Illuminate\Support\Str;
7+
8+
class CiPlatformDetector implements DetectorContract
9+
{
10+
public function detect(): string|false
11+
{
12+
$base = base_path();
13+
14+
do {
15+
$git = $base.'/.git/config';
16+
17+
if (! file_exists($git)) {
18+
$base = Str::beforeLast($base, '/');
19+
20+
continue;
21+
}
22+
23+
$git = file_get_contents($git);
24+
25+
if (Str::contains($git, 'gitlab')) {
26+
return CiPlatform::GITLAB_CI;
27+
}
28+
29+
if (Str::contains($git, 'github')) {
30+
return CiPlatform::GITHUB_ACTIONS;
31+
}
32+
} while (Str::contains($base, '/'));
33+
34+
return false;
35+
}
36+
}

src/DockerServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BlameButton\LaravelDockerBuilder;
44

55
use BlameButton\LaravelDockerBuilder\Commands\DockerBuildCommand;
6+
use BlameButton\LaravelDockerBuilder\Commands\DockerCiCommand;
67
use BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand;
78
use BlameButton\LaravelDockerBuilder\Commands\DockerPushCommand;
89
use Illuminate\Support\ServiceProvider;
@@ -14,6 +15,7 @@ public function boot(): void
1415
if ($this->app->runningInConsole()) {
1516
$this->commands([
1617
DockerBuildCommand::class,
18+
DockerCiCommand::class,
1719
DockerGenerateCommand::class,
1820
DockerPushCommand::class,
1921
]);

0 commit comments

Comments
 (0)