Skip to content

Commit

Permalink
feat(scaffold): support eureka integration
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Jul 1, 2019
1 parent 937bf9d commit cf2e7fd
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 17 deletions.
43 changes: 43 additions & 0 deletions generators/app/handler/application_yaml_th.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const AbstractTemplateHandler = require('yo-power-generator').AbstractTemplateHandler;
const _ = require('lodash')
const fileUtils = require('yo-power-generator').FileUtils;
const myFileUtils = require('../utils/file_utils');
const yaml = require('js-yaml');
const debug = require('debug')('generator:th:application_yaml')
const configurers = require('./configurer/configurers');

class ApplicationYamlTemplateHandler extends AbstractTemplateHandler {
_handle0 () {
const env = myFileUtils.extractApplicationYamlEnv(this.tmpl);
const tpl = _.template(this.generator.fs.read(this.generator.templatePath(this.tmpl)));
const content = tpl(this.props)
const yamlDoc = yaml.safeLoad(content) || {};

switch (this.props.discovery) {
case 'eureka': {
debug(`configure application-${env}.yaml for eureka`);
configurers.eureka.configureApplicationYaml(yamlDoc, env);
break;
}
// case 'zookeeper': {
// break;
// }
default: {
break;
}
}

const destTpl = _.template(fileUtils.tmplToFileName(this.tmpl));
this.generator.fs.write(
this.generator.destinationPath(destTpl(this.props)),
Object.keys(yamlDoc).length > 0 ? yaml.safeDump(yamlDoc) : '' // 避免空对象被渲染为字符串'{}'
)
}
}

module.exports = {
key: 'app_yml',
cls: ApplicationYamlTemplateHandler
};
14 changes: 14 additions & 0 deletions generators/app/handler/configurer/configurers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');
const path = require('path');

const configurers = {};

const files = fs.readdirSync(__dirname);
files.forEach(file => {
const obj = require(path.join(__dirname, file));
if (obj.key && obj.fn) {
configurers[obj.key] = obj.fn;
}
})

module.exports = configurers;
45 changes: 45 additions & 0 deletions generators/app/handler/configurer/eureka_configurer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const _ = require('lodash');

module.exports = {
key: 'eureka',
fn: {
configureProviderPomDependencies (optionalDependencies) {
optionalDependencies.push({
dependency: [
{ groupId: 'org.springframework.cloud' },
{ artifactId: 'spring-cloud-starter-netflix-eureka-client' }
]
})
},
configureApplicationYaml (yaml, env) {
switch (env) {
case 'local': {
_.merge(yaml, {
eureka: {
client: {
'fetch-registry': true,
'register-with-eureka': false
}
}
});
break;
}
case 'default': {
_.merge(yaml, {
eureka: {
client: {
'service-url': {
defaultZone: 'http://user:pass@127.0.0.1:8761/eureka/'
}
}
}
});
break;
}
default: {
break;
}
}
}
}
}
47 changes: 47 additions & 0 deletions generators/app/handler/provider_pom_th.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const AbstractTemplateHandler = require('yo-power-generator').AbstractTemplateHandler;
const _ = require('lodash')
const fileUtils = require('yo-power-generator').FileUtils;
const xml = require('xml');
const configurers = require('./configurer/configurers');
const debug = require('debug')('generator:th:provider_pom')
const prettifyXml = require('prettify-xml')

class ProviderPomTemplateHandler extends AbstractTemplateHandler {
_handle0 () {
const optionalDependencies = []

switch (this.props.discovery) {
case 'eureka': {
debug('configure provider pom dependencies for eureka');
configurers.eureka.configureProviderPomDependencies(optionalDependencies);
break;
}
// case 'zookeeper': {
// break;
// }
default: {
break;
}
}

const tpl = _.template(this.generator.fs.read(this.generator.templatePath(this.tmpl)));
const content = prettifyXml(tpl(_.merge(
{}, this.props, {
optionalDependencies: xml(optionalDependencies)
}
)), { indent: 4, newline: '\n' });

const destTpl = _.template(fileUtils.tmplToFileName(this.tmpl));
this.generator.fs.write(
this.generator.destinationPath(destTpl(this.props)),
content
)
}
}

