Skip to content

Commit a8d2a96

Browse files
committed
Allow custom repositories and versions for composer packages
1 parent 65d8339 commit a8d2a96

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

readme.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,26 @@ The source can be one of:
268268
* git:
269269
* <folder_name> in the project config `templates` folder
270270
* empty
271-
271+
272+
##### Composer Templates
273+
272274
`composer:` will use `composer create-project` and requires that the source be a valid installation
273275
project either registered with packagist.org or with private packagist.com.
274276

277+
To use a custom repository like with Private Packagist add `?repository=https://repo/source`.
278+
279+
To specify a specific version to use add `&version=XXX`. To use the latest version set the version
280+
to `dev-master`.
281+
282+
The full template source would then look like:
283+
`composer:namespace/project-name?repository=https://some.repo/somewhere&version=2.0.2`.
284+
285+
##### Git Templates
286+
275287
`git:` will clone and remove the `.git` folder, essentially using the git repo as a template.
276288

289+
##### Static Templates
290+
277291
`<folder_name>` will copy all files in that folder to the new source. Additionally, if the template
278292
folder contains a `post_copy.php` file, this will be run after the files have been copied. This
279293
script can perform any actions needed for setup. Further: if a `post_copy_args.php` exists, then
@@ -293,6 +307,8 @@ was created. If the return type of the args is not an array nothing will be aske
293307

294308
The `post_copy.php` file should check the arg inputs before running.
295309

310+
##### Generic Template (fallback)
311+
296312
If the template is left empty then a very basic folder is created with some general defaults including:
297313

298314
* .gitignore

src/Services/Installers/ComposerInstaller.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use IlluminateAgnostic\Str\Support\Str;
66
use Somnambulist\ProjectManager\Models\Project;
77
use Somnambulist\ProjectManager\Models\Template;
8+
use function parse_str;
89
use function sprintf;
910

1011
/**
@@ -23,11 +24,17 @@ public function installInto(Project $project, Template $template, string $name,
2324
$this->tools()->step(++$step, 'creating <info>%s</info> from composer project', $this->type);
2425
$this->tools()->warning('installer scripts will not be run!');
2526

27+
$proj = $this->getProjectPackage($template);
28+
$repo = $this->getAlternateRepository($template);
29+
$ver = $this->getProjectPackageVersion($template);
30+
2631
$res = $this->tools()->execute(
2732
sprintf(
28-
'composer create-project --no-scripts --remove-vcs %s %s',
29-
Str::replaceFirst('composer:', '', $template->source()),
30-
$cwd
33+
'composer create-project --no-scripts --remove-vcs %s %s %s %s',
34+
$repo ? '--repository=' . $repo : '',
35+
$proj,
36+
$cwd,
37+
$ver
3138
),
3239
dirname($cwd)
3340
);
@@ -49,4 +56,33 @@ public function installInto(Project $project, Template $template, string $name,
4956

5057
return $this->success();
5158
}
59+
60+
private function getProjectPackage(Template $template): string
61+
{
62+
return Str::before(Str::replaceFirst('composer:', '', $template->source()), '?');
63+
}
64+
65+
private function getAlternateRepository(Template $template): ?string
66+
{
67+
$options = $this->parseTemplateOptions($template);
68+
69+
return $options['repository'] ?? null;
70+
}
71+
72+
private function getProjectPackageVersion(Template $template): ?string
73+
{
74+
$options = $this->parseTemplateOptions($template);
75+
76+
return $options['version'] ?? null;
77+
}
78+
79+
private function parseTemplateOptions(Template $template): array
80+
{
81+
$query = Str::after($template->source(), '?');
82+
$options = [];
83+
84+
parse_str($query, $options);
85+
86+
return $options;
87+
}
5288
}

0 commit comments

Comments
 (0)