Skip to content
This repository has been archived by the owner on Feb 28, 2020. It is now read-only.

Commit

Permalink
fix(java): use glob to copy files without reading them
Browse files Browse the repository at this point in the history
  • Loading branch information
germanattanasio committed Jul 23, 2018
1 parent 0a67057 commit bd91fce
Show file tree
Hide file tree
Showing 16 changed files with 5,259 additions and 2,145 deletions.
15 changes: 12 additions & 3 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ env:
mocha: true

rules:
strict: 2
no-var: 2
no-console: 0
indent: ["error", "tab", { "SwitchCase": 1}]

indent:
- error
- tab
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
extends: eslint:recommended

plugins: [ejs]
160 changes: 82 additions & 78 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,56 @@
'use strict';

const Log4js = require('log4js');
const logger = Log4js.getLogger("generator-usecase-enablement");
const Bundle = require("./../../package.json")
const logger = Log4js.getLogger('generator-usecase-enablement');
const Bundle = require('./../../package.json');
const Shell = require('shelljs');
const Guid = require('guid');
const fs = require('fs');
const Request = require('request');
const Q = require('q');
const Generator = require('yeoman-generator');

const OPTION_BLUEMIX = "bluemix";
const OPTION_STARTER = "starter";
const OPTION_STARTER_OPTIONS = "starterOptions";
const DEFAULT_LOG_LEVEL = "INFO";
const OPTION_BLUEMIX = 'bluemix';
const OPTION_STARTER = 'starter';
const OPTION_STARTER_OPTIONS = 'starterOptions';
const DEFAULT_LOG_LEVEL = 'INFO';
const DEFAULT_LOCAL_SRC = '../../src';

module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
this._setLoggerLevel();
logger.debug(">>> constructor");
logger.info("Package info ::", Bundle.name, Bundle.version);
logger.debug('>>> constructor');
logger.info('Package info ::', Bundle.name, Bundle.version);

//logger.debug(this.options);

this.option(OPTION_BLUEMIX, {
description: "Project configuration received from Scaffolder. Stringified JSON object.",
description: 'Project configuration received from Scaffolder. Stringified JSON object.',
type: String
});

this.option(OPTION_STARTER, {
description: "Starter configuration received from Scaffolder, as defined in blueprint.json.",
description: 'Starter configuration received from Scaffolder, as defined in blueprint.json.',
type: String
});

this.option(OPTION_STARTER_OPTIONS, {
description: "starterOptions from Scaffolder. Stringified JSON object",
description: 'starterOptions from Scaffolder. Stringified JSON object',
type: String
});

this.parentContext = opts.parentContext; //parent context if being composed by another generator
this.parentContext = opts.parentContext; //parent context if being composed by another generator
}

