Unverified Commit 203860a3 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3206219 by mglaman, mherchel: Allow configuring which theme is used as a starterkit theme

parent 2d8630fb
Loading
Loading
Loading
Loading
+27 −3
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ExtensionDiscovery;
use Drupal\Core\Extension\InfoParser;
use Drupal\Core\File\FileSystem;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
@@ -45,7 +46,9 @@ protected function configure() {
      ->addOption('name', NULL, InputOption::VALUE_OPTIONAL, 'A name for the theme.')
      ->addOption('description', NULL, InputOption::VALUE_OPTIONAL, 'A description of your theme.')
      ->addOption('path', NULL, InputOption::VALUE_OPTIONAL, 'The path where your theme will be created. Defaults to: themes')
      ->addUsage('custom_theme --name "Custom Theme" --description "Custom theme generated from a starterkit theme" --path themes');
      ->addOption('starterkit', NULL, InputOption::VALUE_OPTIONAL, 'The theme to use as the starterkit', 'starterkit_theme')
      ->addUsage('custom_theme --name "Custom Theme" --description "Custom theme generated from a starterkit theme" --path themes')
      ->addUsage('custom_theme --name "Custom Theme" --starterkit mystarterkit');
  }

  /**
@@ -68,11 +71,17 @@ protected function execute(InputInterface $input, OutputInterface $output) {
    }

    // Source directory for the theme.
    $source_theme_name = 'starterkit_theme';
    $source_theme_name = $input->getOption('starterkit');
    if (!$source_theme = $this->getThemeInfo($source_theme_name)) {
      $io->getErrorStyle()->error("Theme source theme $source_theme_name cannot be found.");
      return 1;
    }

    if (!$this->isStarterkitTheme($source_theme)) {
      $io->getErrorStyle()->error("Theme source theme $source_theme_name is not a valid starter kit.");
      return 1;
    }

    $source = $source_theme->getPath();

    if (!is_dir($source)) {
@@ -274,6 +283,21 @@ private function getThemeInfo(string $theme): ? Extension {
    return $themes[$theme];
  }

  /**
   * Checks if the theme is a starterkit theme.
   *
   * @param \Drupal\Core\Extension\Extension $theme
   *   The theme extension.
   *
   * @return bool
   */
  private function isStarterkitTheme(Extension $theme): bool {
    $info_parser = new InfoParser($this->root);
    $info = $info_parser->parse($theme->getPathname());

    return $info['starterkit'] ?? FALSE === TRUE;
  }

  /**
   * Gets the current Drupal major version.
   *
+1 −0
Original line number Diff line number Diff line
@@ -857,6 +857,7 @@ mymodule
mysite
mysqladmin
mysqldump
mystarterkit
myverylongurl
myverylongurlexample
műveletek
+63 −4
Original line number Diff line number Diff line
@@ -27,6 +27,9 @@ class GenerateThemeTest extends QuickStartTestBase {
   * {@inheritdoc}
   */
  public function setUp(): void {
    if (version_compare(\SQLite3::version()['versionString'], Tasks::SQLITE_MINIMUM_VERSION) < 0) {
      $this->markTestSkipped();
    }
    parent::setUp();
    $php_executable_finder = new PhpExecutableFinder();
    $this->php = $php_executable_finder->find();
@@ -39,10 +42,6 @@ public function setUp(): void {
   * Tests the generate-theme command.
   */
  public function test() {
    if (version_compare(\SQLite3::version()['versionString'], Tasks::SQLITE_MINIMUM_VERSION) < 0) {
      $this->markTestSkipped();
    }

    $install_command = [
      $this->php,
      'core/scripts/drupal',
@@ -81,4 +80,64 @@ public function test() {
    $this->assertFileDoesNotExist($theme_path_absolute . '/test_custom_theme.theme');
  }

  /**
   * Tests themes that do not exist return an error.
   */
  public function testThemeDoesNotExist(): void {
    $install_command = [
      $this->php,
      'core/scripts/drupal',
      'generate-theme',
      'test_custom_theme',
      '--name="Test custom starterkit theme"',
      '--description="Custom theme generated from a starterkit theme"',
      '--starterkit',
      'foobarbaz',
    ];
    $process = new Process($install_command, NULL);
    $process->setTimeout(60);
    $result = $process->run();
    $this->assertStringContainsString('Theme source theme foobarbaz cannot be found.', trim($process->getErrorOutput()));
    $this->assertSame(1, $result);
  }

  /**
   * Tests that only themes with `starterkit` flag can be used.
   */
  public function testStarterKitFlag(): void {
    // Explicitly not a starter theme.
    $install_command = [
      $this->php,
      'core/scripts/drupal',
      'generate-theme',
      'test_custom_theme',
      '--name="Test custom starterkit theme"',
      '--description="Custom theme generated from a starterkit theme"',
      '--starterkit',
      'stark',
    ];
    $process = new Process($install_command, NULL);
    $process->setTimeout(60);
    $result = $process->run();
    $this->assertStringContainsString('Theme source theme stark is not a valid starter kit.', trim($process->getErrorOutput()));
    $this->assertSame(1, $result);

    // Has not defined `starterkit`.
    $install_command = [
      $this->php,
      'core/scripts/drupal',
      'generate-theme',
      'test_custom_theme',
      '--name="Test custom starterkit theme"',
      '--description="Custom theme generated from a starterkit theme"',
      '--starterkit',
      'bartik',
    ];
    $process = new Process($install_command, NULL);
    $process->setTimeout(60);
    $result = $process->run();
    $this->assertStringContainsString('Theme source theme bartik is not a valid starter kit.', trim($process->getErrorOutput()));
    $this->assertSame(1, $result);
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@ description: 'An intentionally plain theme with no styling to demonstrate defaul
package: Core
version: VERSION
base theme: false
starterkit: false
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ name: starterkit_theme
type: theme
'base theme': stable9
hidden: true
starterkit: true
libraries:
  - starterkit_theme/base
  - starterkit_theme/messages