Skip to content

Instantly share code, notes, and snippets.

@janmartendeboer
Last active May 27, 2022 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janmartendeboer/b48bd498484384975fe7ed338cf9aef4 to your computer and use it in GitHub Desktop.
Save janmartendeboer/b48bd498484384975fe7ed338cf9aef4 to your computer and use it in GitHub Desktop.
Composer Makefile
# Updates at https://gist.github.com/johmanx10/b48bd498484384975fe7ed338cf9aef4
# Composer configuration
PHP := $(shell command -v php || echo php)
COMPOSER := $(shell command -v composer.phar || command -v composer || echo composer)
COMPOSER_VENDOR_DIR := $(shell $(COMPOSER) config vendor-dir || echo vendor)
COMPOSER_AUTOLOAD := $(shell echo "$(COMPOSER_VENDOR_DIR)/autoload.php")
# Install vendor dependencies.
$(COMPOSER_VENDOR_DIR) $(COMPOSER_AUTOLOAD): | composer.lock $(COMPOSER)
$(COMPOSER) install
# Ensure one can always require 'vendor'
vendor: | $(COMPOSER_AUTOLOAD)
# Local application dependencies.
$(COMPOSER): | $(PHP)
# Update the lock file if the package file has changed.
composer.lock: composer.json | $(COMPOSER)
$(COMPOSER) update && touch $@
@janmartendeboer
Copy link
Author

janmartendeboer commented Apr 5, 2021

Installation

  1. Save 💾 composer.mk
  2. Include composer.mk inside Makefile
    include composer.mk
  3. Add vendor as dependency to your own targets
    dist/%.html: | vendor
    	$(PHP) bin/render.php $* > $@

N.B.: Using vendor even works if the vendor-dir setting points to a different directory.

Features

  • Supports custom vendor locations
  • Supports running inside Docker (by only using relative paths in arguments)
  • Automatically distinguishes between install and update
  • Detects changes in composer.json and acts accordingly
  • Supports both composer and composer.phar names

Variables

Exposes the following variables:

Variable Description Example
$(PHP) The path to your locally installed PHP binary. /usr/bin/php
$(COMPOSER) The path to your locally installed Composer. /usr/bin/composer
$(COMPOSER_VENDOR_DIR) The vendor directory. Based on the vendor-dir setting. vendor
$(COMPOSER_AUTOLOAD) The path to autoload.php within the vendor directory. vendor/autoload.php

Example usage

Say we use Makefile to build a static website using PHP. Consider the following targets:

include composer.mk

dist/%.html: | vendor
	$(PHP) bin/render.php $* > $@

Now one can run make dist/index.html and any Composer dependencies are taken care of automatically.

N.B.: Make sure to add vendor after the | (Pipe) to make it an order-only prerequisite.
This prevents changes in vendor/autoload.php from marking your files as outdated.

Tips

  • When including composer.mk at the top of Makefile, when running make without specific target,
    vendor packages will be installed as needed.

Known limitations:

  • Conflicts with having the lock setting set to false. The file composer.lock is a hard requirement for this template. One can tweak it to only look for composer.json, if so desired.

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