<commit_before>/*
 Copyright 2017 IBM Corp.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const Generator = require('yeoman-generator');
let _ = require('lodash');
const Handlebars = require('../lib/helpers').handlebars;
const Utils = require('../lib/utils');

// suffix for other deployment
const DEPLOYMENT_SUFFIX = '.deploy.yaml';

// list of supporting deployments
const supportingStorageTypes = ['mongo'];

// storage directory
const STORAGE_DIR = 'storages/';

const portDefault = {
	java: {
		http: '9080',
		https: '9443'
	},
	spring: {
		http: '8080'
	},
	node: {
		http: '3000'
	},
	python: {
		http: '8080'
	},
	swift: {
		http: '8080'
	}
}

module.exports = class extends Generator {

	constructor(args, opts) {
		super(args, opts);

		if (typeof (opts.bluemix) === 'string') {
			this.bluemix = JSON.parse(opts.bluemix || '{}');
		} else {
			this.bluemix = opts.bluemix;
		}

		if(typeof (opts) === 'string'){
			this.opts = JSON.parse(opts || '{}');
		} else {
			this.opts = opts.cloudContext || opts;
		}

		this.opts.storageDeploys = {env: {}};
	}


	initializing() {
		this.fileLocations = {
			chart: {source : 'Chart.yaml', target : 'chartDir/Chart.yaml', process: true},
			deployment: {source : 'deployment.yaml', target : 'chartDir/templates/deployment.yaml', process: true},
			service: {source : 'service.yaml', target : 'chartDir/templates/service.yaml', process: false},
			hpa: {source : 'hpa.yaml', target : 'chartDir/templates/hpa.yaml', process: true},
			values: {source : 'values.yaml', target : 'chartDir/values.yaml', process: true}
		}
	}

	configuring() {
		// work out app name and language
		this.opts.language = _.toLower(this.bluemix.backendPlatform);
		if(this.opts.language === 'java' || this.opts.language === 'spring') {
			this.opts.applicationName = this.opts.appName || Utils.sanitizeAppName(this.bluemix.name);
		} else {
			this.opts.applicationName = Utils.sanitizeAppName(this.bluemix.name);
		}

		this.opts.storages = typeof(this.opts.storages) === 'string' ? JSON.parse(this.opts.storages || '[]') : this.opts.storages;
		if(this.opts.storages) {
			this.opts.storages.forEach(storage => {
				switch(storage) {
					case 'mongo' :
						this.opts.storageDeploys.env.MONGO_URL = "\"{{ .Values.services.mongo.url }}\"";
						this.opts.storageDeploys.env.MONGO_DB_NAME = "\"{{ .Values.services.mongo.name }}\"";
						break;
				}
			});
		}

		this.opts.servicePorts = {};
		//use port if passed in
		if(this.opts.port) {
			this.opts.servicePorts.http = this.opts.port;
		} else {
			this.opts.servicePorts.http = portDefault[this.opts.language].http;
			if(portDefault[this.opts.language].https) {
				this.opts.servicePorts.https = portDefault[this.opts.language].https;
			}
		}

		this.opts.repositoryURL='';
		if (this.bluemix.server) {
			// TODO(gib): Can we get this from scaffolder (this.bluemix) somehow?
			const namespace = this.bluemix.server.namespace ? this.bluemix.server.namespace : 'replace-me-namespace';
			this.opts.repositoryURL= `registry.${this.bluemix.server.domain}/${namespace}/`;
			this.opts.kubeClusterNamespace =
				this.bluemix.server.cloudDeploymentOptions && this.bluemix.server.cloudDeploymentOptions.kubeClusterNamespace ?
					this.bluemix.server.cloudDeploymentOptions.kubeClusterNamespace : 'default';
		} else {
			// TODO(gib): we seem to be hitting this, not sure how.<commit_msg>Use default domain if not set<commit_after>/*
 Copyright 2017 IBM Corp.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

'use strict';

const Generator = require('yeoman-generator');
let _ = require('lodash');
const Handlebars = require('../lib/helpers').handlebars;
const Utils = require('../lib/utils');

// suffix for other deployment
const DEPLOYMENT_SUFFIX = '.deploy.yaml';

// list of supporting deployments
const supportingStorageTypes = ['mongo'];

// storage directory
const STORAGE_DIR = 'storages/';

const portDefault = {
	java: {
		http: '9080',
		https: '9443'
	},
	spring: {
		http: '8080'
	},
	node: {
		http: '3000'
	},
	python: {
		http: '8080'
	},
	swift: {
		http: '8080'
	}
}

module.exports = class extends Generator {

	constructor(args, opts) {
		super(args, opts);

		if (typeof (opts.bluemix) === 'string') {
			this.bluemix = JSON.parse(opts.bluemix || '{}');
		} else {
			this.bluemix = opts.bluemix;
		}

		if(typeof (opts) === 'string'){
			this.opts = JSON.parse(opts || '{}');
		} else {
			this.opts = opts.cloudContext || opts;
		}

		this.opts.storageDeploys = {env: {}};
	}


	initializing() {
		this.fileLocations = {
			chart: {source : 'Chart.yaml', target : 'chartDir/Chart.yaml', process: true},
			deployment: {source : 'deployment.yaml', target : 'chartDir/templates/deployment.yaml', process: true},
			service: {source : 'service.yaml', target : 'chartDir/templates/service.yaml', process: false},
			hpa: {source : 'hpa.yaml', target : 'chartDir/templates/hpa.yaml', process: true},
			values: {source : 'values.yaml', target : 'chartDir/values.yaml', process: true}
		}
	}

	configuring() {
		// work out app name and language
		this.opts.language = _.toLower(this.bluemix.backendPlatform);
		if(this.opts.language === 'java' || this.opts.language === 'spring') {
			this.opts.applicationName = this.opts.appName || Utils.sanitizeAppName(this.bluemix.name);
		} else {
			this.opts.applicationName = Utils.sanitizeAppName(this.bluemix.name);
		}

		this.opts.storages = typeof(this.opts.storages) === 'string' ? JSON.parse(this.opts.storages || '[]') : this.opts.storages;
		if(this.opts.storages) {
			this.opts.storages.forEach(storage => {
				switch(storage) {
					case 'mongo' :
						this.opts.storageDeploys.env.MONGO_URL = "\"{{ .Values.services.mongo.url }}\"";
						this.opts.storageDeploys.env.MONGO_DB_NAME = "\"{{ .Values.services.mongo.name }}\"";
						break;
				}
			});
		}

		this.opts.servicePorts = {};
		//use port if passed in
		if(this.opts.port) {
			this.opts.servicePorts.http = this.opts.port;
		} else {
			this.opts.servicePorts.http = portDefault[this.opts.language].http;
			if(portDefault[this.opts.language].https) {
				this.opts.servicePorts.https = portDefault[this.opts.language].https;
			}
		}

		this.opts.repositoryURL='';
		if (this.bluemix.server) {
			// TODO(gib): Can we get this from scaffolder (this.bluemix) somehow?
			const namespace = this.bluemix.server.namespace ? this.bluemix.server.namespace : 'replace-me-namespace';
			const domain = this.bluemix.server.domain ? this.bluemix.server.domain : 'ng.bluemix.net';
			this.opts.repositoryURL= `registry.${domain}/${namespace}/`;
			this.opts.kubeClusterNamespace =
				this.bluemix.server.cloudDeploymentOptions && this.bluemix.server.cloudDeploymentOptions.kubeClusterNamespace ?
					this.bluemix.server.cloudDeploymentOptions.kubeClusterNamespace : 'default';
		} else {
			// TODO(gib): we seem to be hitting this, not sure how.
{'fraction_non_alphanumeric': 0.15168110035659704, 'fraction_numerical': 0.00815078960774325, 'mean_word_length': 3.800122249388753, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1660176', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2628, 'n_words': 575}