diff --git a/src/main/java/org/cqfn/astranaut/cli/Generate.java b/src/main/java/org/cqfn/astranaut/cli/Generate.java index 3c6989b8..cd149e33 100644 --- a/src/main/java/org/cqfn/astranaut/cli/Generate.java +++ b/src/main/java/org/cqfn/astranaut/cli/Generate.java @@ -27,11 +27,13 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.Locale; import java.util.Set; import org.cqfn.astranaut.codegen.java.CompilationUnit; import org.cqfn.astranaut.codegen.java.Context; import org.cqfn.astranaut.codegen.java.License; import org.cqfn.astranaut.codegen.java.Package; +import org.cqfn.astranaut.codegen.java.PackageInfo; import org.cqfn.astranaut.codegen.java.RuleGenerator; import org.cqfn.astranaut.core.utils.FilesWriter; import org.cqfn.astranaut.dsl.NodeDescriptor; @@ -91,25 +93,42 @@ public void perform(final Program program, final List args) throws BaseE * @throws BaseException If files cannot be generated */ private void generateNodes(final Program program, final String language) throws BaseException { + final Package pkg = this.basepkg.getSubpackage(language, "nodes"); + final File folder = this.root.resolve(String.format("%s/nodes", language)).toFile(); + folder.mkdirs(); + final String brief; + if (language.equals("common")) { + brief = "This package contains common ('green') nodes"; + } else { + brief = String.format( + "This package contains nodes that describe the syntax of %s%s language", + language.substring(0, 1).toUpperCase(Locale.ENGLISH), + language.substring(1) + ); + } + final PackageInfo info = new PackageInfo(this.license, brief, pkg); + String path = new File(folder, "package-info.java").getAbsolutePath(); + boolean result = new FilesWriter(path).writeStringNoExcept(info.generateJavaCode()); + if (!result) { + throw new CannotWriteFile(path); + } final Context.Constructor cct = new Context.Constructor(); cct.setLicense(this.license); - cct.setPackage(this.basepkg.getSubpackage(language, "nodes")); + cct.setPackage(pkg); cct.setVersion(this.options.getVersion()); final Context context = cct.createContext(); - final File folder = this.root.resolve(String.format("%s/nodes", language)).toFile(); - folder.mkdirs(); for (final NodeDescriptor rule : program.getNodeDescriptorsForLanguage(language)) { final RuleGenerator generator = rule.createGenerator(); if (generator != null) { final Set units = generator.createUnits(context); for (final CompilationUnit unit : units) { final String code = unit.generateJavaCode(); - final String path = new File( + path = new File( folder, rule.getName().concat(".java") ) .getAbsolutePath(); - final boolean result = new FilesWriter(path).writeStringNoExcept(code); + result = new FilesWriter(path).writeStringNoExcept(code); if (!result) { throw new CannotWriteFile(path); } diff --git a/src/main/java/org/cqfn/astranaut/codegen/java/CompilationUnit.java b/src/main/java/org/cqfn/astranaut/codegen/java/CompilationUnit.java index 03d9149e..25601999 100644 --- a/src/main/java/org/cqfn/astranaut/codegen/java/CompilationUnit.java +++ b/src/main/java/org/cqfn/astranaut/codegen/java/CompilationUnit.java @@ -31,7 +31,7 @@ * Compilation unit, that is, whatever is needed to generate a Java source file. * @since 1.0.0 */ -public final class CompilationUnit implements Entity { +public final class CompilationUnit implements JavaFileGenerator { /** * License. */ @@ -65,17 +65,6 @@ public CompilationUnit(final License license, final Package pkg, final ClassOrIn this.coi = coi; } - /** - * Generates the source code of the compilation unit as a string. - * @return Source code of the compilation unit - * @throws BaseException If there are any problems during code generation - */ - public String generateJavaCode() throws BaseException { - final SourceCodeBuilder code = new SourceCodeBuilder(); - this.build(0, code); - return code.toString(); - } - /** * Adds the name of the imported class to the list. * @param klass Full name of the imported class diff --git a/src/main/java/org/cqfn/astranaut/codegen/java/JavaFileGenerator.java b/src/main/java/org/cqfn/astranaut/codegen/java/JavaFileGenerator.java new file mode 100644 index 00000000..0d84b57c --- /dev/null +++ b/src/main/java/org/cqfn/astranaut/codegen/java/JavaFileGenerator.java @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Ivan Kniazkov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cqfn.astranaut.codegen.java; + +import org.cqfn.astranaut.exceptions.BaseException; + +/** + * Entity that represents and generates whole Java files. + * @since 1.0.0 + */ +public interface JavaFileGenerator extends Entity { + /** + * Generates the source code of the entity as a string. + * @return Source code of the entity + * @throws BaseException If there are any problems during code generation + */ + default String generateJavaCode() throws BaseException { + final SourceCodeBuilder code = new SourceCodeBuilder(); + this.build(0, code); + return code.toString(); + } +} diff --git a/src/main/java/org/cqfn/astranaut/codegen/java/PackageInfo.java b/src/main/java/org/cqfn/astranaut/codegen/java/PackageInfo.java new file mode 100644 index 00000000..afcda08d --- /dev/null +++ b/src/main/java/org/cqfn/astranaut/codegen/java/PackageInfo.java @@ -0,0 +1,75 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Ivan Kniazkov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cqfn.astranaut.codegen.java; + +import org.cqfn.astranaut.exceptions.BaseException; + +/** + * Describes 'package-info.java' file. + * @since 1.0.0 + */ +public final class PackageInfo implements JavaFileGenerator { + /** + * License. + */ + private final License license; + + /** + * Documentation. + */ + private final JavaDoc doc; + + /** + * Package itself. + */ + private final Package pkg; + + /** + * Constructor. + * @param license License + * @param brief Brief description of the package + * @param pkg Package itself + */ + public PackageInfo(final License license, final String brief, final Package pkg) { + this.license = license; + this.doc = new JavaDoc(brief); + this.pkg = pkg; + } + + /** + * Sets the version number. It will be added to JavaDoc. + * @param value Version number + */ + public void setVersion(final String value) { + this.doc.setVersion(value); + } + + @Override + public void build(final int indent, final SourceCodeBuilder code) throws BaseException { + this.license.build(indent, code); + code.add(indent, ""); + this.doc.build(indent, code); + this.pkg.build(indent, code); + } +}