0

In generator-jhipster repository, specifically files.js in ../generators/server/files.js, it generates java source files and directories based on some conditions.

For example:

  {
            condition: generator => generator.databaseType === 'cassandra',
            path: SERVER_MAIN_SRC_DIR,
            templates: [
                {
                    file: 'package/config/cassandra/CassandraConfiguration.java',
                    renameTo: generator => `${generator.javaDir}config/cassandra/CassandraConfiguration.java`
                }
            ]
   }

This will take the template file CassandraConfiguration.java.ejs in the package/config/cassandra directory and generate the java source file CassandraConfiguration.java upon running the jhipster application successfully.

My question: is it possible to generate multiple CassandraConfiguration.java files (suppose with different names, such as: CassandraConfiguration0.java, CassandraConfiguration1.java, etc...) inside this template with a "LOOP"? I know I could run a loop with make directories (mkdirp), but I'm not sure how to apply it on making files, because it wouldn't let me create a loop inside the templates: [].

Is this possible? (I'm not familiar with the syntax of yeoman):

  {
        for(var i = 0; i < 3; i++) {
            condition: generator => generator.databaseType === 'cassandra',
            path: SERVER_MAIN_SRC_DIR,
            templates: [
                {
                    file: 'package/config/cassandra/CassandraConfiguration.java',
                    renameTo: generator => `${generator.javaDir}config/cassandra/CassandraConfiguration[i].java`
                }
             ]
         }
   }

This should generate: CassandraConfiguration0.java, CassandraConfiguration1.java and CassandraConfiguration2.java respectively.

Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49
jukyduky
  • 39
  • 6

1 Answers1

0

Your approach is wrong because you're trying to use imperative style in a declarative object (serverFiles).

You should rather create a loop to add entries to serverFiles array.

for(let i = 0; i < 3; i++) {
    let entry = {
        condition: generator => generator.databaseType === 'cassandra',
        path: SERVER_MAIN_SRC_DIR,
        templates: [
            {
                file: 'package/config/cassandra/CassandraConfiguration.java',
                renameTo: generator => `${generator.javaDir}config/cassandra/CassandraConfiguration${i}.java`
            }
        ]
     };
     serverFiles.db.push(entry);
 }
Gaël Marziou
  • 16,028
  • 4
  • 38
  • 49
  • I believe we're pushing into serverFiles.{array}.push(entry), since serverFiles is an object. TypeError: serverFiles.push is not a function. Thanks! – jukyduky Jul 26 '19 at 22:45
  • Yeah I did not test this code but you got the idea. I fixed it. – Gaël Marziou Jul 27 '19 at 07:23
  • Hello Gael, just one more question regarding this issue. How would I pass CassandraConfiguration${i} so that it matches the corresponding class name in the file template (.ejs)? For example, CassandraConfiguration0.java => public class CassandraConfiguration0 {}, and so on... – jukyduky Jul 29 '19 at 21:14
  • Since we're generating multiple files, i'm not sure how to make each file name matches with its corresponding class name. If it were just one name, I could simply set it like this: <%=name%>. – jukyduky Jul 29 '19 at 21:21
  • I don't know, this is why my initial suggestion was simpler: one configuration class defining multiple beans. Alternatively you could define inner classes. – Gaël Marziou Jul 29 '19 at 21:29
  • I didn't say it was not possible, I just said that I didn't know. – Gaël Marziou Jul 29 '19 at 21:46
  • So you're saying if I were to generate multiple files under one configuration class; (CassandraConfiguration.java.ejs), it's not possible to have each file name mapped to the corresponding class names? – jukyduky Jul 29 '19 at 21:47
  • Alright, would you happen to know where I could get help on this issue? – jukyduky Jul 29 '19 at 21:49
  • Regarding your initial suggestion, would it generate multiple files under one configuration class as well? What do you mean by defining multiple beans? – jukyduky Jul 29 '19 at 21:54
  • My question, to be more precise, is to generate multiple CassandraConfiguration files, with different names, say CassandraConfiguration0.java, CassandraConfiguration1.java, etc... under "CassandraConfiguration.java.ejs" and have each file names, such as: CassandraConfiguration0.java => public class CassandraConfiguration0 {}, CassandraConfiguration1.java => public class CassandraConfiguration1 {}, etc... – jukyduky Jul 29 '19 at 21:58
  • Read the code, I suppose that you could use template options or context, look at how 'config/liquibase/fake-data/table.csv' is generated. Your question about multiple beans makes me wonder whether you know enough Spring Boot to generate code for it. – Gaël Marziou Jul 29 '19 at 22:00