Skip to content

Commit fdb871f

Browse files
committed
WIP
1 parent c52870d commit fdb871f

File tree

7 files changed

+227
-1
lines changed

7 files changed

+227
-1
lines changed

.php_cs.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"php":"7.4.2","version":"2.16.3","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"config\/config.php":76455652,"tests\/StartDayCommandTest.php":3282434019,"tests\/UpdateIssueCommandTest.php":1356435344,"tests\/SetRepositoryCommandTest.php":2823014386,"tests\/StartIssueCommandTest.php":10661279,"tests\/CloseIssueCommandTest.php":2994251415,"tests\/TestCase.php":1963381742,"tests\/EndDayCommandTest.php":3510480877,"src\/LaravelGitWorkflowProvider.php":3602220539,"src\/Actions\/ParseGitHubIssues.php":3355812848,"src\/Actions\/GitCommand.php":1506492159,"src\/Actions\/SetBranchForIssue.php":1583261011,"src\/Actions\/GetCurrentBranchName.php":464991683,"src\/Actions\/ParseGitBranches.php":1885888977,"src\/Actions\/RunTests.php":880893366,"src\/Actions\/PruneTestBranches.php":1201807245,"src\/Commands\/EndDay.php":581074216,"src\/Commands\/UpdateIssue.php":1678619067,"src\/Commands\/SetRepository.php":3519907414,"src\/Commands\/StartDay.php":583590763,"src\/Commands\/CloseIssue.php":4093772881,"src\/Commands\/StartIssue.php":555344549}}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": "^7.4",
16-
"illuminate/support": "^7.0"
16+
"illuminate/support": "^7.0",
17+
"ext-json": "*"
1718
},
1819
"require-dev": {
1920
"orchestra/testbench": "^5.0",

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
'github_user' => env('LGW_GITHUB_USER', ''),
77
'wip' => env('LGW_WIP', 'WIP'),
88
'env' => base_path('.env'),
9+
'composer_json' => base_path('/composer.json'),
10+
'repositories' => [],
911
];

src/Commands/SetRepository.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
4+
namespace Grosv\LaravelGitWorkflow\Commands;
5+
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Str;
9+
10+
class SetRepository extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'repo {package} {repo}';
18+
19+
public string $package;
20+
public string $repo;
21+
22+
23+
24+
public function handle(): int
25+
{
26+
$this->package = $this->argument('package');
27+
$this->repo = $this->argument('repo');
28+
29+
$packages = config('laravel-git-workflow.repositories') ?? [];
30+
31+
if (!isset($packages[$this->package])) {
32+
$this->error('You have not configured a package called ' . $this->package);
33+
}
34+
35+
$new = $packages[$this->package];
36+
37+
$composer = json_decode(File::get(config('laravel-git-workflow.composer_json')), true);
38+
39+
$found = false;
40+
41+
foreach ($composer['repositories'] as $k => $v) {
42+
if (Str::contains($v['url'], $this->package)) {
43+
if (!isset($new[$this->repo])) {
44+
unset($composer['repositories'][$k]);
45+
} else {
46+
$composer['repositories'][$k]['url'] = $new[$this->repo];
47+
$composer['repositories'][$k]['type'] = $this->repo;
48+
}
49+
$found = true;
50+
}
51+
}
52+
53+
if (!$found) {
54+
$composer['repositories'][] = ['url' => $new['path'], 'type' => $this->repo];
55+
}
56+
57+
File::put(config('laravel-git-workflow.composer_json'), json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
58+
return 0;
59+
}
60+
}

src/LaravelGitWorkflowProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Grosv\LaravelGitWorkflow\Commands\CloseIssue;
66
use Grosv\LaravelGitWorkflow\Commands\EndDay;
7+
use Grosv\LaravelGitWorkflow\Commands\SetRepository;
78
use Grosv\LaravelGitWorkflow\Commands\StartDay;
89
use Grosv\LaravelGitWorkflow\Commands\StartIssue;
910
use Grosv\LaravelGitWorkflow\Commands\UpdateIssue;
@@ -20,8 +21,15 @@ public function boot(): void
2021
StartDay::class,
2122
StartIssue::class,
2223
UpdateIssue::class,
24+
SetRepository::class,
2325
]);
2426
}
27+
28+
if ($this->app->runningInConsole()) {
29+
$this->publishes([
30+
__DIR__.'/../config/config.php' => config_path('laravel-git-workflow.php'),
31+
], 'config');
32+
}
2533
}
2634

2735
public function register(): void

