77import java .io .IOException ;
88import java .io .InputStream ;
99import java .io .Writer ;
10+ import java .nio .file .Files ;
11+ import java .nio .file .Path ;
12+ import java .nio .file .Paths ;
1013import java .util .Date ;
14+ import java .util .List ;
1115import java .util .Scanner ;
16+ import java .util .stream .Collectors ;
17+ import java .util .stream .Stream ;
1218
1319import org .springframework .core .io .ClassPathResource ;
1420import org .springframework .core .io .Resource ;
21+
1522import com .fasterxml .jackson .core .JsonProcessingException ;
23+
1624import io .github .jeemv .springboot .vuejs .AbstractVueJS ;
1725import io .github .jeemv .springboot .vuejs .beans .RawObject ;
1826import io .github .jeemv .springboot .vuejs .configuration .VueConfig ;
@@ -34,6 +42,7 @@ public class VueComponent extends AbstractVueJS{
3442 private String template ;
3543 private boolean internal ;
3644 private VueProps props ;
45+ private static final String ROOT_FOLDER ="src/main/resources/static/" ;
3746
3847 public VueComponent () {
3948 this (null ,true );
@@ -161,7 +170,7 @@ protected void loadTemplateFile(String filename) throws IOException {
161170 * @throws IOException for file creation
162171 */
163172 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 );
165174 File f =new File (resource .getAbsolutePath ().toString ()+"\\ " +pathFilename );
166175 f .getParentFile ().mkdirs ();
167176 if (f .exists ()) {
@@ -196,9 +205,21 @@ private void writeInfile(File f,String pathFilename,boolean minify) throws IOExc
196205 if (!minify ) {
197206 contents =JsUtils .cleanJS (contents );
198207 }
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 );
202223 }finally {
203224 try {
204225 if ( writer != null )
@@ -207,7 +228,46 @@ private void writeInfile(File f,String pathFilename,boolean minify) throws IOExc
207228 }
208229 }
209230
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." );
212272 }
213273}
0 commit comments