1919import io .sloeber .core .tools .Version ;
2020
2121/**
22- * A class to apply workarounds to installed packages
23- * workaround are done after installation
24- * at usage of boards.txt file
25- * at usage of platform.txt file
22+ * A class to apply workarounds to installed packages workaround are done after
23+ * installation at usage of boards.txt file at usage of platform.txt file
2624 *
27- * The first line of the worked around files contain a key
28- * A newer version of sloeber that has a different workaround shuld change the key
29- * in this way the worked around files can be persisted and updated when needed
25+ * The first line of the worked around files contain a key A newer version of
26+ * sloeber that has a different workaround shuld change the key in this way the
27+ * worked around files can be persisted and updated when needed
3028 *
3129 * @author jan
3230 *
3331 */
3432@ SuppressWarnings ("nls" )
3533public class WorkAround {
36- //Each time this class is touched consider changing the String below to enforce updates
34+ // Each time this class is touched consider changing the String below to enforce
35+ // updates
3736 private static final String FIRST_SLOEBER_WORKAROUND_LINE = "#Sloeber created workaound file V1.00.test 3" ;
3837
39-
4038 /**
41- * workarounds done at installation time.
42- * I try to keep those at a minimum but none platform.txt and boards.txt
43- * workarounds need to be doine during install time
39+ * workarounds done at installation time. I try to keep those at a minimum but
40+ * none platform.txt and boards.txt workarounds need to be doine during install
41+ * time
4442 *
4543 * @param platform
4644 */
47- static public void applyKnownWorkArounds (ArduinoPlatform platform ) {
45+ static synchronized public void applyKnownWorkArounds (ArduinoPlatform platform ) {
4846
4947 /*
5048 * for STM32 V1.8 and later #include "SrcWrapper.h" to Arduino.h remove the
@@ -70,105 +68,101 @@ static public void applyKnownWorkArounds(ArduinoPlatform platform) {
7068 MakeBoardsSloeberTxt (platform .getBoardsFile ());
7169
7270 }
73-
74-
71+
7572 /**
7673 * create a workedaround boards.txt and return that filz
77- *
74+ *
7875 * @param requestedFileToWorkAround
7976 *
80- * @return the worked around file or requestedFileToWorkAround is it does not exist or error
77+ * @return the worked around file or requestedFileToWorkAround is it does not
78+ * exist or error
8179 */
82- static public File MakeBoardsSloeberTxt (File requestedFileToWorkAround ) {
83- if (!requestedFileToWorkAround .exists ()) {
80+ static synchronized public File MakeBoardsSloeberTxt (File requestedFileToWorkAround ) {
81+ if (!requestedFileToWorkAround .exists ()) {
8482 return requestedFileToWorkAround ;
8583 }
86- String inFile = requestedFileToWorkAround .toString ();
87- String actualFileToLoad = inFile .replace (Const .BOARDS_FILE_NAME ,"boards.sloeber.txt" );
88- if (inFile .equals (actualFileToLoad )) {
84+ String inFile = requestedFileToWorkAround .toString ();
85+ String actualFileToLoad = inFile .replace (Const .BOARDS_FILE_NAME , "boards.sloeber.txt" );
86+ if (inFile .equals (actualFileToLoad )) {
8987 Common .log (new Status (IStatus .ERROR , Activator .getId (),
9088 "Boards.txt file is not recognized " + requestedFileToWorkAround .toString ()));
9189 return requestedFileToWorkAround ;
9290 }
93- File boardsSloeberTXT = new File (actualFileToLoad );
94- if (boardsSloeberTXT .exists ()) {
95- //delete if outdated
91+ File boardsSloeberTXT = new File (actualFileToLoad );
92+ if (boardsSloeberTXT .exists ()) {
93+ // delete if outdated
9694 String firstLine = null ;
97- try (BufferedReader Buff = new BufferedReader (new FileReader (boardsSloeberTXT ));) {
95+ try (BufferedReader Buff = new BufferedReader (new FileReader (boardsSloeberTXT ));) {
9896 firstLine = Buff .readLine ();
9997 } catch (Exception e ) {
100- //ignore and delete the file
101- }
102- if (!FIRST_SLOEBER_WORKAROUND_LINE .equals (firstLine )) {
98+ // ignore and delete the file
99+ }
100+ if (!FIRST_SLOEBER_WORKAROUND_LINE .equals (firstLine )) {
103101 boardsSloeberTXT .delete ();
104102 }
105103 }
106- if (!boardsSloeberTXT .exists ()) {
107- if (requestedFileToWorkAround .exists ()) {
108- try {
109- if (SystemUtils .IS_OS_WINDOWS ) {
110- String boardsTXT = FIRST_SLOEBER_WORKAROUND_LINE +"\n " ;
111- boardsTXT += FileUtils .readFileToString (requestedFileToWorkAround , Charset .defaultCharset ());
112- boardsTXT = boardsTXT .replace ("\r \n " , "\n " );
113-
114-
115- // replace FI circuitplay32u4cat.build.usb_manufacturer="Adafruit"
116- // with circuitplay32u4cat.build.usb_manufacturer=Adafruit
117- boardsTXT = boardsTXT .replaceAll ("(\\ S+\\ .build\\ .usb\\ S+)=\\ \" (.+)\\ \" " , "$1=$2" );
118-
119-
120- FileUtils .write (boardsSloeberTXT , boardsTXT , Charset .defaultCharset ());
121- }
122- } catch (IOException e ) {
123- // TODO Auto-generated catch block
124- e .printStackTrace ();
104+ if (!boardsSloeberTXT .exists ()) {
105+ try {
106+ String boardsTXT = FIRST_SLOEBER_WORKAROUND_LINE + "\n " ;
107+ boardsTXT += FileUtils .readFileToString (requestedFileToWorkAround , Charset .defaultCharset ());
108+ boardsTXT = boardsTXT .replace ("\r \n " , "\n " );
109+
110+ if (SystemUtils .IS_OS_WINDOWS ) {
111+ // replace FI circuitplay32u4cat.build.usb_manufacturer="Adafruit"
112+ // with circuitplay32u4cat.build.usb_manufacturer=Adafruit
113+ boardsTXT = boardsTXT .replaceAll ("(\\ S+\\ .build\\ .usb\\ S+)=\\ \" (.+)\\ \" " , "$1=$2" );
125114 }
115+ FileUtils .write (boardsSloeberTXT , boardsTXT , Charset .defaultCharset ());
116+ } catch (IOException e ) {
117+ Common .log (new Status (IStatus .WARNING , Activator .getId (),
118+ "Failed to apply work arounds to " + requestedFileToWorkAround .toString (), e ));
119+ return requestedFileToWorkAround ;
126120 }
127121 }
128122 return boardsSloeberTXT ;
129123 }
130-
131-
124+
132125 /**
133126 * create a workedaround platform.txt and return that filz
134- *
127+ *
135128 * @param requestedFileToWorkAround
136129 *
137- * @return the worked around file or requestedFileToWorkAround is it does not exist or error
130+ * @return the worked around file or requestedFileToWorkAround is it does not
131+ * exist or error
138132 */
139- public static File MakePlatformSloeberTXT (File requestedFileToWorkAround ) {
140- if (!requestedFileToWorkAround .exists ()) {
133+ public synchronized static File MakePlatformSloeberTXT (File requestedFileToWorkAround ) {
134+ if (!requestedFileToWorkAround .exists ()) {
141135 return requestedFileToWorkAround ;
142136 }
143- String inFile = requestedFileToWorkAround .toString ();
144- String actualFileToLoad = inFile .replace (Const .PLATFORM_FILE_NAME ,"platform.sloeber.txt" );
145- if (inFile .equals (actualFileToLoad )) {
137+ String inFile = requestedFileToWorkAround .toString ();
138+ String actualFileToLoad = inFile .replace (Const .PLATFORM_FILE_NAME , "platform.sloeber.txt" );
139+ if (inFile .equals (actualFileToLoad )) {
146140 Common .log (new Status (IStatus .ERROR , Activator .getId (),
147141 "platform.txt file is not recognized " + requestedFileToWorkAround .toString ()));
148142 return requestedFileToWorkAround ;
149143 }
150- File platformSloeberTXT = new File (actualFileToLoad );
151- if (platformSloeberTXT .exists ()) {
152- //delete if outdated
144+ File platformSloeberTXT = new File (actualFileToLoad );
145+ if (platformSloeberTXT .exists ()) {
146+ // delete if outdated
153147 String firstLine = null ;
154- try (BufferedReader Buff = new BufferedReader (new FileReader (platformSloeberTXT ));) {
148+ try (BufferedReader Buff = new BufferedReader (new FileReader (platformSloeberTXT ));) {
155149 firstLine = Buff .readLine ();
156150 } catch (Exception e ) {
157- //ignore and delete the file
158- }
159- if (!FIRST_SLOEBER_WORKAROUND_LINE .equals (firstLine )) {
151+ // ignore and delete the file
152+ }
153+ if (!FIRST_SLOEBER_WORKAROUND_LINE .equals (firstLine )) {
160154 platformSloeberTXT .delete ();
161155 }
162156 }
163157 if (!platformSloeberTXT .exists ()) {
164158 try {
165- String platformTXT = FIRST_SLOEBER_WORKAROUND_LINE + "\n " ;
159+ String platformTXT = FIRST_SLOEBER_WORKAROUND_LINE + "\n " ;
166160 platformTXT += FileUtils .readFileToString (requestedFileToWorkAround , Charset .defaultCharset ());
167161 platformTXT = platformTXT .replace ("\r \n " , "\n " );
168-
169- //Arduino treats core differently so we need to change the location of directly
170- //referenced files this manifestates only in the combine recipe
171- int inCombineStartIndex = platformTXT .indexOf ("\n recipe.c.combine.pattern" )+ 1 ;
162+
163+ // Arduino treats core differently so we need to change the location of directly
164+ // referenced files this manifestates only in the combine recipe
165+ int inCombineStartIndex = platformTXT .indexOf ("\n recipe.c.combine.pattern" ) + 1 ;
172166 if (inCombineStartIndex > 0 ) {
173167 int inCombineEndIndex = platformTXT .indexOf ("\n " , inCombineStartIndex ) - 1 ;
174168 if (inCombineEndIndex > 0 ) {
@@ -180,15 +174,14 @@ public static File MakePlatformSloeberTXT(File requestedFileToWorkAround) {
180174 }
181175 }
182176
183-
184177 // workaround for infineon arm v1.4.0 overwriting the default to a wrong value
185178 platformTXT = platformTXT .replace ("\n build.core.path" , "\n #line removed by Sloeber build.core.path" );
186179
187- Path platformTXTPath = new Path (requestedFileToWorkAround .toString ());
188- int totalSegments = platformTXTPath .segmentCount ();
189- String platformVersion = platformTXTPath .segment (totalSegments - 2 );
190- String platformArchitecture = platformTXTPath .segment (totalSegments - 3 );
191- String platformName = platformTXTPath .segment (totalSegments - 5 );
180+ Path platformTXTPath = new Path (requestedFileToWorkAround .toString ());
181+ int totalSegments = platformTXTPath .segmentCount ();
182+ String platformVersion = platformTXTPath .segment (totalSegments - 2 );
183+ String platformArchitecture = platformTXTPath .segment (totalSegments - 3 );
184+ String platformName = platformTXTPath .segment (totalSegments - 5 );
192185 if (Version .compare ("1.8.0" , platformVersion ) != 1 ) {
193186 if ("stm32" .equals (platformArchitecture )) {
194187 if ("STM32" .equals (platformName )) {
@@ -198,30 +191,29 @@ public static File MakePlatformSloeberTXT(File requestedFileToWorkAround) {
198191 }
199192 }
200193
201-
202- //for adafruit nfr
203- platformTXT = platformTXT . replace ( "-DARDUINO_BSP_VERSION= \" {version} \" " , "\" -DARDUINO_BSP_VERSION=\\ \" {version}\\ \" \" " );
204-
194+ // for adafruit nfr
195+ platformTXT = platformTXT . replace ( "-DARDUINO_BSP_VERSION= \" {version} \" " ,
196+ "\" -DARDUINO_BSP_VERSION=\\ \" {version}\\ \" \" " );
197+
205198 if (SystemUtils .IS_OS_WINDOWS ) {
206199 // replace FI '-DUSB_PRODUCT={build.usb_product}' with
207200 // "-DUSB_PRODUCT=\"{build.usb_product}\""
208201 platformTXT = platformTXT .replaceAll ("\\ '-D(\\ S+)=\\ {(\\ S+)}\\ '" , "\" -D$1=\\ \\ \" {$2}\\ \\ \" \" " );
209-
210-
211- //quoting fixes for embedutils
212- platformTXT = platformTXT .replaceAll ("\" ?(-DMBEDTLS_\\ S+)=\\ \\ ?\" (mbedtls\\ S+)\" \\ \\ ?\" *" , "\" $1=\\ \\ \" $2\\ \\ \" \" " );
213-
214- //Sometimes "-DUSB_MANUFACTURER={build.usb_manufacturer}" "-DUSB_PRODUCT={build.usb_product}"
215- //is used fi LinKit smart
216- platformTXT = platformTXT .replace ("\" -DUSB_MANUFACTURER={build.usb_manufacturer}\" " ,
202+
203+ // quoting fixes for embedutils
204+ platformTXT = platformTXT .replaceAll ("\" ?(-DMBEDTLS_\\ S+)=\\ \\ ?\" (mbedtls\\ S+)\" \\ \\ ?\" *" ,
205+ "\" $1=\\ \\ \" $2\\ \\ \" \" " );
206+
207+ // Sometimes "-DUSB_MANUFACTURER={build.usb_manufacturer}"
208+ // "-DUSB_PRODUCT={build.usb_product}"
209+ // is used fi LinKit smart
210+ platformTXT = platformTXT .replace ("\" -DUSB_MANUFACTURER={build.usb_manufacturer}\" " ,
217211 "\" -DUSB_MANUFACTURER=\\ \" {build.usb_manufacturer}\\ \" \" " );
218- platformTXT = platformTXT .replace ("\" -DUSB_PRODUCT={build.usb_product}\" " ,
212+ platformTXT = platformTXT .replace ("\" -DUSB_PRODUCT={build.usb_product}\" " ,
219213 "\" -DUSB_PRODUCT=\\ \" {build.usb_product}\\ \" \" " );
220- platformTXT = platformTXT .replace (" -DARDUINO_BOARD=\" {build.board}\" " ,
214+ platformTXT = platformTXT .replace (" -DARDUINO_BOARD=\" {build.board}\" " ,
221215 " \" -DARDUINO_BOARD=\\ \" {build.board}\\ \" \" " );
222-
223216
224-
225217 }
226218 FileUtils .write (platformSloeberTXT , platformTXT , Charset .defaultCharset ());
227219 } catch (IOException e ) {
0 commit comments