-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add git packages as VCS repositories #79
base: master
Are you sure you want to change the base?
Changes from 10 commits
2665e3c
88da7a3
0600850
4cfe420
3a2bd4a
f642f7d
eb9c99d
09e173a
3d6b2e5
7b0f6bd
e434f1b
42261ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ report | |
*.sublime-project | ||
*.sublime-workspace | ||
testbench/ | ||
skeleton-cache/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager; | ||
|
||
use Closure; | ||
use RuntimeException; | ||
use Symfony\Component\Finder\Exception\DirectoryNotFoundException; | ||
|
||
trait ComposerHandler | ||
{ | ||
use ProcessRunner; | ||
|
||
protected function removeComposerRepository($name) | ||
{ | ||
return self::modifyComposerJson(function (array $composer) use ($name){ | ||
unset($composer['repositories'][$name]); | ||
return $composer; | ||
}, base_path()); | ||
} | ||
|
||
/** | ||
* Determines the path to Composer executable | ||
* @todo Might not work on Windows | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a Windows machine, so I will test this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I've removed the @todo |
||
* @return string | ||
*/ | ||
protected static function getComposerExecutable(): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you choose to make this (and the ones below) static? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. Mostly just for convenience and old habits. |
||
{ | ||
return trim(shell_exec('which composer')) ?: 'composer'; | ||
} | ||
|
||
protected function removePackage(string $packageName): array | ||
{ | ||
return self::runComposerCommand([ | ||
'remove', | ||
strtolower($packageName), | ||
'--no-progress', | ||
]); | ||
} | ||
|
||
protected function requirePackage(string $packageName, string $version = null, bool $prefer_source = true): bool | ||
{ | ||
$package = strtolower($packageName); | ||
if ($version) { | ||
$package .= ':'.$version; | ||
} | ||
$result = self::runComposerCommand([ | ||
'require', | ||
$package, | ||
'--prefer-'.($prefer_source ? 'source' : 'dist'), | ||
'--prefer-stable', | ||
'--no-suggest', | ||
'--no-progress', | ||
]); | ||
if (!$result['success']) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
protected function addComposerRepository(string $name, string $type = 'path', string $url = null) | ||
{ | ||
$params = [ | ||
'type' => $type, | ||
'url' => $url | ||
]; | ||
return self::modifyComposerJson(function (array $composer) use ($params, $name){ | ||
$composer['repositories'][$name] = $params; | ||
return $composer; | ||
}, base_path()); | ||
} | ||
|
||
/** | ||
* Find the package's path in Composer's vendor folder | ||
* @param string $packageName | ||
* @return string | ||
* @throws RuntimeException | ||
*/ | ||
protected function findInstalledPath(string $packageName): string | ||
{ | ||
$packageName = strtolower($packageName); | ||
$result = self::runComposerCommand([ | ||
'info', | ||
$packageName, | ||
'--path']); | ||
if ($result['success'] &&preg_match('{'.$packageName.' (.*)$}m', $result['output'], $match)) { | ||
return trim($match[1]); | ||
} | ||
throw new DirectoryNotFoundException('Package ' . $packageName.' not found in vendor folder'); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return array | ||
*/ | ||
protected static function getComposerJsonArray(string $path): array | ||
{ | ||
return json_decode(file_get_contents($path), true); | ||
} | ||
|
||
protected static function modifyComposerJson(Closure $callback, string $composer_path) | ||
{ | ||
$composer_path = rtrim($composer_path, '/'); | ||
if (!preg_match('/composer\.json$/', $composer_path)){ | ||
$composer_path .= '/composer.json'; | ||
} | ||
$original = self::getComposerJsonArray($composer_path); | ||
$modified = $callback($original); | ||
return file_put_contents($composer_path, json_encode($modified, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); | ||
} | ||
|
||
/** | ||
* @param array $command | ||
* @param string|null $cwd | ||
* @return array | ||
*/ | ||
protected static function runComposerCommand(array $command, string $cwd = null): array | ||
{ | ||
array_unshift($command, 'php', '-n', self::getComposerExecutable()); | ||
return self::runProcess($command, $cwd); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be a proper place to document this? I'm thinking a line in just the config file might be enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I wonder if it's even worth keeping. The only benefit is that you save about a second of download time (if even that). On the other hands, it adds a layer of complexity, and people might end up using an outdated skeleton without knowing it.