55
66import com .azure .tools .bomgenerator .models .BomDependency ;
77import com .azure .tools .bomgenerator .models .BomDependencyManagement ;
8+ import com .azure .tools .bomgenerator .models .BomDependencyNoVersion ;
89import com .fasterxml .jackson .annotation .JsonInclude ;
910import com .fasterxml .jackson .databind .SerializationFeature ;
1011import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
3839import java .util .ArrayList ;
3940import java .util .Collection ;
4041import java .util .HashMap ;
42+ import java .util .HashSet ;
4143import java .util .List ;
4244import java .util .Map ;
45+ import java .util .Set ;
4346import java .util .regex .Matcher ;
47+ import java .util .regex .Pattern ;
4448import java .util .stream .Collectors ;
4549
4650import static com .azure .tools .bomgenerator .Utils .ANALYZE_MODE ;
@@ -69,19 +73,31 @@ public class BomGenerator {
6973 private String mode ;
7074 private String outputDirectory ;
7175 private String inputDirectory ;
76+ private final Pattern sdkDependencyPattern ;
77+ private final Set <String > groupIds ;
78+ private final DependencyFilter dependencyFilter ;
7279
7380 private static Logger logger = LoggerFactory .getLogger (BomGenerator .class );
7481
75- BomGenerator (String inputDirectory , String outputDirectory , String mode ) throws FileNotFoundException {
82+ BomGenerator (String inputDirectory , String outputDirectory , String mode , List < String > groupIds ) throws IOException {
7683 validateNotNullOrEmpty (inputDirectory , "inputDirectory" );
7784 validateNotNullOrEmpty (outputDirectory , "outputDirectory" );
7885
7986 this .inputDirectory = inputDirectory ;
8087 this .outputDirectory = outputDirectory ;
8188 this .mode = (mode == null ? GENERATE_MODE : mode );
89+ if (groupIds == null || groupIds .isEmpty ()) {
90+ logger .warn ("groupIds null, will use com.azure" );
91+ this .groupIds = Set .of (BASE_AZURE_GROUPID );
92+ this .sdkDependencyPattern = SDK_DEPENDENCY_PATTERN ;
93+ } else {
94+ this .groupIds = new HashSet <>(groupIds );
95+ this .sdkDependencyPattern = Pattern .compile (String .format ("(%s):(.+);(.+);(.+)" , String .join ("|" , this .groupIds )));
96+ }
8297
8398 parseInputs ();
8499 validateInputs ();
100+ this .dependencyFilter = new DependencyFilter (this .inputDirectory );
85101
86102 Path outputDirPath = Paths .get (outputDirectory );
87103 outputDirPath .toFile ().mkdirs ();
@@ -147,7 +163,7 @@ private boolean generate() {
147163 analyzer .reduce ();
148164 Collection <BomDependency > outputDependencies = analyzer .getBomEligibleDependencies ();
149165
150- // 2 . Create the new tree for the BOM.
166+ // 3 . Create the new tree for the BOM.
151167 analyzer = new DependencyAnalyzer (outputDependencies , externalDependencies , this .reportFileName );
152168 boolean validationFailed = analyzer .validate ();
153169 outputDependencies = analyzer .getBomEligibleDependencies ();
@@ -169,7 +185,7 @@ private List<BomDependency> scanVersioningClientFileDependencies() {
169185 for (String line : Files .readAllLines (Paths .get (inputFileName ))) {
170186 BomDependency dependency = scanDependency (line );
171187
172- if (dependency != null ) {
188+ if (dependencyFilter . apply ( dependency ) ) {
173189 inputDependencies .add (dependency );
174190 }
175191 }
@@ -244,17 +260,18 @@ private List<BomDependency> scanOverriddenDependencies() {
244260 }
245261
246262 private BomDependency scanDependency (String line ) {
247- Matcher matcher = SDK_DEPENDENCY_PATTERN .matcher (line );
263+ Matcher matcher = sdkDependencyPattern .matcher (line );
248264 if (!matcher .matches ()) {
249265 return null ;
250266 }
251267
252- if (matcher .groupCount () != 3 ) {
268+ if (matcher .groupCount () != 4 ) {
253269 return null ;
254270 }
255271
256- String artifactId = matcher .group (1 );
257- String version = matcher .group (2 );
272+ String groupId = matcher .group (1 );
273+ String artifactId = matcher .group (2 );
274+ String version = matcher .group (3 );
258275
259276 if (version .contains ("-" )) {
260277 // This is a non-GA library
@@ -264,11 +281,11 @@ private BomDependency scanDependency(String line) {
264281 if (EXCLUSION_LIST .contains (artifactId )
265282 || artifactId .contains (AZURE_PERF_LIBRARY_IDENTIFIER )
266283 || (artifactId .contains (AZURE_TEST_LIBRARY_IDENTIFIER ))) {
267- logger .trace ("Skipping dependency {}:{}" , BASE_AZURE_GROUPID , artifactId );
284+ logger .trace ("Skipping dependency {}:{}" , groupId , artifactId );
268285 return null ;
269286 }
270287
271- return new BomDependency (BASE_AZURE_GROUPID , artifactId , version );
288+ return new BomDependency (groupId , artifactId , version );
272289 }
273290
274291 private Model readModel () {
@@ -338,8 +355,44 @@ private void writeBom(Collection<BomDependency> bomDependencies) {
338355 dependencies .sort (new DependencyComparator ());
339356
340357 // Remove external dependencies from the BOM.
341- dependencies = dependencies .stream ().filter (dependency -> BASE_AZURE_GROUPID . equals (dependency .getGroupId ())).collect (Collectors .toList ());
358+ dependencies = dependencies .stream ().filter (dependency -> groupIds . contains (dependency .getGroupId ())).collect (Collectors .toList ());
342359 management .setDependencies (dependencies );
343360 writeModel (this .pomFileName , this .outputFileName , model );
344361 }
362+
363+ private static class DependencyFilter {
364+ private final Map <String , Set <BomDependencyNoVersion >> groupIdDependenciesMap = new HashMap <>();
365+ DependencyFilter (String inputDirectory ) throws IOException {
366+ Path includesDirectory = Paths .get (inputDirectory , "includes" );
367+ String fileSuffix = ".txt" ;
368+ Files .list (includesDirectory )
369+ .filter (file -> Files .isRegularFile (file ) && file .getFileName ().toString ().endsWith (fileSuffix ))
370+ .forEach (file -> {
371+ try {
372+ String fileName = file .getFileName ().toString ();
373+ String groupId = fileName .substring (0 , fileName .length () - fileSuffix .length ());
374+ Set <BomDependencyNoVersion > dependencies = groupIdDependenciesMap .computeIfAbsent (groupId , ignored -> new HashSet <>());
375+ for (String line : Files .readAllLines (file )) {
376+ if (line != null && !line .trim ().isEmpty ()) {
377+ dependencies .add (new BomDependencyNoVersion (groupId , line ));
378+ }
379+ }
380+ } catch (IOException e ) {
381+ throw new RuntimeException (e );
382+ }
383+ });
384+ }
385+
386+ boolean apply (BomDependency dependency ) {
387+ if (dependency == null ) {
388+ return false ;
389+ }
390+ Set <BomDependencyNoVersion > allowedDependencies = this .groupIdDependenciesMap .get (dependency .getGroupId ());
391+ // if allowed list is null, no filter will be applied
392+ if (allowedDependencies == null ) {
393+ return true ;
394+ }
395+ return allowedDependencies .contains (Utils .toBomDependencyNoVersion (dependency ));
396+ }
397+ }
345398}
0 commit comments