7
7
import java .io .IOException ;
8
8
import java .io .InputStream ;
9
9
import java .io .Writer ;
10
+ import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
12
+ import java .nio .file .Paths ;
10
13
import java .util .Date ;
14
+ import java .util .List ;
11
15
import java .util .Scanner ;
16
+ import java .util .stream .Collectors ;
17
+ import java .util .stream .Stream ;
12
18
13
19
import org .springframework .core .io .ClassPathResource ;
14
20
import org .springframework .core .io .Resource ;
21
+
15
22
import com .fasterxml .jackson .core .JsonProcessingException ;
23
+
16
24
import io .github .jeemv .springboot .vuejs .AbstractVueJS ;
17
25
import io .github .jeemv .springboot .vuejs .beans .RawObject ;
18
26
import io .github .jeemv .springboot .vuejs .configuration .VueConfig ;
@@ -34,6 +42,7 @@ public class VueComponent extends AbstractVueJS{
34
42
private String template ;
35
43
private boolean internal ;
36
44
private VueProps props ;
45
+ private static final String ROOT_FOLDER ="src/main/resources/static/" ;
37
46
38
47
public VueComponent () {
39
48
this (null ,true );
@@ -161,7 +170,7 @@ protected void loadTemplateFile(String filename) throws IOException {
161
170
* @throws IOException for file creation
162
171
*/
163
172
public void createFile (String pathFilename ,boolean minify ) throws IOException {
164
- File resource = new File ("src/main/resources/static/" );
173
+ File resource = new File (ROOT_FOLDER );
165
174
File f =new File (resource .getAbsolutePath ().toString ()+"\\ " +pathFilename );
166
175
f .getParentFile ().mkdirs ();
167
176
if (f .exists ()) {
@@ -196,9 +205,21 @@ private void writeInfile(File f,String pathFilename,boolean minify) throws IOExc
196
205
if (!minify ) {
197
206
contents =JsUtils .cleanJS (contents );
198
207
}
199
- writer .write (contents );
200
- System .out .println ("Script generated in " +f .getCanonicalPath ());
201
- System .out .println ("Insert <script src=\" /" +pathFilename +"\" ></script> in html file to use it." );
208
+ writeInfile (f , pathFilename , contents , "Script generated in " +f .getCanonicalPath ()+"\r \n Insert <script src=\" /" +pathFilename +"\" ></script> in html file to use it." );
209
+ }finally {
210
+ try {
211
+ if ( writer != null )
212
+ writer .close ( );
213
+ }catch ( IOException e ){}
214
+ }
215
+ }
216
+
217
+ private static void writeInfile (File f ,String pathFilename ,String contents ,String consoleMessages ) throws IOException {
218
+ Writer writer =null ;
219
+ try {
220
+ writer = new BufferedWriter (new FileWriter (f ));
221
+ writer .write (contents );
222
+ System .out .println (consoleMessages );
202
223
}finally {
203
224
try {
204
225
if ( writer != null )
@@ -207,7 +228,46 @@ private void writeInfile(File f,String pathFilename,boolean minify) throws IOExc
207
228
}
208
229
}
209
230
210
- private String addGenerated () {
211
- return "//Script generated with " +this .getClass ().getSimpleName ()+" at " +new Date ()+"\r \n " ;
231
+ private static String addGenerated () {
232
+ return "//Script generated with " +VueComponent .class .getSimpleName ()+" at " +new Date ()+"\r \n " ;
233
+ }
234
+
235
+ public static void globalJs () {
236
+ File resource = new File (ROOT_FOLDER );
237
+ String path =resource .getAbsolutePath ().toString ()+"/" +VueConfig .getComponentFolder ();
238
+ try (Stream <Path > walk = Files .walk (Paths .get (path ))) {
239
+
240
+ List <String > result = walk .map (x -> x .toString ())
241
+ .filter (f -> f .endsWith (".js" )).collect (Collectors .toList ());
242
+
243
+ globalJs (result .toArray (new String [0 ]));
244
+
245
+ } catch (IOException e ) {
246
+ e .printStackTrace ();
247
+ }
248
+ }
249
+ public static void globalJs (String ...files ) throws IOException {
250
+ StringBuilder sb = new StringBuilder ("" );
251
+ for (int i =0 ;i <files .length ;i ++) {
252
+ File f =new File (files [i ]);
253
+ if (f .exists () && !f .getName ().equals ("components.js" )) {
254
+ Scanner scanner = new Scanner (f );
255
+ while (scanner .hasNextLine ()) {
256
+ String line =scanner .nextLine ();
257
+ if (!line .startsWith ("//" )) {
258
+ sb .append (line +"\r \n " );
259
+ }
260
+ }
261
+ scanner .close ();
262
+ }
263
+ }
264
+ if (files .length >0 ) {
265
+ sb .append (addGenerated ());
266
+ String pathFilename =VueConfig .getComponentFolder ()+"/components.js" ;
267
+ File resource = new File (ROOT_FOLDER );
268
+ File f =new File (resource .getAbsolutePath ().toString ()+"\\ " +pathFilename );
269
+ writeInfile (f , pathFilename , sb .toString (), "Script generated in " +f .getCanonicalPath ()+"\r \n Insert <script src=\" /" +pathFilename +"\" ></script> in html file to use it." );
270
+ }else
271
+ System .out .println ("No files to parse." );
212
272
}
213
273
}
0 commit comments