2828public class CodeGenerator {
2929 private final StackType type ;
3030 private final String name ;
31+ private final String override ;
3132 private final LocalDragonConfiguration localConfiguration ;
3233 private DSSession .Section section ;
3334 private StackMetadata stackMetadata ;
3435
35- public CodeGenerator (StackType type , String name , LocalDragonConfiguration localConfiguration ) {
36+ public CodeGenerator (StackType type , String name , String override , LocalDragonConfiguration localConfiguration ) {
3637 this .type = type ;
3738 this .name = name ;
39+ this .override = override ;
3840 this .localConfiguration = localConfiguration ;
3941 }
4042
@@ -49,68 +51,141 @@ public void work() throws DSException {
4951 dest .mkdirs ();
5052 }
5153
52- final String rootResourcesDir = "stacks/" ;
53-
54+ String rootResourcesDir = "stacks/" ;
5455 final ObjectMapper mapper = new ObjectMapper ().configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
55- try {
56- stackMetadata = mapper .readValue (Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/metadata.json" ), StackMetadata .class );
5756
58- if (stackMetadata .hasURL ()) {
59- downloadFile (stackMetadata .getUrl (), dest , stackMetadata .getSkipDirectoryLevel ());
57+ if (override != null ) {
58+ switch (type ) {
59+ case REACT :
60+ section .print ("overriding" );
61+ try {
62+ rootResourcesDir = "https://raw.githubusercontent.com/loiclefevre/dragon/master/stacks/create-react-app/" ;
63+ stackMetadata = mapper .readValue (downloadFile (rootResourcesDir + override + "/metadata.json" ), StackMetadata .class );
64+
65+ for (String fileName : stackMetadata .getFiles ()) {
66+ if (fileName .endsWith ("/" )) {
67+ final File dir = new File (dest , fileName );
68+ if (!dir .exists ()) dir .mkdirs ();
69+ } else {
70+ final String path = fileName .substring (fileName .lastIndexOf ('/' ) + 1 );
71+ final String subDir = fileName .substring (0 , fileName .length () - path .length ());
72+ final InputStream inputStream = downloadFile (rootResourcesDir + override + "/data/" + fileName );
73+ if (inputStream == null ) {
74+ throw new StackFileNotFoundException (type .humanName , fileName , rootResourcesDir + type .resourceDir + "/data/" + fileName );
75+ }
76+ extractFileContent (path , new File (dest , subDir ), inputStream );
77+ }
78+ }
79+ } catch (IOException e ) {
80+ throw new LoadStackMetadataException (type .humanName , e );
81+ }
82+ section .printlnOK (type .humanName + ": " + name +"#" +override );
83+
84+
85+ final ST st = new ST (
86+ new BufferedReader (
87+ new InputStreamReader (downloadFile (rootResourcesDir + override + "/message.st" ), StandardCharsets .UTF_8 ))
88+ .lines ()
89+ .collect (Collectors .joining ("\n " )), '<' , '>' );
90+ st .add ("name" , name );
91+ st .add ("path" , dest .getAbsolutePath ());
92+ st .add ("override" , override );
93+
94+ System .out .println (st .render ());
95+
96+ break ;
6097 }
98+ } else {
99+
100+ try {
101+ stackMetadata = mapper .readValue (Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/metadata.json" ), StackMetadata .class );
102+
103+ if (stackMetadata .hasURL ()) {
104+ downloadFile (stackMetadata .getUrl (), dest , stackMetadata .getSkipDirectoryLevel ());
105+ }
61106
62- //System.out.println(stackMetadata.getFiles());
63-
64- for (String fileName : stackMetadata .getFiles ()) {
65- if (fileName .endsWith ("/" )) {
66- final File dir = new File (dest , fileName );
67- if (!dir .exists ()) dir .mkdirs ();
68- } else {
69- final String path = fileName .substring (fileName .lastIndexOf ('/' ) + 1 );
70- final String subDir = fileName .substring (0 , fileName .length () - path .length ());
71- final InputStream inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/data/" + fileName );
72- if (inputStream == null ) {
73- throw new StackFileNotFoundException (type .humanName , fileName , rootResourcesDir + type .resourceDir + "/data/" + fileName );
107+ //System.out.println(stackMetadata.getFiles());
108+
109+ for (String fileName : stackMetadata .getFiles ()) {
110+ if (fileName .endsWith ("/" )) {
111+ final File dir = new File (dest , fileName );
112+ if (!dir .exists ()) dir .mkdirs ();
113+ } else {
114+ final String path = fileName .substring (fileName .lastIndexOf ('/' ) + 1 );
115+ final String subDir = fileName .substring (0 , fileName .length () - path .length ());
116+ final InputStream inputStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/data/" + fileName );
117+ if (inputStream == null ) {
118+ throw new StackFileNotFoundException (type .humanName , fileName , rootResourcesDir + type .resourceDir + "/data/" + fileName );
119+ }
120+ extractFileContent (path , new File (dest , subDir ), inputStream );
74121 }
75- extractFileContent (path , new File (dest , subDir ), inputStream );
76122 }
123+
124+ } catch (IOException e ) {
125+ throw new LoadStackMetadataException (type .humanName , e );
77126 }
78127
79- } catch (IOException e ) {
80- throw new LoadStackMetadataException (type .humanName , e );
81- }
128+ section .printlnOK (type .humanName + ": " + name );
82129
83- section . printlnOK ( type . humanName + ": " + name );
130+ final Map < String , String > patchParameters = new HashMap <>( );
84131
85- final Map <String , String > patchParameters = new HashMap <>();
132+ if (type .codePatcher != null ) {
133+ section = DSSession .Section .PostProcessingStack ;
134+ section .print ("patching" );
135+ patchParameters .putAll (type .codePatcher .patch (dest , localConfiguration ));
136+ section .printlnOK ();
137+ }
86138
87- if (type .codePatcher != null ) {
88- section = DSSession .Section .PostProcessingStack ;
89- section .print ("patching" );
90- patchParameters .putAll (type .codePatcher .patch (dest , localConfiguration ));
91- section .printlnOK ();
92- }
139+ final ST st = new ST (
140+ new BufferedReader (
141+ new InputStreamReader (Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/message.st" ), StandardCharsets .UTF_8 ))
142+ .lines ()
143+ .collect (Collectors .joining ("\n " )), '<' , '>' );
144+ st .add ("name" , name );
145+ st .add ("path" , dest .getAbsolutePath ());
93146
94- final ST st = new ST (
95- new BufferedReader (
96- new InputStreamReader (Thread .currentThread ().getContextClassLoader ().getResourceAsStream (rootResourcesDir + type .resourceDir + "/message.st" ), StandardCharsets .UTF_8 ))
97- .lines ()
98- .collect (Collectors .joining ("\n " )), '<' , '>' );
99- st .add ("name" , name );
100- st .add ("path" , dest .getAbsolutePath ());
147+ for (String key : patchParameters .keySet ()) {
148+ st .add (key , patchParameters .get (key ));
149+ }
101150
102- for (String key : patchParameters .keySet ()) {
103- st .add (key , patchParameters .get (key ));
151+ System .out .println (st .render ());
104152 }
153+ }
154+
155+ private InputStream downloadFile (String url ) throws DSException {
156+ try {
157+ final HttpRequest requestDownload = HttpRequest .newBuilder ()
158+ .uri (new URI (url ))
159+ .setHeader ("Pragma" , "no-cache" )
160+ .setHeader ("Cache-Control" , "no-store" )
161+ .GET ()
162+ .build ();
163+
164+ final HttpResponse <InputStream > responseDownload = HttpClient
165+ .newBuilder ()
166+ .version (HttpClient .Version .HTTP_1_1 )
167+ .proxy (ProxySelector .getDefault ())
168+ .followRedirects (HttpClient .Redirect .NORMAL )
169+ .build ()
170+ .send (requestDownload , HttpResponse .BodyHandlers .ofInputStream ());
171+
172+ if (responseDownload .statusCode () != 200 ) {
173+ section .printlnKO ();
174+ throw new StackFileDownloadException (url , responseDownload .statusCode ());
175+ }
105176
106- System .out .println (st .render ());
177+ return new BufferedInputStream (responseDownload .body (),1024 *128 );
178+ } catch (Exception e ) {
179+ throw new StackFileDownloadException (url , e );
180+ }
107181 }
108182
109183 private void downloadFile (String url , File dest , int skipDirLevel ) throws DSException {
110184 try {
111185 final HttpRequest requestDownload = HttpRequest .newBuilder ()
112186 .uri (new URI (url ))
113187 .setHeader ("Pragma" , "no-cache" )
188+ .setHeader ("Cache-Control" , "no-store" )
114189 .GET ()
115190 .build ();
116191
0 commit comments