Skip to content

Commit acd9ad7

Browse files
committed
DependencyDownloader: Upgrade (transitive) Log4J if needed
1 parent b3d2e34 commit acd9ad7

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

src/main/java/net/fabricmc/loom/util/DependencyDownloader.java

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of fabric-loom, licensed under the MIT License (MIT).
33
*
4-
* Copyright (c) 2021-2023 FabricMC
4+
* Copyright (c) 2021-2024 FabricMC
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -35,17 +35,25 @@
3535
import org.gradle.api.Project;
3636
import org.gradle.api.artifacts.Configuration;
3737
import org.gradle.api.artifacts.Dependency;
38+
import org.gradle.api.artifacts.DependencyResolveDetails;
3839
import org.gradle.api.artifacts.ModuleDependency;
40+
import org.gradle.api.artifacts.ModuleVersionSelector;
3941
import org.gradle.api.artifacts.dsl.DependencyHandler;
4042
import org.gradle.api.attributes.Attribute;
4143
import org.gradle.api.file.FileCollection;
44+
import org.jetbrains.annotations.VisibleForTesting;
4245

4346
/**
4447
* Simplified but powerful dependency downloading.
4548
*
4649
* @author Juuz
4750
*/
4851
public final class DependencyDownloader {
52+
private static final String LOG4J_GROUP = "org.apache.logging.log4j";
53+
private static final String LOG4J_NAME = "log4j-core";
54+
private static final String LOG4J_MINIMUM_VERSION = "2.17.1";
55+
private static final int[] LOG4J_MINIMUM_VERSION_COMPONENTS = {2, 17, 1};
56+
4957
private final Project project;
5058
private final List<DependencyEntry> dependencies = new ArrayList<>();
5159
private final Map<Attribute<?>, Object> attributes = new HashMap<>();
@@ -133,6 +141,7 @@ public FileCollection download(boolean transitive, boolean resolve) {
133141
attributes.attribute((Attribute<Object>) attribute, value);
134142
});
135143
});
144+
config.getResolutionStrategy().eachDependency(DependencyDownloader::upgradeLog4j);
136145
FileCollection files = config.fileCollection(dep -> true);
137146

138147
if (resolve) {
@@ -142,6 +151,49 @@ public FileCollection download(boolean transitive, boolean resolve) {
142151
return files;
143152
}
144153

154+
private static void upgradeLog4j(DependencyResolveDetails details) {
155+
ModuleVersionSelector requested = details.getRequested();
156+
157+
if (LOG4J_GROUP.equals(requested.getGroup()) && LOG4J_NAME.equals(requested.getName())) {
158+
final String requestedVersion = requested.getVersion();
159+
160+
if (requestedVersion != null && shouldUpgradeLog4jVersion(requestedVersion)) {
161+
details.useVersion(LOG4J_MINIMUM_VERSION);
162+
}
163+
}
164+
}
165+
166+
@VisibleForTesting
167+
public static boolean shouldUpgradeLog4jVersion(String requestedVersion) {
168+
final String[] splitVersion = requestedVersion.split("\\.");
169+
170+
for (int i = 0; i < LOG4J_MINIMUM_VERSION_COMPONENTS.length; i++) {
171+
if (i >= splitVersion.length) {
172+
// Not enough version components in the requested version, upgrade just to be sure.
173+
return true;
174+
}
175+
176+
final int minimumComponent = LOG4J_MINIMUM_VERSION_COMPONENTS[i];
177+
final String givenComponentStr = splitVersion[i];
178+
final int givenComponent;
179+
180+
try {
181+
givenComponent = Integer.parseInt(givenComponentStr);
182+
} catch (NumberFormatException e) {
183+
// We can't read the version component for comparing, upgrade just to be sure.
184+
return true;
185+
}
186+
187+
if (givenComponent < minimumComponent) {
188+
// Too old, upgrade.
189+
return true;
190+
}
191+
}
192+
193+
// Seems to be new enough, let's not upgrade.
194+
return false;
195+
}
196+
145197
/**
146198
* Resolves a dependency as well as its transitive dependencies into a {@link FileCollection}.
147199
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of fabric-loom, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) 2024 FabricMC
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package net.fabricmc.loom.test.unit.architectury
26+
27+
import spock.lang.Specification
28+
29+
class DependencyDownloaderTest extends Specification {
30+
def "upgrading log4j (should upgrade: #shouldUpgrade, requested: #version)"() {
31+
where:
32+
version | shouldUpgrade
33+
'2.17.1' | false
34+
'2.hello.3' | true
35+
'world.1.0' | true
36+
'3.0.0-beta1' | false
37+
'3.0.0-alpha1' | false
38+
'2.16.0' | true
39+
}
40+
}

0 commit comments

Comments
 (0)