From 6b5e802793e2f560a9e777c4f49c72482cce53b2 Mon Sep 17 00:00:00 2001 From: narendra-drupal <87118318+narendra-drupal@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:57:23 +0530 Subject: [PATCH 1/5] install_tasks_alter used instead of install_tasks --- installer/starshot_installer.info.yml | 7 ----- installer/starshot_installer.profile | 43 ++++++++++++--------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/installer/starshot_installer.info.yml b/installer/starshot_installer.info.yml index a80cab15..ffd7ff8e 100644 --- a/installer/starshot_installer.info.yml +++ b/installer/starshot_installer.info.yml @@ -9,10 +9,3 @@ distribution: install: # Redirect to the project browser after installing. finish_url: 'admin/modules/browse' -install: - # We try to get Package Manager configured early. - # @see starshot_installer_form_install_configure_form_alter() - - package_manager - # Install Project Browser as early as possible so it can track recipes that - # get applied. - - project_browser diff --git a/installer/starshot_installer.profile b/installer/starshot_installer.profile index e035016d..b894a941 100644 --- a/installer/starshot_installer.profile +++ b/installer/starshot_installer.profile @@ -2,27 +2,34 @@ declare(strict_types=1); -use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Extension\ModuleInstallerInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Recipe\InputCollector; -use Drupal\Core\Recipe\Recipe; -use Drupal\Core\Recipe\RecipeRunner; use Symfony\Component\Process\ExecutableFinder; /** - * Implements hook_install_tasks(). + * Implements hook_install_tasks_alter(). */ -function starshot_installer_install_tasks(): array { - return [ +function starshot_installer_install_tasks_alter(&$tasks, $install_state) { + $recipe_tasks = [ 'starshot_installer_apply_recipes' => [ - 'type' => 'batch', + 'run' => INSTALL_TASK_RUN_IF_REACHED, 'display_name' => t('Apply recipes'), - ], + ] + ]; + $key = array_search('install_select_profile', array_keys($tasks), TRUE); + $tasks = array_slice($tasks, 0, $key, TRUE) + + $recipe_tasks + + array_slice($tasks, $key, NULL, TRUE); + + $recipe_tasks = [ 'starshot_installer_uninstall_myself' => [ // As a final task, this profile should uninstall itself. ], ]; + $key = array_search('install_finished', array_keys($tasks), TRUE); + $tasks = array_slice($tasks, 0, $key, TRUE) + + $recipe_tasks + + array_slice($tasks, $key, NULL, TRUE); } /** @@ -92,22 +99,10 @@ function _starshot_installer_install_configure_form_submit(array &$form, FormSta } /** - * Runs a batch job that applies all of the Starshot recipes. - * - * @return array - * The batch job definition. + * Apply Starshot recipes. */ -function starshot_installer_apply_recipes(): array { - $batch = new BatchBuilder(); - $batch->setTitle(t('Applying recipes')); - - $recipe = Recipe::createFromDirectory(Drupal::root() . '/recipes/starshot'); - Drupal::classResolver(InputCollector::class)->prepare($recipe); - - foreach (RecipeRunner::toBatchOperations($recipe) as [$callback, $arguments]) { - $batch->addOperation($callback, $arguments); - } - return $batch->toArray(); +function starshot_installer_apply_recipes(&$install_state) { + $install_state['parameters']['recipe'] = 'recipes/starshot'; } /** From ceb6663cbf85a41cbd5de6150d062d33ce49808c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Tue, 30 Jul 2024 14:54:28 -0400 Subject: [PATCH 2/5] Test that the installer is not installed --- tests/src/ExistingSite/BasicExpectationsTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/src/ExistingSite/BasicExpectationsTest.php b/tests/src/ExistingSite/BasicExpectationsTest.php index f16121e5..8a7cc3cf 100644 --- a/tests/src/ExistingSite/BasicExpectationsTest.php +++ b/tests/src/ExistingSite/BasicExpectationsTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\starshot\ExistingSite; +use Drupal\Core\Config\ConfigFactoryInterface; use weitzman\DrupalTestTraits\ExistingSiteBase; /** @@ -20,6 +21,11 @@ public function testBasicExpectations(): void { $assert_session = $this->assertSession(); $assert_session->statusCodeEquals(200); $assert_session->elementAttributeContains('css', 'meta[name="Generator"]', 'content', 'Drupal'); + + // The installer should not still be, er, installed. + /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */ + $config_factory = $this->container->get(ConfigFactoryInterface::class); + $this->assertFalse($config_factory->get('core.extension')->get('profile')); } } From 4dbfdf715327abda362885c4f5d2d35db3fab1a9 Mon Sep 17 00:00:00 2001 From: narendra-drupal <87118318+narendra-drupal@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:51:36 +0530 Subject: [PATCH 3/5] 2 tasks added --- installer/starshot_installer.info.yml | 7 +++ installer/starshot_installer.profile | 78 ++++++++++++++++++++------- 2 files changed, 66 insertions(+), 19 deletions(-) diff --git a/installer/starshot_installer.info.yml b/installer/starshot_installer.info.yml index ffd7ff8e..a80cab15 100644 --- a/installer/starshot_installer.info.yml +++ b/installer/starshot_installer.info.yml @@ -9,3 +9,10 @@ distribution: install: # Redirect to the project browser after installing. finish_url: 'admin/modules/browse' +install: + # We try to get Package Manager configured early. + # @see starshot_installer_form_install_configure_form_alter() + - package_manager + # Install Project Browser as early as possible so it can track recipes that + # get applied. + - project_browser diff --git a/installer/starshot_installer.profile b/installer/starshot_installer.profile index b894a941..478a80b0 100644 --- a/installer/starshot_installer.profile +++ b/installer/starshot_installer.profile @@ -2,34 +2,35 @@ declare(strict_types=1); +use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Extension\ModuleInstallerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Recipe\InputCollector; +use Drupal\Core\Recipe\Recipe; +use Drupal\Core\Recipe\RecipeRunner; use Symfony\Component\Process\ExecutableFinder; /** - * Implements hook_install_tasks_alter(). + * Implements hook_install_tasks(). */ -function starshot_installer_install_tasks_alter(&$tasks, $install_state) { - $recipe_tasks = [ - 'starshot_installer_apply_recipes' => [ +function starshot_installer_install_tasks(): array { + return [ + 'starshot_installer_choose_template' => [ 'run' => INSTALL_TASK_RUN_IF_REACHED, + 'display_name' => t('Choose template'), + ], + 'starshot_installer_apply_recipes' => [ + 'type' => 'batch', 'display_name' => t('Apply recipes'), - ] - ]; - $key = array_search('install_select_profile', array_keys($tasks), TRUE); - $tasks = array_slice($tasks, 0, $key, TRUE) + - $recipe_tasks + - array_slice($tasks, $key, NULL, TRUE); - - $recipe_tasks = [ + ], + 'starshot_installer_additional_recipes' => [ + 'run' => INSTALL_TASK_RUN_IF_REACHED, + 'display_name' => t('Apply additional recipes'), + ], 'starshot_installer_uninstall_myself' => [ // As a final task, this profile should uninstall itself. ], ]; - $key = array_search('install_finished', array_keys($tasks), TRUE); - $tasks = array_slice($tasks, 0, $key, TRUE) + - $recipe_tasks + - array_slice($tasks, $key, NULL, TRUE); } /** @@ -99,10 +100,27 @@ function _starshot_installer_install_configure_form_submit(array &$form, FormSta } /** - * Apply Starshot recipes. + * Runs a batch job that applies all of the Starshot recipes. + * + * @param $install_state + * An array of information about the current installation state. + * + * @return array + * The batch job definition. */ -function starshot_installer_apply_recipes(&$install_state) { - $install_state['parameters']['recipe'] = 'recipes/starshot'; +function starshot_installer_apply_recipes(&$install_state): array { + if (!empty($install_state['parameters']['template'])) { + $batch = new BatchBuilder(); + $batch->setTitle(t('Applying recipes')); + + $recipe = Recipe::createFromDirectory(Drupal::root() . $install_state['parameters']['template']); + Drupal::classResolver(InputCollector::class)->prepare($recipe); + + foreach (RecipeRunner::toBatchOperations($recipe) as [$callback, $arguments]) { + $batch->addOperation($callback, $arguments); + } + return $batch->toArray(); + } } /** @@ -113,3 +131,25 @@ function starshot_installer_uninstall_myself(): void { 'starshot_installer', ]); } + +/** + * Sets up Starshot base recipe. + * + * @param $install_state + * An array of information about the current installation state. + * + * @see starshot_installer_apply_recipes() + */ +function starshot_installer_choose_template(&$install_state) { + $install_state['parameters']['template'] = '/recipes/starshot'; +} + +/** + * Gets additional recipes list. + * + * @param $install_state + * An array of information about the current installation state. + */ +function starshot_installer_additional_recipes(&$install_state) { + // @todo Get list once https://www.drupal.org/project/project_browser/issues/3450629 is fixed. +} From 83734b2fc6f08e1136d4310204e66e24f98ffb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Wed, 31 Jul 2024 08:22:21 -0400 Subject: [PATCH 4/5] Move a few things around and add comments --- installer/starshot_installer.profile | 78 +++++++++++++--------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/installer/starshot_installer.profile b/installer/starshot_installer.profile index 478a80b0..de7abe3f 100644 --- a/installer/starshot_installer.profile +++ b/installer/starshot_installer.profile @@ -16,17 +16,19 @@ use Symfony\Component\Process\ExecutableFinder; function starshot_installer_install_tasks(): array { return [ 'starshot_installer_choose_template' => [ - 'run' => INSTALL_TASK_RUN_IF_REACHED, - 'display_name' => t('Choose template'), + // Because the choice of template is currently hard-coded, this should + // not be presented to the user. + // 'display_name' => t('Choose template'), ], - 'starshot_installer_apply_recipes' => [ + 'starshot_installer_apply_template' => [ 'type' => 'batch', - 'display_name' => t('Apply recipes'), - ], - 'starshot_installer_additional_recipes' => [ - 'run' => INSTALL_TASK_RUN_IF_REACHED, - 'display_name' => t('Apply additional recipes'), ], + // 'starshot_installer_choose_add_on_recipes' => [ + // We don't currently have the ability to present add-on recipes, so for + // now this task doesn't do anything and is hidden from users. + // @todo Fill this in after https://www.drupal.org/i/3450629 is fixed. + // 'display_name' => t('Choose add-ons'), + // ], 'starshot_installer_uninstall_myself' => [ // As a final task, this profile should uninstall itself. ], @@ -100,27 +102,41 @@ function _starshot_installer_install_configure_form_submit(array &$form, FormSta } /** - * Runs a batch job that applies all of the Starshot recipes. + * Presents the user which a choice of which template should set up the site. * - * @param $install_state + * @param array $install_state + * An array of information about the current installation state. + * + * @see starshot_installer_apply_template() + */ +function starshot_installer_choose_template(array &$install_state): void { + // For now, hard-code the choice to the main Starshot recipe. When more + // choices are available, this should present a form whose submit handler + // should set the `template` install parameter for + // starshot_installer_apply_template() to act upon. + $install_state['parameters']['template'] = Drupal::root() . '/recipes/starshot'; +} + +/** + * Runs a batch job that applies the template recipe. + * + * @param array $install_state * An array of information about the current installation state. * * @return array * The batch job definition. */ -function starshot_installer_apply_recipes(&$install_state): array { - if (!empty($install_state['parameters']['template'])) { - $batch = new BatchBuilder(); - $batch->setTitle(t('Applying recipes')); +function starshot_installer_apply_template(array &$install_state): array { + $batch = new BatchBuilder(); + $batch->setTitle(t('Applying recipes')); - $recipe = Recipe::createFromDirectory(Drupal::root() . $install_state['parameters']['template']); - Drupal::classResolver(InputCollector::class)->prepare($recipe); + $recipe = Recipe::createFromDirectory($install_state['parameters']['template']); + Drupal::classResolver(InputCollector::class)->prepare($recipe); - foreach (RecipeRunner::toBatchOperations($recipe) as [$callback, $arguments]) { - $batch->addOperation($callback, $arguments); - } - return $batch->toArray(); + foreach (RecipeRunner::toBatchOperations($recipe) as [$callback, $arguments]) { + $batch->addOperation($callback, $arguments); } + return $batch->toArray(); } /** @@ -131,25 +147,3 @@ function starshot_installer_uninstall_myself(): void { 'starshot_installer', ]); } - -/** - * Sets up Starshot base recipe. - * - * @param $install_state - * An array of information about the current installation state. - * - * @see starshot_installer_apply_recipes() - */ -function starshot_installer_choose_template(&$install_state) { - $install_state['parameters']['template'] = '/recipes/starshot'; -} - -/** - * Gets additional recipes list. - * - * @param $install_state - * An array of information about the current installation state. - */ -function starshot_installer_additional_recipes(&$install_state) { - // @todo Get list once https://www.drupal.org/project/project_browser/issues/3450629 is fixed. -} From dd2483ef4affeb94a943aa5ff6add92ba4d50d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=C3=A9na=20Proxima?= Date: Wed, 31 Jul 2024 08:26:52 -0400 Subject: [PATCH 5/5] Restore a line that should not have been removed --- installer/starshot_installer.profile | 1 + 1 file changed, 1 insertion(+) diff --git a/installer/starshot_installer.profile b/installer/starshot_installer.profile index de7abe3f..f7e85ad9 100644 --- a/installer/starshot_installer.profile +++ b/installer/starshot_installer.profile @@ -22,6 +22,7 @@ function starshot_installer_install_tasks(): array { ], 'starshot_installer_apply_template' => [ 'type' => 'batch', + 'display_name' => t('Apply recipes'), ], // 'starshot_installer_choose_add_on_recipes' => [ // We don't currently have the ability to present add-on recipes, so for