Skip to content

Commit

Permalink
Restructure dirs and get lampy stuff back into recipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
reynoldsalec committed Dec 20, 2023
1 parent 20e68ec commit e51f9bb
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 77 deletions.
15 changes: 15 additions & 0 deletions builders/laravel-mariadb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const _ = require('lodash');
const LandoMariadb = require('./../node_modules/@lando/mariadb/builders/mariadb.js');

// Builder
module.exports = {
name: 'laravel-mariadb',
parent: '_service',
builder: (parent, config) => class LaravelMariadb extends LandoMariadb.builder(parent, LandoMariadb.config) {
constructor(id, options = {}) {
super(id, options, {services: _.set({}, options.name)});
};
},
};
15 changes: 15 additions & 0 deletions builders/laravel-mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const _ = require('lodash');
const LandoMysql = require('./../node_modules/@lando/mysql/builders/mysql.js');

// Builder
module.exports = {
name: 'laravel-mysql',
parent: '_service',
builder: (parent, config) => class LaravelMysql extends LandoMysql.builder(parent, LandoMysql.config) {
constructor(id, options = {}) {
super(id, options, {services: _.set({}, options.name)});
};
},
};
18 changes: 18 additions & 0 deletions builders/laravel-nginx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const _ = require('lodash');
const PhpNginx = require('@lando/php/builders/php-nginx.js');

// Builder
module.exports = {
name: 'laravel-nginx',
parent: '_webserver',
config: {
version: '1.25',
},
builder: (parent, config) => class LaravelNginx extends PhpNginx.builder(parent, PhpNginx.config) {
constructor(id, options = {}) {
super(id, _.merge({}, config, options), {services: _.set({}, options.name)});
};
},
};
32 changes: 32 additions & 0 deletions builders/laravel-php.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const landoPhpPath = path.join(__dirname, '../node_modules/@lando/php');
const LandoPhp = require(`${landoPhpPath}/builders/php.js`);

const loadScripts = options => {
const lando = _.get(options, '_app._lando');
// Move the script to the confDir and make executable.
if (fs.existsSync(path.join(landoPhpPath, 'scripts'))) {
const confDir = path.join(lando.config.userConfRoot, 'scripts');
const dest = lando.utils.moveConfig(path.join(landoPhpPath, 'scripts'), confDir);
lando.utils.makeExecutable(fs.readdirSync(dest), dest);
lando.log.debug('automoved scripts from %s to %s and set to mode 755',
path.join(landoPhpPath, 'scripts'), confDir);
}
};

// Builder
module.exports = {
name: 'laravel-php',
parent: '_appserver',
builder: (parent, config) => class LaravelPhp extends LandoPhp.builder(parent, LandoPhp.config) {
constructor(id, options = {}, factory) {
loadScripts(options);
options.nginxServiceType = 'laravel-nginx';
super(id, options, factory);
};
},
};
15 changes: 15 additions & 0 deletions builders/laravel-postgres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const _ = require('lodash');
const LandoPostgres = require('./../node_modules/@lando/postgres/builders/postgres.js');

// Builder
module.exports = {
name: 'laravel-postgres',
parent: '_service',
builder: (parent, config) => class LaravelPostgres extends LandoPostgres.builder(parent, LandoPostgres.config) {
constructor(id, options = {}) {
super(id, options, {services: _.set({}, options.name)});
};
},
};
15 changes: 15 additions & 0 deletions builders/laravel-redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const _ = require('lodash');
const LandoRedis = require('@lando/redis/builders/redis.js');

// Builder
module.exports = {
name: 'laravel-redis',
parent: '_service',
builder: (parent, config) => class LaravelRedis extends LandoRedis.builder(parent, LandoRedis.config) {
constructor(id, options = {}) {
super(id, options, {services: _.set({}, options.name)});
};
},
};
263 changes: 263 additions & 0 deletions builders/laravel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
'use strict';

// Modules
const fs = require('fs');
const _ = require('lodash');
const path = require('path');

/*
* Helper to get cache
*/
const getCache = cache => {
const version = _.includes(cache, ':') ? _.last(cache.split(':')) : false;
// Return redis
if (_.includes(cache, 'redis')) {
return {
type: version ? `laravel-redis:${version}` : 'laravel-redis',
portforward: true,
persist: true,
};
// Or memcached
} else if (_.includes(cache, 'memcached')) {
return {
type: version ? `laravel-memcached:${version}` : 'laravel-memcached',
portforward: true,
};
}
};

// Tooling defaults
const toolingDefaults = {
'composer': {
service: 'appserver',
cmd: 'composer --ansi',
},
'db-import <file>': {
service: ':host',
description: 'Imports a dump file into a database service',
cmd: '/helpers/sql-import.sh',
user: 'root',
options: {
'host': {
description: 'The database service to use',
default: 'database',
alias: ['h'],
},
'no-wipe': {
description: 'Do not destroy the existing database before an import',
boolean: true,
},
},
},
'db-export [file]': {
service: ':host',
description: 'Exports database from a database service to a file',
cmd: '/helpers/sql-export.sh',
user: 'root',
options: {
host: {
description: 'The database service to use',
default: 'database',
alias: ['h'],
},
stdout: {
description: 'Dump database to stdout',
},
},
},
'php': {
service: 'appserver',
cmd: 'php',
},
};

// Default DB cli commands
const mysqlCli = {
service: ':host',
description: 'Drops into a MySQL shell on a database service',
cmd: 'mysql -uroot',
options: {
host: {
description: 'The database service to use',
default: 'database',
alias: ['h'],
},
},
};
const postgresCli = {
service: ':host',
description: 'Drops into a psql shell on a database service',
cmd: 'psql -Upostgres',
user: 'root',
options: {
host: {
description: 'The database service to use',
default: 'database',
alias: ['h'],
},
},
};

