<commit_before>'use strict';

const yeoman = require('yeoman-generator');
const chalk = require('chalk');
const _ = require('lodash');
const randexp = require('randexp');
const fs = require('fs');
const pluralize = require('pluralize');
const packagejs = require(__dirname + '/../../package.json');

// Stores JHipster variables
var jhipsterVar = { moduleName: 'grpc' };

// Stores JHipster functions
var jhipsterFunc = {};

module.exports = yeoman.Base.extend({

    initializing: {

        compose: function (args) {
            this.entityConfig = this.options.entityConfig;
            this.composeWith('jhipster:modules', {
                options: {
                    jhipsterVar: jhipsterVar,
                    jhipsterFunc: jhipsterFunc
                }
            });
        },

        displayLogo: function () {
            this.log(chalk.white('Running ' + chalk.bold('JHipster gRPC') + ' Generator! ' + chalk.yellow('v' + packagejs.version + '\n')));
        },

        validate: function () {
            // this should not be run directly
            if (!this.entityConfig) {
                this.env.error(chalk.red.bold('ERROR!') + ' This sub generator should be used only from JHipster and cannot be run directly...\n');
            }
            this.noEntityService = this.entityConfig.data.service !== 'serviceClass' && this.entityConfig.data.service !== 'serviceImpl';
            if (this.noEntityService) {
                this.log(chalk.yellow('Entity ' + this.entityConfig.entityClass + ' doesn\'t have a service layer so skipping gRPC generator !'));
            }
        }
    },

    prompting: function () {
        if (this.noEntityService) {
            return;
        }
        // don't prompt if data are imported from a file
        if (this.entityConfig.useConfigurationFile === true && this.entityConfig.data && typeof this.entityConfig.data.grpcService !== 'undefined') {
            this.grpcService = this.entityConfig.data.grpcService;
            return;
        }
        var done = this.async();
        var prompts = [
            {
                type: 'confirm',
                name: 'grpcService',
                message: 'Do you want to generate a gRPC service for this entity ?',
                default: true
            }
        ];

        this.prompt(prompts, function (props) {
            this.props = props;
            this.grpcService = this.props.grpcService;
            done();
        }.bind(this));
    },
    writing: {
        updateFiles: function () {
            if (!this.grpcService) {
                return;
            }
            this.mainClass = jhipsterVar.mainClassName;
            this.packageName = jhipsterVar.packageName;
            this.databaseType = jhipsterVar.databaseType;
            this.applicationType = jhipsterVar.applicationType;
            this.authenticationType = jhipsterVar.authenticationType;
            this.searchEngine = jhipsterVar.searchEngine;
            this.entityClass = this.entityConfig.entityClass;
            this.entityClassPlural = pluralize(this.entityClass);
            this.entityInstance = this.entityConfig.entityInstance;
            this.entityInstancePlural = pluralize(this.entityInstance);
            this.entityUnderscoredName = _.snakeCase(this.entityClass).toLowerCase();
            this.dto = this.entityConfig.data.dto || 'no';
            this.instanceType = (this.dto === 'mapstruct') ? this.entityClass + 'DTO' : this.entityClass;
            this.instanceName = (this.dto === 'mapstruct') ? this.entityInstance + 'DTO' : this.entityInstance;
            this.fieldsContainInstant = this.entityConfig.fieldsContainInstant;
            this.fieldsContainZonedDateTime = this.entityConfig.fieldsContainZonedDateTime;
            this.fieldsContainLocalDate = this.entityConfig.fieldsContainLocalDate;
            this.fieldsContainBigDecimal = this.entityConfig.fieldsContainBigDecimal;
            this.fieldsContainBlob = this.entityConfig.fieldsContainBlob;
            this.pagination = this.entityConfig.data.pagination || 'no';
            if(this.entityConfig.data.javadoc === undefined) {
                this.entityJavadoc = '// Protobuf message for entity ' + this.entityClass;
            } else {
                this.entityJavadoc = '// ' + this.entityConfig.data.javadoc.replace('\n', '\n// ');
            }
            this.fluentMethods = this.entityConfig.data.fluentMethods;
            this.fields = this.entityConfig.data.fields;
            this.fields.forEach(f => {
                if (f.fieldTypeBlobContent === 'text') {
                    f.fieldDomainType = 'String';
                } else {
                    f.fieldDomainType = f.fieldType;
                }
                f.fieldProtobufType = getProtobufType(f.fieldDomainType);
                f.isProtobufCustomType = isProtobufCustomType(f.fieldProtobufType);
            });
            this.relationships = this.entityConfig.data.relationships;
            this.relationships.forEach(r => {
                r.relationshipNameUnderscored = _.snakeCase(r.relationshipName).toLowerCase();
                r.otherEntityNameUnderscored = _.snakeCase(r.otherEntityName).toLowerCase();
                r.otherEntityFieldUnderscored = _.snakeCase(r.otherEntityField).toLowerCase();
                if(r.otherEntityNameCapitalized === 'User') {
                    r.otherEntityProtobufType = this.packageName + '.UserProto';
                    r.otherEntityProtoMapper = this.packageName + '.grpc.UserProtoMapper';
                    r.otherEntityTest = this.packageName + '.grpc.UserGrpcServiceIntTest';
                    r.otherEntityProtobufFile = jhipsterVar.packageFolder + '/user.proto';
                } else {
                    r.otherEntityProtobufType = this.packageName + '.entity.' + r.otherEntityNameCapitalized + 'Proto';
                    r.otherEntityProtoMapper = this.packageName + '.grpc.entity.' + r.otherEntityNameUnderscored +
                        '.' + r.otherEntityNameCapitalized + 'ProtoMapper';
                    r.otherEntityTest = this.packageName + '.grpc.entity.' + r.otherEntityNameUnderscored +
                        '.' + r.otherEntityNameCapitalized + 'GrpcServiceIntTest';
                    r.otherEntityProtobufFile = jhipsterVar.packageFolder + '/entity/' + r.otherEntityNameUnderscored + '.proto';
                }
            });

            if (this.databaseType === 'sql') {
                this.idProtoType = 'int64';
                this.idProtoWrappedType = 'Int64Value';
            } else {
                this.idProtoType = 'string';
                this.idProtoWrappedType = 'StringValue';
            }
        },

        writeFiles: function () {
            if (!this.grpcService) {<commit_msg>Fix for entities that have no relationships or no fields<commit_after>'use strict';

const yeoman = require('yeoman-generator');
const chalk = require('chalk');
const _ = require('lodash');
const randexp = require('randexp');
const fs = require('fs');
const pluralize = require('pluralize');
const packagejs = require(__dirname + '/../../package.json');

// Stores JHipster variables
var jhipsterVar = { moduleName: 'grpc' };

// Stores JHipster functions
var jhipsterFunc = {};

module.exports = yeoman.Base.extend({

    initializing: {

        compose: function (args) {
            this.entityConfig = this.options.entityConfig;
            this.composeWith('jhipster:modules', {
                options: {
                    jhipsterVar: jhipsterVar,
                    jhipsterFunc: jhipsterFunc
                }
            });
        },

        displayLogo: function () {
            this.log(chalk.white('Running ' + chalk.bold('JHipster gRPC') + ' Generator! ' + chalk.yellow('v' + packagejs.version + '\n')));
        },

        validate: function () {
            // this should not be run directly
            if (!this.entityConfig) {
                this.env.error(chalk.red.bold('ERROR!') + ' This sub generator should be used only from JHipster and cannot be run directly...\n');
            }
            this.noEntityService = this.entityConfig.data.service !== 'serviceClass' && this.entityConfig.data.service !== 'serviceImpl';
            if (this.noEntityService) {
                this.log(chalk.yellow('Entity ' + this.entityConfig.entityClass + ' doesn\'t have a service layer so skipping gRPC generator !'));
            }
        }
    },

    prompting: function () {
        if (this.noEntityService) {
            return;
        }
        // don't prompt if data are imported from a file
        if (this.entityConfig.useConfigurationFile === true && this.entityConfig.data && typeof this.entityConfig.data.grpcService !== 'undefined') {
            this.grpcService = this.entityConfig.data.grpcService;
            return;
        }
        var done = this.async();
        var prompts = [
            {
                type: 'confirm',
                name: 'grpcService',
                message: 'Do you want to generate a gRPC service for this entity ?',
                default: true
            }
        ];

        this.prompt(prompts, function (props) {
            this.props = props;
            this.grpcService = this.props.grpcService;
            done();
        }.bind(this));
    },
    writing: {
        updateFiles: function () {
            if (!this.grpcService) {
                return;
            }
            this.mainClass = jhipsterVar.mainClassName;
            this.packageName = jhipsterVar.packageName;
            this.databaseType = jhipsterVar.databaseType;
            this.applicationType = jhipsterVar.applicationType;
            this.authenticationType = jhipsterVar.authenticationType;
            this.searchEngine = jhipsterVar.searchEngine;
            this.entityClass = this.entityConfig.entityClass;
            this.entityClassPlural = pluralize(this.entityClass);
            this.entityInstance = this.entityConfig.entityInstance;
            this.entityInstancePlural = pluralize(this.entityInstance);
            this.entityUnderscoredName = _.snakeCase(this.entityClass).toLowerCase();
            this.dto = this.entityConfig.data.dto || 'no';
            this.instanceType = (this.dto === 'mapstruct') ? this.entityClass + 'DTO' : this.entityClass;
            this.instanceName = (this.dto === 'mapstruct') ? this.entityInstance + 'DTO' : this.entityInstance;
            this.fieldsContainInstant = this.entityConfig.fieldsContainInstant;
            this.fieldsContainZonedDateTime = this.entityConfig.fieldsContainZonedDateTime;
            this.fieldsContainLocalDate = this.entityConfig.fieldsContainLocalDate;
            this.fieldsContainBigDecimal = this.entityConfig.fieldsContainBigDecimal;
            this.fieldsContainBlob = this.entityConfig.fieldsContainBlob;
            this.pagination = this.entityConfig.data.pagination || 'no';
            if(this.entityConfig.data.javadoc === undefined) {
                this.entityJavadoc = '// Protobuf message for entity ' + this.entityClass;
            } else {
                this.entityJavadoc = '// ' + this.entityConfig.data.javadoc.replace('\n', '\n// ');
            }
            this.fluentMethods = this.entityConfig.data.fluentMethods;
            this.fields = this.entityConfig.data.fields || [];
            this.fields.forEach(f => {
                if (f.fieldTypeBlobContent === 'text') {
                    f.fieldDomainType = 'String';
                } else {
                    f.fieldDomainType = f.fieldType;
                }
                f.fieldProtobufType = getProtobufType(f.fieldDomainType);
                f.isProtobufCustomType = isProtobufCustomType(f.fieldProtobufType);
            });
            this.relationships = this.entityConfig.data.relationships || [];
            this.relationships.forEach(r => {
                r.relationshipNameUnderscored = _.snakeCase(r.relationshipName).toLowerCase();
                r.otherEntityNameUnderscored = _.snakeCase(r.otherEntityName).toLowerCase();
                r.otherEntityFieldUnderscored = _.snakeCase(r.otherEntityField).toLowerCase();
                if(r.otherEntityNameCapitalized === 'User') {
                    r.otherEntityProtobufType = this.packageName + '.UserProto';
                    r.otherEntityProtoMapper = this.packageName + '.grpc.UserProtoMapper';
                    r.otherEntityTest = this.packageName + '.grpc.UserGrpcServiceIntTest';
                    r.otherEntityProtobufFile = jhipsterVar.packageFolder + '/user.proto';
                } else {
                    r.otherEntityProtobufType = this.packageName + '.entity.' + r.otherEntityNameCapitalized + 'Proto';
                    r.otherEntityProtoMapper = this.packageName + '.grpc.entity.' + r.otherEntityNameUnderscored +
                        '.' + r.otherEntityNameCapitalized + 'ProtoMapper';
                    r.otherEntityTest = this.packageName + '.grpc.entity.' + r.otherEntityNameUnderscored +
                        '.' + r.otherEntityNameCapitalized + 'GrpcServiceIntTest';
                    r.otherEntityProtobufFile = jhipsterVar.packageFolder + '/entity/' + r.otherEntityNameUnderscored + '.proto';
                }
            });

            if (this.databaseType === 'sql') {
                this.idProtoType = 'int64';
                this.idProtoWrappedType = 'Int64Value';
            } else {
                this.idProtoType = 'string';
                this.idProtoWrappedType = 'StringValue';
            }
        },

        writeFiles: function () {
            if (!this.grpcService) {
{'fraction_non_alphanumeric': 0.11316616711747934, 'fraction_numerical': 0.0005848380729585496, 'mean_word_length': 2.0065934065934066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5771550', 'n_tokens_mistral': 3692, 'n_tokens_neox': 3591, 'n_words': 619}