tests/SetRepositoryCommandTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
4+
namespace Tests;
5+
6+
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\Facades\File;
8+
9+
class SetRepositoryCommandTest extends TestCase
10+
{
11+
private $composer;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
Config::set('laravel-git-workflow.composer_json', __DIR__ . '/test_composer.json');
18+
Config::set('laravel-git-workflow.repositories', [
19+
'my-happy-package' => [
20+
'git' => 'https://github.com/edgrosvenor/my-happy-package',
21+
'path' => '../../packages/edgrosvenor/my-happy-package',
22+
],
23+
'my-crazy-package' => [
24+
'git' => 'https://github.com/edgrosvenor/my-crazy-package',
25+
'path' => '../../packages/edgrosvenor/my-crazy-package',
26+
],
27+
'my-sad-package' => [
28+
'path' => '../../packages/edgrosvenor/my-sad-package',
29+
],
30+
]);
31+
32+
$this->composer = File::get(config('laravel-git-workflow.composer_json'));
33+
}
34+
35+
public function tearDown(): void
36+
{
37+
File::put(config('laravel-git-workflow.composer_json'), $this->composer);
38+
}
39+
40+
/** @test */
41+
public function it_happily_switches_between_two_repos()
42+
{
43+
$composer = json_decode($this->composer, true);
44+
45+
$this->assertEquals(['type' => 'path', 'url' => '../../packages/edgrosvenor/my-crazy-package'], $composer['repositories'][1]);
46+
47+
$this->artisan('repo my-crazy-package git');
48+
49+
$updated = json_decode(File::get(config('laravel-git-workflow.composer_json')), true);
50+
51+
$this->assertEquals(['type' => 'git', 'url' => 'https://github.com/edgrosvenor/my-crazy-package'], $updated['repositories'][1]);
52+
53+
$this->artisan('repo my-crazy-package path');
54+
55+
$updated = json_decode(File::get(config('laravel-git-workflow.composer_json')), true);
56+
57+
$this->assertEquals(['type' => 'path', 'url' => '../../packages/edgrosvenor/my-crazy-package'], $updated['repositories'][1]);
58+
59+
$this->artisan('repo my-crazy-package packagist');
60+
61+
$updated = File::get(config('laravel-git-workflow.composer_json'));
62+
63+
$this->assertStringNotContainsString('my-crazy-package', $updated);
64+
65+
$this->artisan('repo my-crazy-package path');
66+
67+
$updated = json_decode(File::get(config('laravel-git-workflow.composer_json')), true);
68+
69+
$this->assertEquals(['type' => 'path', 'url' => '../../packages/edgrosvenor/my-crazy-package'], $updated['repositories'][1]);
70+
71+
$this->artisan('repo my-crazy-package git');
72+
73+
$updated = json_decode(File::get(config('laravel-git-workflow.composer_json')), true);
74+
75+
$this->assertEquals(['type' => 'git', 'url' => 'https://github.com/edgrosvenor/my-crazy-package'], $updated['repositories'][1]);
76+
}
77+
78+
/** @test */
79+
public function it_removes_the_repo_if_not_defined_for_a_package()
80+
{
81+
$this->assertStringContainsString('../../packages/edgrosvenor/my-sad-package', $this->composer);
82+
83+
$this->artisan('repo my-sad-package git');
84+
85+
$updated = File::get(config('laravel-git-workflow.composer_json'));
86+
87+
$this->assertStringNotContainsString('my-sad-package', $updated);
88+
89+
$this->assertIsArray(json_decode($updated, true));
90+
}
91+
92+
/** @test */
93+
public function it_sets_a_repo_to_the_local_path()
94+
{
95+
$this->assertStringNotContainsString('../../packages/edgrosvenor/my-happy-package', $this->composer);
96+
97+
$this->artisan('repo my-happy-package path');
98+
99+
$updated = File::get(config('laravel-git-workflow.composer_json'));
100+
101+
$this->assertStringContainsString('../../packages/edgrosvenor/my-happy-package', $updated);
102+
103+
$this->assertIsArray(json_decode($updated, true));
104+
}
105+
}

tests/test_composer.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "grosv/totally-fake-package",
3+
"description": "A beautiful work of fiction",
4+
"type": "pipe-dream",
5+
"license": "Imaginary",
6+
"authors": [
7+
{
8+
"name": "Ed Grosvenor",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"prefer-stable": true,
14+
"require": {
15+
"php": "^7.4",
16+
"illuminate/support": "^7.0",
17+
"ext-json": "*"
18+
},
19+
"require-dev": {
20+
"orchestra/testbench": "^5.0",
21+
"nunomaduro/collision": "^4.1"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Grosv\\LaravelGitWorkflow\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Tests\\": "tests/",
31+
"App\\": "vendor/orchestra/testbench-core/laravel/app"
32+
}
33+
},
34+
"repositories": [
35+
{
36+
"type": "path",
37+
"url": "../../packages/edgrosvenor/my-sad-package"
38+
},
39+
{
40+
"url": "../../packages/edgrosvenor/my-crazy-package",
41+
"type": "path"
42+
}
43+
],
44+
"extra": {
45+
"laravel": {
46+
"providers": "Grosv\\LaravelGitWorkflow\\LaravelGitWorkflowProvider"
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)