forked from benfry/processing4
-
Notifications
You must be signed in to change notification settings - Fork 5
/
InstallCommander.java
164 lines (134 loc) · 6.21 KB
/
InstallCommander.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.tools;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import processing.app.Base;
import processing.app.Language;
import processing.app.Messages;
import processing.app.Platform;
import processing.app.ui.Editor;
import processing.core.PApplet;
import processing.data.StringList;
public class InstallCommander implements Tool {
Base base;
public String getMenuTitle() {
return Language.text("menu.tools.install_processing_java");
}
public void init(Base base) {
this.base = base;
}
public void run() {
try {
Editor editor = base.getActiveEditor();
final String primary =
"Install processing-java for all users?";
final String secondary =
"This will install the processing-java program, which is capable " +
"of building and running Java Mode sketches from the command line. " +
"Click “Yes” to install it for all users (an administrator password " +
"is required), or “No” to place the program in your home directory. " +
"If you rename or move Processing.app, " +
"you'll need to reinstall the tool.";
int result =
JOptionPane.showConfirmDialog(editor,
"<html> " +
"<head> <style type=\"text/css\">"+
"b { font: 13pt \"Lucida Grande\" }"+
"p { font: 11pt \"Lucida Grande\"; margin-top: 8px; width: 300px }"+
"</style> </head>" +
"<b>" + primary + "</b>" +
"<p>" + secondary + "</p>",
"Commander",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.CANCEL_OPTION) {
return;
}
File file = File.createTempFile("processing", "commander");
PrintWriter writer = PApplet.createWriter(file);
writer.print("#!/bin/sh\n\n");
writer.print("# Prevents processing-java from stealing focus, see:\n" +
"# https://github.com/processing/processing/issues/3996.\n" +
"OPTION_FOR_HEADLESS_RUN=\"\"\n" +
"for ARG in \"$@\"\n" +
"do\n" +
" if [ \"$ARG\" = \"--build\" ]; then\n" +
" OPTION_FOR_HEADLESS_RUN=\"-Djava.awt.headless=true\"\n" +
" fi\n" +
"done\n\n");
String javaRoot = Platform.getContentFile(".").getCanonicalPath();
StringList jarList = new StringList();
addJarList(jarList, new File(javaRoot));
addJarList(jarList, new File(javaRoot, "core/library"));
addJarList(jarList, new File(javaRoot, "modes/java/mode"));
String classPath = jarList.join(":").replaceAll(javaRoot + "\\/?", "");
writer.println("cd \"" + javaRoot + "\" && " +
Platform.getJavaPath().replaceAll(" ", "\\\\ ") +
" -Djna.nosys=true" +
" $OPTION_FOR_HEADLESS_RUN" +
" -cp \"" + classPath + "\"" +
" processing.mode.java.Commander \"$@\"");
writer.flush();
writer.close();
file.setExecutable(true);
String sourcePath = file.getAbsolutePath();
if (result == JOptionPane.YES_OPTION) {
// Moving to /usr/local/bin instead of /usr/bin for compatibility
// with OS X 10.11 and its "System Integrity Protection"
// https://github.com/processing/processing/issues/3497
String targetPath = "/usr/local/bin/processing-java";
// Remove the old version in case it exists
// https://github.com/processing/processing/issues/3786
String oldPath = "/usr/bin/processing-java";
String shellScript = "/bin/rm -f " + oldPath +
" && /bin/mkdir -p /usr/local/bin" +
" && /bin/mv " + sourcePath + " " + targetPath;
String appleScript =
"do shell script \"" + shellScript + "\" with administrator privileges";
PApplet.exec(new String[] { "osascript", "-e", appleScript });
} else if (result == JOptionPane.NO_OPTION) {
File targetFile = new File(System.getProperty("user.home"), "processing-java");
String targetPath = targetFile.getAbsolutePath();
if (targetFile.exists()) {
Messages.showWarning("File Already Exists",
"The processing-java program already exists at:\n" +
targetPath + "\n" +
"Please remove it and try again.");
} else {
PApplet.exec(new String[] { "mv", sourcePath, targetPath });
}
}
editor.statusNotice("Finished.");
} catch (IOException e) {
Messages.showWarning("Error while installing",
"An error occurred and the tool was not installed.", e);
}
}
static private void addJarList(StringList list, File dir) {
File[] jars = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jar") && !name.startsWith(".");
}
});
for (File jar : jars) {
list.append(jar.getAbsolutePath());
}
}
}