-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #428 from phovea/develop_refactoring
Develop refactoring
- Loading branch information
Showing
65 changed files
with
3,316 additions
and
1,991 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,25 @@ | ||
'use strict'; | ||
const BaseInitPluginGenerator = require('./BaseInitPluginGenerator'); | ||
const {basetype} = require('./config'); | ||
|
||
class BaseInitHybridGenerator extends BaseInitPluginGenerator { | ||
|
||
constructor(args, options) { | ||
super(args, options, basetype.HYBRID); | ||
} | ||
|
||
initializing() { | ||
// since just last in the hierarchy used, need to do super calls | ||
super.initializing(); | ||
} | ||
|
||
default() { | ||
return super.default(); | ||
} | ||
|
||
writing() { | ||
return super.writing(); | ||
} | ||
} | ||
|
||
module.exports = BaseInitHybridGenerator; |
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,95 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const fs = require('fs-extra'); | ||
const chalk = require('chalk'); | ||
const GeneratorUtils = require('../utils/GeneratorUtils'); | ||
const BasePhoveaGenerator = require('../base/BasePhoveaGenerator'); | ||
const config = require('./config'); | ||
|
||
class BaseInitPluginGenerator extends BasePhoveaGenerator { | ||
|
||
constructor(args, options, basetype) { | ||
super(args, options); | ||
this.type = path.basename(path.dirname(this.resolved)).substring(5); // init-web ... web | ||
this.basetype = basetype || config.basetype.WEB; | ||
// Make options available | ||
this.option('skipInstall'); | ||
this.option('noSamples'); | ||
this.option('useDefaults'); | ||
this.cwd = ''; | ||
} | ||
|
||
initializing() { | ||
if (this._isInvalidWorkspace()) { | ||
throw new Error(chalk.red('Execution failed, because a ".yo-rc.json" and ".yo-rc-workspace.json" file was found. If this directory is a workspace, please remove the ".yo-rc.json" file and try again.\n')); | ||
} | ||
|
||
this.composeWith(['phovea:_check-own-version', 'phovea:check-node-version']); | ||
|
||
this.config.defaults({ | ||
type: this.type | ||
}); | ||
} | ||
|
||
_isWorkspace() { | ||
return fs.existsSync(this.destinationPath('.yo-rc-workspace.json')); | ||
} | ||
|
||
_hasConfigFile() { | ||
return fs.existsSync(this.destinationPath('.yo-rc.json')); | ||
} | ||
|
||
/** | ||
* If there is both a `.yo-rc-workspace.json` and `.yo-rc.json` file in the current directory | ||
* the workspace is invalid and the generator cannot function properly. | ||
*/ | ||
_isInvalidWorkspace() { | ||
return this._isWorkspace() && this._hasConfigFile(); | ||
} | ||
|
||
/** | ||
* Create a subdirectory in the current directory. | ||
* Initialize the property cwd. | ||
* @param {string} dir Directory name. | ||
*/ | ||
async _createSubDir(dir) { | ||
if (this._isWorkspace() && this.cwd !== dir + '/') { | ||
this.cwd = dir + '/'; | ||
return GeneratorUtils.mkdir(dir); | ||
} | ||
return Promise.resolve(); | ||
} | ||
|
||
readmeAddon() { | ||
const f = this.templatePath('README.partial.md'); | ||
if (fs.existsSync(f)) { | ||
return this.fs.read(f); | ||
} | ||
return ''; | ||
} | ||
|
||
default() { | ||
this.composeWith('phovea:_init-' + this.basetype, { | ||
options: Object.assign({ | ||
readme: this.readmeAddon() + (this.options.readme ? `\n\n${this.options.readme}` : '') | ||
}, this.options), | ||
isWorkspace: this._isWorkspace() // inform the sub generator that the cwd is the workspace to avoid reading prompt default values from the workspace package.json | ||
}); | ||
} | ||
|
||
async writing() { | ||
const config = this.config.getAll(); | ||
await this._createSubDir(config.app || config.serviceName || config.name); | ||
if (fs.existsSync(this.templatePath('package.tmpl.json'))) { | ||
this._patchPackageJSON(config, null, null, null, this.cwd); | ||
} | ||
if (fs.existsSync(this.templatePath('_gitignore'))) { | ||
this.fs.copy(this.templatePath('_gitignore'), this.destinationPath(this.cwd + '.gitignore')); | ||
} | ||
|
||
this._writeTemplates(config, !this.options.noSamples, this.cwd); | ||
|
||
} | ||
} | ||
|
||
module.exports = BaseInitPluginGenerator; |
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,26 @@ | ||
'use strict'; | ||
|
||
const BaseInitPluginGenerator = require('./BaseInitPluginGenerator'); | ||
const {basetype} = require('./config'); | ||
|
||
class BaseInitServerGenerator extends BaseInitPluginGenerator { | ||
|
||
constructor(args, options) { | ||
super(args, options, basetype.PYTHON); | ||
} | ||
|
||
initializing() { | ||
// since just last in the hierarchy used, need to do super calls | ||
super.initializing(); | ||
} | ||
|
||
default() { | ||
return super.default(); | ||
} | ||
|
||
writing() { | ||
return super.writing(); | ||
} | ||
} | ||
|
||
module.exports = BaseInitServerGenerator; |
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,98 @@ | ||
'use strict'; | ||
const Generator = require('yeoman-generator'); | ||
const {merge, template} = require('lodash'); | ||
const path = require('path'); | ||
const glob = require('glob').sync; | ||
const fs = require('fs-extra'); | ||
const GeneratorUtils = require('../utils/GeneratorUtils'); | ||
|
||
class BasePhoveaGenerator extends Generator { | ||
|
||
/** | ||
* Modify package.json by passing the configuration. | ||
* | ||
* @param {object} config Current configuration | ||
* @param {*} unset | ||
* @param {*} extra | ||
* @param {*} replaceExtra | ||
* @param {string} cwd The directory from which the generator is being called, i.e., `tdp_core/`. | ||
* If cwd is provided than the `package.json` is going to be written to that subdirectory, otherwise to the current directory. | ||
*/ | ||
_patchPackageJSON(config, unset, extra, replaceExtra, cwd = '') { | ||
const pkg = this.fs.readJSON(this.destinationPath(cwd + 'package.json'), {}); | ||
let pkgPatch; | ||
if (fs.existsSync(this.templatePath('package.tmpl.json'))) { | ||
pkgPatch = JSON.parse(template(this.fs.read(this.templatePath('package.tmpl.json')))(config)); | ||
} else { | ||
pkgPatch = {}; | ||
} | ||
merge(pkg, pkgPatch); | ||
if (replaceExtra && extra) { | ||
Object.assign(pkg, extra); | ||
} else { | ||
merge(pkg, extra || {}); | ||
} | ||
|
||
(unset || []).forEach((d) => delete pkg[d]); | ||
|
||
this.fs.writeJSON(this.destinationPath(cwd + 'package.json'), pkg); | ||
} | ||
|
||
/** | ||
* Copies the template files to the current directory or to a subdirectory if `cwd` is provided. | ||
* @param {object} config Current configuration | ||
* @param {*} withSamples | ||
* @param {string} cwd The directory from which the generator is being called, i.e., `tdp_core/`. | ||
* If `cwd` is provided than the `package.json` is going to be written to that subdirectory, otherwise to the current directory. | ||
*/ | ||
_writeTemplates(config, withSamples, cwd = '') { | ||
const includeDot = { | ||
globOptions: { | ||
dot: true | ||
} | ||
}; | ||
|
||
const pattern = GeneratorUtils.stringifyAble(config); | ||
|
||
const copyTpl = (base, dbase, initialize_once) => { | ||
// see https://github.com/SBoudrias/mem-fs-editor/issues/25 | ||
// copyTpl doesn't support glob options | ||
const f = glob(base + '/**/*', { | ||
dot: true, | ||
nodir: true | ||
}); | ||
f.forEach((fi) => { | ||
const rel = path.relative(base, fi); | ||
if (!initialize_once || !fs.existsSync(this.destinationPath(cwd + dbase + rel))) { | ||
this.fs.copyTpl(fi, this.destinationPath(cwd + dbase + rel), pattern); | ||
} | ||
}); | ||
}; | ||
|
||
const copy = (prefix) => { | ||
if (fs.existsSync(this.templatePath(prefix + 'plain'))) { | ||
this.fs.copy(this.templatePath(prefix + 'plain/**/*'), this.destinationPath(cwd), includeDot); | ||
} | ||
|
||
const plainTemplatePath = this.templatePath(prefix + 'plain_initialize_once'); | ||
if (fs.existsSync(plainTemplatePath)) { | ||
copyTpl(plainTemplatePath, '', true); | ||
} | ||
|
||
copyTpl(this.templatePath(prefix + 'processed'), '', false); | ||
|
||
if (config.name) { | ||
if (fs.existsSync(this.templatePath(prefix + 'pluginname_plain'))) { | ||
this.fs.copy(this.templatePath(prefix + 'pluginname_plain/**/*'), this.destinationPath(cwd + config.name.toLowerCase() + '/'), includeDot); | ||
} | ||
copyTpl(this.templatePath(prefix + 'pluginname_processed'), config.name.toLowerCase() + '/', false); | ||
} | ||
}; | ||
copy(''); | ||
if (withSamples) { | ||
copy('sample_'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = BasePhoveaGenerator; |
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,13 @@ | ||
|
||
/** | ||
* Base classes types each plugin type can belong to. | ||
*/ | ||
const basetype = { | ||
PYTHON: 'python', | ||
WEB: 'web', | ||
HYBRID: 'hybrid' | ||
}; | ||
|
||
module.exports = { | ||
basetype | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const Base = require('yeoman-generator'); | ||
const {writeTemplates, patchPackageJSON, stringifyAble, useDevVersion} = require('../../utils'); | ||
const {parseRequirements} = require('../../utils/pip'); | ||
const PipUtils = require('../../utils/PipUtils'); | ||
const NpmUtils = require('../../utils/NpmUtils'); | ||
const GeneratorUtils = require('../../utils/GeneratorUtils'); | ||
const fs = require('fs'); | ||
const BasePhoveaGenerator = require('../../base/BasePhoveaGenerator'); | ||
|
||
const known = () => require('../../utils/known'); | ||
|
||
class Generator extends Base { | ||
class Generator extends BasePhoveaGenerator { | ||
constructor(args, options) { | ||
super(args, options); | ||
|
||
|
@@ -66,8 +67,8 @@ class Generator extends Base { | |
} | ||
|
||
_generateDependencies(useDevelopDependencies, cwd) { | ||
let requirements = parseRequirements(this.fs.read(this.destinationPath(cwd + 'requirements.txt'), {defaults: ''})); | ||
const dockerPackages = parseRequirements(this.fs.read(this.destinationPath(cwd + 'docker_packages.txt'), {defaults: ''})); | ||
let requirements = PipUtils.parseRequirements(this.fs.read(this.destinationPath(cwd + 'requirements.txt'), {defaults: ''})); | ||
const dockerPackages = PipUtils.parseRequirements(this.fs.read(this.destinationPath(cwd + 'docker_packages.txt'), {defaults: ''})); | ||
|
||
const concat = (p) => Object.keys(p).map((pi) => pi + p[pi]); | ||
// merge dependencies | ||
|
@@ -78,9 +79,9 @@ class Generator extends Base { | |
modules.filter(known().plugin.isTypeServer).forEach((m) => { | ||
const p = known().plugin.byName(m); | ||
|
||
// avoid having a requirement twice in two different formats that occurs when in the requirements.txt a requirement is written | ||
// in the format -e git+https://github.com/phovea/[email protected]#egg=phovea_server | ||
// and the incoming format is phovea_server>=5.0.1,<6.0.0 | ||
// avoid having a requirement twice in two different formats that occurs when in the requirements.txt a requirement is written | ||
// in the format -e git+https://github.com/phovea/[email protected]#egg=phovea_server | ||
// and the incoming format is phovea_server>=5.0.1,<6.0.0 | ||
if (!useDevelopDependencies) { | ||
const devRequirement = Object.keys(p.develop.requirements)[0]; | ||
const masterRequirment = Object.keys(p.requirements)[0]; | ||
|
@@ -105,18 +106,19 @@ class Generator extends Base { | |
} | ||
writing() { | ||
const config = this.config.getAll(); | ||
this.cwd = this.options.isWorkspace ? (config.cwd || config.name) + '/' : ''; | ||
const deps = this._generateDependencies(useDevVersion.call(this, this.cwd), this.cwd); | ||
this.cwd = this.options.isWorkspace ? (config.app || config.serviceName || config.name) + '/' : ''; | ||
const {version} = this.fs.readJSON(this.destinationPath(this.cwd + 'package.json'), {version: '1.0.0'}); | ||
const deps = this._generateDependencies(NpmUtils.useDevVersion(version), this.cwd); | ||
|
||
patchPackageJSON.call(this, config, ['devDependencies'], null, null, this.cwd); | ||
writeTemplates.call(this, config, !this.options.noSamples, this.cwd); | ||
this._patchPackageJSON(config, ['devDependencies'], null, null, this.cwd); | ||
this._writeTemplates.call(this, config, !this.options.noSamples, this.cwd); | ||
|
||
this.fs.write(this.destinationPath(this.cwd + 'requirements.txt'), deps.requirements.join('\n')); | ||
this.fs.write(this.destinationPath(this.cwd + 'docker_packages.txt'), deps.dockerPackages.join('\n')); | ||
|
||
// don't overwrite existing registry file | ||
if (!fs.existsSync(this.destinationPath(this.cwd + config.name.toLowerCase() + '/__init__.py'))) { | ||
this.fs.copyTpl(this.templatePath('__init__.tmpl.py'), this.destinationPath(this.cwd + config.name.toLowerCase() + '/__init__.py'), stringifyAble(config)); | ||
this.fs.copyTpl(this.templatePath('__init__.tmpl.py'), this.destinationPath(this.cwd + config.name.toLowerCase() + '/__init__.py'), GeneratorUtils.stringifyAble(config)); | ||
} | ||
this.fs.copy(this.templatePath('_gitignore'), this.destinationPath(this.cwd + '.gitignore')); | ||
this.fs.copy(this.templatePath('docs_gitignore'), this.destinationPath(this.cwd + 'docs/.gitignore')); | ||
|
Oops, something went wrong.