Using Grunt to Manage Drupal Build and Testing Tools DrupalCon Los - - PowerPoint PPT Presentation

using grunt
SMART_READER_LITE
LIVE PREVIEW

Using Grunt to Manage Drupal Build and Testing Tools DrupalCon Los - - PowerPoint PPT Presentation

Using Grunt to Manage Drupal Build and Testing Tools DrupalCon Los Angeles 2015 5/13/2015 Joe Turgeon Director of Engineering Email: jturgeon@phase2technology.com GitHub: @arithmetric Twitter: @arithmetric Does Drupal Need a Build Tool?


slide-1
SLIDE 1

Using Grunt

to Manage Drupal Build and Testing Tools

DrupalCon Los Angeles 2015 5/13/2015

slide-2
SLIDE 2

Director of Engineering

Email: jturgeon@phase2technology.com

Joe Turgeon

GitHub: @arithmetric Twitter: @arithmetric

slide-3
SLIDE 3

Does Drupal Need a Build Tool?

slide-4
SLIDE 4

Drupal development evolved

Install and run approach:

Download
 from d.o Add custom code Rsync to server Drush make Add custom code git commit/ deploy Run code checks Compile Sass Behat tests

Build and test approach:

slide-5
SLIDE 5

Why build Drupal?

  • End anti-pattern of mixing core/contrib and

custom code

  • Improve visibility into projects with a make file
  • Simplify use of external libraries and resources
  • Enforce reproducibility and automation
slide-6
SLIDE 6

Why test Drupal?

  • Complex sites need more testing than “clicking

around”

  • Empower devs to produce quality code
  • Find regressions, code style issues before

integration

slide-7
SLIDE 7

Open questions

  • How can we make sure everyone uses the same

tools in the same way?

  • How can we separate source code from

dependencies and build output?

slide-8
SLIDE 8

Enter Grunt

slide-9
SLIDE 9

Why Grunt?

  • JavaScript-based task runner
  • Community-supported, widely-adopted, flexible
  • Only requirement is Node.js
  • Straightforward approach: tasks in JS, config in

JSON

  • 4,000+ contributed plugins
slide-10
SLIDE 10

Grunt, sold!

  • Tutorial on writing a Grunt script to minify

JavaScript using the “uglify” tool:
 
 http://gruntjs.com/getting-started

slide-11
SLIDE 11

Introducing Grunt Drupal Tasks

  • Grunt plugin for common Drupal build and

testing tasks

  • Started as R&D project in January 2014
  • Since March 2014, used for many client projects
  • Released on GitHub in September 2014
  • Supports Drupal 7 and 8
slide-12
SLIDE 12

It has opinions

  • Enforces practice across team and CI env
  • Presumes code workflow
  • Verify quality of custom code
  • Assemble core and contrib with Drush make
  • Run tests
  • Project scafgolding
slide-13
SLIDE 13

It brings greatness together

slide-14
SLIDE 14

It stays out of your way

  • Sensible defaults that can be overridden
  • Manages its own dependencies
  • Tools are ready to use and discoverable
slide-15
SLIDE 15

Introducing Gadget

  • Easy to start a Drupal project with Grunt Drupal

Tasks

  • Yeoman Generator
  • Released on GitHub in April 2015
slide-16
SLIDE 16

Getting Started

slide-17
SLIDE 17

Dependencies

  • Before starting, make sure you have:
  • Node.js (includes npm)
  • Recommended for all supporting tools:
  • Bundler, Composer, Ruby, and RubyGems
slide-18
SLIDE 18

Install Gadget

  • Install Gadget and its dependencies:


npm install -g generator-gadget grunt-cli yo

slide-19
SLIDE 19

Project set up

  • Use Gadget to set up a new Drupal project using

Grunt Drupal Tasks:
 


yo gadget

slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24

Project scafgolding

GDT project script and config

slide-25
SLIDE 25

Project scafgolding

Defines GDT and project dependencies

slide-26
SLIDE 26

Project scafgolding

GDT dependencies

slide-27
SLIDE 27

Project scafgolding

Custom Drupal code and Drush make file

slide-28
SLIDE 28

Project scafgolding

Test cases and config

slide-29
SLIDE 29

Everything in it’s right place

  • Modules
slide-30
SLIDE 30

Everything in it’s right place

  • Modules
  • Install profiles
slide-31
SLIDE 31

Everything in it’s right place

  • Modules
  • Install profiles
  • Drush make file
slide-32
SLIDE 32

Everything in it’s right place

  • Modules
  • Install profiles
  • Drush make file
  • Single-/multi-site config
slide-33
SLIDE 33

Everything in it’s right place

  • Modules
  • Install profiles
  • Drush make file
  • Single-/multi-site config
  • Static file replacements
slide-34
SLIDE 34

Everything in it’s right place

  • Modules
  • Install profiles
  • Drush make file
  • Single-/multi-site config
  • Static file replacements
  • Themes
slide-35
SLIDE 35

Goals

  • Accommodate (and isolate) custom code,

Drupal core and contrib code, site settings files, and supporting tools

  • Allow tools to work together with minimal glue
  • Standardize project code base structure
slide-36
SLIDE 36

Building

slide-37
SLIDE 37

Project build

  • Use Grunt to build the project:


grunt


slide-38
SLIDE 38

Default build steps

  • Composer/Bundler
  • Validate code
  • Drush make
  • Scafgold (injects custom files into build output)
  • Compile theme

* = if applicable/needed

slide-39
SLIDE 39
slide-40
SLIDE 40
slide-41
SLIDE 41
slide-42
SLIDE 42
slide-43
SLIDE 43

