diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3f04135 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.lock +vendor/ +phpspec.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c9eb398 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/LICENSE b/LICENSE index 3481b37..61c97f9 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 47b9b15..cdd6d73 100644 --- a/README.md +++ b/README.md @@ -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": { @@ -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 +``` diff --git a/composer.json b/composer.json index 94c0aba..f242485 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "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": [ @@ -10,11 +10,21 @@ "name": "Yohan Giarelli", "email": "yohan@giarel.li", "homepage": "http://yohan.giarel.li" + }, + { + "name": "Maxime Veber", + "email": "nek.dev@gmail.com", + "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/" } diff --git a/phpspec.yml.dist b/phpspec.yml.dist new file mode 100644 index 0000000..2cde5c0 --- /dev/null +++ b/phpspec.yml.dist @@ -0,0 +1,2 @@ +extensions: + - PhpSpec\NyanFormattersExtension\Extension diff --git a/spec/Yohang/DependencyToolsSpec.php b/spec/Yohang/DependencyToolsSpec.php new file mode 100644 index 0000000..f4d32b8 --- /dev/null +++ b/spec/Yohang/DependencyToolsSpec.php @@ -0,0 +1,14 @@ +shouldHaveType('Yohang\DependencyTools'); + } +} diff --git a/src/Yohang/DependencyTools.php b/src/Yohang/DependencyTools.php index 0eda273..0a9d26e 100644 --- a/src/Yohang/DependencyTools.php +++ b/src/Yohang/DependencyTools.php @@ -4,19 +4,40 @@ 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 + * @author Maxime Veber */ 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']) { @@ -24,7 +45,7 @@ public static function installDeps($event) static::execCommand( $options['npm'], 'npm', - array('install'), + array($type), 'An error occuring when installing NPM dependencies' ); } @@ -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) @@ -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'])) { @@ -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; + } }