Skip to content
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

bin/generate-gutenberg-php.php should be refactored into a PHP class #46016

Open
anton-vlasenko opened this issue Nov 23, 2022 · 0 comments
Open
Labels
[Type] Code Quality Issues or PRs that relate to code quality

Comments

@anton-vlasenko
Copy link
Contributor

anton-vlasenko commented Nov 23, 2022

What problem does this address?

This is a follow-up task to #45817.
bin/generate-gutenberg-php.php uses a global variable - $plugin_version. This seems unnecessary and causes linter errors when enabling the WordPress-Extra ruleset.
Please see #18502 for more details.

What is your proposed solution?

Let's refactor this code into a PHP class and add unit tests to ensure it works as expected.
I've created a draft version of such a class, but I didn't have time to test it and write proper unit tests.

#!/usr/bin/env php
<?php
/**
 * Generates the production (plugin build) version of `gutenberg.php`,
 * containing alternate `define` statements from the development version.
 *
 * @package gutenberg-build
 */

/**
 * Prints `define` statements for the production version of `gutenberg.php`
 * (the plugin entry point).
 */
class Gutenberg_Header_File_Generator {
	/**
	 * Resource to a file stream.
	 *
	 * @var resource
	 */
	private $file;

	/**
	 * Gutenberg version.
	 *
	 * @var string|null
	 */
	private $version;

	/**
	 * Defines if the current line is inside a defines block.
	 *
	 * @var bool
	 */
	private $inside_defines_block;

	/**
	 * Constructor.
	 *
	 * @param string $path Absolute file path to gutenberg.php.
	 */
	public function __construct( $path ) {
		$this->file = fopen( $path, 'r' );

		$this->version              = null;
		$this->inside_defines_block = false;
	}

	/**
	 * Prints the header file.
	 */
	public function print_header_file() {
		while ( true ) {
			$line = fgets( $this->file );
			if ( false === $line ) {
				break;
			}

			$matches = array();

			if (
					! $this->version &&
					preg_match( '@^\s*\*\s*Version:\s*([0-9.]+)@', $line, $matches )
			) {
				$this->version = $matches[1];
			}

			switch ( trim( $line ) ) {
				case '### BEGIN AUTO-GENERATED DEFINES':
					$this->inside_defines_block = true;
					echo $line;
					$this->print_version();
					break;

				case '### END AUTO-GENERATED DEFINES':
					$this->inside_defines_block = false;
					echo $line;
					break;

				default:
					if ( ! $this->inside_defines_block ) {
						echo $line;
					}
					break;
			}
		}
	}

	/**
	 * Prints plugin header.
	 */
	private function print_version() {
		echo "define( 'GUTENBERG_VERSION', '{$this->version}' );\n";

		$git_commit = trim( shell_exec( 'git rev-parse HEAD' ) );

		echo "define( 'GUTENBERG_GIT_COMMIT', '{$git_commit}' );\n";
	}

	/**
	 * Destructor.
	 */
	public function __destruct() {
		fclose( $this->file );
	}
}

$gutenberg_header_generator = new Gutenberg_Header_File_Generator( dirname( __DIR__ ) . '/gutenberg.php' );
$gutenberg_header_generator->print_header_file();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

No branches or pull requests

1 participant