Skip to content

Commit 4e6d6de

Browse files
Setting build
1 parent 164f30c commit 4e6d6de

File tree

3 files changed

+100
-67
lines changed

3 files changed

+100
-67
lines changed
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,9 @@
11
package se.kth.transitive_changes;
22

3-
import se.kth.breaking_changes.ApiMetadata;
4-
import se.kth.data.JsonUtils;
5-
import se.kth.japianalysis.BreakingChange;
6-
7-
import java.nio.file.Path;
8-
import java.util.List;
9-
import java.util.Set;
10-
11-
import static se.kth.transitive_changes.Dependency.findUniqueDependencies;
12-
133
public class Main {
144

155

166
public static void main(String[] args) {
177

18-
ApiMetadata apiMetadata = new ApiMetadata(Path.of("/Users/frank/Documents/Work/PHD/Explaining/breaking-good/projects/db02c6bcb989a5b0f08861c3344b532769530467/asto-core-v1.13.0.jar"));
19-
ApiMetadata apiMetadata2 = new ApiMetadata(Path.of("/Users/frank/Documents/Work/PHD/Explaining/breaking-good/projects/db02c6bcb989a5b0f08861c3344b532769530467/asto-core-v1.15.3.jar"));
20-
21-
Dependency oldVersion = new Dependency("com.artipie", "asto-core", "v1.13.0", "jar", "compile");
22-
Dependency newVersion = new Dependency("com.artipie", "asto-core", "v1.15.3", "jar", "compile");
23-
24-
25-
Set<Dependency> v1 = MavenTree.read(apiMetadata, oldVersion);
26-
Set<Dependency> v2 = MavenTree.read(apiMetadata2, newVersion);
27-
28-
Set<PairTransitiveDependency> transitiveDependencies = MavenTree.diff(v1, v2);
29-
30-
31-
Set<Dependency> newDependencies = findUniqueDependencies(v1, v2);
32-
33-
34-
35-
Set<DependencyRecord> changedDependencies = Dependency.findChangedDependenciesBySemVer(v1, v2);
36-
System.out.println("Dependencias que han cambiado según SemVer:");
37-
for (DependencyRecord record : changedDependencies) {
38-
System.out.println(record.oldDependency().getArtifactId() + " - " + record.changeType() + ": " +
39-
record.oldDependency().getVersion() + " -> " + record.newDependency().getVersion());
40-
}
41-
42-
System.out.println();
43-
44-
// Encontrar dependencias que han modificado solo versión y tipo
45-
Set<DependencyRecord> modifiedDependencies = Dependency.findModifiedDependenciesByVersionAndScope(v1, v2);
46-
System.out.println("Dependencias que han modificado solo versión y tipo:");
47-
for (DependencyRecord record : modifiedDependencies) {
48-
System.out.println(record.oldDependency().getArtifactId() + ": " +
49-
record.oldDependency().getVersion() + " -> " + record.newDependency().getVersion());
50-
}
51-
52-
53-
Set<Dependency> removedDependencies = findUniqueDependencies(v2, v1);
54-
55-
for (PairTransitiveDependency pair : transitiveDependencies) {
56-
try {
57-
System.out.println("Comparing " + pair.newVersion() + " and " + pair.oldVersion());
58-
CompareTransitiveDependency compareTransitiveDependency = new CompareTransitiveDependency(pair.newVersion(), pair.oldVersion());
59-
List<BreakingChange> changes = compareTransitiveDependency.getChangesBetweenDependencies();
60-
System.out.println("Breaking changes for " + pair.newVersion() + " and " + pair.oldVersion());
61-
System.out.println("Breaking Changes amount: " + changes.size());
62-
63-
JsonUtils.writeToFile(Path.of("breaking-changes-%s.json".formatted(pair.oldVersion().getArtifactId())), compareTransitiveDependency);
64-
65-
66-
67-
68-
} catch (Exception e) {
69-
e.printStackTrace();
70-
}
71-
}
728
}
73-
74-
759
}

src/main/java/se/kth/transitive_changes/MavenTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import lombok.Setter;
55
import se.kth.breaking_changes.ApiMetadata;
66
import se.kth.breaking_changes.Download;
7-
import se.kth.data.JsonUtils;
7+
import util.JsonUtils;
88
import util.MavenCommand;
99

1010
import java.io.File;

src/main/java/util/JsonUtils.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package util;
2+
3+
import com.fasterxml.jackson.databind.JavaType;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.type.TypeFactory;
6+
import com.fasterxml.jackson.databind.util.StdDateFormat;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.StandardOpenOption;
12+
13+
/**
14+
* The JsonUtils class provides a simple interface for writing and reading JSON files
15+
*
16+
* @author <a href="mailto:[email protected]">Gabriel Skoglund</a>
17+
*/
18+
public class JsonUtils {
19+
20+
/** The default JSON file ending ".json" */
21+
public static final String JSON_FILE_ENDING = ".json";
22+
23+
/** The string representing an empty JSON object */
24+
public static final String EMPTY_JSON_OBJECT = "{}";
25+
26+
private static final ObjectMapper mapper =
27+
new ObjectMapper().setDateFormat(new StdDateFormat());
28+
29+
private JsonUtils() { /* Nothing to see here... */ }
30+
31+
/**
32+
* Read a JSON object from file
33+
* @param file the path to the JSON file to read.
34+
* @param jsonType the type that the data should be considered as.
35+
* @return an object of the specified type as read from the given file.
36+
*/
37+
public static <T> T readFromFile(Path file, Class<T> jsonType) {
38+
try {
39+
return mapper.readValue(Files.readString(file), jsonType);
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
45+
/**
46+
* Read a JSON object from file
47+
* @param file the path to the JSON file to read.
48+
* @param jsonType the type that the data should be considered as.
49+
* @return an object of the specified type as read from the given file.
50+
*/
51+
public static <T> T readFromFile(Path file, JavaType jsonType) {
52+
try {
53+
return mapper.readValue(Files.readString(file), jsonType);
54+
} catch (IOException e) {
55+
throw new RuntimeException(e);
56+
}
57+
}
58+
59+
/**
60+
* Read a JSON object from file which can be empty
61+
* @param file the path to the JSON file to read which can be empty.
62+
* @param jsonType the type that the data should be considered as.
63+
* @return an object of the specified type as read from the given file.
64+
*/
65+
public static <T> T readFromNullableFile(Path file, JavaType jsonType) {
66+
try {
67+
if (Files.readString(file).isEmpty()) {
68+
// Handle empty JSON file.
69+
return null;
70+
} else {
71+
return mapper.readValue(Files.readString(file), jsonType);
72+
}
73+
} catch (IOException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
78+
/**
79+
* Write an object to a JSON file.
80+
* @param outputFilePath the file path where the data should be written.
81+
* @param data the object to be stored as JSON.
82+
*/
83+
public static void writeToFile(Path outputFilePath, Object data) {
84+
try {
85+
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data);
86+
Files.writeString(outputFilePath, json, StandardOpenOption.WRITE, StandardOpenOption.CREATE,
87+
StandardOpenOption.TRUNCATE_EXISTING);
88+
} catch (IOException e) {
89+
throw new RuntimeException(e);
90+
}
91+
}
92+
93+
/**
94+
* @return a {@link TypeFactory} from the underlying {@link ObjectMapper}.
95+
*/
96+
public static TypeFactory getTypeFactory() {
97+
return mapper.getTypeFactory();
98+
}
99+
}

0 commit comments

Comments
 (0)