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

WIP: Add setup wizard? #384

Closed
wants to merge 1 commit into from
Closed

WIP: Add setup wizard? #384

wants to merge 1 commit into from

Conversation

webflo
Copy link
Member

@webflo webflo commented Apr 13, 2018

I wrote a setup class, which provides some configuration options prior project initialization and deletes itself afterwards. This could be interesting for the composer initiative, or evaluator experience. We could add more features to this template without enforcing every feature.

The code is not pretty, but it does the job.

Current features:

  • Change drupal root? web => docroot
  • Remove drupal-console / drush / dotenv
  • Remove setup wizard itself
  • Remove default .travis config and phpunit

Possible features:

  • Configure dev env? Lando / ddev / etc.
  • Add webflo/drupal-core-strict?

Command for evaluation:

composer create-project --repository '{"type": "git", "url": "git@github.com:webflo/drupal-project.git"}' drupal-composer/drupal-project my-project dev-setup -vvv

Related issues:

@jcnventura
Copy link
Collaborator

jcnventura commented Apr 16, 2018

Would make sense to also remove LICENSE and README.md.

Sites that are not distributed may not need to be GPL-licensed.

@pfrenssen
Copy link
Collaborator

Sites that are not distributed may not need to be GPL-licensed.

They still need to be GPL-licensed even if you don't intend to distribute them to a client.

@jcnventura
Copy link
Collaborator

@pfrenssen Yes, you're right. I got confused. Of course the site is GPL-licensed even in that case. What happens when you don't distribute the site is that you're not forced to provide the site user with a copy of it's source code.

Still, the drupal-project README.md should be removed from the created project.

}
$config['extra']['installer-paths'] = $installer_paths;
}
$fs->dumpFile('.gitignore', $gitIgnore);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this before line 23. The logical flow would be to save the new gitignore just after the preg_replace on the gitIgnore lines, and not after the installer_paths block.

}
$fs->dumpFile('.gitignore', $gitIgnore);
}
if ($event->getIO()->askConfirmation('<info>Remove dotenv?</info> [<comment>y,N</comment>]? ', TRUE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since default is true, it should be 'Y,n'.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, remove the .env.example and load.environment.php files (same as other scaffold files below).

unset($config['require']['vlucas/phpdotenv']);
unset($config['require-dev']['vlucas/phpdotenv']);
}
if ($event->getIO()->askConfirmation('<info>Remove drush?</info> [<comment>y,N</comment>]? ', FALSE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, also remove the drush/Commands/PolicyCommands.php, drush/README.md, drush/drush.yml and drush/sites/self.site.yml files (same as other scaffold files below).

'.travis.yml',
'phpunit.xml.dist',
'scripts/composer/Setup.php',
]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove also the drupal-composer README.md file.

unset($config['require']['drupal/console']);
unset($config['require-dev']['drupal/console']);
}
if ($event->getIO()->askConfirmation('<info>Remove the installer and other scaffold files?</info> [<comment>y,N</comment>]? ', FALSE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Project would be more user friendly if this option was TRUE by default. In which case, make the comment text 'Y,n'.

}
}
unset($config['require']['vlucas/phpdotenv']);
unset($config['require-dev']['vlucas/phpdotenv']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no phpdotenv in the require-dev section of composer.json.

@pfrenssen
Copy link
Collaborator

This works great but it throws an error when it starts installing dependencies because the altered composer.json file is not reloaded and it still tries to load the Setup.php file which has in the meantime been deleted.

I have been looking through the code if it is possible to trigger a reload of the composer.json file (it is tracked in Config::$configSource) but I couldn't find any code that allows to do this.

In my own project I solved it by implementing post-create-project-cmd hook and delaying the deletion of the file until this hook fires.

Here's the patch I used in my project to fix this, it might help you:

diff --git a/composer.json b/composer.json
index 969d52c..38670cb 100644
--- a/composer.json
+++ b/composer.json
@@ -31,9 +31,13 @@
         "post-root-package-install": [
             "@setup"
         ],
+        "post-create-project-cmd": [
+            "@cleanup"
+        ],
         "post-update-cmd": [
             "./vendor/bin/run drupal:site-setup"
         ],
+        "cleanup": "DrupalSiteTemplate\\composer\\Setup::cleanup",
         "setup": "DrupalSiteTemplate\\composer\\Setup::setup"
     },
     "extra": {
diff --git a/scripts/composer/Setup.php b/scripts/composer/Setup.php
index e59d29d..30592ab 100644
--- a/scripts/composer/Setup.php
+++ b/scripts/composer/Setup.php
@@ -75,25 +75,31 @@ class Setup {
       file_put_contents($filename, $file);
     }
 
-    // Remove the setup wizard.
-    unlink('scripts/composer/Setup.php');
-
+    // Remove the configuration related to the setup wizard.
+    unset($config['scripts']['cleanup']);
     unset($config['scripts']['setup']);
     $config['autoload']['classmap'] = array_diff($config['autoload']['classmap'], ['scripts/composer/Setup.php']);
     if (empty($config['autoload']['classmap'])) {
       unset($config['autoload']['classmap']);
     }
+    $config['scripts']['post-create-project-cmd'] = array_diff($config['scripts']['post-create-project-cmd'], ['@cleanup']);
+    if (empty($config['scripts']['post-create-project-cmd'])) {
+      unset($config['scripts']['post-create-project-cmd']);
+    }
     $config['scripts']['post-root-package-install'] = array_diff($config['scripts']['post-root-package-install'], ['@setup']);
     if (empty($config['scripts']['post-root-package-install'])) {
       unset($config['scripts']['post-root-package-install']);
     }
 
     $composer_json->write($config);
 
     return TRUE;
   }
+
+  /**
+   * Removes the setup wizard.
+   */
+  public static function cleanup(): void {
+    unlink('scripts/composer/Setup.php');
+  }
 }

