From adfe784341a582e58079a71a60a69dcc093a2a91 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 6 Feb 2022 18:54:52 -0500 Subject: [PATCH] split use of Sketch.getName() with new Sketch.getMainName() method --- app/src/processing/app/Sketch.java | 10 +++++++ java/src/processing/mode/java/JavaBuild.java | 28 +++++++++---------- .../mode/java/preproc/PdePreprocessor.java | 15 ++++------ 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index b0cd4c8a7..33bb20bce 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -1674,6 +1674,16 @@ public class Sketch { // } + /** + * Returns the name (without extension) of the main tab. + * Most uses of getName() prior to 4.0 beta 6 were to get the main class, + * but this allows the sketch to be decoupled from the main tab name. + */ + public String getMainName() { + return code[0].getPrettyName(); + } + + /** * Returns path to the main .pde file for this sketch. */ diff --git a/java/src/processing/mode/java/JavaBuild.java b/java/src/processing/mode/java/JavaBuild.java index 680de9178..ade527aec 100644 --- a/java/src/processing/mode/java/JavaBuild.java +++ b/java/src/processing/mode/java/JavaBuild.java @@ -140,7 +140,7 @@ public class JavaBuild { * @return null if compilation failed, main class name if not */ public String preprocess(File srcFolder, boolean sizeWarning) throws SketchException { - PdePreprocessor preprocessor = PdePreprocessor.builderFor(sketch.getName()).build(); + PdePreprocessor preprocessor = PdePreprocessor.builderFor(sketch.getMainName()).build(); return preprocess(srcFolder, null, preprocessor, sizeWarning); } @@ -231,7 +231,7 @@ public class JavaBuild { srcFolder : new File(srcFolder, packageName.replace('.', '/')); outputFolder.mkdirs(); // Base.openFolder(outputFolder); - final File java = new File(outputFolder, sketch.getName() + ".java"); + final File java = new File(outputFolder, sketch.getMainName() + ".java"); try (PrintWriter stream = PApplet.createWriter(java)) { result = preprocessor.write(stream, bigCode.toString(), codeFolderPackages); } @@ -517,7 +517,7 @@ public class JavaBuild { } // If not the preprocessed file at this point, then need to get out - if (!dotJavaFilename.equals(sketch.getName() + ".java")) { + if (!dotJavaFilename.equals(sketch.getMainName() + ".java")) { return null; } @@ -566,10 +566,10 @@ public class JavaBuild { // if name != exportSketchName, then that's weirdness // BUG unfortunately, that can also be a bug in the preproc :( - if (!sketch.getName().equals(foundName)) { + if (!sketch.getMainName().equals(foundName)) { Messages.showWarning("Error during export", - "Sketch name is " + sketch.getName() + " but the sketch\n" + - "name in the code was " + foundName, null); + "Main tab is named " + sketch.getMainName() + " but the\n" + + "sketch name in the code was " + foundName, null); return false; } @@ -710,7 +710,7 @@ public class JavaBuild { macosFolder.mkdirs(); // This is an unsigned copy of the app binary (see build/build.xml) Util.copyFile(mode.getContentFile("application/mac-app-stub"), - new File(contentsFolder, "MacOS/" + sketch.getName())); + new File(contentsFolder, "MacOS/" + sketch.getMainName())); File pkgInfo = new File(contentsFolder, "PkgInfo"); PrintWriter writer = PApplet.createWriter(pkgInfo); @@ -755,7 +755,7 @@ public class JavaBuild { /// create the main .jar file FileOutputStream zipOutputFile = - new FileOutputStream(new File(jarFolder, sketch.getName() + ".jar")); + new FileOutputStream(new File(jarFolder, sketch.getMainName() + ".jar")); ZipOutputStream zos = new ZipOutputStream(zipOutputFile); // add the manifest file so that the .jar can be double-clickable @@ -797,7 +797,7 @@ public class JavaBuild { zos.flush(); zos.close(); - jarList.append(sketch.getName() + ".jar"); + jarList.append(sketch.getMainName() + ".jar"); /// add contents of 'library' folders to the export @@ -904,7 +904,7 @@ public class JavaBuild { } while ((index = sb.indexOf("@@sketch@@")) != -1) { sb.replace(index, index + "@@sketch@@".length(), - sketch.getName()); + sketch.getMainName()); } while ((index = sb.indexOf("@@lsuipresentationmode@@")) != -1) { sb.replace(index, index + "@@lsuipresentationmode@@".length(), @@ -961,7 +961,7 @@ public class JavaBuild { config.addChild("icon").setContent(iconFile.getAbsolutePath()); XML clazzPath = config.addChild("classPath"); - clazzPath.addChild("mainClass").setContent(sketch.getName()); + clazzPath.addChild("mainClass").setContent(sketch.getMainName()); for (String jarName : jarList) { clazzPath.addChild("cp").setContent("lib/" + jarName); } @@ -1015,7 +1015,7 @@ public class JavaBuild { //" -Djava.ext.dirs=\"$APPDIR/java/lib/ext\"" + //" -Djna.nosys=true" + " -cp \"" + exportClassPath + "\"" + - " " + sketch.getName() + " \"$@\"\n"); + " " + sketch.getMainName() + " \"$@\"\n"); pw.flush(); pw.close(); @@ -1042,7 +1042,7 @@ public class JavaBuild { } } // move the .java file from the preproc there too - String preprocFilename = sketch.getName() + ".java"; + String preprocFilename = sketch.getMainName() + ".java"; File preprocFile = new File(srcFolder, preprocFilename); if (preprocFile.exists()) { Util.copyFile(preprocFile, new File(sourceFolder, preprocFilename)); @@ -1163,7 +1163,7 @@ public class JavaBuild { String contents = "Manifest-Version: 1.0\n" + "Created-By: Processing " + Base.getVersionName() + "\n" + - "Main-Class: " + sketch.getName() + "\n"; // TODO not package friendly + "Main-Class: " + sketch.getMainName() + "\n"; // TODO not package friendly zos.write(contents.getBytes()); zos.closeEntry(); } diff --git a/java/src/processing/mode/java/preproc/PdePreprocessor.java b/java/src/processing/mode/java/preproc/PdePreprocessor.java index fad1e8c43..f2b67bfb4 100644 --- a/java/src/processing/mode/java/preproc/PdePreprocessor.java +++ b/java/src/processing/mode/java/preproc/PdePreprocessor.java @@ -143,10 +143,7 @@ public class PdePreprocessor { * @return Information about the preprocessing operation. */ public PreprocessorResult write(Writer outWriter, String inProgram, - Iterable codeFolderPackages) - throws SketchException { - - // Determine inports + Iterable codeFolderPackages) throws SketchException { ArrayList codeFolderImports = new ArrayList<>(); if (codeFolderPackages != null) { for (String item : codeFolderPackages) { @@ -275,7 +272,7 @@ public class PdePreprocessor { */ public static class PdePreprocessorBuilder { - private final String sketchName; + private final String mainName; private Optional tabSize; private Optional isTesting; private Optional parseTreeFactory; @@ -327,10 +324,10 @@ public class PdePreprocessor { * this constructor. *

* - * @param newSketchName The name of the sketch. + * @param newMainName The name of the sketch. */ - private PdePreprocessorBuilder(String newSketchName) { - sketchName = newSketchName; + private PdePreprocessorBuilder(String newMainName) { + mainName = newMainName; tabSize = Optional.empty(); isTesting = Optional.empty(); parseTreeFactory = Optional.empty(); @@ -436,7 +433,7 @@ public class PdePreprocessor { ); return new PdePreprocessor( - sketchName, + mainName, effectiveTabSize, effectiveIsTesting, effectiveFactory,