Repository navigation
Single Command Installation of Rector + Targets for Managing Tools #20
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
Changes from all commits
4f3bdd3
7d36bc4
43342ce
c8ab547
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 |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| RECTOR_DIRECTORY ?= $(PROJECT_DIR)tools/rector | ||
| HAS_RECTOR := $(strip $(shell if [ -d $(RECTOR_DIRECTORY) ]; then echo TRUE; fi)) | ||
|
|
||
|
|
||
| ifeq ("$(HAS_RECTOR)", "TRUE") | ||
|
|
||
| BUMP_TOOLS_TARGETS += rector-bump | ||
| UPDATE_TOOLS_TARGETS += rector-update | ||
|
|
||
| HELP += $(call MK_HELP,'rector','Check the codebase with Rector') | ||
| rector: | ||
| @$(DOCKER_RUN) $(DOCKER_IMAGE_NAME) $(RECTOR_DIRECTORY)/vendor/bin/rector process --dry-run -c $(RECTOR_DIRECTORY)/rector.php | ||
| .PHONY: rector | ||
|
|
||
| HELP += $(call MK_HELP,'rector-fix','Fix code style inconsistencies with Rector') | ||
| rector-fix: | ||
| @$(DOCKER_RUN) $(DOCKER_IMAGE_NAME) $(RECTOR_DIRECTORY)/vendor/bin/rector process -c $(RECTOR_DIRECTORY)/rector.php | ||
| .PHONY: rector-fix | ||
|
|
||
| rector-update: | ||
| $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer \ | ||
| --working-dir=$(RECTOR_DIRECTORY) \ | ||
| update | ||
| .PHONY: rector-update | ||
|
|
||
| rector-bump: rector-update _do-rector-bump rector-update | ||
| .PHONY: rector-bump | ||
|
|
||
| _do-rector-bump: | ||
| $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer \ | ||
| --working-dir=$(RECTOR_DIRECTORY) \ | ||
| bump -D | ||
| .PHONY: _do-rector-bump | ||
|
|
||
| endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # | ||
| # If _any_ tools are installed, and the targets for updating or bumping those tools are non-empty, then | ||
| # add targets for bumping or updating _all_ stand-alone tools | ||
| # | ||
|
|
||
| ifeq ("$(HAS_TOOLS)", "TRUE") | ||
|
|
||
| ifneq ($(strip $(UPDATE_TOOLS_TARGETS)), "") | ||
| HELP += $(call MK_HELP,'update-tools','Update all standalone tools') | ||
| update-tools: $(UPDATE_TOOLS_TARGETS) | ||
| .PHONY: update-tools | ||
| endif | ||
|
|
||
| ifneq ($(strip $(BUMP_TOOLS_TARGETS)), "") | ||
| HELP += $(call MK_HELP,'bump-tools','Bump and update the versions for all standalone tools') | ||
| bump-tools: $(BUMP_TOOLS_TARGETS) | ||
| .PHONY: bump-tools | ||
| endif | ||
|
|
||
| endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| CURRENT_DIRECTORY := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) | ||
| RECTOR_DIRECTORY := $(PROJECT_DIR)tools/rector | ||
|
Member
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. Same comment as for
Member
Author
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. Also, it seems like the trailing slash is a documented "standard": https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html#index-dir |
||
| HAS_RECTOR_DIR := $(strip $(shell if [ -d $(RECTOR_DIRECTORY) ]; then echo TRUE; fi)) | ||
|
|
||
| ifneq ("$(HAS_RECTOR_DIR)","TRUE") | ||
|
|
||
| PROJECT_COMPOSER_PLATFORM := $(shell $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer config platform.php) | ||
| HELP += $(call MK_HELP,'install-rector','Install a standalone copy of rector into tools/rector') | ||
|
|
||
| install-rector: _do-install-rector $(RECTOR_DIRECTORY) | ||
| .PHONY: install-rector | ||
|
|
||
| _do-install-rector: | ||
| @$(call MK_INFO,"Installing Rector") | ||
| @$(DOCKER_RUN) ${DOCKER_IMAGE_NAME} php $(CURRENT_DIRECTORY)migrate $(PROJECT_DIR) | ||
|
Member
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. will |
||
| .PHONY: _do-install-rector | ||
|
|
||
|
|
||
| # Installation routine for rector: | ||
| $(RECTOR_DIRECTORY): | ||
| mkdir -p $(RECTOR_DIRECTORY) | ||
| echo '{}' > $(RECTOR_DIRECTORY)/composer.json | ||
| $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer \ | ||
| --working-dir=$(RECTOR_DIRECTORY) \ | ||
| config platform.php $(PROJECT_COMPOSER_PLATFORM) | ||
| $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer \ | ||
| --working-dir=$(RECTOR_DIRECTORY) \ | ||
| require --dev rector/rector | ||
| cp $(CURRENT_DIRECTORY)templates/rector.php $(RECTOR_DIRECTORY)/ | ||
| echo vendor > $(RECTOR_DIRECTORY)/.gitignore | ||
| $(DOCKER_RUN) ${DOCKER_IMAGE_NAME} composer \ | ||
| --working-dir=$(RECTOR_DIRECTORY) \ | ||
| install | ||
| # end | ||
|
|
||
| endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Install Stand-Alone Rector | ||
|
|
||
| This migration installs rector to `./tools/rector` in the project and ensures that the tools directory is `export-ignore`d. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Laminas\Internal\Migrations\GitAttributes; | ||
| use Laminas\Internal\Migrations\ProjectInformation; | ||
|
|
||
| require __DIR__ . '/../autoloader.php'; | ||
|
|
||
| /** @var mixed $rootDirectory */ | ||
| $rootDirectory = $argv[1] ?? null; | ||
| if ( | ||
| ! is_string($rootDirectory) | ||
| || $rootDirectory === '' | ||
| || ! is_dir($rootDirectory) | ||
| ) { | ||
| throw new RuntimeException('Pass a single argument representing the root directory of the target library'); | ||
| } | ||
|
|
||
| $project = ProjectInformation::fromDirectory($rootDirectory); | ||
|
|
||
| $attributes = new GitAttributes($project->gitAttributes); | ||
| $attributes->ignore('tools'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| require_once 'vendor/autoload.php'; | ||
|
froschdesign marked this conversation as resolved.
|
||
|
|
||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withPhpSets() | ||
| ->withPaths([ | ||
| __DIR__ . '/../../src', | ||
| __DIR__ . '/../../test', | ||
| ]); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
|
|
||
| include $(_MAKEFILE_DIR)migrations/PHP-86/PHP86.mk | ||
| include $(_MAKEFILE_DIR)migrations/InstallStandaloneRector/InstallStandaloneRector.mk |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| $paths = [ | ||
| __DIR__ . '/../../../autoload.php', | ||
| __DIR__ . '/../vendor/autoload.php', | ||
| ]; | ||
|
|
||
| $autoloader = null; | ||
| foreach ($paths as $file) { | ||
| if (! file_exists($file)) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var mixed $autoloader */ | ||
| $autoloader = require $file; | ||
| } | ||
|
|
||
| if ($autoloader === null) { | ||
| throw new RuntimeException('Cannot determine autoloader'); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Laminas\Internal\Migrations; | ||
|
|
||
| use function assert; | ||
| use function explode; | ||
| use function file_get_contents; | ||
| use function file_put_contents; | ||
| use function ltrim; | ||
| use function sprintf; | ||
| use function str_starts_with; | ||
| use function trim; | ||
|
|
||
| use const DIRECTORY_SEPARATOR; | ||
| use const PHP_EOL; | ||
|
|
||
| final readonly class GitAttributes | ||
| { | ||
| public function __construct( | ||
| private string $file, | ||
| ) {} | ||
|
|
||
| /** @return list<string> */ | ||
| private function getLines(): array | ||
| { | ||
| return explode(PHP_EOL, $this->read()); | ||
| } | ||
|
|
||
| public function ignore(string $path): void | ||
| { | ||
| if ($this->isExportIgnored($path)) { | ||
| return; | ||
| } | ||
|
|
||
| $content = sprintf( | ||
| "%s\n%s export-ignore\n", | ||
| trim($this->read()), | ||
| $path, | ||
| ); | ||
|
|
||
| $this->write($content); | ||
| } | ||
|
|
||
| private function isExportIgnored(string $path): bool | ||
| { | ||
| foreach ($this->getLines() as $line) { | ||
| if (! $this->isExportIgnoreLine($line)) { | ||
| continue; | ||
| } | ||
|
|
||
| $result = str_starts_with( | ||
| ltrim(trim($line), DIRECTORY_SEPARATOR), | ||
| ltrim(trim($path), DIRECTORY_SEPARATOR), | ||
| ); | ||
|
|
||
| if ($result) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function isExportIgnoreLine(string $line): bool | ||
| { | ||
| return (bool) preg_match('/\s+export-ignore\s*$/i', $line); | ||
| } | ||
|
|
||
| /** @return non-empty-string */ | ||
| private function read(): string | ||
| { | ||
| $contents = file_get_contents($this->file); | ||
| assert( | ||
| $contents !== false && $contents !== '', | ||
| 'The file could not be read or it is empty', | ||
| ); | ||
|
|
||
| return $contents; | ||
| } | ||
|
|
||
| /** @param non-empty-string $data */ | ||
| private function write(string $data): void | ||
| { | ||
| file_put_contents($this->file, $data); | ||
| } | ||
| } |
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.
is
$(PROJECT_DIR)guaranteed to end with a/?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.
In local testing,
$(dir $(abspath $(lastword $(MAKEFILE_LIST))))yields directories with trailing slashes, so I've adopted that convention for these types of variables, which is where most of them come from.