Build output

slide-44
SLIDE 44

Build output

slide-45
SLIDE 45

Build output

slide-46
SLIDE 46

Into build/html/

src/modules

slide-47
SLIDE 47

Into build/html/

src/profiles/mysite

slide-48
SLIDE 48

Into build/html/

src/sites/default src/sites/…

slide-49
SLIDE 49

Into build/html/

src/themes

slide-50
SLIDE 50

Symlinks, really?

  • No need to re-build afuer each change to a source file
  • Build tasks ensure the links are set up correctly
  • Relative links within the scafgold structure are

portable

  • XDebug works across symlinks
  • Windows users: Beware! Copy alternative in the

backlog

slide-51
SLIDE 51

Running automatically

  • Runs validate, theme compile, drush make (if

needed) whenever file changes are detected:
 


grunt watch

slide-52
SLIDE 52
slide-53
SLIDE 53
slide-54
SLIDE 54
slide-55
SLIDE 55
slide-56
SLIDE 56

The way of the build

  • Captures the daily development workflow
  • Produces a complete Drupal site docroot from

custom source and a make file

  • Build locally, build on integration
slide-57
SLIDE 57

Have it your way

  • Build tasks can be run individually
  • Let’s look at the core tasks
slide-58
SLIDE 58

Validating

slide-59
SLIDE 59

Validating code quality

  • Uses lints and static code analysis to quantify

code quality

  • Run PHP Lint and PHP Code Snifger (with Drupal

coding standards) with:
 


grunt validate

slide-60
SLIDE 60

PHP Lint

  • Verifies syntax with PHP’s built-in lint tool
slide-61
SLIDE 61

PHP Code Snifger

  • Verifies code style according to Drupal’s code

standards

slide-62
SLIDE 62

Check yo’ self

  • Encourage devs to validate code before committing
  • Focus peer code review on architecture
  • Fail integration builds or reject pull requests at

certain thresholds

slide-63
SLIDE 63

Compiling Themes

slide-64
SLIDE 64

Front end tools

  • Compass was one of the first non-PHP tools to enter

common Drupal practice

  • Configure GDT to define themes and compilation

approach

  • Run individually with `grunt compile-theme`
slide-65
SLIDE 65

Compass

  • Include a Gemfile to install Compass and required gems
  • Compass compile is run during build
slide-66
SLIDE 66

Grunt-to-Grunt

  • GDT will be able to delegate theme compilation to

Grunt-powered themes

  • Theme-supplied Grunt scripts are run during build for

any configured themes

slide-67
SLIDE 67

Testing

slide-68
SLIDE 68

Testing site features

  • Provides Behat, the Drupal Extension, other

dependencies

  • Testing with Behat requires an installed Drupal

site accessible by URL

  • Two options for running tests locally
  • Using a local env (VM, Docker, W/MAMP)
  • Using PHP/drush’s local test server
slide-69
SLIDE 69
slide-70
SLIDE 70
slide-71
SLIDE 71
slide-72
SLIDE 72
slide-73
SLIDE 73

Running tests

  • Run tests with:


grunt test

slide-74
SLIDE 74
slide-75
SLIDE 75
slide-76
SLIDE 76
slide-77
SLIDE 77

Towards BDD

  • GDT supports a TDD/BDD workflow
  • Write tests first
  • Code iteratively with test feedback
  • Automate using “watch” tasks
  • Includes tools and test scripts to kickstart

testing

slide-78
SLIDE 78

Packaging and Deployment

slide-79
SLIDE 79

Packaging and deployment

  • GDT and scafgolding are meant for local and

integration envs

  • Prepares code for release
  • Hand ofg to other tools for deployment
slide-80
SLIDE 80

Packaging

  • Produces a standalone, deployable Drupal docroot
  • Excludes supporting tools and config
  • Run with `grunt package`
slide-81
SLIDE 81

Rolling a release

  • Exploring how GDT can support the release process
  • Planned approaches
  • Commit to deployment repo (Acquia, Pantheon model)
  • Integrate with release/deployment tools (Capistrano,

ShipIt)

slide-82
SLIDE 82

Configuring and Extending

slide-83
SLIDE 83

Configuring

  • Configurable settings in Gruntconfig.json
  • Source and build paths
  • CLI options for Behat
  • Drush make arguments
  • Theme compilation options
  • Files to include/exclude from package
slide-84
SLIDE 84

Example configuration

  • Using Drush make `--working-copy` to keep git

files

slide-85
SLIDE 85

Extending

  • Extensions possible through Gruntfile.js changes
  • Adding new tasks, replacing existing ones
  • Running a shell script as a task
  • Overriding the steps in the default build process
slide-86
SLIDE 86

Example extension

  • Adding the `grunt-eslint` plugin to perform JS linting
slide-87
SLIDE 87

Wrapping Up

slide-88
SLIDE 88

Pitching in

  • Try using Grunt Drupal Tasks on your next project
  • File issues on GitHub to share how it could work

better

  • Patches welcome! (Submit pull requests on GitHub)
  • Take on a “help wanted” issue on GitHub
slide-89
SLIDE 89

On the roadmap

  • More quality/testing tools (Sass/JS linting, PHPUnit)
  • Delegation for Grunt-powered themes
  • More options for releasing/deployment
  • Build Drupal with Composer
  • Integrate with Drupal Console
  • More Gadget options (make files, theme support)
slide-90
SLIDE 90

Thanks! Questions?

  • Evaluate this session, download slides, and find

links:
 https://events.drupal.org/node/641

slide-91
SLIDE 91

PHASE2TECHNOLOGY.COM