Skip to content
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

JSON & CSV parameters #100

Merged
merged 8 commits into from
Aug 12, 2021
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
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Gemfile.lock
*.gem
*.idea
*.iml
*.gradle/
build/
userHome/

target/

Expand All @@ -15,13 +18,12 @@ depclean-maven-plugin/target

depclean-core/depclean-core.iml
depclean-core/target/generated-sources/
depclean-gradle-plugin/.gradle/4.10.2/vcsMetadata-1/
depclean-gradle-plugin/build/tmp/compileJava/
depclean-gradle-plugin/depclean-gradle-plugin.iml
depclean-gradle-plugin/example/.gradle/4.10.2/vcsMetadata-1/
depclean-gradle-plugin/example/build/dependencies/
depclean-gradle-plugin/example/build/tmp/compileJava/
depclean-gradle-plugin/target/generated-sources/
depclean-gradle-plugin/src/test/resources-fts/*/originalOutputFile.txt
depclean-gradle-plugin/src/test/resources-fts/*/debloated-dependencies.gradle
depclean-maven-plugin/depclean-maven-plugin.iml
depclean-maven-plugin/target/generated-sources/
depclean-maven-plugin/target/generated-test-sources/
Expand Down
3 changes: 0 additions & 3 deletions depclean-gradle-plugin/.gitignore

This file was deleted.

36 changes: 24 additions & 12 deletions depclean-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
plugins {
id 'java'
id 'java-gradle-plugin'
id 'java'
id 'groovy'
id 'java-gradle-plugin'
id 'maven-publish'
}

group 'se.kth.castor'
version '1.0-SNAPSHOT'

repositories {
mavenLocal()
mavenCentral()
mavenLocal()
mavenCentral()
}

dependencies {
implementation(gradleApi())
implementation('se.kth.castor:depclean-core:2.0.2-SNAPSHOT')
implementation('se.kth.castor:depclean-maven-plugin:2.0.2-SNAPSHOT')
compileOnly('org.projectlombok:lombok:1.18.20')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation(gradleApi())
implementation(gradleTestKit())
implementation('se.kth.castor:depclean-core:2.0.2-SNAPSHOT')
implementation('se.kth.castor:depclean-maven-plugin:2.0.2-SNAPSHOT')
compileOnly('org.projectlombok:lombok:1.18.20')
testImplementation('org.spockframework:spock-core:2.0-groovy-3.0')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
}

test {
useJUnitPlatform()
}
useJUnitPlatform()
}

gradlePlugin {
plugins {
demoPlugin {
id = 'se.kth.castor.depclean-gradle-plugin'
implementationClass = 'se.kth.depclean.DepCleanGradlePlugin'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
Expand Down Expand Up @@ -32,6 +34,7 @@
import se.kth.depclean.analysis.GradleProjectDependencyAnalysis;
import se.kth.depclean.core.analysis.ProjectDependencyAnalyzerException;
import se.kth.depclean.util.JarUtils;
import se.kth.depclean.utils.json.writeJsonResult;

/**
* Depclean default and only action.
Expand Down Expand Up @@ -60,7 +63,6 @@ public class DepCleanGradleAction implements Action<Project> {
private boolean failIfUnusedTransitive;
private boolean failIfUnusedInherited;
private boolean createBuildDebloated;
// TODO : The implementation of next two parameters will be done later.
private boolean createResultJson;
private boolean createClassUsageCsv;
private Set<String> ignoreConfiguration;
Expand Down Expand Up @@ -402,6 +404,50 @@ public void execute(@NotNull Project project) {
logger.lifecycle("debloated-dependencies.gradle file created in: "
+ pathToDebloatedDependencies);
}

/* Writing the JSON file with the debloat results */
if (createResultJson) {
printString("Creating depclean-results.json, please wait...");
final File jsonFile = projectDirPath.resolve("build" + File.separator + "depclean-results.json").toFile();
final File classUsageFile = projectDirPath.resolve("build" + File.separator + "class-usage.csv").toFile();
if (createClassUsageCsv) {
printString("Creating class-usage.csv, please wait...");
try {
FileUtils.write(classUsageFile, "OriginClass,TargetClass,Dependency\n", Charset.defaultCharset());
} catch (IOException e) {
logger.error("Error writing the CSV header.");
}
}
writeJsonResult writeJsonResult = new writeJsonResult(
project,
classUsageFile,
dependencyAnalyzer,
SizeOfDependencies,
createClassUsageCsv,
declaredDependencies,
usedDirectArtifactsCoordinates,
usedInheritedArtifactsCoordinates,
usedTransitiveArtifactsCoordinates,
unusedDirectArtifactsCoordinates,
unusedInheritedArtifactsCoordinates,
unusedTransitiveArtifactsCoordinates
);
try {
FileWriter fw = new FileWriter(jsonFile, Charset.defaultCharset());
writeJsonResult.write(fw);
fw.flush();
fw.close();
} catch (IOException e) {
logger.error("Unable to generate JSON file.");
}
if (jsonFile.exists()) {
logger.lifecycle("depclean-results.json file created in: " + jsonFile.getAbsolutePath());
}
if (classUsageFile.exists()) {
logger.lifecycle("class-usage.csv file created in: " + classUsageFile.getAbsolutePath());
}
}

}

/**
Expand Down Expand Up @@ -561,9 +607,7 @@ private String getSize(final String dependency) {
* @return Name of artifact
*/
public static String getName(final ResolvedArtifact artifact) {
String[] artifactGroupArtifactIds = artifact.toString().split(" \\(");
String[] artifactGroupArtifactId = artifactGroupArtifactIds[1].split("\\)");
return artifactGroupArtifactId[0] + ":" + ArtifactConfigurationMap.get(artifact);
return artifact.getModuleVersion() + ":" + ArtifactConfigurationMap.get(artifact);
}

/**
Expand Down Expand Up @@ -624,5 +668,4 @@ public Set<ResolvedDependency> getAllChildren(final Set<ResolvedDependency> allD
}
return allChildren;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class DepCleanGradlePlugin implements Plugin<Project> {

@Override
public void apply(@NotNull Project project) {

final String depCleanConfigurationName = "depclean";

// Creating extra configurations for the plugin to provide more flexibility.
project.getExtensions().create(depCleanConfigurationName, DepCleanGradlePluginExtension.class);

// Creating the default task.
createTask(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedDependency;
import se.kth.depclean.utils.DependencyUtils;
import se.kth.depclean.core.analysis.ArtifactTypes;
import se.kth.depclean.utils.DependencyUtils;
import se.kth.depclean.core.analysis.ClassAnalyzer;
import se.kth.depclean.core.analysis.DefaultClassAnalyzer;
import se.kth.depclean.core.analysis.DependencyAnalyzer;
Expand Down Expand Up @@ -311,4 +311,29 @@ private Set<ResolvedArtifact> removeAll(
return results;
}

/**
* Computes a map of [artifact] -> [allTypes, usedTypes].
*
* @return A map of [artifact] -> [allTypes, usedTypes]
*/
public Map<String, ArtifactTypes> getArtifactClassesMap() {
Map<String, ArtifactTypes> output = new HashMap<>();
for (Map.Entry<ResolvedArtifact, Set<String>> entry : artifactClassesMap.entrySet()) {
ResolvedArtifact key = entry.getKey();
if (artifactUsedClassesMap.containsKey(key)) {
output.put(key.getModuleVersion().toString(),
new ArtifactTypes(
artifactClassesMap.get(key), // get all the types
artifactUsedClassesMap.get(key) // get used types
));
} else {
output.put(key.getModuleVersion().toString(),
new ArtifactTypes(
artifactClassesMap.get(key), // get all the types
new HashSet<>() // get used types
));
}
}
return output;
}
}
Loading