1
1
import os
2
2
import re
3
- import subprocess
3
+ import sys
4
4
5
5
import analyser
6
6
import filecmp
10
10
import utility
11
11
import subprocess
12
12
13
- import xml .dom .minidom
14
-
15
13
class CollectedJavaBinaries :
16
14
def __init__ (self ):
17
15
self .class_files = []
@@ -88,16 +86,16 @@ def run_di_model_generation(configuration, cmdline):
88
86
# If we don't have anything vaguely looking like a spring library being used, just skip generation.
89
87
if not next ((lib for lib in cmdline .libraries if re .search ("spring-[a-zA-Z]*-[0-9]*\.[0-9]*" , lib )), None ) \
90
88
or cmdline .skip_di_generation :
91
- print ("No Spring Framework libraries passed in, skipping DI generation." )
92
- return
89
+ print ("No Spring Framework libraries passed in, cannot generate any DI-related code." , file = sys . stderr )
90
+ return None
93
91
94
92
di_output_file_path = configuration ["diConfigurationPath" ]
95
93
detected_entry_points_path = configuration ["detectedEntryPointsPath" ]
96
- di_starting_script = os .path .abspath (os .path .join (os .path .dirname (os .path .realpath (__file__ )), ".." , "env-model-generator" , "built" , " env-model-generator.js " ))
94
+ di_starting_script = os .path .abspath (os .path .join (os .path .dirname (os .path .realpath (__file__ )), ".." , "env-model-generator" , "env-model-generator" ))
97
95
98
96
# Right now we're only using our metadata generated files, but this should also include XML.
99
97
spring_files = []
100
- if os .path .exists ( di_output_file_path ) and os . path . isfile (di_output_file_path ):
98
+ if os .path .isfile (di_output_file_path ):
101
99
spring_files .append (di_output_file_path )
102
100
103
101
# Clear down any existing files.
@@ -106,18 +104,25 @@ def run_di_model_generation(configuration, cmdline):
106
104
os .rmdir (generated_source_file_path )
107
105
os .mkdir (generated_source_file_path )
108
106
109
- di_generation_commandline = ["node" , di_starting_script , "" .join (spring_files [:1 ]), "--input-file" , " " .join (spring_files [1 :]), "--output-path" , generated_source_file_path , "--entry-points-input-file" , detected_entry_points_path ]
107
+ primary_input_file = "" .join (spring_files [:1 ])
108
+ if not os .path .isfile (primary_input_file ):
109
+ print ("No files available to generate DI overlays." )
110
+ return None
110
111
111
- print ("Running commandline: " + " " .join (di_generation_commandline ))
112
- di_generation_result = subprocess .run (di_generation_commandline , stderr = subprocess .STDOUT )
113
- if di_generation_result .returncode != 0 :
114
- print ("DI generation failed." )
115
- return
112
+ di_generation_commandline = [di_starting_script , primary_input_file , "--output-path" , generated_source_file_path , "--entry-points-input-file" , detected_entry_points_path ]
113
+
114
+ additional_spring_files = spring_files [1 :]
115
+ if additional_spring_files :
116
+ di_generation_commandline .extend (["--input-file" ] + additional_spring_files )
117
+
118
+ di_generation_result = utility .call (di_generation_commandline , "DI model generator" , cmdline .verbosity , stderr = subprocess .STDOUT )
119
+ if di_generation_result != 0 :
120
+ return None
116
121
117
122
# Quick check to see if we have any output files.
118
123
if not next ((file for root , dirs , files in os .walk (generated_source_file_path ) for file in files ), None ):
119
124
print ("No Java source files emitted by DI generation." )
120
- return
125
+ return None
121
126
122
127
# Clear down yet more existing files.
123
128
java_binaries_path = os .path .join (cmdline .common_dir , "GENERATED_BINARIES" )
@@ -130,29 +135,35 @@ def run_di_model_generation(configuration, cmdline):
130
135
class_paths = cmdline .libraries + [os .path .join (cmdline .common_dir , "collected_classes.jar" )]
131
136
132
137
generated_jar_path = os .path .join (cmdline .common_dir , "DI-models.jar" )
133
- ant_build_file = ('<project default="compile">'
134
- '<target name="compile">'
135
- '<javac includeantruntime="true" srcdir="' + generated_source_file_path + '" destdir="' + java_binaries_path + '">'
136
- + '<classpath>' + "" .join (["<pathelement location=\" " + path + "\" />" for path in class_paths ]) + '</classpath>' +
137
- '</javac>'
138
- '<jar destfile="' + generated_jar_path + '" basedir="' + java_binaries_path + '"/>'
139
- '</target>'
140
- '</project>' )
141
-
142
- # Attempt to prettify output a little.
143
- ant_build_file = xml .dom .minidom .parseString (ant_build_file ).toprettyxml ()
138
+
139
+ ant_format_arguments = {
140
+ "generated_source_file_path" : generated_source_file_path ,
141
+ "class_paths" : os .linesep .join ([" <pathelement location=\" " + path + "\" />" for path in class_paths ]),
142
+ "generated_jar_path" : generated_jar_path ,
143
+ "java_binaries_path" : java_binaries_path
144
+ }
145
+
146
+ ant_build_statement = """
147
+ <project default="compile">
148
+ <target name="compile">
149
+ <javac includeantruntime="true" srcdir="{generated_source_file_path}" destdir="{java_binaries_path}">
150
+ <classpath>
151
+ {class_paths}
152
+ </classpath>
153
+ </javac>
154
+ <jar destfile="{generated_jar_path}" basedir="{java_binaries_path}"/>
155
+ </target>
156
+ </project>
157
+ """ .format (** ant_format_arguments )
144
158
145
159
ant_build_path = os .path .join (generated_source_file_path , "build.xml" )
146
160
with open (ant_build_path , 'x' ) as ant_xml_file :
147
- ant_xml_file .write (ant_build_file )
148
-
149
- javac_commandline = ["ant" , "-file" , ant_build_path , "-lib" , os .pathsep .join (class_paths )]
161
+ ant_xml_file .write (ant_build_statement )
150
162
151
- print ("Running commandline: " + " " .join (javac_commandline ))
152
- javac_result = subprocess .run (javac_commandline , stderr = subprocess .STDOUT )
153
- if javac_result .returncode != 0 :
163
+ javac_result = utility .call (["ant" , "-file" , ant_build_path ], "Java Compiler for DI model overlays" , cmdline .verbosity , stderr = subprocess .STDOUT )
164
+ if javac_result != 0 :
154
165
print ("Java compilation of DI-generated source failed." )
155
- return
166
+ return None
156
167
157
168
return generated_jar_path
158
169
@@ -259,9 +270,11 @@ def collect_java_binaries(cmdline):
259
270
# If DI generation has run we want to target its output jar as our starting point.
260
271
target_binary = java_binaries .jar_file
261
272
if di_output_path :
262
- class_paths .append (java_binaries . jar_file )
273
+ class_paths .append (target_binary )
263
274
class_paths .append (di_output_path ) # Appended because we need overlay classes too.
264
275
target_binary = di_output_path
276
+ else :
277
+ print ("DI generation failed, continuing without synthetic entry points." , file = sys .stderr )
265
278
266
279
with open (entry_points_file ) as ep_config_file :
267
280
ep_config = json .load (ep_config_file )
0 commit comments