1
1
package io .sloeber .core .api ;
2
2
3
- import java .net .URL ;
3
+ import static io .sloeber .core .common .Const .*;
4
+
4
5
import java .util .List ;
5
6
import java .util .Map ;
6
7
import java .util .Set ;
7
- import java .util .TreeMap ;
8
8
9
9
import org .eclipse .cdt .core .model .CoreModel ;
10
- import org .eclipse .cdt .core .model .ICModelMarker ;
11
10
import org .eclipse .cdt .core .settings .model .ICConfigurationDescription ;
12
11
import org .eclipse .cdt .core .settings .model .ICProjectDescription ;
13
- import org .eclipse .core .resources .IMarker ;
14
12
import org .eclipse .core .resources .IProject ;
15
- import org .eclipse .core .resources .IResource ;
16
- import org .eclipse .core .resources .IncrementalProjectBuilder ;
17
13
import org .eclipse .core .runtime .CoreException ;
18
14
import org .eclipse .core .runtime .IPath ;
19
- import org .eclipse .core .runtime .IProgressMonitor ;
20
15
import org .eclipse .core .runtime .IStatus ;
21
16
import org .eclipse .core .runtime .Path ;
22
17
import org .eclipse .core .runtime .Status ;
23
- import org .eclipse .core .runtime .jobs .Job ;
24
- import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
25
- import org .eclipse .core .runtime .preferences .InstanceScope ;
26
- import org .eclipse .ui .console .MessageConsole ;
27
- import org .osgi .service .prefs .BackingStoreException ;
28
18
29
- import io .sloeber .core .Activator ;
30
19
import io .sloeber .core .Messages ;
31
- import io .sloeber .core .common .Common ;
32
- import io .sloeber .core .common .Const ;
33
20
import io .sloeber .core .common .IndexHelper ;
34
21
import io .sloeber .core .tools .Helpers ;
35
22
import io .sloeber .core .tools .Libraries ;
36
- import io .sloeber .core .tools .uploaders .UploadSketchWrapper ;
37
23
38
- @ SuppressWarnings ("unused" )
39
24
public class Sketch {
40
- // preference nodes
41
- public static final String NODE_ARDUINO = Activator .NODE_ARDUINO ;
42
25
43
26
public static IStatus isUploadableProject (IProject project ) {
44
27
try {
45
- if (project == null || !project .hasNature (Const . ARDUINO_NATURE_ID )) {
46
- return new Status (IStatus .ERROR , Const . CORE_PLUGIN_ID , Messages .Upload_no_arduino_sketch , null );
28
+ if (project == null || !project .hasNature (ARDUINO_NATURE_ID )) {
29
+ return new Status (IStatus .ERROR , CORE_PLUGIN_ID , Messages .Upload_no_arduino_sketch , null );
47
30
}
48
31
} catch (CoreException e ) {
49
- return new Status (IStatus .ERROR , Const . CORE_PLUGIN_ID , Messages .Upload_Project_nature_unaccesible , e );
32
+ return new Status (IStatus .ERROR , CORE_PLUGIN_ID , Messages .Upload_Project_nature_unaccesible , e );
50
33
}
51
34
return Status .OK_STATUS ;
52
35
}
53
36
54
- /**
55
- * Asynchronous upload of the sketch. The upload status has to be followed in
56
- * the GUI
57
- *
58
- * @param project
59
- *
60
- */
61
- public static Job asyncUpload (IProject project ) {
62
- IStatus ret = isUploadableProject (project );
63
- if (!ret .isOK ()) {
64
- Common .log (ret );
65
- return null ;
66
- }
67
-
68
- return UploadSketchWrapper .upload (project ,
69
- CoreModel .getDefault ().getProjectDescription (project ).getActiveConfiguration ());
70
37
71
- }
72
38
73
39
/**
74
40
* Synchronous upload of the sketch returning the status.
@@ -77,104 +43,13 @@ public static Job asyncUpload(IProject project) {
77
43
* @return the status of the upload. Status.OK means upload is OK
78
44
*/
79
45
public static IStatus syncUpload (IProject project ) {
46
+
80
47
IStatus ret = isUploadableProject (project );
81
48
if (!ret .isOK ()) {
82
49
return ret ;
83
50
}
84
- Job upLoadJob = UploadSketchWrapper .upload (project ,
85
- CoreModel .getDefault ().getProjectDescription (project ).getActiveConfiguration ());
86
-
87
- if (upLoadJob == null )
88
- return new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID , Messages .Upload_failed , null );
89
- try {
90
- upLoadJob .join ();
91
- return upLoadJob .getResult ();
92
- } catch (InterruptedException e ) {
93
- // not sure if this is needed
94
- return new Status (IStatus .ERROR , Const .CORE_PLUGIN_ID , Messages .Upload_failed , e );
95
- }
96
- }
97
-
98
- /**
99
- * Synchronous upload of the sketch with the errors shown in the gui.
100
- *
101
- * @param project
102
- * @return the status of the upload. true means upload is OK
103
- */
104
- public static boolean upload (IProject project ) {
105
- IStatus ret = syncUpload (project );
106
- if (!ret .isOK ()) {
107
- Common .log (ret );
108
- }
109
-
110
- return ret .isOK ();
111
- }
112
-
113
- /**
114
- * Verifies a project. Builds the active configuration If the build fails
115
- * returns false else tru
116
- *
117
- * @param project
118
- * @return
119
- */
120
- public static boolean verify (IProject project , IProgressMonitor monitor ) {
121
- MessageConsole theconsole = Helpers .findConsole ("CDT Build Console (" + project .getName () + ")" ); //$NON-NLS-1$ //$NON-NLS-2$
122
- if (theconsole != null ) {
123
- theconsole .activate ();
124
- }
125
- try {
126
- project .build (IncrementalProjectBuilder .INCREMENTAL_BUILD , monitor );
127
-
128
- Job job = new Job ("Start build Activator" ) { //$NON-NLS-1$
129
- @ Override
130
- protected IStatus run (IProgressMonitor _monitor ) {
131
- try {
132
- String buildflag = "FbStatus" ; //$NON-NLS-1$
133
- char [] uri = { 'h' , 't' , 't' , 'p' , ':' , '/' , '/' , 'b' , 'a' , 'e' , 'y' , 'e' , 'n' , 's' , '.' , 'i' ,
134
- 't' , '/' , 'e' , 'c' , 'l' , 'i' , 'p' , 's' , 'e' , '/' , 'd' , 'o' , 'w' , 'n' , 'l' , 'o' , 'a' ,
135
- 'd' , '/' , 'b' , 'u' , 'i' , 'l' , 'd' , 'S' , 't' , 'a' , 'r' , 't' , '.' , 'h' , 't' , 'm' , 'l' ,
136
- '?' , 'b' , '=' };
137
- IEclipsePreferences myScope = InstanceScope .INSTANCE .getNode (NODE_ARDUINO );
138
- int curFsiStatus = myScope .getInt (buildflag , 0 ) + 1 ;
139
- myScope .putInt (buildflag , curFsiStatus );
140
- try {
141
- myScope .flush ();
142
- } catch (BackingStoreException e ) {
143
- // this should not happen
144
- }
145
- URL pluginStartInitiator = new URL (new String (uri ) + Integer .toString (curFsiStatus ));
146
- pluginStartInitiator .getContent ();
147
- } catch (Exception e ) {
148
- e .printStackTrace ();
149
- }
150
- return Status .OK_STATUS ;
151
- }
152
- };
153
- job .setPriority (Job .DECORATE );
154
- job .schedule ();
155
- return isBuildSuccessFull (project );
156
- } catch (CoreException e ) {
157
- // don't care about the error; the only thing that matters is: the
158
- // build failed
159
- return true ;
160
- }
161
- }
162
-
163
- /**
164
- * Checks if build completed successfully.
165
- *
166
- * @return true iff project was built successfully last time.
167
- * @throws CoreException
168
- * if current project does not exist or is not open.
169
- */
170
- private static boolean isBuildSuccessFull (IProject project ) throws CoreException {
171
- IMarker [] markers = project .findMarkers (ICModelMarker .C_MODEL_PROBLEM_MARKER , true , IResource .DEPTH_INFINITE );
172
- for (IMarker marker : markers ) {
173
- if (marker .getAttribute (IMarker .SEVERITY , IMarker .SEVERITY_INFO ) == IMarker .SEVERITY_ERROR ) {
174
- return false ;
175
- }
176
- }
177
- return true ;
51
+ SloeberProject sProject = SloeberProject .getSloeberProject (project , true );
52
+ return sProject .upload ();
178
53
}
179
54
180
55
/**
@@ -205,7 +80,7 @@ public static void reAttachLibrariesToProject(IProject iProject) {
205
80
206
81
public static boolean isSketch (IProject proj ) {
207
82
try {
208
- return proj .hasNature (Const . ARDUINO_NATURE_ID );
83
+ return proj .hasNature (ARDUINO_NATURE_ID );
209
84
} catch (CoreException e ) {
210
85
// ignore
211
86
e .printStackTrace ();
@@ -221,19 +96,12 @@ public static boolean removeLibrariesFromProject(IProject project, ICProjectDesc
221
96
222
97
public static boolean addLibrariesToProject (IProject project , ICConfigurationDescription confDesc ,
223
98
Set <String > libraries ) {
224
- Map <String , List <IPath >> foldersToChange = Libraries .addLibrariesToProject (project , confDesc ,
225
- libraries );
99
+ Map <String , List <IPath >> foldersToChange = Libraries .addLibrariesToProject (project , confDesc , libraries );
226
100
return Libraries .adjustProjectDescription (confDesc , foldersToChange );
227
101
}
228
102
229
- public static Map <String , IPath > getAllAvailableLibraries (IProject project ) {
230
- ICProjectDescription prjDesc = CoreModel .getDefault ().getProjectDescription (project );
231
- if (prjDesc != null ) {
232
- return Libraries .getAllInstalledLibraries (prjDesc .getActiveConfiguration ());
233
- }
234
- Common .log (new Status (IStatus .WARNING , Const .CORE_PLUGIN_ID ,
235
- "Is the selected project a C/Cpp project? " + project .getName (), null )); //$NON-NLS-1$
236
- return new TreeMap <>(String .CASE_INSENSITIVE_ORDER );
103
+ public static Map <String , IPath > getAllAvailableLibraries (ICConfigurationDescription confDesc ) {
104
+ return Libraries .getAllInstalledLibraries (confDesc );
237
105
}
238
106
239
107
public static Set <String > getAllImportedLibraries (IProject project ) {
@@ -263,8 +131,7 @@ public static void addCodeFolder(IProject project, Path path) throws CoreExcepti
263
131
}
264
132
}
265
133
if (projDescNeedsSaving ) {
266
- coreModel .getProjectDescriptionManager ().setProjectDescription (project , projectDescription ,
267
- true , null );
134
+ coreModel .getProjectDescriptionManager ().setProjectDescription (project , projectDescription , true , null );
268
135
}
269
136
}
270
137
0 commit comments