SerializerFileSystem: Rename to Serializer

This commit is contained in:
ac360 2016-03-14 19:01:29 -07:00
parent ebf084fa63
commit 22c4af6b90
12 changed files with 82 additions and 224 deletions

View File

@ -1,182 +0,0 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
BbPromise = require('bluebird'),
path = require('path'),
_ = require('lodash'),
fs = require('fs');
let supportedRuntimes = {
"nodejs": require('./RuntimeNode'),
"python2.7": require('./RuntimePython27')
};
let SUtils;
class Component extends SerializerFileSystem {
/**
* Constructor
*/
constructor(S, data, filePath) {
super(S);
SUtils = S.utils;
this._S = S;
this._class = 'Component';
this._config = config || {};
// Default Properties
this.name = data.name || 'component' + SUtils.generateShortId(6);
this.setRuntime(data.runtime || 'nodejs');
this.custom = {};
this.templates = new this._S.classes.Templates(this._S, this); //TODO: add filepath
this._filePath = filePath || this.getProject().getRootPath(this.name);
if (data) this.fromObject(data);
}
static getSupportedRuntimes() {
return supportedRuntimes;
}
updateConfig(config) {
if (config) this._config = _.merge(this._config, config);
}
load() {
return this.deserialize(this);
}
save(options) {
return this.serialize(this, options);
}
toObject() {
return SUtils.exportObject(_.cloneDeep(this));
}
toObjectPopulated(options) {
options = options || {};
// Validate: Check Stage & Region
if (!options.stage || !options.region) throw new SError('Both "stage" and "region" params are required');
// Validate: Check project path is set
if (!this._S.hasProject()) throw new SError('Function could not be populated because no project path has been set on Serverless instance');
let obj = this.toObject();
// Populate Sub-Assets Separately
let functions;
if (this.functions) {
functions = _.mapValues(this.functions, (f) => f.toObjectPopulated(options));
delete obj.functions;
}
// Merge templates
let templates = _.merge(this.getProject().getTemplates().toObject(),
this.getTemplates().toObject());
// Populate
let populated = SUtils.populate(this.getProject(), templates, obj, options.stage, options.region);
if (functions) populated.functions = functions;
return populated;
}
fromObject(data) {
// Flush data
this.functions = {};
if(data.runtime) {
this.setRuntime(data.runtime);
}
if (data.functions) {
let temp = {};
for (let f of Object.keys(data.functions)) {
if (this.functions[f]) {
temp[f] = this.functions[f].fromObject(data.functions[f]);
} else {
temp[f] = new this._S.classes.Function(this._S, this, data.functions[f]);
}
}
delete data.functions;
this.functions = temp;
}
if (data.templates) {
this.templates.fromObject(data.templates);
delete data.templates;
}
_.assign(this, data);
return this;
}
setTemplates(templates) {
this.templates = templates;
}
getTemplates() {
return this.templates;
}
getName(){
return this.name;
}
getRuntime() {
return this._runtime;
}
setRuntime( runtimeName ) {
let runtime = supportedRuntimes[ runtimeName ];
if (runtime) {
this.runtime = runtimeName;
this._runtime = new runtime( this._S );
} else {
throw new SError( `Runtime ${runtimeName} is not supported!` );
}
}
getProject() {
return this._project;
}
getAllFunctions() {
return _.values( this.functions );
}
getAllEndpoints() {
return _.flatten( _.map( this.getAllFunctions(), f => f.getAllEndpoints() ) );
}
setFunction( func ){
this.functions[ func.name ] = func;
}
getFilePath() {
return this._filePath;
}
getRootPath() {
let args = _.toArray( arguments );
args.unshift(path.dirname(this.getFilePath()));
return path.join.apply( path, args );
}
static validateName(name) {
return /^[a-zA-Z\d]+$/.test(name);
}
}
module.exports = Component;

View File

