Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Make function command #125

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ Every Sidecar Function requires two things:
- A PHP Class
- Files that you want deployed to Lambda

For example, if we were wanting to use Node on Lambda to generate an og:image for all of our blog posts, we would first set up a simple class in PHP called `OgImage`.
For example, if we were wanting to use Node on Lambda to generate an `og:image` for all of our blog posts, we would first set up a simple class in PHP called `OgImage`.
To do this we can run the command:

```bash
php artisan make:lambda-function OgImage
```


`App\Sidecar\OgImage.php`

Expand Down
21 changes: 18 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ php artisan sidecar:install

## Configure

The configure command is an interactive command that walks you through setting up your AWS credentials. Dealing with AWS IAM can be a pain, so we wrote this command to do it for you.
The configure command is an interactive command that walks you through setting up your AWS credentials. Dealing with AWS IAM can be a pain, so we wrote this command to do it for you.

```text
php artisan sidecar:configure
```

## Make

The make command will create a new function class in your `app/Sidecar` directory.

```text
php artisan make:lambda-function MyFunction
```

You can also pass a `--runtime=` flag to specify the runtime you want to use. The default runtime for newly created functions is `nodejs20.x`.
To see a list of available runtimes, see the [Runtime](functions/customization#runtime) section.

```text
php artisan make:lambda-function MyFunction --runtime=python3.10
```

## Deploy

The deploy command deploys your functions to Lambda, and can optionally activate them.

To deploy but not activate, run the command without any arguments.
To deploy but not activate, run the command without any arguments.

```text
php artisan sidecar:deploy
```
Expand Down
98 changes: 98 additions & 0 deletions src/Commands/MakeLambdaFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* @author Wilsen Hernández <[email protected]|https://github.com/wilsenhc>
*/

namespace Hammerstone\Sidecar\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'make:lambda-function')]
class MakeLambdaFunction extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:lambda-function {name} {--runtime= : The runtime that will be used to create the lambda function}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Sidecar Lambda function class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Lambda';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->resolveStubPath('/stubs/lambda-function.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__ . $stub;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return "{$rootNamespace}\Sidecar";
}

/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);

$runtime = $this->option('runtime') ?: 'nodejs20.x';

return str_replace(['nodejs20.x', '{{ runtime }}'], $runtime, $stub);
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the lambda function already exists'],
['runtime', null, InputOption::VALUE_OPTIONAL, 'The runtime that will be used to create the lambda function'],
];
}
}
1 change: 0 additions & 1 deletion src/Commands/Warm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Hammerstone\Sidecar\Commands;

use Hammerstone\Sidecar\Sidecar;
use Illuminate\Console\Command;

class Warm extends EnvironmentAwareCommand
{
Expand Down
31 changes: 31 additions & 0 deletions src/Commands/stubs/lambda-function.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace {{ namespace }};

use Hammerstone\Sidecar\LambdaFunction;

class {{ class }} extends LambdaFunction
{
/**
* The function within your code that Lambda calls to begin execution.
* @inheritDoc
*
* @return string
*/
public function handler()
{
return 'sidecar/function.handler';
}

/**
* The runtime environment for the Lambda function.
*
* @see https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
*
* @return string
*/
public function runtime()
{
return '{{ runtime }}';
}
}
2 changes: 2 additions & 0 deletions src/Providers/SidecarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Hammerstone\Sidecar\Commands\Configure;
use Hammerstone\Sidecar\Commands\Deploy;
use Hammerstone\Sidecar\Commands\Install;
use Hammerstone\Sidecar\Commands\MakeLambdaFunction;
use Hammerstone\Sidecar\Commands\Warm;
use Hammerstone\Sidecar\Contracts\AwsClientConfiguration as AwsClientConfigurationContract;
use Hammerstone\Sidecar\Manager;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function boot()
Warm::class,
Deploy::class,
Install::class,
MakeLambdaFunction::class,
]);
}

Expand Down