-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffold): support eureka integration
- Loading branch information
Showing
15 changed files
with
240 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters