-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
206 lines (189 loc) · 10.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const chalk = require('chalk');
const semver = require('semver');
const BaseGenerator = require('generator-jhipster/generators/generator-base');
const jhipsterConstants = require('generator-jhipster/generators/generator-constants');
const packagejs = require('../../package.json');
const utilYaml = require('./utilYaml.js');
const RABBITMQ = 'RabbitMQ';
const KAFKA = 'Kafka';
const DEFAULT_BROKER_TYPE = RABBITMQ;
const DEFAULT_RABBITMQ_MESSAGE_NAME = 'message';
module.exports = class extends BaseGenerator {
get initializing() {
return {
init(args) {
if (args === 'default') {
this.defaultOptions = true;
}
},
readConfig() {
this.jhipsterAppConfig = this.getAllJhipsterConfig();
if (!this.jhipsterAppConfig) {
this.error('Can\'t read .yo-rc.json');
}
},
displayLogo() {
// it's here to show that you can use functions from generator-jhipster
// this function is in: generator-jhipster/generators/generator-base.js
const logo = '\n'
+ `${chalk.bold.yellow(' ████ ████ \n')}`
+ `${chalk.bold.yellow(' ████ ████ \n')}`
+ `${chalk.bold.yellow(' ████ ████ \n')}`
+ `${chalk.bold.yellow(' ████ ████ \n')}`
+ `${chalk.bold.yellow(' ████████████████████\n')}`
+ `${chalk.bold.yellow(' ████████████████████\n')}`
+ `${chalk.bold.yellow(' ████████████ ████\n')}`
+ `${chalk.bold.yellow(' ████████████ ████\n')}`
+ `${chalk.bold.yellow(' ████████████████████\n')}`
+ `${chalk.bold.yellow(' ████████████████████')}`;
this.log(logo);
// Have Yeoman greet the user.
this.log(`\nWelcome to the ${chalk.bold.yellow('JHipster spring-cloud-stream')} generator! ${chalk.yellow(`v${packagejs.version}\n`)}`);
},
checkJhipster() {
const currentJhipsterVersion = this.jhipsterAppConfig.jhipsterVersion;
const minimumJhipsterVersion = packagejs.dependencies['generator-jhipster'];
if (!semver.satisfies(currentJhipsterVersion, minimumJhipsterVersion)) {
this.warning(`\nYour generated project used an old JHipster version (${currentJhipsterVersion})... you need at least (${minimumJhipsterVersion})\n`);
}
},
checkKafka() {
if (this.jhipsterAppConfig.messageBroker === 'kafka') {
this.error('You already have Kafka in your project, no need to add RabbitMQ.');
}
},
};
}
prompting() {
const done = this.async();
const prompts = [{
type: 'list',
name: 'messageBrokerType',
message: `Which ${chalk.yellow('*type*')} of message broker would you like to add?`,
choices: [
{
value: RABBITMQ,
name: 'RabbitMQ message broker (recommended for simple projects)'
},
// keep commented as it's not implemented yet
// {
// value: KAFKA,
// name: 'Kafka message broker (recommended for advanced projects) not implemented yet'
// }
],
store: true,
default: DEFAULT_BROKER_TYPE
},
{
when: response => response.messageBrokerType === RABBITMQ,
type: 'input',
name: 'rabbitMqNameOfMessage',
message: 'Please choose the name of the message class use by rabbit',
default: DEFAULT_RABBITMQ_MESSAGE_NAME,
store: true
}];
if (this.defaultOptions) {
this.messageBrokerType = DEFAULT_BROKER_TYPE;
this.rabbitMqNameOfMessage = DEFAULT_RABBITMQ_MESSAGE_NAME;
done();
} else {
this.prompt(prompts).then((props) => {
this.props = props;
// variable from questions
this.messageBrokerType = this.props.messageBrokerType;
if (this.props.messageBrokerType === RABBITMQ) {
this.rabbitMqNameOfMessage = this.props.rabbitMqNameOfMessage;
}
done();
});
}
}
writing() {
// function to use directly template
this.template = function (source, destination) {
this.fs.copyTpl(
this.templatePath(source),
this.destinationPath(destination),
this
);
};
switch (this.messageBrokerType) {
case RABBITMQ:
this.installRabbitMq();
break;
case KAFKA:
this.installKafka();
break;
default:
break;
}
}
end() {
this.log('End of spring-cloud-stream generator');
}
installRabbitMq() {
const STREAM_RABBIT_VERSION = '1.3.0.RELEASE';
const STREAM_CLOUD_DEPENDENCY_VERSION = 'Chelsea.SR2';
const STREAM_CLOUD_STREAM_VERSION = '1.3.0.RELEASE';
// read config from .yo-rc.json
this.baseName = this.jhipsterAppConfig.baseName;
this.packageName = this.jhipsterAppConfig.packageName;
this.packageFolder = this.jhipsterAppConfig.packageFolder;
this.clientFramework = this.jhipsterAppConfig.clientFramework;
this.clientPackageManager = this.jhipsterAppConfig.clientPackageManager;
this.buildTool = this.jhipsterAppConfig.buildTool;
// use function in generator-base.js from generator-jhipster
this.angularAppName = this.getAngularAppName();
// const webappDir = jhipsterConstants.CLIENT_MAIN_SRC_DIR;
this.log(`\nmessage broker type = ${this.messageBrokerType}`);
this.log(`\nmessage broker type = ${this.rabbitMqNameOfMessage}`);
this.log('------\n');
// use constants from generator-constants.js
const javaDir = `${jhipsterConstants.SERVER_MAIN_SRC_DIR + this.packageFolder}/`;
const resourceDir = jhipsterConstants.SERVER_MAIN_RES_DIR;
// add dependencies
if (this.buildTool === 'maven') {
if (typeof this.addMavenDependencyManagement === 'function') {
this.addMavenDependencyManagement('org.springframework.cloud', 'spring-cloud-stream-dependencies', STREAM_CLOUD_DEPENDENCY_VERSION, 'pom', 'import');
this.addMavenDependency('org.springframework.cloud', 'spring-cloud-stream');
this.addMavenDependency('org.springframework.cloud', 'spring-cloud-starter-stream-rabbit');
} else {
this.addMavenDependency('org.springframework.cloud', 'spring-cloud-stream', STREAM_CLOUD_STREAM_VERSION);
this.addMavenDependency('org.springframework.cloud', 'spring-cloud-starter-stream-rabbit', STREAM_RABBIT_VERSION);
}
} else if (this.buildTool === 'gradle') {
if (typeof this.addGradleDependencyManagement === 'function') {
this.addGradleDependencyManagement('mavenBom', 'org.springframework.cloud', 'spring-cloud-stream-dependencies', STREAM_CLOUD_DEPENDENCY_VERSION);
this.addGradleDependency('compile', 'org.springframework.cloud', 'spring-cloud-stream');
this.addGradleDependency('compile', 'org.springframework.cloud', 'spring-cloud-starter-stream-rabbit');
} else {
this.addGradleDependency('compile', 'org.springframework.cloud', 'spring-cloud-stream', STREAM_CLOUD_STREAM_VERSION);
this.addGradleDependency('compile', 'org.springframework.cloud', 'spring-cloud-starter-stream-rabbit', STREAM_RABBIT_VERSION);
}
}
// add docker-compose file
this.template('src/main/docker/_rabbitmq.yml', 'src/main/docker/rabbitmq.yml');
const messageName = this.rabbitMqNameOfMessage.charAt(0).toUpperCase() + this.rabbitMqNameOfMessage.slice(1);
this.rabbitMessageName = messageName;
this.rabbitMessageNameNonUcFirst = messageName.charAt(0).toLowerCase() + messageName.slice(1);
// add Java classes
this.template('src/main/java/package/domain/_JhiMessage.java', `${javaDir}domain/Jhi${this.rabbitMessageName}.java`);
this.template('src/main/java/package/service/stream/_MessageSink.java', `${javaDir}service/stream/${this.rabbitMessageName}Sink.java`);
this.template('src/main/java/package/web/rest/_MessageResource.java', `${javaDir}web/rest/${this.rabbitMessageName}Resource.java`);
this.template('src/main/java/package/config/_CloudMessagingConfiguration.java', `${javaDir}config/CloudMessagingConfiguration.java`);
// application-dev.yml
const yamlAppDevProperties = { };
utilYaml.updatePropertyInArray(yamlAppDevProperties, 'spring.cloud.stream.default.contentType', this, 'application/json');
utilYaml.updatePropertyInArray(yamlAppDevProperties, 'spring.cloud.stream.bindings.input.destination', this, 'topic-jhipster');
utilYaml.updatePropertyInArray(yamlAppDevProperties, 'spring.cloud.stream.bindings.output.destination', this, 'topic-jhipster');
utilYaml.updatePropertyInArray(yamlAppDevProperties, 'spring.cloud.stream.bindings.rabbit.bindings.output.producer.routingKeyExpression', this, 'headers.title');
utilYaml.updateYamlProperties(`${resourceDir}config/application-dev.yml`, yamlAppDevProperties, this);
// application-prod.yml
const yamlAppProdProperties = yamlAppDevProperties;
utilYaml.updatePropertyInArray(yamlAppProdProperties, 'spring.cloud.stream.bindings.rabbit.bindings.output.producer.routingKeyExpression', this, 'payload.title');
utilYaml.updateYamlProperties(`${resourceDir}config/application-prod.yml`, yamlAppProdProperties, this);
}
installKafka() {
this.log('Not implemented yet');
}
};