Skip to content

Commit

Permalink
Generating 'package-info.java' files
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Oct 18, 2024
1 parent f65b7b5 commit f906caf
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 17 deletions.
29 changes: 24 additions & 5 deletions src/main/java/org/cqfn/astranaut/cli/Generate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,25 +93,42 @@ public void perform(final Program program, final List<String> 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<CompilationUnit> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/cqfn/astranaut/codegen/java/PackageInfo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f906caf

Please sign in to comment.