1313import com .google .cloud .model .Result ;
1414import com .google .cloud .model .Version ;
1515import com .google .cloud .model .VersionKey ;
16+ import com .google .cloud .tools .opensource .classpath .ClassPathBuilder ;
17+ import com .google .cloud .tools .opensource .classpath .DependencyMediation ;
18+ import com .google .cloud .tools .opensource .dependencies .Bom ;
19+ import com .google .cloud .tools .opensource .dependencies .MavenRepositoryException ;
1620import java .io .IOException ;
1721import java .net .URISyntaxException ;
1822import java .net .http .HttpClient ;
23+ import java .nio .file .Paths ;
1924import java .util .ArrayDeque ;
2025import java .util .ArrayList ;
2126import java .util .HashSet ;
2227import java .util .List ;
2328import java .util .Queue ;
2429import java .util .Set ;
30+ import org .eclipse .aether .artifact .Artifact ;
31+ import org .eclipse .aether .version .InvalidVersionSpecificationException ;
2532
2633public class DependencyAnalyzer {
2734
@@ -34,6 +41,48 @@ public DependencyAnalyzer(DepsDevClient depsDevClient) {
3441 public AnalysisResult analyze (String system , String packageName , String packageVersion )
3542 throws URISyntaxException , IOException , InterruptedException , IllegalArgumentException {
3643 VersionKey root = VersionKey .from (system , packageName , packageVersion );
44+ return AnalysisResult .of (getPackageInfoFrom (root ));
45+ }
46+
47+ public AnalysisResult analyze (String bomPath ) {
48+ List <PackageInfo > packageInfos = new ArrayList <>();
49+ try {
50+ Set <VersionKey > roots = getManagedDependenciesFromBom (Bom .readBom (Paths .get (bomPath )));
51+ for (VersionKey versionKey : roots ) {
52+ packageInfos .addAll (getPackageInfoFrom (versionKey ));
53+ }
54+
55+ } catch (MavenRepositoryException | InvalidVersionSpecificationException ex ) {
56+ System .out .printf ("Caught exception when resolving dependencies from %s." , bomPath );
57+ ex .printStackTrace ();
58+ System .exit (1 );
59+ } catch (URISyntaxException | IOException | InterruptedException ex ) {
60+ System .out .print ("Caught exception when retrieving dependency info from https://deps.dev/." );
61+ ex .printStackTrace ();
62+ System .exit (2 );
63+ }
64+
65+ return AnalysisResult .of (packageInfos );
66+ }
67+
68+ private static Set <VersionKey > getManagedDependenciesFromBom (Bom bom )
69+ throws InvalidVersionSpecificationException {
70+ Set <VersionKey > res = new HashSet <>();
71+ new ClassPathBuilder ()
72+ .resolve (bom .getManagedDependencies (), false , DependencyMediation .MAVEN )
73+ .getClassPath ()
74+ .forEach (
75+ classPath -> {
76+ Artifact artifact = classPath .getArtifact ();
77+ String pkg = String .format ("%s:%s" , artifact .getGroupId (), artifact .getArtifactId ());
78+ res .add (VersionKey .from ("MAVEN" , pkg , artifact .getVersion ()));
79+ });
80+
81+ return res ;
82+ }
83+
84+ private List <PackageInfo > getPackageInfoFrom (VersionKey root )
85+ throws URISyntaxException , IOException , InterruptedException {
3786 Set <VersionKey > seenPackage = new HashSet <>();
3887 seenPackage .add (root );
3988 Queue <VersionKey > queue = new ArrayDeque <>();
@@ -68,7 +117,7 @@ public AnalysisResult analyze(String system, String packageName, String packageV
68117 result .add (new PackageInfo (versionKey , licenses , advisories ));
69118 }
70119
71- return AnalysisResult . of ( result ) ;
120+ return result ;
72121 }
73122
74123 /**
0 commit comments