module.exports = {
key: 'provider_pom',
cls: ProviderPomTemplateHandler
};
24 changes: 12 additions & 12 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ const obj = {
},
option: { desc: 'API文档', type: String, default: 'swagger2' }
},
// discovery: {
// prompting: {
// type: 'list',
// choices: [
// 'eureka',
// 'zookeeper',
// 'none'
// ],
// message: '请选择你使用的注册中心类型'
// },
// option: { desc: '注册中心', type: String, default: 'eureka' }
// },
discovery: {
prompting: {
type: 'list',
choices: [
'eureka',
// 'zookeeper',
'none'
],
message: '请选择你使用的注册中心类型'
},
option: { desc: '注册中心', type: String, default: 'eureka' }
}
// configservice: {
// prompting: {
// type: 'list',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
<artifactId>${artifactId}-api</artifactId>
<%='<version>${project.version}</version>'%>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
${optionalDependencies}
</dependencies>

<build>
Expand Down
10 changes: 10 additions & 0 deletions generators/app/utils/file_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
extractApplicationYamlEnv (name) {
const result = /application(-(\w+))?.*\.ya?ml/.exec(name);
if (result) {
const env = result[2];
return env || 'default';
}
return 'unknown';
}
}
32 changes: 32 additions & 0 deletions generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
const assert = require('yeoman-assert')
const helpers = require('yeoman-test')
const path = require('path')
const fs = require('fs');
const yaml = require('js-yaml');

describe('generate app', () => {
before(() => {
Expand Down Expand Up @@ -47,6 +49,7 @@ describe('generate app', () => {
it('should exists java files', () => {
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/StartupApplication.java')
assert.file('foo-service-provider/src/main/java/com/deepexi/foo/config/ApplicationConfiguration.java')
assert.file('foo-service-provider/pom.xml')
})

it('should exists resources files', () => {
Expand All @@ -64,3 +67,32 @@ describe('generate app', () => {
})
});
})

describe('optional dependencies', () => {
describe('eureka', () => {
before(() => {
return helpers
.run(path.join(__dirname, '../../app'))
.withPrompts({
groupId: 'com.deepexi',
artifactId: 'foo-service',
basePackage: 'com.deepexi.foo',
discovery: 'eureka'
})
.then(() => {
})
});

it('should have dependency', () => {
assert.fileContent([
['foo-service-provider/pom.xml', /<groupId>org.springframework.cloud<\/groupId>/],
['foo-service-provider/pom.xml', /<artifactId>spring-cloud-starter-netflix-eureka-client<\/artifactId>/]
])
});

it('should have properties', () => {
assert(yaml.safeLoad(fs.readFileSync('foo-service-provider/src/main/resources/application.yml')).eureka.client);
assert(yaml.safeLoad(fs.readFileSync('foo-service-provider/src/main/resources/application-local.yml')).eureka.client);
});
});
});
27 changes: 27 additions & 0 deletions generators/test/app/utils/file_utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-undef */

const fileUtils = require('../../../app/utils/file_utils');
const assert = require('assert');

describe('generators/app/utils/file_utils.test.js', () => {
describe('extractApplicationYamlEnv()', () => {
it('should pass on simple cases', () => {
assert(fileUtils.extractApplicationYamlEnv('application-local.yaml') === 'local');
assert(fileUtils.extractApplicationYamlEnv('application-prod.yaml') === 'prod');
});

it('should return default on env not specified', () => {
assert(fileUtils.extractApplicationYamlEnv('application.yaml') === 'default');
});

it('should pass on complex cases', () => {
assert(fileUtils.extractApplicationYamlEnv('application-local.tmpl.yaml') === 'local');
assert(fileUtils.extractApplicationYamlEnv('application.tmpl.yaml') === 'default');
});

it('should return unknown on match fail', () => {
assert(fileUtils.extractApplicationYamlEnv('app-local.yaml') === 'unknown');
assert(fileUtils.extractApplicationYamlEnv('app.local.yaml') === 'unknown');
});
});
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"yeoman-generator"
],
"dependencies": {
"debug": "^4.1.1",
"js-yaml": "^3.13.1",
"prettify-xml": "^1.2.0",
"xml": "^1.0.1",
"yeoman-generator": "^1.1.1",
"yo-power-generator": "^0.0.3"
},
Expand All @@ -25,7 +29,7 @@
},
"main": "index.js",
"scripts": {
"test": "mocha generators/test/**/*.test.js --reporter spec",
"test": "mocha generators/test/app/*.test.js generators/test/app/**/*.test.js --reporter spec",
"lint": "eslint .",
"cov": "nyc npm run test && nyc report --reporter=text-lcov > coverage.lcov"
},
Expand Down

0 comments on commit cf2e7fd

Please sign in to comment.