Skip to content

Commit

Permalink
Add build hooks
Browse files Browse the repository at this point in the history
Remove the `serverless-plugin-scripts` plugin and insert our own custom serverless plugin.
  • Loading branch information
mnapoli committed Dec 10, 2017
1 parent ff65110 commit ad3d2dc
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 35 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/vendor
/.idea
/.phplambda
/node_modules
/.serverless
/bin
/tests/Bridge/Symfony/cache
Expand Down
33 changes: 33 additions & 0 deletions .serverless_plugins/phplambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const execSync = require('child_process').execSync;

class PhpServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'deploy:createDeploymentArtifacts': this.build.bind(this),
};
}

/**
* Build step.
*/
build() {
const service = this.serverless.service;
const scripts = service.custom && service.custom.phpbuild;

// Download PHP binary
execSync('vendor/bin/phplambda package', {stdio: 'inherit' });

// If there are build hooks, execute them
if (scripts instanceof Array) {
for (const script of scripts) {
execSync(script, {stdio: 'inherit' });
}
}
}
}

module.exports = PhpServerlessPlugin;
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ Use cases:
## TODO

- init: ask for the project name
- init: create files and explain files to add to gitignore (node_modules, etc.)
- Deploy: auto-add `handler.js`
- Deploy: script (e.g. composer install, etc.)
- Allow configuring the file name of the application (`lambda.php`)
- Handle errors/exceptions and logs
- Avoid using a temporary file for the output
- Test framework
- Move to serverless plugin entirely?

## Setup

Expand Down Expand Up @@ -242,20 +241,12 @@ To test your CLI commands locally, simply run:
$ php lambda.php <commands and options>
```

## Tests
## Build hooks

TODO
You can execute scripts when the lambda is being prepared:

How to test a lambda?

```php
// Grab the $app
$lambda = \PhpLambda\TestClient($app);
$result = $lambda->invoke($functionName, [
...
]);
$this->assertEquals(0, $result->getExitCode());
$this->assertEquals('Foo bar', $result->getOutput());
```yaml
custom:
phpbuild:
- 'composer install --no-dev --optimize-autoloader'
```
25 changes: 18 additions & 7 deletions phplambda
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ $app = new Silly\Application;
$containerBuilder = new ContainerBuilder;
$app->useContainer($containerBuilder->build(), true, true);

$app->command('init', function (SymfonyStyle $io, CommandRunner $commandRunner) {
if (file_exists('serverless.yml')) {
$io->error('A serverless.yml file already exists in this directory, aborting.');
return;
$app->command('init', function (SymfonyStyle $io, CommandRunner $commandRunner, Filesystem $fs) {
if (!file_exists('serverless.yml')) {
$io->writeln('Creating serverless.yml');
$fs->copy(__DIR__ . '/template/serverless.yml', 'serverless.yml');
}
copy(__DIR__ . '/template/serverless.yml', 'serverless.yml');
$commandRunner->run('npm install --save serverless-plugin-scripts');
$io->success('Project initialized and configured in file serverless.yml.');
if (!file_exists('.serverless_plugins/phplambda.js')) {
$io->writeln('Installing the serverless plugin in .serverless_plugins/phplambda.js');
$fs->copy(__DIR__ . '/template/serverless-plugin.js', '.serverless_plugins/phplambda.js');
}
$io->success([
'Project initialized and ready to deploy using "serverless deploy"',
'If you are using git, you will need to commit the following files:',
'- serverless.yml',
'- .serverless_plugins/phplambda.js',
'You can add the following paths to your .gitignore:',
'- /.serverless/',
'- /bin/php',
'- /bin/usr',
]);
});

$app->command('package', function (
Expand Down
8 changes: 3 additions & 5 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ functions:
- http: 'ANY {proxy+}'

plugins:
# This plugin allows to run scripts on specific hooks
- serverless-plugin-scripts
- phplambda

custom:
scripts:
hooks:
'deploy:createDeploymentArtifacts': vendor/bin/phplambda package
phpbuild:
- 'composer install --no-dev --optimize-autoloader'
33 changes: 33 additions & 0 deletions template/serverless-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const execSync = require('child_process').execSync;

class PhpServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.hooks = {
'deploy:createDeploymentArtifacts': this.build.bind(this),
};
}

/**
* Build step.
*/
build() {
const service = this.serverless.service;
const scripts = service.custom && service.custom.phpbuild;

// Download PHP binary
execSync('vendor/bin/phplambda package', {stdio: 'inherit' });

// If there are build hooks, execute them
if (scripts instanceof Array) {
for (const script of scripts) {
execSync(script, {stdio: 'inherit' });
}
}
}
}

module.exports = PhpServerlessPlugin;
9 changes: 4 additions & 5 deletions template/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ functions:
# Use the minimum amount of memory (Mb) by default to optimize costs
# Increase if necessary
memorySize: 128
timeout: 20
# The function will match all HTTP URLs
events:
- http: 'ANY /'
- http: 'ANY {proxy+}'

plugins:
# This plugin allows to run scripts on specific hooks
- serverless-plugin-scripts
- phplambda

custom:
scripts:
hooks:
'deploy:createDeploymentArtifacts': vendor/bin/phplambda package
phpbuild:
- 'composer install --no-dev --optimize-autoloader'

0 comments on commit ad3d2dc

Please sign in to comment.