Lean Drupal Repositories with Composer and Drush

  • View
    816

  • Download
    1

Embed Size (px)

Transcript

  • Lean Drupal Repositories with

    Composer and Drush

    by Greg Anderson

    Photo by Adam Wyles2016

    https://www.flickr.com/photos/the-travelling-bum/5905853321/https://www.flickr.com/photos/the-travelling-bum/5905853321/

  • Pantheon.io 2

    Session Description

    Composer is the industry-standard PHP dependency manager that is now in use in Drupal 8 core. This session will show the current best practices for

    using Composer, drupal-composer, drupal-scaffold, Drush, Drupal Console and Drush site-local aliases to streamline your Drupal 7 and Drupal 8 site

    repositories for optimal use on teams.

    We will answer such gripping questions as:

    How do I avoid placing a copy all of the Drupal core and contrib modules into my repository?

    How do I keep my core and contrib modules up-to-date?

    What if I need to customize .htaccess, or some other file?

    Can I work with a "lean" repository, and still seamlessly use a full repository to deploy?

    How do I share Drush aliases with team members, without using another repository, and without making my alias list too long?

    What is a Drush wrapper script, and how can it help me?

    How does Drupal Console fit in to all of this?

    Should I use test fixtures in my Behat tests, or make a copy of the production site database?

    These techniques will also be helpful for solo site developers--you never know, the next person who needs to check out, build and test your Drupal site

    from scratch might be you!

  • Pantheon.io

    Then create a Git repository:

    cd myproject

    git init

    git add .

    git commit -m My new project

    3

    Getting Started with Drupal and Composer

    One easy step in Composer:

    composer create-project drupal-composer/drupal-project myproject --stability=dev

    Install and run with Drush:

    cd web

    drush qd --db-url=mysql://root@localhost/myprojectdb

    Answer y

    es when C

    omposer

    asks to re

    move VCS

    files, or r

    un

    with --no

    -interac

    tion opti

    on

  • Pantheon.io 4

    Preferred Project Structure

    webcomposer.json

    drupal console

    circle.yml

    external libraries

    behat.yml

    behat drush

    drupal/core

    index.phpcustom-installers

    drupal-scaffoldmodules

    contrib

    custom

    vendor

  • Pantheon.io 5

    Specifying Project Layout

    {

    "name": "drupal-composer/drupal-project",

    "scripts": {

    "drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",

    "post-install-cmd": "sh ./scripts/composer/post-install.sh"

    },

    "extra": {

    "installer-paths": {

    "web/core": ["type:drupal-core"],

    "web/modules/contrib/{$name}": ["type:drupal-module"],

    "web/profiles/contrib/{$name}": ["type:drupal-profile"],

    "web/themes/contrib/{$name}": ["type:drupal-theme"],

    "drush/contrib/{$name}": ["type:drupal-drush"]

    }

    }

    }

  • Pantheon.io 6

    Renaming Document Root

    # Ignore directories generated by Composer

    drush/contrib

    vendor

    docroot/core

    docroot/modules/contrib

    docroot/themes/contrib

    docroot/profiles/contrib

    # Ignore Drupal's file directory

    docroot/sites/default/files

    .gitignore

    #!/bin/sh

    DOCUMENTROOT=docroot

    # Prepare the scaffold files if they are not

    if [ ! -f $DOCUMENTROOT/autoload.php ]

    then

    composer drupal-scaffold

    mkdir -p $DOCUMENTROOT/modules

    mkdir -p $DOCUMENTROOT/themes

    mkdir -p $DOCUMENTROOT/profiles

    post-install.sh

    { "extra": { "installer-paths": { "docroot/core": ["type:drupal-co "docroot/modules/contrib/{$name} "docroot/profiles/contrib/{$name "docroot/themes/contrib/{$name}" "drush/contrib/{$name}": ["type: } }}

    composer.json

  • Pantheon.io 7

    Scaffold Files

    web

    drupal/core

    scaffold files

    modules

    contrib

    custom

    .htaccess

    autoload.php

    index.php

    robots.txt

    updates.php

    sites

    examples.sites.php

    default

    default.services.yml

    default.settings.php

    Drupal.org

    drupal-scaffold

  • Pantheon.io 8

    Custom Scaffold Configuration

    {

    "name": "pantheon-systems/drupal-project",

    "scripts": {

    "drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",

    "post-install-cmd": "sh ./scripts/composer/post-install.sh"

    },

    "extra": {

    "drupal-scaffold": {

    "source": "https://github.com/pantheon-systems/drops-8/archive/{version}.tar.gz",

    "includes": [

    "sites/default/settings.php",

    "sites/default/settings.pantheon.php"

    ]

    }

    }

    }

  • Pantheon.io 9

    Document Root at Project Root

    composer.json

    drupal console

    external libraries

    drush

    core

    index.phpcustom-installers

    drupal-scaffoldmodules

    contrib

    custom

    vendor

    circle.yml

    private

    Privatesubfolder

  • Pantheon.io 10

    composer.json Relocated to Private Directory

    {

    "name": "pantheon-systems/drupal-project",

    "scripts": {

    "drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",

    "post-install-cmd": "sh ./scripts/composer/post-install.sh"

    },

    "extra": {

    "installer-paths": {

    "../core": ["type:drupal-core"],

    "../modules/contrib/{$name}": ["type:drupal-module"],

    "../profiles/contrib/{$name}": ["type:drupal-profile"],

    "../themes/contrib/{$name}": ["type:drupal-theme"],

    "../drush/contrib/{$name}": ["type:drupal-drush"]

    }

    }

    }

  • Pantheon.io 11

    Updating Code

    web

    drupal/core

    scaffold files

    modules

    contrib

    custom

    .htaccess

    autoload.php

    index.php

    robots.txt

    updates.php

    sites

    examples.sites.php

    default

    default.services.yml

    default.settings.php

    Drupal.org

    drupal-scaffold

    composer update

    composer drupal-scaffoldgit pull

    Project FilesOnly

    Scaffold FilesOnly

    Composer and Scaffold

    Files

  • Pantheon.io 12

    Customizing Scaffold Files

    {

    "name": "pantheon-systems/drupal-project",

    "scripts": {

    "drupal-scaffold": "DrupalComposer\\DrupalScaffold\\Plugin::scaffold",

    "post-install-cmd": "sh ./scripts/composer/post-install.sh",

    "post-drupal-scaffold-cmd": "cat htaccess-append.txt >> ../.htaccess"

    }

    }

    To patch

    non-sca

    ffold files

    , use:

    cweaga

    ns/com

    poser-

    patche

    s

  • Pantheon.io 13

    Checking for Security Updates

    With Drush

    With Composer

    $ drush pm-updatestatusChecking available update data ... [ok]Checking available update data for Drupal. [ok]Checking available update data for Token (token). [ok] Name Installed Version Proposed version Message Token (token) 7.x-1.2 7.x-1.6 SECURITY UPDATE available

    $ composer require roave/security-advisories:dev-master$ composer require drupal-composer/drupal-security-advisories:7.x-dev./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev)Your requirements could not be resolved to an installable set of packages.

    Problem 1 - drupal-composer/drupal-security-advisories 7.x-dev conflicts with drupal/token[7.1.2]. - Installation request for drupal-composer/drupal-security-advisories 7.x-dev -> satisfiable by drupal-composer/drupal-security-advisories[7.x-dev]. - Installation request for drupal/token 7.1.2 -> satisfiable by drupal/token[7.1.2].

    n.b. Requires git_deploy module if using Composer with --prefer-source, or if dev modules are used.

  • Pantheon.io 14

    Manage Custom Modules with Composer

    webcomposer.json

    drupal console

    circle.yml

    external libraries

    behat.yml

    behat drush

    drupal/core

    index.phpcustom-installers

    drupal-scaffoldmodules

    contrib

    custom

    vendor

    Variant

    WHY?

    Use private modules in multiple projects.

  • Pantheon.io 15

    Lets Make a Module!

    $ drupal generate:module $ drupal generate:controller

  • Pantheon.io 16

    Customize Modules composer.json

    { "name": "greg-1-anderson/snazzy", "type": "drupal-module", "description": "A Snazzy Module", "keywords": ["Drupal"], "license": "GPL-2.0+", "homepage": "https://github.com/greg-1-anderson/snazzy", "minimum-stability": "dev", "support": { "issues": "https://github.com/greg-1-anderson/snazzy/issues", "source": "https://github.com/greg-1-anderson/snazzy" }, "require": { }}

  • Pantheon.io 17

    Add Our Custom Module to Drupal Project

    { "repositories": [ { "type": "vcs", "url": "https://github.com/greg-1-anderson/snazzy.git" } ], "require": { "composer/installers": "^1.0.20", "drupal-composer/drupal-scaffold": "^1.0", "gre