@FatherShawn
Copy link

I added a lot of wizard-like behavior in digitalpulp/ballast which is downstream to this project. post-create-command-project-cmd is a great event - I should have looked at the event list. I'll move some of my tasks to more appropriate events- thanks for the tip @pfrenssen !

@pfrenssen
Copy link
Collaborator

I'm also having another problem related to modifying the composer.json file: the autoloader is generated using the original file, so I need to execute $ composer dump-autoload after running the wizard. I have no time unfortunately right now to look into a structural solution for this.

We are also working on a downstream variant of this, it might serve as inspiration: openeuropa/drupal-site-template#1

@hansfn
Copy link
Contributor

hansfn commented Aug 3, 2018

Are we about to recreate drupal-init from hussainweb/drupal-composer-init. Probably not :-)

@RaphTbm
Copy link

RaphTbm commented Aug 17, 2018

Hi!

Is the current whole code in ScriptHandler.php should be moved in this initial setup too?

I don't have a full scope of it, but the only necessity for ScriptHandler.php to be in the post-install/update commands seems to be relative to the "Prepare the settings file for installation" part which is dependent of drupal/core and what is retrieved by drupal/scaffold.
But to the end, drupal/core and the drupal scaffolded files (as sites/default/default.settings.php) are installed during the create-project process and the files created/updated in ScriptHandler.php seems to be related to a need on the initial install/setup only.

I supposed that post-create-project-cmd was not knew previously (?)

edit: for ScriptHandler.php, it's not the whole code of the file, but the whole code of createRequiredFiles() function!

Cheers!

@Chi-teck
Copy link

DCG has got similar wizard recently.
drush generate project --directory=/path/to/project

@leymannx
Copy link
Collaborator

What's DCG?

@Chi-teck
Copy link

What's DCG?

https://github.com/Chi-teck/drupal-code-generator

I think the approach with script handlers in Composer template will always be tricky because of the need to update/remove existing files.

@AlexSkrypnyk
Copy link
Collaborator

This pull request/issue has been inactive for over a year and is being closed due to inactivity. If the issue still persists or the contribution is still relevant, please feel free to reopen it or create a new one.

Thank you for your understanding and your contributions to the project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants