Skip to content

Fix issue 74 #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=1.1.0
version=1.1.1

3 changes: 3 additions & 0 deletions src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class CodeAnalyzer implements Runnable {
@Option(names = {"-v", "--verbose"}, description = "Print logs to console.")
private static boolean verbose = false;

@Option(names = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies")
public static boolean noCleanDependencies = false;

private static final String outputFileName = "analysis.json";

public static Gson gson = new GsonBuilder()
Expand Down
41 changes: 30 additions & 11 deletions src/main/java/com/ibm/cldk/utils/BuildProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.ibm.cldk.utils.ProjectDirectoryScanner.classFilesStream;
import static com.ibm.cldk.CodeAnalyzer.projectRootPom;
import static com.ibm.cldk.CodeAnalyzer.noCleanDependencies;

public class BuildProject {
public static Path libDownloadPath;
Expand Down Expand Up @@ -188,6 +189,17 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
return buildProject(projectPath, build) ? classFilesStream(projectPath) : new ArrayList<>();
}

private static boolean mkLibDepDirs(String projectPath) {
if (!Files.exists(libDownloadPath)) {
try {
Files.createDirectories(libDownloadPath);
} catch (IOException e) {
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
return false;
}
}
return true;
}
/**
* Downloads library dependency jars of the given project so that the jars can be used
* for type resolution during symbol table creation.
Expand All @@ -198,17 +210,15 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
public static boolean downloadLibraryDependencies(String projectPath, String projectRootPom) throws IOException {
// created download dir if it does not exist
String projectRoot = projectRootPom != null ? projectRootPom : projectPath;
libDownloadPath = Paths.get(projectPath, LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (!Files.exists(libDownloadPath)) {
try {
Files.createDirectory(libDownloadPath);
} catch (IOException e) {
Log.error("Error creating library dependency directory for " + projectPath + ": " + e.getMessage());
return false;
}
}

File pomFile = new File(projectRoot, "pom.xml");
if (pomFile.exists()) {
libDownloadPath = Paths.get(projectPath, "target", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (mkLibDepDirs(projectPath))
Log.debug("Dependencies found/created in " + libDownloadPath);
else
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);

if (MAVEN_CMD == null || !commandExists(new File(MAVEN_CMD)).getKey()) {
String msg = MAVEN_CMD == null ?
"Could not find Maven or a valid Maven Wrapper" :
Expand All @@ -225,6 +235,12 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
return buildWithTool(mavenCommand);
} else if (new File(projectRoot, "build.gradle").exists() || new File(projectRoot, "build.gradle.kts").exists()) {
if (GRADLE_CMD == null || !commandExists(new File(GRADLE_CMD)).getKey()) {
libDownloadPath = Paths.get(projectPath, "build", LIB_DEPS_DOWNLOAD_DIR).toAbsolutePath();
if (mkLibDepDirs(projectPath))
Log.debug("Dependencies found/created in " + libDownloadPath);
else
throw new IllegalStateException("Error creating library dependency directory in " + libDownloadPath);

String msg = GRADLE_CMD == null ?
"Could not find Gradle or valid Gradle Wrapper" :
MessageFormat.format("Could not verify that {0} exists", GRADLE_CMD);
Expand All @@ -249,20 +265,23 @@ public static boolean downloadLibraryDependencies(String projectPath, String pro
}

public static void cleanLibraryDependencies() {
if (noCleanDependencies) {
return;
}
if (libDownloadPath != null) {
Log.info("Cleaning up library dependency directory: " + libDownloadPath);
try {
Files.walk(libDownloadPath).filter(Files::isRegularFile).map(Path::toFile).forEach(File::delete);
Files.delete(libDownloadPath);
} catch (IOException e) {
Log.error("Error deleting library dependency directory: " + e.getMessage());
Log.warn("Unable to fully delete library dependency directory: " + e.getMessage());
}
}
if (tempInitScript != null) {
try {
Files.delete(tempInitScript);
} catch (IOException e) {
Log.error("Error deleting temporary Gradle init script: " + e.getMessage());
Log.warn("Error deleting temporary Gradle init script: " + e.getMessage());
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/ibm/cldk/utils/ProjectDirectoryScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ProjectDirectoryScanner {
public static List<Path> classFilesStream(String projectPath) throws IOException {
Expand Down Expand Up @@ -37,6 +39,30 @@ public static List<Path> jarFilesStream(String projectPath) throws IOException {
return null;
}

/**
* Returns a stream of class files inside the jars and class files in the project.
* @param projectPath
* @return
* @throws IOException
*/
public static Stream<String> classesFromJarFileStream(String projectPath) throws IOException {
List<Path> jarPaths = jarFilesStream(projectPath);

if (jarPaths == null) {
return Stream.empty();
}

return jarPaths.stream().flatMap(jarPath -> {
try (ZipFile zip = new ZipFile(jarPath.toFile())) {
return zip.stream()
.filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class"))
.map(ZipEntry::getName);
} catch (IOException e) {
return Stream.empty();
}
});
}

public static List<Path> sourceFilesStream(String projectPath) throws IOException {
Path projectDir = Paths.get(projectPath);
Log.info("Finding *.java files in " + projectDir);
Expand Down