Skip to content

Commit adfe784

Browse files
committed
split use of Sketch.getName() with new Sketch.getMainName() method
1 parent 22d6aca commit adfe784

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

app/src/processing/app/Sketch.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,16 @@ public String getName() {
16741674
// }
16751675

16761676

1677+
/**
1678+
* Returns the name (without extension) of the main tab.
1679+
* Most uses of getName() prior to 4.0 beta 6 were to get the main class,
1680+
* but this allows the sketch to be decoupled from the main tab name.
1681+
*/
1682+
public String getMainName() {
1683+
return code[0].getPrettyName();
1684+
}
1685+
1686+
16771687
/**
16781688
* Returns path to the main .pde file for this sketch.
16791689
*/

java/src/processing/mode/java/JavaBuild.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public String getSketchClassName() {
140140
* @return null if compilation failed, main class name if not
141141
*/
142142
public String preprocess(File srcFolder, boolean sizeWarning) throws SketchException {
143-
PdePreprocessor preprocessor = PdePreprocessor.builderFor(sketch.getName()).build();
143+
PdePreprocessor preprocessor = PdePreprocessor.builderFor(sketch.getMainName()).build();
144144
return preprocess(srcFolder, null, preprocessor, sizeWarning);
145145
}
146146

@@ -231,7 +231,7 @@ public String preprocess(File srcFolder,
231231
srcFolder : new File(srcFolder, packageName.replace('.', '/'));
232232
outputFolder.mkdirs();
233233
// Base.openFolder(outputFolder);
234-
final File java = new File(outputFolder, sketch.getName() + ".java");
234+
final File java = new File(outputFolder, sketch.getMainName() + ".java");
235235
try (PrintWriter stream = PApplet.createWriter(java)) {
236236
result = preprocessor.write(stream, bigCode.toString(), codeFolderPackages);
237237
}
@@ -517,7 +517,7 @@ public SketchException placeException(String message,
517517
}
518518

519519
// If not the preprocessed file at this point, then need to get out
520-
if (!dotJavaFilename.equals(sketch.getName() + ".java")) {
520+
if (!dotJavaFilename.equals(sketch.getMainName() + ".java")) {
521521
return null;
522522
}
523523

@@ -566,10 +566,10 @@ protected boolean exportApplication() throws IOException, SketchException {
566566

567567
// if name != exportSketchName, then that's weirdness
568568
// BUG unfortunately, that can also be a bug in the preproc :(
569-
if (!sketch.getName().equals(foundName)) {
569+
if (!sketch.getMainName().equals(foundName)) {
570570
Messages.showWarning("Error during export",
571-
"Sketch name is " + sketch.getName() + " but the sketch\n" +
572-
"name in the code was " + foundName, null);
571+
"Main tab is named " + sketch.getMainName() + " but the\n" +
572+
"sketch name in the code was " + foundName, null);
573573
return false;
574574
}
575575

@@ -710,7 +710,7 @@ protected boolean exportApplication(File destFolder,
710710
macosFolder.mkdirs();
711711
// This is an unsigned copy of the app binary (see build/build.xml)
712712
Util.copyFile(mode.getContentFile("application/mac-app-stub"),
713-
new File(contentsFolder, "MacOS/" + sketch.getName()));
713+
new File(contentsFolder, "MacOS/" + sketch.getMainName()));
714714

715715
File pkgInfo = new File(contentsFolder, "PkgInfo");
716716
PrintWriter writer = PApplet.createWriter(pkgInfo);
@@ -755,7 +755,7 @@ protected boolean exportApplication(File destFolder,
755755
/// create the main .jar file
756756

757757
FileOutputStream zipOutputFile =
758-
new FileOutputStream(new File(jarFolder, sketch.getName() + ".jar"));
758+
new FileOutputStream(new File(jarFolder, sketch.getMainName() + ".jar"));
759759
ZipOutputStream zos = new ZipOutputStream(zipOutputFile);
760760

761761
// add the manifest file so that the .jar can be double-clickable
@@ -797,7 +797,7 @@ protected boolean exportApplication(File destFolder,
797797
zos.flush();
798798
zos.close();
799799

800-
jarList.append(sketch.getName() + ".jar");
800+
jarList.append(sketch.getMainName() + ".jar");
801801

802802

803803
/// add contents of 'library' folders to the export
@@ -904,7 +904,7 @@ protected boolean exportApplication(File destFolder,
904904
}
905905
while ((index = sb.indexOf("@@sketch@@")) != -1) {
906906
sb.replace(index, index + "@@sketch@@".length(),
907-
sketch.getName());
907+
sketch.getMainName());
908908
}
909909
while ((index = sb.indexOf("@@lsuipresentationmode@@")) != -1) {
910910
sb.replace(index, index + "@@lsuipresentationmode@@".length(),
@@ -961,7 +961,7 @@ protected boolean exportApplication(File destFolder,
961961
config.addChild("icon").setContent(iconFile.getAbsolutePath());
962962

963963
XML clazzPath = config.addChild("classPath");
964-
clazzPath.addChild("mainClass").setContent(sketch.getName());
964+
clazzPath.addChild("mainClass").setContent(sketch.getMainName());
965965
for (String jarName : jarList) {
966966
clazzPath.addChild("cp").setContent("lib/" + jarName);
967967
}
@@ -1015,7 +1015,7 @@ protected boolean exportApplication(File destFolder,
10151015
//" -Djava.ext.dirs=\"$APPDIR/java/lib/ext\"" +
10161016
//" -Djna.nosys=true" +
10171017
" -cp \"" + exportClassPath + "\"" +
1018-
" " + sketch.getName() + " \"$@\"\n");
1018+
" " + sketch.getMainName() + " \"$@\"\n");
10191019

10201020
pw.flush();
10211021
pw.close();
@@ -1042,7 +1042,7 @@ protected boolean exportApplication(File destFolder,
10421042
}
10431043
}
10441044
// move the .java file from the preproc there too
1045-
String preprocFilename = sketch.getName() + ".java";
1045+
String preprocFilename = sketch.getMainName() + ".java";
10461046
File preprocFile = new File(srcFolder, preprocFilename);
10471047
if (preprocFile.exists()) {
10481048
Util.copyFile(preprocFile, new File(sourceFolder, preprocFilename));
@@ -1163,7 +1163,7 @@ protected void addManifest(ZipOutputStream zos) throws IOException {
11631163
String contents =
11641164
"Manifest-Version: 1.0\n" +
11651165
"Created-By: Processing " + Base.getVersionName() + "\n" +
1166-
"Main-Class: " + sketch.getName() + "\n"; // TODO not package friendly
1166+
"Main-Class: " + sketch.getMainName() + "\n"; // TODO not package friendly
11671167
zos.write(contents.getBytes());
11681168
zos.closeEntry();
11691169
}

java/src/processing/mode/java/preproc/PdePreprocessor.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ public PreprocessorResult write(final Writer out, String program) throws SketchE
143143
* @return Information about the preprocessing operation.
144144
*/
145145
public PreprocessorResult write(Writer outWriter, String inProgram,
146-
Iterable<String> codeFolderPackages)
147-
throws SketchException {
148-
149-
// Determine inports
146+
Iterable<String> codeFolderPackages) throws SketchException {
150147
ArrayList<String> codeFolderImports = new ArrayList<>();
151148
if (codeFolderPackages != null) {
152149
for (String item : codeFolderPackages) {
@@ -275,7 +272,7 @@ public List<String> getDefaultImports() {
275272
*/
276273
public static class PdePreprocessorBuilder {
277274

278-
private final String sketchName;
275+
private final String mainName;
279276
private Optional<Integer> tabSize;
280277
private Optional<Boolean> isTesting;
281278
private Optional<ParseTreeListenerFactory> parseTreeFactory;
@@ -327,10 +324,10 @@ public static class PdePreprocessorBuilder {
327324
* this constructor.
328325
* </p>
329326
*
330-
* @param newSketchName The name of the sketch.
327+
* @param newMainName The name of the sketch.
331328
*/
332-
private PdePreprocessorBuilder(String newSketchName) {
333-
sketchName = newSketchName;
329+
private PdePreprocessorBuilder(String newMainName) {
330+
mainName = newMainName;
334331
tabSize = Optional.empty();
335332
isTesting = Optional.empty();
336333
parseTreeFactory = Optional.empty();
@@ -436,7 +433,7 @@ public PdePreprocessor build() {
436433
);
437434

438435
return new PdePreprocessor(
439-
sketchName,
436+
mainName,
440437
effectiveTabSize,
441438
effectiveIsTesting,
442439
effectiveFactory,

0 commit comments

Comments
 (0)