/*
* Helper to get config defaults
*/
const getConfigDefaults = options => {
// Get the viaconf
if (_.startsWith(options.via, 'nginx')) options.defaultFiles.vhosts = 'default.conf.tpl';

// Get the default db conf
const dbConfig = _.get(options, 'database', 'mysql');
const database = _.first(dbConfig.split(':'));
const version = _.last(dbConfig.split(':')).substring(0, 2);
if (database === 'mysql' || database === 'mariadb') {
if (version === '8.') {
options.defaultFiles.database = 'mysql8.cnf';
} else {
options.defaultFiles.database = 'mysql.cnf';
}
}

// Verify files exist and remove if it doesn't
_.forEach(options.defaultFiles, (file, type) => {
if (!fs.existsSync(`${options.confDest}/${file}`)) {
delete options.defaultFiles[type];
}
});

// Return
return options.defaultFiles;
};

/*
* Helper to get services
*/
const getServices = options => ({
appserver: {
build_as_root_internal: options.build_root,
build_internal: options.build,
composer: options.composer,
composer_version: options.composer_version,
config: getServiceConfig(options),
run_as_root_internal: options.run_root,
ssl: true,
type: `laravel-php:${options.php}`,
via: options.via,
xdebug: options.xdebug,
webroot: options.webroot,
},
database: {
config: getServiceConfig(options, ['database']),
authentication: 'mysql_native_password',
type: `laravel-${options.database}`,
portforward: true,
creds: {
user: options.recipe,
password: options.recipe,
database: options.recipe,
},
},
});

/*
* Helper to get the phar build command
*/
const getDbTooling = database => {
// Make sure we strip out any version number
database = database.split(':')[0];
// Choose wisely
if (_.includes(['mysql', 'mariadb'], database)) {
return {mysql: mysqlCli};
} else if (database === 'postgres') {
return {psql: postgresCli};
} else if (database === 'mongo') {
return {mongo: {
service: 'database',
description: 'Drop into the mongo shell',
}};
}
};

/*
* Helper to get service config
*/
const getServiceConfig = (options, types = ['php', 'server', 'vhosts']) => {
const config = {};
_.forEach(types, type => {
if (_.has(options, `config.${type}`)) {
config[type] = options.config[type];
} else if (!_.has(options, `config.${type}`) && _.has(options, `defaultFiles.${type}`)) {
if (_.has(options, 'confDest')) {
config[type] = path.join(options.confDest, options.defaultFiles[type]);
}
}
});
return config;
};

/*
* Helper to get tooling
*/
const getTooling = options => _.merge({}, toolingDefaults, getDbTooling(options.database));

/*
* Build Laravel
*/
module.exports = {
name: 'laravel',
parent: '_recipe',
config: {
confSrc: path.resolve(__dirname, '..', 'config'),
config: {},
composer: {},
database: 'mysql',
defaultFiles: {
php: 'php.ini',
},
php: '7.4',
services: {appserver: {overrides: {environment: {
APP_LOG: 'errorlog',
}}}},
tooling: {laravel: {service: 'appserver'}},
via: 'apache',
webroot: '.',
xdebug: false,
},
builder: (parent, config) => class LandoLaravel extends parent {
constructor(id, options = {}) {
options = _.merge({}, config, options);
// Add the laravel cli installer command
if (options.php >= 7.3) {
options.composer['laravel/installer'] = '^4.0.0';
} else if (options.php < 7.2) {
options.composer['laravel/installer'] = '^2.3.0';
} else {
options.composer['laravel/installer'] = '^3.2.0';
}

if (_.has(options, 'cache') && options.cache !== 'none') {
options.services.cache = getCache(options.cache);
}

// Rebase on top of any default config we might already have
options.defaultFiles = _.merge({}, getConfigDefaults(_.cloneDeep(options)), options.defaultFiles);
options.services = _.merge({}, getServices(options), options.services);
options.tooling = _.merge({}, getTooling(options), options.tooling);
// Add in artisan tooling
// @NOTE: does artisan always live one up of the webroot?
options.tooling.artisan = {
service: 'appserver',
cmd: `php /app/${options.webroot}/../artisan`,
};

// Switch the proxy if needed
if (!_.has(options, 'proxyService')) {
if (_.startsWith(options.via, 'nginx')) options.proxyService = 'appserver_nginx';
else if (_.startsWith(options.via, 'apache')) options.proxyService = 'appserver';
}
options.proxy = _.set(options.proxy, options.proxyService, [`${options.app}.${options._app._config.domain}`]);

// Send downstream
super(id, _.merge({}, config, options));
};
},
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion examples/.lando.local.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
plugins:
"@lando/laravel": ./../../
"@lando/laravel": ./../../../
2 changes: 1 addition & 1 deletion examples/7.1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ lando poweroff

# Initialize an empty laravel recipe
rm -rf laravel && mkdir -p laravel && cd laravel
lando init --source cwd --recipe laravel --webroot app/public --name lando-laravel --option cache=redis --option php='7.1' --option composer_version='1-latest'
cp -f ../../.lando.local.yml .lando.local.yml && cat .lando.local.yml
lando init --source cwd --recipe laravel --webroot app/public --name lando-laravel --option cache=redis --option php='7.1' --option composer_version='1-latest'

# Should compose create-project a new laravel app
cd laravel
Expand Down
File renamed without changes.
Loading

0 comments on commit e51f9bb

Please sign in to comment.