Skip to content

Commit 056be41

Browse files
authored
Merge pull request #5 from kravemir/optimization
Optimize resolution of configuration dependencies
2 parents 617c7d1 + 18f7950 commit 056be41

File tree

4 files changed

+369
-136
lines changed

4 files changed

+369
-136
lines changed

plugin/src/main/java/io/github/jwharm/flatpakgradlegenerator/ArtifactResolver.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
package io.github.jwharm.flatpakgradlegenerator;
2121

22-
import org.gradle.api.artifacts.Configuration;
2322
import org.gradle.api.artifacts.ModuleVersionIdentifier;
2423
import org.gradle.api.artifacts.ResolvedArtifact;
2524

2625
import java.io.BufferedInputStream;
26+
import java.io.ByteArrayInputStream;
2727
import java.io.ByteArrayOutputStream;
28+
import java.io.InputStream;
2829
import java.io.File;
2930
import java.io.IOException;
3031
import java.net.HttpURLConnection;
@@ -71,17 +72,16 @@ static ArtifactResolver getInstance(String dest) {
7172
*
7273
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
7374
*/
74-
Optional<byte[]> tryResolve(DependencyDetails dep,
75-
String repository,
76-
String filename)
77-
throws NoSuchAlgorithmException {
75+
Optional<byte[]> tryResolve(DependencyDetails dep,
76+
String repository,
77+
String filename) throws IOException, NoSuchAlgorithmException {
7878

7979
// Build the url and try to download the file
8080
String url = repository + dep.path() + "/" + filename;
8181
var contents = getFileContentsFrom(url);
8282

8383
if (contents.isPresent()) {
84-
String sha512 = calculateSHA512(contents.get());
84+
String sha512 = calculateSHA512(new ByteArrayInputStream(contents.get()));
8585
String dest = dep.path();
8686

8787
// If the filename contains a path, cut it out and append it to the dest field
@@ -111,7 +111,7 @@ Optional<byte[]> tryResolve(DependencyDetails dep,
111111
* find the artifact in the local Gradle file, and use that file to calculate the SHA-512 hash
112112
* and add it to the JSON output.
113113
*
114-
* @param configuration the Gradle configuration that contains the artifact
114+
* @param artifacts resolved artifacts for the gradle configuration containing the dependency
115115
* @param id the id of the dependency
116116
* @param dep a DependencyDetail instance with the Maven coordinates of the artifact
117117
* @param repository the repository to try to download from
@@ -120,7 +120,7 @@ Optional<byte[]> tryResolve(DependencyDetails dep,
120120
* @throws IOException error while reading the jar file
121121
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
122122
*/
123-
void tryResolveCached(Configuration configuration,
123+
void tryResolveCached(Set<ResolvedArtifact> artifacts,
124124
ModuleVersionIdentifier id,
125125
DependencyDetails dep,
126126
String repository,
@@ -129,18 +129,13 @@ void tryResolveCached(Configuration configuration,
129129
String altName)
130130
throws IOException, NoSuchAlgorithmException {
131131

132-
// Find the jar in the local Gradle cache
133-
Set<ResolvedArtifact> artifacts = configuration
134-
.getResolvedConfiguration()
135-
.getResolvedArtifacts();
136-
137132
for (var artifact : artifacts) {
138133
if (artifact.getModuleVersion().getId().equals(id)) {
139134
File file = artifact.getFile();
140135

141136
// Check against filenames in the .module file
142137
if (checkName)
143-
if (! (file.getName().equals(filename) ||
138+
if (!(file.getName().equals(filename) ||
144139
file.getName().equals(altName)))
145140
continue;
146141

@@ -155,7 +150,7 @@ void tryResolveCached(Configuration configuration,
155150
+ file.getName();
156151
var isValid = isValid(url);
157152

158-
if ((! isValid) && filename.contains("SNAPSHOT")) {
153+
if ((!isValid) && filename.contains("SNAPSHOT")) {
159154
// Try again, but this time, replace SNAPSHOT with snapshot details
160155
url = repository
161156
+ dep.path()
@@ -165,9 +160,12 @@ void tryResolveCached(Configuration configuration,
165160
}
166161

167162
if (isValid) {
163+
String sha512;
164+
168165
// Read the file from the local Gradle cache and calculate the SHA-512 hash
169-
byte[] bytes = Files.readAllBytes(file.toPath());
170-
String sha512 = calculateSHA512(bytes);
166+
try (var fileInputStream = Files.newInputStream(file.toPath())) {
167+
sha512 = calculateSHA512(fileInputStream);
168+
}
171169

172170
// Generate and append the json
173171
generateJsonBlock(
@@ -280,16 +278,25 @@ private Optional<byte[]> getFileContents(String url) {
280278
/**
281279
* Generate an SHA-512 hash for a byte array using MessageDigest.
282280
*
283-
* @param contents the input byte array
281+
* @param contentsInputStream the input stream for contents to compute hash of
284282
* @return the SHA-512 hash
285283
* @throws NoSuchAlgorithmException no provider for the SHA-512 algorithm
284+
* @throws IOException error when reading input
286285
*/
287-
private String calculateSHA512(byte[] contents) throws NoSuchAlgorithmException {
286+
private String calculateSHA512(InputStream contentsInputStream) throws NoSuchAlgorithmException, IOException {
287+
byte[] buffer = new byte[4096];
288+
288289
// Create a MessageDigest object for SHA-512
289290
MessageDigest md = MessageDigest.getInstance("SHA-512");
290291

291-
// Update the MessageDigest with the bytes from the input String
292-
md.update(contents);
292+
int numRead;
293+
do {
294+
numRead = contentsInputStream.read(buffer);
295+
if (numRead > 0) {
296+
md.update(buffer, 0, numRead);
297+
}
298+
} while (numRead != -1);
299+
293300
byte[] hashBytes = md.digest();
294301

295302
// Convert the byte array to a hexadecimal string

plugin/src/main/java/io/github/jwharm/flatpakgradlegenerator/FlatpakGradleGeneratorTask.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,8 @@ public void apply() throws NoSuchAlgorithmException, IOException {
9292
"Resolving buildscript dependencies using repositories: {}",
9393
buildScriptRepositories
9494
);
95-
for (var configuration : project.getBuildscript().getConfigurations()) {
96-
if (configuration.isCanBeResolved()) {
97-
generateSourcesList(buildScriptRepositories, configuration);
98-
}
99-
}
95+
generateSourcesListForConfigurations(buildScriptRepositories, project.getBuildscript().getConfigurations());
96+
LOGGER.info("Buildscript dependencies resolved successfully");
10097

10198
// List the declared repositories; include the buildscript classpath
10299
// repositories too
@@ -110,12 +107,10 @@ public void apply() throws NoSuchAlgorithmException, IOException {
110107
"Resolving project dependencies using repositories: {}",
111108
repositories
112109
);
113-
for (var configuration : project.getConfigurations()) {
114-
if (configuration.isCanBeResolved()) {
115-
generateSourcesList(repositories, configuration);
116-
}
117-
}
110+
generateSourcesListForConfigurations(repositories, project.getConfigurations());
111+
LOGGER.info("Project dependencies resolved successfully");
118112

113+
LOGGER.info("Writing result to " + getOutputFile().getAsFile());
119114
// Merge the json blocks into a json list, and write the result to the
120115
// output file
121116
Files.writeString(
@@ -124,6 +119,21 @@ public void apply() throws NoSuchAlgorithmException, IOException {
124119
);
125120
}
126121

122+
private void generateSourcesListForConfigurations(
123+
List<String> repositories,
124+
Collection<Configuration> configurations
125+
) throws IOException, NoSuchAlgorithmException {
126+
var resolvedConfigurations = configurations.stream()
127+
.filter(Configuration::isCanBeResolved)
128+
.toList();
129+
130+
LOGGER.info("Following configurations will be processed: {}", resolvedConfigurations);
131+
132+
for (Configuration configuration : resolvedConfigurations) {
133+
generateSourcesList(repositories, configuration);
134+
}
135+
}
136+
127137
/**
128138
* Get all Maven repositories that were declared in the build file.
129139
*
@@ -190,11 +200,21 @@ private void generateSourcesList(List<String> repositories,
190200
var dependencies = listDependencies(configuration);
191201

192202
LOGGER.info(
193-
"Resolving configuration {} with {} dependencies",
203+
"Configuration processing of {}: starting with {} dependencies",
194204
configuration.getName(),
195205
dependencies.size()
196206
);
197207

208+
var resolvedArtifactsForConfiguration = configuration
209+
.getResolvedConfiguration()
210+
.getResolvedArtifacts();
211+
212+
LOGGER.info(
213+
"Configuration processing of {}: artifacts resolved, total {}",
214+
configuration.getName(),
215+
resolvedArtifactsForConfiguration.size()
216+
);
217+
198218
for (var dependency : dependencies) {
199219

200220
String id = dependency.getSelected().getId().getDisplayName();
@@ -229,7 +249,7 @@ private void generateSourcesList(List<String> repositories,
229249
if (files != null) {
230250
for (var file : files) {
231251
resolver.tryResolveCached(
232-
configuration,
252+
resolvedArtifactsForConfiguration,
233253
dependency.getSelected().getModuleVersion(),
234254
dep,
235255
repository,
@@ -243,7 +263,7 @@ private void generateSourcesList(List<String> repositories,
243263
// No files declared for this variant in the Gradle module
244264
// Get jar artifact from local Gradle cache
245265
resolver.tryResolveCached(
246-
configuration,
266+
resolvedArtifactsForConfiguration,
247267
dependency.getSelected().getModuleVersion(),
248268
dep,
249269
repository,
@@ -257,7 +277,7 @@ private void generateSourcesList(List<String> repositories,
257277
// Get jar artifact from local Gradle cache
258278
else {
259279
resolver.tryResolveCached(
260-
configuration,
280+
resolvedArtifactsForConfiguration,
261281
dependency.getSelected().getModuleVersion(),
262282
dep,
263283
repository,
@@ -284,6 +304,12 @@ private void generateSourcesList(List<String> repositories,
284304
if (module.isPresent() || pom.isPresent())
285305
break;
286306
}
307+
308+
LOGGER.info(
309+
"Dependency {} in {} processed",
310+
id,
311+
configuration.getName()
312+
);
287313
}
288314
}
289315

@@ -320,7 +346,7 @@ private List<ResolvedDependencyResult> listDependencies(Configuration configurat
320346
*
321347
* @param dep details about the dependency
322348
*/
323-
private void addPluginMarker(DependencyDetails dep) throws NoSuchAlgorithmException {
349+
private void addPluginMarker(DependencyDetails dep) throws IOException, NoSuchAlgorithmException {
324350

325351
// This is the marker artifact we are looking for
326352
var marker = new DependencyDetails(

plugin/src/test/resources/dokka/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ plugins {
33
id 'org.jetbrains.dokka' version '1.8.10'
44
}
55

6+
repositories {
7+
mavenCentral()
8+
}
9+
610
tasks.flatpakGradleGenerator {
711
outputFile = file('%s')
812
}

0 commit comments

Comments
 (0)