Skip to content

Commit f55d4c3

Browse files
committed
fix for #arduino/Arduino/2982
When selecting the arduino in the preference page a file is made in the root of the workspace based on the command provided by arduino When creatig/modifying a project this file is parced after the defaults.
1 parent 26b88a2 commit f55d4c3

File tree

8 files changed

+202
-136
lines changed

8 files changed

+202
-136
lines changed

it.baeyens.arduino.common/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Require-Bundle: org.eclipse.ui,
88
org.eclipse.core.runtime,
99
org.eclipse.jface.text,
1010
org.eclipse.ui.console,
11-
org.eclipse.cdt;bundle-version="8.5.0"
11+
org.eclipse.cdt;bundle-version="8.5.0",
12+
org.eclipse.osgi
1213
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
1314
Bundle-ActivationPolicy: lazy
1415
Export-Package: it.baeyens.arduino.arduino,

it.baeyens.arduino.common/src/it/baeyens/arduino/common/Activator.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ public boolean belongsTo(Object family) {
3636
*/
3737
public class Activator extends AbstractUIPlugin {
3838

39+
private static Activator instance;
40+
41+
public static Activator getDefault() {
42+
return instance;
43+
}
44+
3945
// The plug-in ID
4046
public static final String PLUGIN_ID = "it.baeyens.arduino.common"; //$NON-NLS-1$
4147

4248
// The shared instance
43-
private static Activator plugin;
4449
private static final String flagStart = "F" + "s" + "S" + "t" + "a" + "t" + "u" + "s";
4550
private static final String flagMonitor = "F" + "m" + "S" + "t" + "a" + "t" + "u" + "s";
4651
private static final String uploadflag = "F" + "u" + "S" + "t" + "a" + "t" + "u" + "s";
@@ -63,7 +68,7 @@ public Activator() {
6368
@Override
6469
public void start(BundleContext context) throws Exception {
6570
super.start(context);
66-
plugin = this;
71+
instance = this;
6772

6873
// add required properties for Arduino serial port on linux, if not
6974
// defined
@@ -115,19 +120,10 @@ public void stop(BundleContext context) throws Exception {
115120
IJobManager jobMan = Job.getJobManager();
116121
jobMan.cancel(FamilyJob.MY_FAMILY);
117122
jobMan.join(FamilyJob.MY_FAMILY, null);
118-
plugin = null;
123+
instance = null;
119124
super.stop(context);
120125
}
121126

122-
/**
123-
* Returns the shared instance
124-
*
125-
* @return the shared instance
126-
*/
127-
public static Activator getDefault() {
128-
return plugin;
129-
}
130-
131127
static boolean isInternetReachable() {
132128
HttpURLConnection urlConnect = null;
133129

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoConst.java

Lines changed: 111 additions & 109 deletions
Large diffs are not rendered by default.

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoInstancePreferences.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,10 @@ public static String GetARDUINODefineValue() {
231231
String fields[] = Ret.split("\\.");
232232
if (fields.length != 3) {
233233
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Malformed Arduino IDE version expected X.Y.Z got " + Ret, null));
234+
} else {
235+
Ret = String.format("%d%02d%02d", Integer.valueOf(fields[0]), Integer.valueOf(fields[1]), Integer.valueOf(fields[2]));
234236
}
235-
Ret = String.format("%d%02d%02d", Integer.valueOf(fields[0]), Integer.valueOf(fields[1]), Integer.valueOf(fields[2]));
236-
237237
}
238-
239238
return Ret;
240239
}
241240

it.baeyens.arduino.common/src/it/baeyens/arduino/common/Common.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import it.baeyens.arduino.arduino.Serial;
44

5+
import java.io.File;
56
import java.util.HashSet;
67
import java.util.Iterator;
78
import java.util.Vector;
@@ -15,6 +16,7 @@
1516
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
1617
import org.eclipse.core.resources.IProject;
1718
import org.eclipse.core.resources.IResource;
19+
import org.eclipse.core.resources.IWorkspaceRoot;
1820
import org.eclipse.core.resources.ResourcesPlugin;
1921
import org.eclipse.core.runtime.CoreException;
2022
import org.eclipse.core.runtime.IAdaptable;
@@ -457,7 +459,7 @@ public static String[] listBaudRates() {
457459
return outgoing;
458460
}
459461

460-
public static Object listLineEndings() {
462+
public static String[] listLineEndings() {
461463
String outgoing[] = { "none", "CR", "NL", "CR/NL" };
462464
return outgoing;
463465
}
@@ -576,4 +578,15 @@ public static String getDefaultPrivateHardwarePath() {
576578
return homPath.append("Arduino").append("hardware").toString();
577579
}
578580

581+
public static File getArduinoIDEEnvVarsName() {
582+
return getPluginWritePath(ARDUINO_IDE_ENVIRONMENT_VAR_FILE_NAME);
583+
}
584+
585+
private static File getPluginWritePath(String name) {
586+
IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
587+
File ret = myWorkspaceRoot.getLocation().append(name).toFile();
588+
return ret;
589+
// return PlatformUI.getWorkbench(). .getWorkbench().getActiveWorkbenchWindow();
590+
}
591+
579592
}

it.baeyens.arduino.core/src/it/baeyens/arduino/toolchain/ArduinoLanguageProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected String getCompilerCommand(String languageId) {
158158
try {
159159
compilerCommand = envManager.getVariable(ArduinoConst.ENV_KEY_recipe_c_o_pattern, confDesc, true).getValue().replace(" -o ", " ");
160160
} catch (Exception e) {
161-
compilerCommand = "gcc";
161+
compilerCommand = "";
162162
}
163163
IEnvironmentVariable op1 = envManager.getVariable(ArduinoConst.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc, true);
164164
IEnvironmentVariable op2 = envManager.getVariable(ArduinoConst.ENV_KEY_JANTJE_ADDITIONAL_C_COMPILE_OPTIONS, confDesc, true);
@@ -170,7 +170,11 @@ protected String getCompilerCommand(String languageId) {
170170
}
171171
compilerCommand = compilerCommand + " -D__IN_ECLIPSE__=1";
172172
} else if (languageId.equals("org.eclipse.cdt.core.g++")) {
173-
compilerCommand = envManager.getVariable(ArduinoConst.ENV_KEY_recipe_cpp_o_pattern, confDesc, true).getValue().replace(" -o ", " ");
173+
try {
174+
compilerCommand = envManager.getVariable(ArduinoConst.ENV_KEY_recipe_cpp_o_pattern, confDesc, true).getValue().replace(" -o ", " ");
175+
} catch (Exception e) {
176+
compilerCommand = "";
177+
}
174178
IEnvironmentVariable op1 = envManager.getVariable(ArduinoConst.ENV_KEY_JANTJE_ADDITIONAL_COMPILE_OPTIONS, confDesc, true);
175179
IEnvironmentVariable op2 = envManager.getVariable(ArduinoConst.ENV_KEY_JANTJE_ADDITIONAL_CPP_COMPILE_OPTIONS, confDesc, true);
176180
if (op1 != null) {

it.baeyens.arduino.core/src/it/baeyens/arduino/tools/ArduinoHelpers.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -618,17 +618,17 @@ private static void setTheEnvironmentVariablesSetTheDefaults(IContributedEnviron
618618
}
619619

620620
/**
621-
* This method parses the platform.txt file for values to be added to the environment variables
621+
* This method parses a file with environment variables like the platform.txt file for values to be added to the environment variables
622622
*
623623
* @param contribEnv
624624
* @param confDesc
625-
* @param platformFile
625+
* @param envVarFile
626626
* The file to parse
627627
* @throws IOException
628628
*/
629-
private static void setTheEnvironmentVariablesAddthePlatformTxt(IContributedEnvironment contribEnv, ICConfigurationDescription confDesc,
630-
IPath platformFile) throws IOException {
631-
try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(platformFile.toOSString()));
629+
private static void setTheEnvironmentVariablesAddAFile(IContributedEnvironment contribEnv, ICConfigurationDescription confDesc, IPath envVarFile)
630+
throws IOException {
631+
try (DataInputStream dataInputStream = new DataInputStream(new FileInputStream(envVarFile.toOSString()));
632632
BufferedReader br = new BufferedReader(new InputStreamReader(dataInputStream));) {
633633
String strLine;
634634

@@ -799,10 +799,13 @@ public static void setTheEnvironmentVariables(IProject project, ICConfigurationD
799799
// and boards.txt will
800800
// overwrite the default settings
801801
setTheEnvironmentVariablesSetTheDefaults(contribEnv, confDesc, platformFilename);
802+
802803
try {
804+
IPath arduinoIDEVarsFile = new Path(Common.getArduinoIDEEnvVarsName().getCanonicalPath());
805+
setTheEnvironmentVariablesAddAFile(contribEnv, confDesc, arduinoIDEVarsFile);
803806
// process the platform.txt file first. This way the boards.txt will
804807
// overwrite the default settings
805-
setTheEnvironmentVariablesAddthePlatformTxt(contribEnv, confDesc, platformFilename);
808+
setTheEnvironmentVariablesAddAFile(contribEnv, confDesc, platformFilename);
806809
// now process the boards file
807810
setTheEnvironmentVariablesAddtheBoardsTxt(contribEnv, confDesc, boardFileName, boardName);
808811
// Do some post processing

it.baeyens.arduino.core/src/it/baeyens/arduino/ui/ArduinoPreferencePage.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import it.baeyens.arduino.common.ArduinoInstancePreferences;
55
import it.baeyens.arduino.common.Common;
66
import it.baeyens.arduino.tools.ArduinoHelpers;
7+
import it.baeyens.arduino.tools.ExternalCommandLauncher;
78
import it.baeyens.arduino.tools.MyDirectoryFieldEditor;
89

910
import java.awt.Desktop;
11+
import java.io.BufferedWriter;
1012
import java.io.File;
13+
import java.io.FileWriter;
1114
import java.io.IOException;
1215
import java.net.URI;
1316
import java.net.URISyntaxException;
17+
import java.util.List;
1418

1519
import org.eclipse.core.filesystem.URIUtil;
1620
import org.eclipse.core.resources.IPathVariableManager;
@@ -136,15 +140,14 @@ private boolean showError(String dialogMessage) {
136140
public boolean performOk() {
137141
if (!testStatus())
138142
return false;
139-
if (!mIsDirty)
140-
return true;
141143

142144
if (mArduinoIdeVersion.getStringValue().compareTo("1.5.0") < 0) {
143145
showError("This plugin is for Arduino IDE 1.5.x. \nPlease use V1 of the plugin for earlier versions.");
144146
return false;
145147
}
146148
String infoMessage = null;
147149
boolean addMake = true;
150+
boolean getEnvVarsFromArduinoIDE = true;
148151
switch (mArduinoIdeVersion.getStringValue()) {
149152
case "1.5.0":
150153
case "1.5.1":
@@ -153,12 +156,14 @@ public boolean performOk() {
153156
case "1.5.4":
154157
infoMessage = "Arduino IDE " + mArduinoIdeVersion.getStringValue() + " is not supported.";
155158
addMake = false;
159+
getEnvVarsFromArduinoIDE = false;
156160
break;
157161
case "1.5.5":
158162
case "1.5.6":
159163
case "1.5.6-r2":
160164
infoMessage = "Arduino IDE " + mArduinoIdeVersion.getStringValue() + " works but you need to adapt some libraries. Not Advised";
161165
addMake = false;
166+
getEnvVarsFromArduinoIDE = false;
162167
break;
163168
case "1.5.7":
164169
case "1.5.8":
@@ -168,14 +173,17 @@ public boolean performOk() {
168173
} else {
169174
infoMessage = "Arduino IDE " + mArduinoIdeVersion.getStringValue() + " works.";
170175
}
176+
getEnvVarsFromArduinoIDE = false;
171177
break;
172178
case "1.6.1":
173179
infoMessage = "Arduino IDE " + mArduinoIdeVersion.getStringValue() + " works great.";
180+
getEnvVarsFromArduinoIDE = false;
174181
break;
175182
case "1.6.2":
176183
case "1.6.3":
177184
case "1.6.4":
178185
infoMessage = "Arduino IDE " + mArduinoIdeVersion.getStringValue() + " only works with Teensy.";
186+
getEnvVarsFromArduinoIDE = false;
179187
break;
180188
default:
181189
infoMessage = "You are using a version of the Arduino IDE that is unknow or newer than available at the release of this plugin.";
@@ -192,6 +200,46 @@ public boolean performOk() {
192200

193201
super.performOk();
194202
setWorkSpacePathVariables();
203+
File arduinoIDEenvVars = Common.getArduinoIDEEnvVarsName();
204+
205+
if (getEnvVarsFromArduinoIDE) {
206+
IPath ArduinoIDEPath = ArduinoInstancePreferences.getArduinoPath();
207+
String command = "\"" + URIUtil.toURI(ArduinoIDEPath.append("arduino")).getPath() + "\" --get-pref";
208+
ExternalCommandLauncher commandLauncher = new ExternalCommandLauncher(command);
209+
try {
210+
if (commandLauncher.launch() != 0) {
211+
Common.log(new Status(IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID,
212+
"Failed to extract environment info from arduino ide 'arduino --get-pref'. The setup will not work properly", null));
213+
}
214+
} catch (IOException e) {
215+
Common.log(new Status(IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID,
216+
"Failed to extract environment info from arduino ide 'arduino --get-pref'. The setup will not work properly", e));
217+
}
218+
// List<String> err = commandLauncher.getStdErr();
219+
List<String> out = commandLauncher.getStdOut();
220+
try (BufferedWriter output = new BufferedWriter(new FileWriter(arduinoIDEenvVars));) {
221+
output.write("This file has been generated automatically.");
222+
output.newLine();
223+
output.write("Please do not change.");
224+
output.newLine();
225+
for (String item : out) {
226+
output.write(item);
227+
output.newLine();
228+
}
229+
output.close();
230+
} catch (IOException e) {
231+
Common.log(new Status(IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID,
232+
"Failed to extract environment info from arduino ide 'arduino --get-pref'. The setup will not work properly", e));
233+
234+
}
235+
} else {
236+
try {
237+
arduinoIDEenvVars.delete();
238+
} catch (Exception e) {
239+
Common.log(new Status(IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID, "Failed to delete the file " + arduinoIDEenvVars
240+
+ ". The setup may not work properly", e));
241+
}
242+
}
195243
// reset the previous selected values
196244
ArduinoInstancePreferences.SetLastUsedArduinoBoard("");
197245
ArduinoInstancePreferences.SetLastUsedUploadPort("");

0 commit comments

Comments
 (0)