intializing() {
logger.debug(">>> initializing");
logger.debug('>>> initializing');
this._sanitizeOption(this.options, OPTION_BLUEMIX);
this._sanitizeOption(this.options, OPTION_STARTER);
this._sanitizeOption(this.options, OPTION_STARTER_OPTIONS);

let deferred = Q.defer();

let context = this.parentContext || {};
//add bluemix options from this.options to existing bluemix options on parent context
context[OPTION_BLUEMIX] = Object.assign(context[OPTION_BLUEMIX] || {}, this.options[OPTION_BLUEMIX]);
Expand All @@ -75,55 +75,57 @@ module.exports = class extends Generator {
context.loggerLevel = logger.level;
context.language = context.bluemix.backendPlatform.toLowerCase();
let localsrc = this.options[OPTION_STARTER_OPTIONS];
if(typeof (localsrc) !== "object") {
if (typeof (localsrc) !== 'object') {
context.localsrc = (typeof localsrc === 'string') && localsrc.length ? localsrc : DEFAULT_LOCAL_SRC;
}

this._getStarterKitSourcesPath(this, context)
.then((starterKitSourcesPath) => {
try {
fs.accessSync(starterKitSourcesPath);
logger.info("StarterKit sources found at", starterKitSourcesPath);
logger.info('StarterKit sources found at', starterKitSourcesPath);
} catch (e) {
logger.fatal("Can't find StarterKit sources at", starterKitSourcesPath);
return deferred.reject("Can't find StarterKit sources at " + starterKitSourcesPath);
logger.fatal('Can\'t find StarterKit sources at', starterKitSourcesPath);
return deferred.reject('Can\'t find StarterKit sources at ' + starterKitSourcesPath);
}
context.starterKitSourcesPath = starterKitSourcesPath;

// Load EJS exclude patterns
try {
let blueprintJson = this.fs.readJSON(starterKitSourcesPath + "/../blueprint.json");
let blueprintJson = this.fs.readJSON(starterKitSourcesPath + '/../blueprint.json');
this.ejsExcludePatterns = blueprintJson.ejsExcludePatterns;
logger.debug("Loaded ejsExcludePatterns", this.ejsExcludePatterns);
logger.debug('Loaded ejsExcludePatterns', this.ejsExcludePatterns);
} catch (e) {
logger.warn("Failed to retrieve ejsExcludePatterns from blueprint.json");
logger.warn('Failed to retrieve ejsExcludePatterns from blueprint.json');
}
context.ejsExcludePatterns = this.ejsExcludePatterns || [];
logger.debug("context.ejsExcludePatterns === ", context.ejsExcludePatterns);
logger.debug('context.ejsExcludePatterns === ', context.ejsExcludePatterns);

let languageGeneratorPath;
switch (context.language) {
case "node":
languageGeneratorPath = '../language-node-express';
break;
case "python":
languageGeneratorPath = '../language-python-flask';
break;
case "java":
languageGeneratorPath = '../language-java';
context.language = 'java-liberty';
break;
case "spring":
languageGeneratorPath = '../language-java';
context.language = 'java-spring';
break;
case "swift":
languageGeneratorPath = '../language-swift-kitura';
break;
case 'node':
languageGeneratorPath = '../language-node-express';
break;
case 'python':
languageGeneratorPath = '../language-python-flask';
break;
case 'java':
languageGeneratorPath = '../language-java';
context.language = 'java-liberty';
break;
case 'spring':
languageGeneratorPath = '../language-java';
context.language = 'java-spring';
break;
case 'swift':
languageGeneratorPath = '../language-swift-kitura';
break;
}

logger.info("Composing with", languageGeneratorPath)
this.composeWith(require.resolve(languageGeneratorPath), {context: context});
logger.info('Composing with', languageGeneratorPath);
this.composeWith(require.resolve(languageGeneratorPath), {
context: context
});
deferred.resolve();
})
.fail((err) => {
Expand All @@ -134,24 +136,24 @@ module.exports = class extends Generator {
}

configuring() {
logger.debug(">>> configuring");
logger.debug('>>> configuring');
}

writing() {
logger.debug(">>> writing");
logger.debug('>>> writing');
}

end() {
logger.debug(">>> end");
logger.debug('>>> end');
if (this.tempDirectoryPath) {
logger.info("Deleting temp directory", this.tempDirectoryPath);
logger.info('Deleting temp directory', this.tempDirectoryPath);
Shell.rm('-rf', this.tempDirectoryPath);
}
}

_getStarterKitSourcesPath(generator, generationContext) {
logger.debug(">>> _getStarterKitSourcesPath");
logger.debug("context.localsrc ==", generationContext.localsrc);
logger.debug('>>> _getStarterKitSourcesPath');
logger.debug('context.localsrc ==', generationContext.localsrc);
let deferred = Q.defer();

let starterKitSourcesPath;
Expand All @@ -161,53 +163,55 @@ module.exports = class extends Generator {
} else {
let downloadURL = generationContext[OPTION_STARTER_OPTIONS].downloadURL;
let authToken = generationContext[OPTION_STARTER_OPTIONS].objectStorageAuthToken;
logger.info("Loading remote starterkit from", downloadURL);
let tempDirectoryPath = (process.env.TMPDIR ? process.env.TMPDIR : "/tmp/") + Guid.raw();
logger.info('Loading remote starterkit from', downloadURL);
let tempDirectoryPath = (process.env.TMPDIR ? process.env.TMPDIR : '/tmp/') + Guid.raw();
generator.tempDirectoryPath = tempDirectoryPath; // Save for eventual clean up
Shell.mkdir(tempDirectoryPath);
let tempFilePath = tempDirectoryPath + "/starterkit.zip";
logger.debug("tempFilePath ::", tempFilePath);
let tempFilePath = tempDirectoryPath + '/starterkit.zip';
logger.debug('tempFilePath ::', tempFilePath);

let responseContentLength;
let receivedContentLength = 0;
Request
.get(downloadURL, {
headers: {
"X-Auth-Token": authToken
'X-Auth-Token': authToken
}
})
.on('request', () => {
logger.debug('Starting download')
logger.debug('Starting download');
})
.on('response', (resp) => {
logger.debug('Got response with statusCode', resp.statusCode)
responseContentLength = parseInt(resp.headers["content-length"]);
logger.debug('Got response with statusCode', resp.statusCode);
responseContentLength = parseInt(resp.headers['content-length']);
if (resp.statusCode !== 200) {
return deferred.reject("Failed to download StarterKit from " + downloadURL);
return deferred.reject('Failed to download StarterKit from ' + downloadURL);
}
})
.on('data', (data) => {
receivedContentLength += data.length;
let downloadedPercent = parseInt(receivedContentLength / responseContentLength * 100) + "%";
let downloadedPercent = parseInt(receivedContentLength / responseContentLength * 100) + '%';
logger.debug('Downloaded', receivedContentLength, downloadedPercent);
})
.on('error', (err) => {
console.error("Failed to download", err);
return deferred.reject("Failed to download StarterKit from " + downloadURL);
console.error('Failed to download', err);
return deferred.reject('Failed to download StarterKit from ' + downloadURL);
})
.pipe(fs.createWriteStream(tempFilePath))
.on('close', () => {
logger.info("Download complete");
logger.info('Download complete');
if (!generator.fs.exists(tempFilePath)) {
logger.fatal("Failed to download StarterKit from", downloadURL);
return deferred.reject("Failed to download StarterKit from " + downloadURL);
logger.fatal('Failed to download StarterKit from', downloadURL);
return deferred.reject('Failed to download StarterKit from ' + downloadURL);
} else {
logger.info("Successfully downloaded StarterKit from", downloadURL);
logger.info('Successfully downloaded StarterKit from', downloadURL);
}
Shell.exec("unzip " + tempFilePath + " -d " + tempDirectoryPath, {async: true}, () => {
Shell.exec('unzip ' + tempFilePath + ' -d ' + tempDirectoryPath, {
async: true
}, () => {
Shell.rm(tempFilePath);
tempDirectoryPath += "/" + Shell.ls(tempDirectoryPath).stdout.trim();
starterKitSourcesPath = tempDirectoryPath + "/src";
tempDirectoryPath += '/' + Shell.ls(tempDirectoryPath).stdout.trim();
starterKitSourcesPath = tempDirectoryPath + '/src';
return deferred.resolve(starterKitSourcesPath);
});
});
Expand All @@ -216,38 +220,38 @@ module.exports = class extends Generator {
return deferred.promise;
}

_setLoggerLevel(){
_setLoggerLevel() {
let level = (process.env.GENERATOR_LOG_LEVEL || DEFAULT_LOG_LEVEL).toUpperCase();
logger.info("Setting log level to", level);
/* istanbul ignore else */ //ignore for code coverage as the else block will set a known valid log level
if(Log4js.levels.hasOwnProperty(level)) {
logger.info('Setting log level to', level);
/* istanbul ignore else */ //ignore for code coverage as the else block will set a known valid log level
if (Log4js.levels.hasOwnProperty(level)) {
logger.setLevel(Log4js.levels[level]);
} else {
logger.warn("Invalid log level specified (using default) : " + level);
logger.warn('Invalid log level specified (using default) : ' + level);
logger.setLevel(DEFAULT_LOG_LEVEL.toUpperCase());
}
}

_sanitizeOption(options, name) {
let optionValue = options[name];
if (!optionValue) {
return logger.warn("Missing", name, "parameter");
return logger.warn('Missing', name, 'parameter');
}

if (optionValue.indexOf("file:") === 0) {
let fileName = optionValue.replace("file:", "");
let filePath = this.destinationPath("./" + fileName);
logger.info("Reading", name, "parameter from local file", filePath);
if (optionValue.indexOf('file:') === 0) {
let fileName = optionValue.replace('file:', '');
let filePath = this.destinationPath('./' + fileName);
logger.info('Reading', name, 'parameter from local file', filePath);
this.options[name] = this.fs.readJSON(filePath);
return;
}

try {
this.options[name] = typeof(this.options[name]) === "string" ?
this.options[name] = typeof (this.options[name]) === 'string' ?
JSON.parse(this.options[name]) : this.options[name];
} catch (e) {
logger.error(e);
throw name + " parameter is expected to be a valid stringified JSON object";
throw name + ' parameter is expected to be a valid stringified JSON object';
}
}
};
Loading

0 comments on commit bd91fce

Please sign in to comment.