Skip to content

Commit

Permalink
add synchronize generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 7, 2024
1 parent 73b663f commit 531d4cb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .blueprint/cli/commands.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const defaultCommands = {
desc: 'Generate a test sample',
blueprint: '@jhipster/jhipster-dev',
},
synchronize: {
desc: 'Synchronize templates from generator-jhipster',
blueprint: '@jhipster/jhipster-dev',
},
};

export default defaultCommands;
40 changes: 40 additions & 0 deletions .blueprint/synchronize/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* 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
*
* https://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.
*/
const command = {
configs: {
newFiles: {
description: 'Add new files only',
cli: {
type: Boolean,
},
scope: 'generator',
},
convert: {
description: 'Apply Partial Java files to Kotlin files convertion',
cli: {
type: Boolean,
alias: 'c',
},
scope: 'generator',
},
},
import: [],
};

export default command;
89 changes: 89 additions & 0 deletions .blueprint/synchronize/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* 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
*
* https://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.
*/
import { basename, dirname, join, relative } from 'path';
import { fileURLToPath } from 'url';
import { existsSync } from 'fs';
import BaseGenerator from 'generator-jhipster/generators/base';
import { globby } from 'globby';
import { passthrough } from '@yeoman/transform';
import { convertToKotlinFile } from '../../generators/kotlin/support/index.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
// const jhipster7TemplatesPackage = dirname(fileURLToPath(import.meta.resolve('jhipster-7-templates/package.json')));
const jhipster8Generators = join(__dirname, '../../node_modules/generator-jhipster/dist/generators');

export default class SynchronizeGenerator extends BaseGenerator {
constructor(args, options, features) {
super(args, options, { queueCommandTasks: true, ...features });
}

get [BaseGenerator.DEFAULT]() {
return this.asDefaultTaskGroup({
default() {
if (this.options.convert === false) {
return;
}
this.queueTransformStream(
{
name: 'convert to kt file',
filter: file => file.path.endsWith('.kt.ejs'),
},
passthrough(file => {
file.contents = Buffer.from(
file.contents
.toString()
.replaceAll(';\n', '\n')
.replaceAll('\nimport static ', '\nimport ')
.replaceAll('public class ', 'class ')
.replaceAll('.class', '::class')
.replaceAll(/ (?:extends|implements) (\w+)/g, ' : $1')
.replaceAll(/ (?:extends|implements) /g, ' : ')
.replaceAll(/@Override\n(\s*)/g, 'override ')
.replaceAll(/\n( {4})(private |)?(?:final )?(\w+) (\w+)(\n| = )/g, '\n$1$2lateinit var $4: $3$5')
.replaceAll(/private static final (\w+) /g, 'private val ')
.replaceAll(/(?:public |protected )?(\w+) (\w+)\((.*)\) {/g, 'fun $2($3): $1 {')
.replaceAll(': void', ''),
);
}),
);
},
});
}

get [BaseGenerator.WRITING]() {
return this.asWritingTaskGroup({
async writing() {
const files = await globby(`${jhipster8Generators}/**/*.java.ejs`);
const ktTemplatesPath = this.destinationPath('generators/spring-boot/templates');
for (const file of files.filter(
file => !file.includes('/spring-data-couchbase/') && !['GeneratedByJHipster.java.ejs'].includes(basename(file)),
)) {
const relativeDestinationFile = convertToKotlinFile(relative(jhipster8Generators, file))
.replace('spring-boot/templates/', '')
.replace('server/templates/', '')
.replace(/(?:(?:.*)\/generators\/)?(.*)\/templates/, '$1');
const destinationFile = join(ktTemplatesPath, relativeDestinationFile);
if (this.options.newFiles !== true || !existsSync(destinationFile)) {
this.copyTemplate(file, destinationFile);
}
}
},
});
}
}
20 changes: 20 additions & 0 deletions .blueprint/synchronize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* 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
*
* https://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.
*/
export { default } from './generator.js';
export { default as command } from './command.js';
2 changes: 1 addition & 1 deletion .yo-resolve
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
generators/**
generators/*.*
.prettierrc.yml skip
cli/cli.cjs skip
README.md skip

0 comments on commit 531d4cb

Please sign in to comment.