diff --git a/README.md b/README.md index a480a83..54d1501 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,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` diff --git a/docs/commands.md b/docs/commands.md index 54d4735..5b954a7 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -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 ``` diff --git a/src/Commands/MakeLambdaFunction.php b/src/Commands/MakeLambdaFunction.php new file mode 100644 index 0000000..a05e837 --- /dev/null +++ b/src/Commands/MakeLambdaFunction.php @@ -0,0 +1,98 @@ + + */ + +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=nodejs20.x : 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 options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['runtime', null, InputOption::VALUE_OPTIONAL, 'The runtime that will be used to create the lambda function', 'nodejs20.x'], + ]; + } +} diff --git a/src/Commands/Warm.php b/src/Commands/Warm.php index 5946d11..8dc0e87 100644 --- a/src/Commands/Warm.php +++ b/src/Commands/Warm.php @@ -7,7 +7,6 @@ namespace Hammerstone\Sidecar\Commands; use Hammerstone\Sidecar\Sidecar; -use Illuminate\Console\Command; class Warm extends EnvironmentAwareCommand { diff --git a/src/Commands/stubs/lambda-function.stub b/src/Commands/stubs/lambda-function.stub new file mode 100644 index 0000000..9efbe18 --- /dev/null +++ b/src/Commands/stubs/lambda-function.stub @@ -0,0 +1,31 @@ +