Skip to content

Instantly share code, notes, and snippets.

@edgrosvenor
Last active May 15, 2020 17:53
Show Gist options
  • Save edgrosvenor/5ad4d3b0654d8c4acba6bb6e74c8a086 to your computer and use it in GitHub Desktop.
Save edgrosvenor/5ad4d3b0654d8c4acba6bb6e74c8a086 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class SetRepositories extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'repo {package} {repo}';
public string $package;
public string $repo;
private function getMyPackages()
{
return [
'blade-components' => [
'path' => '../../../Packages/edgrosvenor/blade-components',
'git' => 'https://github.com/edgrosvenor/blade-components'
]
];
}
public function handle(): int
{
$this->package = $this->argument('package');
$this->repo = $this->argument('repo');
$mypackages = $this->getMyPackages();
if (!isset($mypackages[$this->package])) {
$this->error('You have not configured a package called ' . $this->package);
}
$new = $mypackages[$this->package];
$composer = json_decode(File::get(base_path('/composer.json')), true);
foreach ($composer['repositories'] as $k => $v) {
if (Str::contains($v['url'], $this->package)) {
$composer['repositories'][$k]['url'] = $new[$this->repo];
$composer['repositories'][$k]['type'] = $this->repo;
}
}
File::put(base_path('/composer.json'), json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
return 0;
}
}
@edgrosvenor
Copy link
Author

edgrosvenor commented May 8, 2020

This is my first 5-minute pass at solving the "I hate swapping repositories while working with my own packages" problem. I'll get it cleaned up and added to my own Laravel boilerplate. The way I'd use this is to set up the array of my own packages (or packages I'm forking and modifying locally) in the getMyPackages() method with each repository keyed by its type within that package's array.

Then, as configured above, I can quickly swap between the local and git repositories by running it like this:

php artisan repo blade-components git
php artisan repo blade-components path

My next step is going to be to call this command within my GitHub workflow package so that the repo is set to git before my changes are pushed up to GitHub and then changed back to path on my local machine. But that's a much more complex topic that will require more than a single gist to cover.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment