Skip to content

Add Composer based workflow #2405

Closed
Closed

Description

@mdrmike

I was perusing the issue queue related to issue #1993 and was going to submit some feedback related to #2251 (comment). I was thinking about how Drupal 9 workflow has embraced composer and the workflow isn't like Drupal 7 - Drupal 8.7 where you download the project. I feel Lando should add features to simplify working with composer based projects, such as Drupal 8.8/9+ and I'm sure WordPress is close behind (johnpbloch/wordpress, fancyguy/webroot-installer). There are obviously many others.

Issue: It seems that going forward for Drupal 8.8+/9 that lando init command should not use a remote-url to download Drupal. At least not as the default.

Instead, lando docs and lando init process should reflect Drupal best practices for local development to build a project using composer. (Drupal.org guide currently suggests DDEV for local setup!?).

Lando Idea: Composer Project Made Easy

Lando should make initializing a new php project from composer easy, such as starting a new Drupal or WordPress project.

Perhaps something similar to:

lando init
From where should we get your app's codebase?
❯ composer 👈 👈
? What recipe do you want to use? drupal9
❯ drupal9
? Where is your webroot relative to the init destination? web
? What do you want to call this app? my-drupal9-project

Note: composer as a source for codebase would be a ✨ new feature. ✨

Example Result

The above example process might result in a lando.yml config similar to:

name: my-drupal9-project
recipe: drupal9
config:
  webroot: drupal9/web
  
services:
  appserver:
    composer:
     drupal/recommended-project: ^9
     drush/drush: ^10
    build:
     - composer install

Note: The above .lando.yml config does not work. I'm not certain why not, but it seems close. It's based on limited Lando docs about composer, see: Automatically composer install before my app starts


Working Example

An example .lando.yml that does work, and is how I currently accomplish managing sites with composer, uses run: commands during build, re-build, and destroy. I've been using this basic file for drupal 8+ projects.

name: my-drupal9-project
recipe: drupal9
config:
  webroot: web

services:
  appserver:
    run:
      - echo "▶️  Downloading Drupal and dependencies using Composer ..."
      - composer create-project --no-install --remove-vcs 'drupal/recommended-project:^9' temp
      - bash -c "(shopt -s dotglob && mv /app/temp/* /app) && rmdir temp" # move composer files to "app" then remove "temp"
      - composer require --sort-packages -- 'drush/drush:^10' 
      - echo "▶️  Installing Drupal ..."
      - drush si standard -y --db-url=mysql://drupal9:drupal9@database:3306/drupal9 
      - echo " ▶️  Note login credentials                    ⬆                     ⬆"


events:
  # Fix composer permissions bug during re-build/destroy: https://www.drupal.org/docs/develop/using-composer/starting-a-site-using-drupal-composer-project-templates
  pre-uninstall:
    - echo "▶️  Fix composer bug permissions for rebuild and remove composer files"
    - chmod u+wX /app/web/sites/default && rm -R /app/composer.* 

Voilà! lando start will build and install Drupal. lando rebuild and lando destroy require events due to a known composer bug. Note: I also delete composer files as I consider it generated code based on contents of .lando.yml.

