Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
phpspec.yml
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- 5.4
- 5.5
- hhvm

matrix:
allow_failures:
- php: hhvm

before_script:
- cp phpspec.yml.dist phpspec.yml
- composer install

script:
- bin/phpspec run --format nyan.cat
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 Fréquence Web
Copyright (c) 2012 Fréquence Web & Nekland

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ Install NPM & Bower dependencies with Composer

This simple tools allows you to run `npm install` and/or `bower install` each time you run composer install / update.

*Note: This is Windows-compatible !*

Usage
-----

### Basic usage

Add this lines to your composer.json file (only works with root file) :

```json

{
"require": {
"yohang/dependency-tools": "1.0.*"
"yohang/dependency-tools": "1.1.*"
},
"scripts": {
"post-install-cmd": [
"Yohang\\DependencyTools::installDeps"
],
"post-update-cmd": [
"Yohang\\DependencyTools::installDeps"
"Yohang\\DependencyTools::updateDeps"
]
},
"extra": {
Expand All @@ -33,3 +37,30 @@ Add this lines to your composer.json file (only works with root file) :
```

And that's all, your NPM and Bower dependencies will be installed just after your Composer dependencies.

### Advanced usage

If you don't have a global install of bower, you would maybe like to specify a path to bower. If you uses npm to install
bower directly in your project here is an example of how you can configure composer:

```json
{
"extra": {
"dependency-tools": {
"npm": true,
"bower": {
"path": "node_modules/.bin/bower"
}
}
}
}
```

Known issue
-----------

Bower executable is searching for `node`, if you installed nodejs on ubuntu, the executable is `nodejs`, an easy fix is to execute the following command to make a shortcut named `node` to `nodejs`.

```bash
sudo ln -vs /usr/bin/nodejs /usr/bin/node
```
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
{
"name": "yohang/dependency-tools",
"name": "nekland/dependency-tools",
"description": "A simple Composer script to install NPM and/or Bower dependencies",
"keywords": ["dependency", "bower", "npm", "composer"],
"homepage": "https://github.com/yohang/dependency-tools",
"homepage": "https://github.com/nekland/dependency-tools",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Yohan Giarelli",
"email": "[email protected]",
"homepage": "http://yohan.giarel.li"
},
{
"name": "Maxime Veber",
"email": "[email protected]",
"homepage": "http://nekland.fr"
}
],
"require": {
"php": ">=5.3.0",
"symfony/process": ">=2.0,<3.0"
"symfony/process": "~2.0|~3.0"
},
"require-dev": {
"phpspec/phpspec": "~2.0",
"phpspec/nyan-formatters": "1.*",
"composer/composer": "dev-master"
},
"autoload": {
"psr-0": { "Yohang": "src/" }
Expand Down
2 changes: 2 additions & 0 deletions phpspec.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extensions:
- PhpSpec\NyanFormattersExtension\Extension
14 changes: 14 additions & 0 deletions spec/Yohang/DependencyToolsSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace spec\Yohang;


use PhpSpec\ObjectBehavior;

class DependencyToolsSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Yohang\DependencyTools');
}
}
54 changes: 49 additions & 5 deletions src/Yohang/DependencyTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@

use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Process\ExecutableFinder;
use Composer\Script\Event;

/**
* Simple static class that installs non-composer dependencies
*
* @author Yohan Giarelli <[email protected]>
* @author Maxime Veber <[email protected]>
*/
class DependencyTools
{
/**
* @param $event
* @param Event $event
* @throws \RuntimeException
*/
public static function installDeps(Event $event)
{
static::setup($event, 'install');
}

/**
* @param Event $event
* @throws \RuntimeException
*/
public static function updateDeps(Event $event)
{
static::setup($event, 'update');
}

/**
* @param Event $event
* @param string $type
* @throws \RuntimeException
*/
public static function installDeps($event)
protected static function setup(Event $event, $type)
{
$options = static::getOptions($event);
if (false !== $options['npm']) {
echo "Installing NPM dependencies\n";
static::execCommand(
$options['npm'],
'npm',
array('install'),
array($type),
'An error occuring when installing NPM dependencies'
);
}
Expand All @@ -33,15 +54,15 @@ public static function installDeps($event)
static::execCommand(
$options['bower'],
'bower',
array('install'),
array($type),
'An error occuring when installing Bower dependencies'
);
}
}


/**
* @param $event
*
* @return array
*/
protected static function getOptions($event)
Expand Down Expand Up @@ -74,6 +95,8 @@ protected static function execCommand($options, $cmd, array $args, $ifError)
$cmd = $executableFinder->find($cmd);
}

$cmd = static::guessCorrectPath($cmd);

$out = '';
$process = ProcessBuilder::create(array_merge(array($cmd), $args))->getProcess();
if (isset($options['timeout'])) {
Expand All @@ -85,4 +108,25 @@ protected static function execCommand($options, $cmd, array $args, $ifError)
throw new \RuntimeException($ifError."\n\n".$out);
}
}

/**
* This is a fix for windows that need path with "\" instead of "/"
*
* @param string $path
* @return string
*/
protected static function guessCorrectPath($path)
{
if (DIRECTORY_SEPARATOR === '/') {
if (!file_exists($path)) {
$path = str_replace('\\', '/', $path);
}
} else {
if (!file_exists($path)) {
$path = str_replace('/', '\\', $path);
}
}

return $path;
}
}