@ -1,7 +1,7 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
const SError = require('./Error'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
path = require('path'),
fs = require('fs'),
@ -9,7 +9,7 @@ const SError = require('./Error'),
let SUtils;
class Endpoint extends SerializerFileSystem {
class Endpoint extends Serializer {
constructor(S, func, data) {

View File

@ -1,7 +1,7 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
path = require('path'),
fs = require('fs'),
@ -9,7 +9,7 @@ const SError = require('./Error'),
let SUtils;
class Event extends SerializerFileSystem {
class Event extends Serializer {
constructor(S, func, data) {

View File

@ -1,7 +1,7 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
const SError = require('./Error'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
async = require('async'),
path = require('path'),
@ -10,7 +10,7 @@ const SError = require('./Error'),
let SUtils;
class Function extends SerializerFileSystem {
class Function extends Serializer {
constructor(S, data, filePath) {

View File

@ -1,7 +1,7 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
path = require('path'),
_ = require('lodash'),
@ -10,7 +10,7 @@ const SError = require('./Error'),
let SUtils;
class Project extends SerializerFileSystem {
class Project extends Serializer {
constructor(S, data) {

View File

@ -1,13 +1,13 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
_ = require('lodash');
let SUtils;
class Region extends SerializerFileSystem {
class Region extends Serializer {
constructor(S, stage, data) {
super(S);

View File

@ -1,14 +1,14 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
fs = require('fs'),
_ = require('lodash'),
BbPromise = require('bluebird');
let SUtils;
class Resources extends SerializerFileSystem {
class Resources extends Serializer {
constructor(S, data, filePath) {

View File

@ -1,6 +1,6 @@
'use strict';
const SError = require('./Error'),
const SError = require('./Error'),
SCli = require('./utils/cli'),
RuntimeBase = require('./RuntimeBase'),
BbPromise = require('bluebird'),
@ -14,13 +14,14 @@ const SError = require('./Error'),
/**
* Pip install using prefix strategy (not virtualenv), requires a modern `pip` version
*/
function pipPrefixInstall(requirements, dir) {
if (exec(`pip install -t "${dir}" -r "${requirements}"`, { silent: false }).code !== 0) {
throw new SError(`Error executing pip install on ${dir}`, SError.errorCodes.UNKNOWN);
}
process.chdir(process.cwd());
};
}
let SUtils;
@ -31,15 +32,9 @@ class ServerlessRuntimePython27 extends RuntimeBase {
SUtils = S.utils;
}
installDepedencies( dir ) {
SCli.log("Installing default python dependencies with pip...");
SCli.log(`-----------------`);
pipPrefixInstall(
this.S.getProject().getRootPath( dir, 'requirements.txt'),
this.S.getProject().getRootPath( dir, 'vendored')
);
SCli.log(`-----------------`);
}
/**
* Scaffold
*/
scaffold(func) {
const handlerPath = path.join(this.S.getServerlessPath(), 'templates', 'python2.7', 'handler.py');
@ -51,14 +46,18 @@ class ServerlessRuntimePython27 extends RuntimeBase {
]));
}
/**
* Run
*/
run(func) {
return SUtils
.readFile(func.getRootPath('event.json'))
.then((functionEvent) => {
const handlerArr = func.handler.split('/').pop().split('.'),
functionFile = func.getRootPath(handlerArr[0] + '.py'),
functionHandler = handlerArr[1],
result = {};
functionFile = func.getRootPath(handlerArr[0] + '.py'),
functionHandler = handlerArr[1],
result = {};
const childArgs = [
'--event', JSON.stringify(functionEvent),
@ -96,18 +95,46 @@ class ServerlessRuntimePython27 extends RuntimeBase {
});
}
/**
* Build
* - Build the function in this runtime
*/
build(func, stage, region) {
// Validate
if (!func._class || func._class !== 'Function') return BbPromise.reject(new SError('A function instance is required'));
let pathDist;
return this.createDistDir(func.name)
.then(function(distDir) { pathDist = distDir })
.then(() => this.copyFunction(func, pathDist, stage, region))
.then(() => this._addEnvVarsInline(func, pathDist, stage, region))
.then(() => this.generatePaths(func, pathDist));
}
/**
* Get Handler
*/
getHandler(func) {
return path.join(path.dirname(func.handler), "_serverless_handler.handler").replace(/\\/g, '/');
}
_afterCopyDir(func, pathDist, stage, region) {
/**
* Add ENV Vars In-line
* - Adds a new handler that loads in ENV vars before running the main handler
*/
_addEnvVarsInline(func, pathDist, stage, region) {
return this.getEnvVars(func, stage, region)
.then(envVars => {
const handlerArr = func.handler.split('.'),
handlerDir = path.dirname(func.handler),
handlerFile = handlerArr[0].split('/').pop(),
handlerMethod = handlerArr[1];
const handlerArr = func.handler.split('.'),
handlerDir = path.dirname(func.handler),
handlerFile = handlerArr[0].split('/').pop(),
handlerMethod = handlerArr[1];
let loader = ['import os'];
loader = loader.concat(_.map(envVars, (value, key) => `os.environ['${key}'] = str('${value}')`));
@ -117,6 +144,20 @@ class ServerlessRuntimePython27 extends RuntimeBase {
});
}
/**
* Install NPM Dependencies
*/
installDependencies(dir ) {
SCli.log("Installing default python dependencies with pip...");
SCli.log(`-----------------`);
pipPrefixInstall(
this.S.getProject().getRootPath( dir, 'requirements.txt'),
this.S.getProject().getRootPath( dir, 'vendored')
);
SCli.log(`-----------------`);
}
}
module.exports = ServerlessRuntimePython27;

View File

@ -10,11 +10,10 @@ const SError = require('./Error'),
let SUtils;
class SerializerFileSystem {
class Serializer {
constructor(S) {
this._S = S;
this._projectPath = S.config.projectPath;
this._class = this.constructor.name;
SUtils = S.utils;
}
@ -476,7 +475,7 @@ class SerializerFileSystem {
}
}
module.exports = SerializerFileSystem;
module.exports = Serializer;
/**
* Globber

View File

@ -1,13 +1,13 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
BbPromise = require('bluebird'),
_ = require('lodash');
let SUtils;
class Stage extends SerializerFileSystem {
class Stage extends Serializer {
constructor(S, project, data) {

View File

@ -1,7 +1,7 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
fs = require('fs'),
path = require('path'),
_ = require('lodash'),
@ -9,7 +9,7 @@ const SError = require('./Error'),
let SUtils;
class Templates extends SerializerFileSystem {
class Templates extends Serializer {
constructor(S, data, filePath) {
super(S);

View File

@ -1,14 +1,14 @@
'use strict';
const SError = require('./Error'),
SerializerFileSystem = require('./SerializerFileSystem'),
Serializer = require('./Serializer'),
fs = require('fs'),
_ = require('lodash'),
BbPromise = require('bluebird');
let SUtils;
class Variables extends SerializerFileSystem {
class Variables extends Serializer {
constructor(S, data, filePath) {
super(S);