From 781c8035fd06d339d734489686c39a58e3bb12a4 Mon Sep 17 00:00:00 2001 From: Eduardo Reveles Date: Wed, 26 Feb 2020 23:05:49 -0600 Subject: [PATCH] feat(commands): support Lumen projects Add support for Lumen projects. fix #8 --- README.md | 1 + environment/docker-compose.yml | 2 +- .../{app.dockerfile => laravel.dockerfile} | 0 .../resources/dockerfiles/lumen.dockerfile | 38 +++++++++++++++++++ src/actions/dockerfile-config.ts | 16 ++++++-- src/actions/prompt-environment.ts | 15 +++++++- src/actions/publish-environment.ts | 18 +++++++-- src/actions/utility.ts | 12 ++++++ src/commands/new.ts | 4 +- src/constants.ts | 2 + 10 files changed, 97 insertions(+), 11 deletions(-) rename environment/resources/dockerfiles/{app.dockerfile => laravel.dockerfile} (100%) create mode 100644 environment/resources/dockerfiles/lumen.dockerfile diff --git a/README.md b/README.md index 1a3f4d5..466e962 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Unlike other tools in the ecosystem like Laradock and Vessel, the container imag - [x] Automatically configures XDebug - [x] Supports MySQL and Postgres environments - [x] Automatically configures Laravel ENV to connect to services in the Docker network +- [x] Support both Laravel and Lumen projects # CLI diff --git a/environment/docker-compose.yml b/environment/docker-compose.yml index 396e8e8..e2132e2 100644 --- a/environment/docker-compose.yml +++ b/environment/docker-compose.yml @@ -4,7 +4,7 @@ services: app: build: context: ./ - dockerfile: environment/dockerfiles/app.dockerfile + dockerfile: environment/dockerfiles/laravel.dockerfile args: xdebug: "true" tinker: "true" diff --git a/environment/resources/dockerfiles/app.dockerfile b/environment/resources/dockerfiles/laravel.dockerfile similarity index 100% rename from environment/resources/dockerfiles/app.dockerfile rename to environment/resources/dockerfiles/laravel.dockerfile diff --git a/environment/resources/dockerfiles/lumen.dockerfile b/environment/resources/dockerfiles/lumen.dockerfile new file mode 100644 index 0000000..1aab593 --- /dev/null +++ b/environment/resources/dockerfiles/lumen.dockerfile @@ -0,0 +1,38 @@ +FROM php:7.3.6-fpm-stretch + +ARG xdebug=false + +WORKDIR /var/www + +# These are considered sane defaults for the cloud deployments +ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS="0" \ + PHP_OPCACHE_MAX_ACCELERATED_FILES="100000" \ + PHP_OPCACHE_MEMORY_CONSUMPTION="192" \ + PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10" \ + PHP_XDEBUG_DEFAULT_ENABLE="0" \ + PHP_XDEBUG_REMOTE_ENABLE="0" + +RUN docker-php-ext-install opcache + +# + +# Conditionally install the necessary components to allow XDebug +RUN if [ "$xdebug" = "true" ] ; then pecl install xdebug && docker-php-ext-enable xdebug ; else echo Running app without XDEBUG ; fi + +COPY vendor/ ./vendor/ +COPY artisan composer.json composer.lock ./ + +COPY app/ ./app/ +COPY bootstrap/ ./bootstrap/ +COPY database/ ./database/ +COPY public/ ./public/ +COPY resources/ ./resources/ +COPY routes/ ./routes/ +COPY storage/ ./storage/ + +COPY environment/config/php/conf.d/*.ini /usr/local/etc/php/conf.d/ +COPY environment/config/fpm/*.conf /usr/local/etc/php-fpm.d/ + +RUN chmod -R 777 /var/www/storage +RUN chmod -R 777 /var/www/bootstrap + diff --git a/src/actions/dockerfile-config.ts b/src/actions/dockerfile-config.ts index 69d9d8f..b0cde18 100644 --- a/src/actions/dockerfile-config.ts +++ b/src/actions/dockerfile-config.ts @@ -2,10 +2,15 @@ import { replaceFileContent } from "./utility"; import { join } from "path"; import { DB_DRIVER_REPLACE_STRING } from "../constants"; -export const pSqlDriverInstall = async (projectPath: string) => { +type ProjectType = "lumen" | "laravel"; + +export const pSqlDriverInstall = async ( + projectPath: string, + projectType: ProjectType +) => { const srcAppDockerFile = join( projectPath, - "environment/dockerfiles/app.dockerfile" + `environment/dockerfiles/${projectType}.dockerfile` ); const replaceArgs = [ DB_DRIVER_REPLACE_STRING, @@ -18,10 +23,13 @@ export const pSqlDriverInstall = async (projectPath: string) => { await replaceFileContent(srcAppDockerFile, replaceArgs); }; -export const mysqlDriverInstall = async (projectPath: string) => { +export const mysqlDriverInstall = async ( + projectPath: string, + projectType: ProjectType +) => { const srcAppDockerFile = join( projectPath, - "environment/dockerfiles/app.dockerfile" + `environment/dockerfiles/${projectType}.dockerfile` ); const replaceArgs = [ DB_DRIVER_REPLACE_STRING, diff --git a/src/actions/prompt-environment.ts b/src/actions/prompt-environment.ts index 2718d73..5577db7 100644 --- a/src/actions/prompt-environment.ts +++ b/src/actions/prompt-environment.ts @@ -1,5 +1,5 @@ import * as inquirer from "inquirer"; -import { MYSQL, POSTGRES } from "../constants"; +import { MYSQL, POSTGRES, LARAVEL, LUMEN } from "../constants"; type ThenArg = T extends Promise ? U : T; export type PromptEnvironmentDef = ThenArg< @@ -8,6 +8,19 @@ export type PromptEnvironmentDef = ThenArg< export const promptEnvironment = async () => { const dbPrompt = await inquirer.prompt([ + { + name: "projectType", + message: "What type of project you want to create?", + type: "list", + choices: [ + { + name: LARAVEL + }, + { + name: LUMEN + } + ] + }, { name: "webPort", message: "What port would you like the app to be hosted on locally?", diff --git a/src/actions/publish-environment.ts b/src/actions/publish-environment.ts index 333ba91..bf97032 100644 --- a/src/actions/publish-environment.ts +++ b/src/actions/publish-environment.ts @@ -4,7 +4,7 @@ import * as jsyaml from "js-yaml"; import * as path from "path"; import * as shelljs from "shelljs"; import { PromptEnvironmentDef, readFileAsync, writeFileAsync } from "."; -import { POSTGRES } from "../constants"; +import { POSTGRES, LARAVEL } from "../constants"; import { DbServiceConfig, makeMySqlService, @@ -12,6 +12,7 @@ import { } from "../services"; import { mysqlDriverInstall, pSqlDriverInstall } from "./dockerfile-config"; import { UpEnvironmentConfig } from "../types"; +import { resolvePath } from "./utility"; export const publishEnvironment = async ( location: string, @@ -22,7 +23,10 @@ export const publishEnvironment = async ( const sourceEnvPath = path.join(baseEnvPath, "resources"); const dockerComposeContents = await readFileAsync(srcDockerComposePath); - shelljs.cd(location); + + const realLocation = resolvePath(location); + + shelljs.cd(realLocation); const fullProjectPath = shelljs.pwd().toString(); @@ -48,14 +52,20 @@ export const publishEnvironment = async ( switch (envConfig.engine) { case POSTGRES: dbService = makePostgresService(dbConfig); - await pSqlDriverInstall(location); + await pSqlDriverInstall(realLocation, envConfig.projectType); break; default: dbService = makeMySqlService(dbConfig); - await mysqlDriverInstall(location); + await mysqlDriverInstall(realLocation, envConfig.projectType); break; } + if (envConfig.projectType !== LARAVEL) { + composeYaml["services"]["app"]["build"][ + "dockerfile" + ] = `environment/dockerfiles/${envConfig.projectType}.dockerfile`; + } + if (envConfig.webPort != 8080) { composeYaml["services"]["web"]["ports"][0] = `${envConfig.webPort}:80`; } diff --git a/src/actions/utility.ts b/src/actions/utility.ts index 0ffec82..b0d9a92 100644 --- a/src/actions/utility.ts +++ b/src/actions/utility.ts @@ -1,6 +1,7 @@ import chalk from "chalk"; import * as fs from "fs"; import * as shelljs from "shelljs"; +import * as os from "os"; import { promisify } from "util"; export const readFileAsync = promisify(fs.readFile); @@ -34,3 +35,14 @@ export const replaceFileContent = async ( .replace(replaceArgs[0], replaceArgs[1]); await writeFileAsync(filePath, newContent); }; + +/** + * From https://stackoverflow.com/a/57243075/717643 + */ +export const resolvePath = (filePath: string) => { + // '~/folder/path' or '~' + if (filePath[0] === "~" && (filePath[1] === "/" || filePath.length === 1)) { + return filePath.replace("~", os.homedir()); + } + return filePath; +}; diff --git a/src/commands/new.ts b/src/commands/new.ts index cf14888..861be77 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -62,7 +62,9 @@ export default class New extends BaseCommand { shelljs.exec( `docker container run --rm ${ !this.config.windows ? "--user $(id -u):$(id -g)" : "" - } -v ${directory}:/app composer create-project --prefer-dist laravel/laravel ${projectName}`, + } -v ${directory}:/app composer create-project --prefer-dist laravel/${ + dbPrompt.projectType + } ${projectName}`, { silent: !flags.verbose } ); diff --git a/src/constants.ts b/src/constants.ts index 8f9a737..f49e551 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -4,3 +4,5 @@ export const MARIA = "MariaDb"; export const DOCKER_DB_CONFIG_INJECT = "DOCKER_DB_CONFIG_INJECT"; export const VERBOSE_DESCRIPTION = "Include additional diagnostic logs"; export const DB_DRIVER_REPLACE_STRING = "# "; +export const LARAVEL = "laravel"; +export const LUMEN = "lumen";