-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathCompileAndUpload.java
202 lines (170 loc) · 6.77 KB
/
CompileAndUpload.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package io.sloeber.core;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.stream.Stream;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swt.widgets.Display;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import io.sloeber.arduinoFramework.api.BoardsManager;
import io.sloeber.core.api.CodeDescription;
import io.sloeber.core.api.CompileDescription;
import io.sloeber.core.api.ConfigurationPreferences;
import io.sloeber.core.api.ISloeberConfiguration;
import io.sloeber.core.api.ConfigurationPreferences;
import io.sloeber.core.api.Sketch;
import io.sloeber.core.api.SloeberProject;
import io.sloeber.providers.Arduino;
import io.sloeber.providers.ESP32;
import io.sloeber.providers.ESP8266;
import io.sloeber.providers.MCUBoard;
import io.sloeber.providers.Teensy;
import io.sloeber.ui.monitor.SerialConnection;
@SuppressWarnings({ "nls", "unused" })
public class CompileAndUpload {
private static final boolean reinstall_boards_and_libraries = false;
private static String interval = "1500";// change between 1500 and 100
static Stream<Arguments> uploadBourds() throws Exception {
installAdditionalBoards();
Shared.waitForAllJobsToFinish();
File file = ConfigurationPreferences.getInstallationPath().append("test.properties").toFile();
if (!file.exists()) {
file.createNewFile();
}
Properties properties = new Properties();
try (FileInputStream fileInput = new FileInputStream(file)) {
properties.load(fileInput);
fileInput.close();
}
String key = "Last Used Blink Interval";
interval = properties.getProperty(key);
if ("100".equals(interval)) {
interval = "1500";
} else {
interval = "100";
}
properties.put(key, interval);
try (FileOutputStream fileOutput = new FileOutputStream(file);) {
properties.store(fileOutput, "This is a file with values for unit testing");
fileOutput.close();
}
MCUBoard[] boards = MySystem.getUploadBoards();
List<Arguments> ret = new LinkedList<>();
for (MCUBoard curBoard : boards) {
ret.add(Arguments.of(curBoard));
}
return ret.stream();
}
public static void installAdditionalBoards() {
ConfigurationPreferences.setUseBonjour(false);
String[] packageUrlsToAdd = { ESP32.packageURL, ESP8266.packageURL };
BoardsManager.addPackageURLs(new HashSet<>(Arrays.asList(packageUrlsToAdd)));
if (reinstall_boards_and_libraries) {
BoardsManager.removeAllInstalledPlatforms();
}
// make sure the needed boards are available
ESP8266.installLatest();
Arduino.installLatestAVRBoards();
Arduino.installLatestSamDBoards();
Arduino.installLatestIntellCurieBoards();
Arduino.installLatestSamBoards();
Teensy.installLatest();
}
@ParameterizedTest
@MethodSource("uploadBourds")
public void testExamples(MCUBoard myBoard) throws Exception {
String myName = myBoard.getID();
IPath templateFolder = Shared.getTemplateFolder("fastBlink");
CompileDescription compileOptions = new CompileDescription();
DateTimeFormatter df = DateTimeFormatter.ofPattern("YYYY/MM/dd-HH-mm-ss");
String SerialDumpContent = myName + '-' + df.format(LocalDateTime.now());
compileOptions.set_C_andCPP_CompileOptions("-DINTERVAL=" + interval + " -DSERIAlDUMP=" + SerialDumpContent);
CodeDescription codeDescriptor = CodeDescription.createCustomTemplate(templateFolder);
Map<String, String> replacers = new TreeMap<>();
replacers.put("{SerialMonitorSerial}", myBoard.mySerialPort);
codeDescriptor.setReplacers(replacers);
Build_Verify_upload(myBoard, codeDescriptor, compileOptions, SerialDumpContent);
}
public void Build_Verify_upload(MCUBoard myBoard, CodeDescription codeDescriptor, CompileDescription compileOptions,
String SerialDumpContent) throws Exception {
String myName = myBoard.getID();
IProject theTestProject = null;
NullProgressMonitor monitor = new NullProgressMonitor();
String projectName = String.format("%05d_%s", Integer.valueOf(Shared.buildCounter++), myName);
theTestProject = SloeberProject.createArduinoProject(projectName, null, myBoard.getBoardDescriptor(),
codeDescriptor, compileOptions, monitor);
Shared.waitForIndexer(theTestProject);
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
if (Shared.hasBuildErrors(theTestProject) != null) {
// try again because the libraries may not yet been added
Shared.waitForIndexer(theTestProject);
Thread.sleep(3000);// seen sometimes the libs were still not
// added
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
// give up
assertNull(Shared.hasBuildErrors(theTestProject));
}
ISloeberConfiguration sloeberConfiguration = ISloeberConfiguration.getActiveConfig(theTestProject);
IStatus uploadStatus = sloeberConfiguration.upload();
if (!uploadStatus.isOK()) {
fail("Failed to upload:" + projectName);
}
// Wait a while for the board to recover from the upload
Thread.sleep(10000);
verifySerialOutput(myBoard, SerialDumpContent);
}
boolean serialOutputMismatch;
public void verifySerialOutput(MCUBoard myBoard, String serialDumpContent) throws Exception {
String comPort = myBoard.getBoardDescriptor().getActualUploadPort();
Display display = SerialConnection.getDisplay();
display.syncExec(new Runnable() {
@Override
public void run() {
SerialConnection.show();
SerialConnection.clearMonitor();
SerialConnection.add(comPort, 9600);
}
});
Thread.sleep(2000);
serialOutputMismatch = true;
display.syncExec(new Runnable() {
@Override
public void run() {
SerialConnection.remove(comPort);
List<String> lines = SerialConnection.getContent();
serialOutputMismatch = (!lines.contains(serialDumpContent));
if (serialOutputMismatch) {
System.err.println("recieved from: " + comPort);
System.err.println(lines);
System.err.println("End of serial input");
}
}
});
if (serialOutputMismatch) {
fail("Serial output does not match epectation " + serialDumpContent);
}
}
}