Activity

  1. added
    featureRequesting that Lando do something new
    on Jun 17, 2020
  2. pirog commented on Jun 17, 2020

    @pirog
    SponsorMember

    @mdrmike i think this idea is good.

    however, its worth noting that there is no "default" for initializing a drupal site, the documented "getting started" section in the drupal docs is just one possible way to get drupal.

    the primary reason it does not use composer currently is because a composer based drupal installation is fundamentally at odds with how lando init works. in particular:

    1. the utility container that is used to grab code from a source in lando init does not have php/composer installed in it by default. when that code runs we also do not yet know details of your application which may be relevant for dependency installation eg the php version. so it is sort of a chicken and the egg problem.

    2. I think in order to get this to work we'd need to have some sort of logic that tells lando to do stuff using the appserver container after the landofile from the init is created. this would be a big change. we also would probably want this to be generic enough to handle other create-project scaffolding.

  3. mdrmike commented on Jun 17, 2020

    @mdrmike
    Author

    I'm happy for the feedback! Yes, using the word "default" was confusing. I just meant Lando documentation/examples going forward should reflect current Drupal8/9 recommended practices, and why I linked to Drupal.org local development guide.

    Lando Init Use Cases

    My understanding of lando init is to allow a new user to get to work, by removing complexity of internals, such as details on how to create a .lando.yml? If this is correct, I think it follows that lando init should support the "new &/or less technical" Drupal/WorPress developers.

    In other words, lando init doesn't have to be robust and generic enough to handle all composer projects, it already does if you're technically adept with the internals. But lando init should handle typical use cases so that its so easy Drupal.org or WordPress.org would link to it as an easy way to get started, yet robust enough to be professional and build upon.

    Update Lando Docs

    In the meantime, maybe its easiest to just update Lando Drupal 8/9 Docs to demonstrate current Drupal 8/9.x practices?

    Example: Drupal 9 Local Development

    Here are the steps to install Drupal 9 using lando and composer. The workflow is based upon Drupal 8.8+ recommendation to use composer to manage Drupal and all dependencies (modules, themes, libraries). Lando makes it simple in a few easy steps.

    Overview

    1. Use lando init to create configuration for new Drupal project by answering prompts
    2. Launch the new project environment using: lando start
    3. Use lando composer to create Drupal scaffolding
    4. Use lando composer --dev to get drush, and optionally other development modules
    5. Use lando composer to get project modules
    6. Use lando drush to install website
    7. Suggestion: Initialize git and commit .lando.yml and composer.json files to your git repo. Note: it's typically best practice to ignore other project code from your own git repo.
    # 1a. In an empty directory, initialize a new project
    lando init
    
    # 1b. and setup Drupal by answering prompts
    WARN ==> May need site-local drush  👈 👈 👈
    
    ? From where should we get your app's codebase? ⌨ current working directory
    ? What recipe do you want to use? ⌨ drupal9
    ? Where is your webroot relative to the init destination? ⌨ drupal/web
    ? What do you want to call this app? ⌨ my-drupal9-project
    
       _  __                       _         
      / |/ /__ _    __  _    _____( )_______ 
     /    / _ \ |/|/ / | |/|/ / -_)// __/ -_)
    /_/|_/\___/__,__/  |__,__/\__/ /_/  \__/ 
                                             
      _________  ____  __ _______  _______  _      ______________ __  ___________  ______
     / ___/ __ \/ __ \/ //_/  _/ |/ / ___/ | | /| / /  _/_  __/ // / / __/  _/ _ \/ __/ /
    / /__/ /_/ / /_/ / ,< _/ //    / (_ /  | |/ |/ // /  / / / _  / / _/_/ // , _/ _//_/ 
    \___/\____/\____/_/|_/___/_/|_/\___/   |__/|__/___/ /_/ /_//_/ /_/ /___/_/|_/___(_)  
                                                                                         
    Your app has been initialized!
    
    Go to the directory where your app was initialized and run lando start to get rolling.
    Check the LOCATION printed below if you are unsure where to go.
    
    Oh... and here are some vitals:
    
     NAME      my-drupal9-project                         
     LOCATION  /path/to/my/drupal9/project     
     RECIPE    drupal9                                    
     DOCS      https://docs.lando.dev/config/drupal9.html 
    
    # 2. Start it up. (Note: once it starts, ignore the "links in red", we still need Drupal in the next steps)
    lando start
    
    # 3. Use composer to prepare a new Drupal9 Project. (Note composer will create directory: drupal)
    lando composer create-project --no-dev --no-install --remove-vcs 'drupal/recommended-project:^9' drupal
    
    # 3a. Change directory to new project 
    cd drupal
    
    # 4. Add Drush for development (require-dev). For details see: lando composer install --no-dev --help
    lando composer require --dev --sort-packages -- 'drush/drush:^10'
    
    # 5. Add any other dupal modules, typically of format: drupal/module_name
    lando composer require --sort-packages -- drupal/admin_toolbar
    
    # 6. Use Drush to perform site-install. See lando drush site:install --help
    lando drush site:install -y --db-url=mysql://drupal9:drupal9@database:3306/drupal9
    

    Note 👈 👈 👈 Above: IMO, This warning is both ugly and confusing for a new user. (Lando v3.0.5)

    Additional Resources on Developing with Composer

  4. pirog commented on Jun 29, 2020

    @pirog
    SponsorMember

    @mdrmike i think the lowest barrier to entry flow should be what we recommend, right now i think that is to lando init with the tar/git repo. We certainly can document how to currently do this with composer create-project IN ADDITION. You can see the Laravel docs for an example of how that would work now.

    I have to say that i think we want to have a low touch way to handle composer create-project generally as part of lando init. If we are getting into the business of implementing specific start states we are going to find ourselves in a difficult position as far as maintenance and upkeep is concerned. Ultimately if we dont do that upkeep the user is likely to pay the price. I'd much rather prefer something like:

    lando init --source composer --composer-project "drupal/recommended-project:^9" --composer-opts "create-project --no-dev --no-install --remove-vcs"

    We could also consider "aliases" for specific implementations of the above, which feel very similar to your proposal eg the above would be equivalent to

    lando init --source composer --recipe drupal9
    

    And then when you are done you've got the drupal project running in the directory you indicated for installation, usually this is just the directory you invoke the command from. I think this would provide a more consistent overall experience for lando init. The trick here is going to be figuring out the chicken and the egg problem discussed here #2405 (comment) but i think there are a few ways to get around that. If we can do that then everything after that just becomes the usual business eg more or less what you had

    lando start
    lando composer require --dev --sort-packages -- 'drush/drush:^10'
    lando drush site:install -y --db-url=mysql://drupal9:drupal9@database:3306/drupal9

    On the Drush warning front: FWIW WARN ==> May need site-local drush no longer shows up like this as of 3.0.7, instead a larger message with an explanation and link to the drush docs is provided.

  5. pirog commented on Jun 29, 2020

    @pirog
    SponsorMember

    @dustinleblanc @serundeputy id be interested to get your feedback here. it would be great (and easy, and robust) way to support something like this.

  6. gitressa commented on Jul 18, 2020

    @gitressa
    Contributor

    I just revisited the chicken/egg challenge, using an OS which comes with PHP 7.2 by default: If I try to start a fresh Drupal 9 project with composer create-project drupal/recommended-project Composer complains about un-resolvable PHP version requirements (Your requirements could not be resolved [...] drupal/core 9.0.2 requires php >=7.3 ...) since Composer is checking against the host PHP version, not Lando, since it is not part of the picture yet.

    The chicken/egg situation of course happens since I need to be in the Drupal folder to start Lando before I can use lando composer with Lando's version 7.3 of PHP.

    In theory you can override Composer PHP version via .composer/config.json file, which doesn't seem to work. The "hacky" solution I use is to install PHP 7.3 on the host machine. --ignore-platform-reqs flag also exists, but it doesn't seem ideal.

    I would be fine with something like this, to start a Composer based Drupal 8/9 installation (is --no-dev still a thing though?), allowing configurable options, as suggested in #2405 (comment):

    lando init --source composer --composer-project "drupal/recommended-project:^9" --composer-opts "create-project --no-dev --no-install --remove-vcs"

    An alias option like lando init --source composer --recipe drupal9 would be great, with as few assumptions as possible, I guess the equivalent of this?

    lando init --source composer --composer-project "drupal/recommended-project:^9" --composer-opts "create-project" --webroot web

    I suppose the --recipe drupal9 should emulate the current procedure you need to run achieve this?

    composer create-project drupal/recommended-project d9
    cd d9
    lando init --source cwd --recipe drupal9 --webroot web
    

    DDEV seem to have a start-d9-composer-based-installation-in-folder solution:

    mkdir my-drupal9-site
    cd my-drupal9-site
    ddev config --project-type=drupal9 --docroot=web --create-docroot
    ddev start
    ddev composer create "drupal/recommended-project" --no-install
    ddev composer require drush/drush --no-install
    ddev composer install
    ddev drush site:install -y
    ddev drush uli
    ddev launch
    
  7. dustinleblanc commented on Jul 20, 2020

    @dustinleblanc
    Collaborator

    That method DDev uses should work with Lando too, init an empty project in the current working directory, start lando, run composer, bingo. Having a wrapper for a composer source is a cool idea though. I like lando init --source composer vendor/project --recipe some-recipe

  8. gitressa commented on Jul 20, 2020

    @gitressa
    Contributor

    Do you mean the DDEV approach should work currently in Lando @dustinleblanc? Would you care to share the commands? Like I outlined before, my OS has PHP 7.2, so using host Composer does not work for me ... it has to be Lando Composer.

  9. dustinleblanc commented on Jul 20, 2020

    @dustinleblanc
    Collaborator

    Yeah this should work:

    mkdir my-app
    cd my-app
    lando init -r drupal8 # d9 should also work I think? Also, select current working directory for the code location
    lando start
    lando composer create-project some/project --some-flags-and-stuff

    The only thing you have to deal with then is that the project will be nested one directory deep and you may have to configure the web root, etc.

  10. gitressa commented on Jul 20, 2020

    @gitressa
    Contributor

    Thanks for a fast reply. Having to configure and move stuff around is what I want to avoid, though ... I assume this is what DDEV has solved.

  11. pirog commented on Jul 20, 2020

    @pirog
    SponsorMember

    i think @dustinleblanc is saying "this is something you can do in the meantime" while we work on advancing this issue

  12. dustinleblanc commented on Jul 20, 2020

    @dustinleblanc
    Collaborator

    If not for having to keep the .lando.yml in the root directory, you'd be able to init right into your current working directory, but alas, composer will yell at you that the directory is not empty, which the reason our examples init the project in a nested directory. I definitely agree that being able to one-liner it to setup in your current directory would be awesome, but the current workflow is only a hop skip and a jump away from that. We can definitely put the smoother version on the list o things to do, I can imagine it not being hard with our current init formats anyway

  13. gitressa commented on Jul 20, 2020

    @gitressa
    Contributor

    Yeah, there are many possible ways to jump through hoops to make it somehow work ... Like I wrote, I currently just bump up my host PHP version, but ideally Lando should be able contain "everything", to be able to define in code (.lando.yml) PHP versions, MySQL, Solr, etc. as well as create a project with Composer. So a one-liner would be awesome, and high on my wish list :-)

  14. mdrmike commented on Aug 1, 2020

    @mdrmike
    Author

    @gitressa I think your comments are good to note, but are dependent on OS environment and I would think outside scope of this feature request. But not my place to say. It's also worth noting in my example, lando handles php+composer setup ... so no version mismatches... or chicken+egg issue to speak of.

    FWIW, the .lando.yml Working Example in my original post does the same thing as #2405 (comment)

    Using Working Example

    mkdir my-app
    cd my-app
    {copy-paste} > .lando.yml
    lando start

    thats it

  15. mdrmike commented on Aug 1, 2020

    @mdrmike
    Author

    @pirog You say "the chicken and the egg..." ??? I feel I'm missing something... at least for Drupal? This is a Working Example (.lando.yml) from my original post, the only requirement is lando. That means nothing to do with host environment as @gitressa mentioned in a comment. Maybe my example works because it uses a drupal recipe? If so, perhaps a solution/requirement is to determine a compatible recipe before allowing to use composer as a source?

    Sooo, I agree with basically everything else you & @dustinleblanc have written since. The crux of what I was hoping for was something like lando init --source composer vendor/project --recipe some-recipe would generate a working .lando.yml for non-technical/lazy users. The upside is it uses "Drupal recommend practices".


    Example

    This is a non-working init example to generate the config file, but the .lando.yml file works as is.

    1. lando init --name test --source composer 'drupal/recommended-project' --recipe drupal9
      would generate something like:

    .lando.yml

    name: test
    recipe: drupal9
    config:
      webroot: web
    
    services:
      appserver:
        build:
          - composer create-project --no-install drupal/recommended-project temp
          - bash -c "(shopt -s dotglob && mv /app/temp/* /app) && rmdir temp"
          - composer require --sort-packages -- 'drush/drush:^10' 
    1. lando start
      That's it! serious ... boomshakalaka !
      image
  16. gitressa commented on Sep 12, 2020

    @gitressa
    Contributor

    When the patch in the issue composer create-project should require PHP 7.3 gets implemented, being able to start a project, without checking the (irrelevant) host machine PHP with lando init --source composer --recipe drupal9 will become more urgent.

  17. stale commented on Oct 2, 2021

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues.

  18. added
    staleIssue has been auto-flagged as stale
    on Oct 2, 2021
  19. stale commented on Apr 16, 2022

    @stale

    We haven't heard anything here for about a year so we are automatically closing this issue to keep things tidy. If this is in error then please post in this thread and request the issue be reopened!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureRequesting that Lando do something newstaleIssue has been auto-flagged as stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions