Skip to content

Commit

Permalink
[4.x] Fix starter-kit:install with custom branch when branch has sl…
Browse files Browse the repository at this point in the history
…ash (#9978)
  • Loading branch information
jesseleite authored Apr 30, 2024
1 parent 897803d commit 3d93e66
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/Console/Commands/StarterKitInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class StarterKitInstall extends Command
*/
public function handle()
{
if ($this->validationFails($package = $this->getPackage(), new ComposerPackage)) {
[$package, $branch] = $this->getPackageAndBranch();

if ($this->validationFails($package, new ComposerPackage)) {
return;
}

Expand All @@ -56,6 +58,7 @@ public function handle()
}

$installer = StarterKitInstaller::package($package, $this, $licenseManager)
->branch($branch)
->fromLocalRepo($this->option('local'))
->withConfig($this->option('with-config'))
->withoutDependencies($this->option('without-dependencies'))
Expand Down Expand Up @@ -85,13 +88,21 @@ public function handle()
}

/**
* Get composer package.
* Get composer package (and optional branch).
*
* @return string
*/
protected function getPackage()
protected function getPackageAndBranch()
{
return $this->argument('package') ?: $this->ask('Package');
$package = $this->argument('package') ?: $this->ask('Package');

$parts = explode(':', $package);

if (count($parts) === 1) {
$parts[] = null;
}

return $parts;
}

/**
Expand Down
17 changes: 8 additions & 9 deletions src/StarterKits/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class Installer
*/
public function __construct(string $package, $console = null, ?LicenseManager $licenseManager = null)
{
[$this->package, $this->branch] = $this->parseRawPackageArg($package);
$this->package = $package;

$this->licenseManager = $licenseManager;

Expand All @@ -60,17 +60,16 @@ public static function package(string $package, $console = null, ?LicenseManager
}

/**
* Parse out package and branch from raw package arg.
* Install from specific branch.
*
* @param string|null $branch
* @return $this
*/
protected function parseRawPackageArg(string $package): array
public function branch($branch = null)
{
$parts = explode(':', $package);
$this->branch = $branch;

if (count($parts) === 1) {
$parts[] = null;
}

return $parts;
return $this;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/StarterKits/InstallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,29 @@ public function it_parses_branch_from_package_param_when_installing()
$this->assertFileExists(base_path('copied.md'));
}

/** @test */
public function it_installs_branch_with_slash_without_failing_package_validation()
{
$this->assertFileDoesNotExist($this->kitVendorPath());
$this->assertComposerJsonDoesntHave('repositories');
$this->assertFileDoesNotExist(base_path('copied.md'));

$this->installCoolRunnings([
'package' => 'statamic/cool-runnings:dev-feature/custom-branch',
]);

// Ensure `Composer::requireDev()` gets called with `package:branch`
$this->assertEquals(Blink::get('composer-require-dev-package'), 'statamic/cool-runnings');
$this->assertEquals(Blink::get('composer-require-dev-branch'), 'dev-feature/custom-branch');

// But ensure the rest of the installer handles parsed `package` without branch messing things up
$this->assertFalse(Blink::has('starter-kit-repository-added'));
$this->assertFileDoesNotExist($this->kitVendorPath());
$this->assertFileDoesNotExist(base_path('composer.json.bak'));
$this->assertComposerJsonDoesntHave('repositories');
$this->assertFileExists(base_path('copied.md'));
}

private function kitRepoPath($path = null)
{
return collect([base_path('repo/cool-runnings'), $path])->filter()->implode('/');
Expand Down

0 comments on commit 3d93e66

Please sign in to comment.