-
Notifications
You must be signed in to change notification settings - Fork 187
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
Open
mortenscheel
wants to merge
12
commits into
Jeroen-G:master
Choose a base branch
from
mortenscheel:vcs-repositories
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2665e3c
Install git packages through composer
mortenscheel 88da7a3
Add git status column to packager:list output
mortenscheel 0600850
Add test
mortenscheel 4cfe420
Merge master
mortenscheel 3a2bd4a
Update readme
mortenscheel f642f7d
Fix detection of git repository.
mortenscheel eb9c99d
Use static cache folder regardless of environment
mortenscheel 09e173a
Refactor helper methods to traits.
mortenscheel 3d6b2e5
Fix trait collision.
mortenscheel 7b0f6bd
Use packager-skeleton for git test
mortenscheel e434f1b
Refactor static methods
mortenscheel 42261ac
Run process without timeout.
mortenscheel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ report | |
*.sublime-project | ||
*.sublime-workspace | ||
testbench/ | ||
skeleton-cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace JeroenG\Packager; | ||
|
||
use Closure; | ||
use RuntimeException; | ||
use Symfony\Component\Finder\Exception\DirectoryNotFoundException; | ||
|
||
trait ComposerHandler | ||
{ | ||
use ProcessRunner; | ||
|
||
protected function removeComposerRepository($name) | ||
{ | ||
return $this->modifyComposerJson(function (array $composer) use ($name){ | ||
unset($composer['repositories'][$name]); | ||
return $composer; | ||
}, base_path()); | ||
} | ||
|
||
/** | ||
* Determines the path to Composer executable | ||
* @return string | ||
*/ | ||
public function getComposerExecutable(): string | ||
{ | ||
return trim(shell_exec('which composer')) ?: 'composer'; | ||
} | ||
|
||
protected function removePackage(string $packageName): array | ||
{ | ||
return $this->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 = $this->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 $this->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 | ||
*/ | ||
public function findInstalledPath(string $packageName): string | ||
{ | ||
$packageName = strtolower($packageName); | ||
$result = $this->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 | ||
*/ | ||
public function getComposerJsonArray(string $path): array | ||
{ | ||
return json_decode(file_get_contents($path), true); | ||
} | ||
|
||
public function modifyComposerJson(Closure $callback, string $composer_path) | ||
{ | ||
$composer_path = rtrim($composer_path, '/'); | ||
if (!preg_match('/composer\.json$/', $composer_path)){ | ||
$composer_path .= '/composer.json'; | ||
} | ||
$original = $this->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 | ||
*/ | ||
public function runComposerCommand(array $command, string $cwd = null): array | ||
{ | ||
array_unshift($command, 'php', '-n', $this->getComposerExecutable()); | ||
return $this->runProcess($command, $cwd); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.