Skip to content

Replace project usages in ProGuardTask by injected Gradle services #380

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 1 commit into from
Jan 19, 2024
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
41 changes: 22 additions & 19 deletions gradle-plugin/src/main/java/proguard/gradle/ProGuardTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.file.*;
import org.gradle.api.logging.*;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.*;
import org.gradle.api.tasks.Optional;
import org.gradle.util.GradleVersion;
Expand All @@ -32,6 +33,7 @@
import proguard.classfile.util.ClassUtil;
import proguard.util.ListUtil;

import javax.inject.Inject;
import java.io.*;
import java.net.*;
import java.util.*;
Expand All @@ -44,6 +46,11 @@
@CacheableTask
public abstract class ProGuardTask extends DefaultTask
{
@Inject
protected abstract ObjectFactory getObjectFactory();
@Inject
protected abstract ProjectLayout getProjectLayout();

// Accumulated input and output, for the sake of Gradle's lazy file
// resolution and lazy task execution.
private final List inJarFiles = new ArrayList();
Expand All @@ -63,16 +70,12 @@ public abstract class ProGuardTask extends DefaultTask
private ClassSpecification classSpecification;

public static final String DEFAULT_CONFIG_RESOURCE_PREFIX = "/lib";
private final String resolvedConfigurationFileNamePrefix = getProject().file(DEFAULT_CONFIG_RESOURCE_PREFIX).toString();
private final String resolvedConfigurationFileNamePrefix = getProjectLayout().files(DEFAULT_CONFIG_RESOURCE_PREFIX).getSingleFile().toString();

// INTERNAL USE ONLY - write extra data entries to this jar
private File extraJar;

public ProGuardTask() {
if (GradleVersion.current().compareTo(GradleVersion.version("7.4")) >= 0) {
// This method was added in Gradle 7.4
notCompatibleWithConfigurationCache("https://github.com/Guardsquare/proguard/issues/254");
}
}

// Gradle task inputs and outputs, because annotations on the List fields
Expand All @@ -82,26 +85,26 @@ public ProGuardTask() {
@Classpath
public FileCollection getInJarFileCollection()
{
return getProject().files(inJarFiles);
return getProjectLayout().files(inJarFiles);
}

@OutputFiles
public FileCollection getOutJarFileCollection()
{
return getProject().files(outJarFiles);
return getProjectLayout().files(outJarFiles);
}

@Classpath
public FileCollection getLibraryJarFileCollection()
{
return getProject().files(libraryJarFiles);
return getProjectLayout().files(libraryJarFiles);
}

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileCollection getConfigurationFileCollection()
{
return getProject().files(configurationFiles);
return getProjectLayout().files(configurationFiles);
}

// Convenience methods to retrieve settings from outside the task.
Expand Down Expand Up @@ -672,7 +675,7 @@ public void printseeds()
public void printseeds(Object printSeeds)
throws ParseException
{
configuration.printSeeds = getProject().file(printSeeds);
configuration.printSeeds = getProjectLayout().files(printSeeds).getSingleFile();
}

@Optional
Expand Down Expand Up @@ -710,7 +713,7 @@ public void printusage()
public void printusage(Object printUsage)
throws ParseException
{
configuration.printUsage = getProject().file(printUsage);
configuration.printUsage = getProjectLayout().files(printUsage).getSingleFile();
}

@Optional
Expand Down Expand Up @@ -925,7 +928,7 @@ public void printmapping()
public void printmapping(Object printMapping)
throws ParseException
{
configuration.printMapping = getProject().file(printMapping);
configuration.printMapping = getProjectLayout().files(printMapping).getSingleFile();
}

@Optional
Expand All @@ -937,7 +940,7 @@ public File getPrintMappingFile() {
public void applymapping(Object applyMapping)
throws ParseException
{
configuration.applyMapping = getProject().file(applyMapping);
configuration.applyMapping = getProjectLayout().files(applyMapping).getSingleFile();
}

public void obfuscationdictionary(Object obfuscationDictionary)
Expand Down Expand Up @@ -1219,7 +1222,7 @@ public void android()
public void keystore(Object keyStore)
{
configuration.keyStores =
extendList(configuration.keyStores, getProject().file(keyStore));
extendList(configuration.keyStores, getProjectLayout().files(keyStore).getSingleFile());
}

public void keystorepassword(String keyStorePassword)
Expand Down Expand Up @@ -1321,7 +1324,7 @@ public void printconfiguration(Object printConfiguration)
throws ParseException
{
configuration.printConfiguration =
getProject().file(printConfiguration);
getProjectLayout().files(printConfiguration).getSingleFile();
}

@Optional
Expand All @@ -1346,7 +1349,7 @@ public void dump()
public void dump(Object dump)
throws ParseException
{
configuration.dump = getProject().file(dump);
configuration.dump = getProjectLayout().files(dump).getSingleFile();
}

@Optional
Expand Down Expand Up @@ -1490,7 +1493,7 @@ private Configuration getConfiguration() throws Exception
Object fileObject = configurationFiles.get(index);

ConfigurableFileCollection fileCollection =
getProject().files(fileObject);
getObjectFactory().fileCollection().from(fileObject);

// Parse the configuration as a collection of files.
Iterator<File> files = fileCollection.iterator();
Expand Down Expand Up @@ -1595,7 +1598,7 @@ private ClassPath extendClassPath(ClassPath classPath,
Map filterArgs,
boolean output)
{
ConfigurableFileCollection fileCollection = getProject().files(files);
ConfigurableFileCollection fileCollection = getObjectFactory().fileCollection().from(files);

if (classPath == null)
{
Expand Down Expand Up @@ -2299,7 +2302,7 @@ private List extendList(List list, Object object)
*/
private URL url(Object fileObject) throws MalformedURLException
{
File file = getProject().file(fileObject);
File file = getProjectLayout().files(fileObject).getSingleFile();
return
fileObject instanceof URL ? (URL)fileObject :
fileObject instanceof String &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class GradlePluginIntegrationTest : FreeSpec({
}
}

// ProguardTask is still incompatible, but will not fail the build. Once the issue is resolved, this test will fail
// and should be updated to demonstrate compatibility.
"ProguardTask will not fail when configuration cache is used" - {
val projectRoot = tempdir()
val fixture = File(GradlePluginIntegrationTest::class.java.classLoader.getResource("application").path)
Expand All @@ -76,8 +74,7 @@ class GradlePluginIntegrationTest : FreeSpec({

"Then the build was successful with configuration cache" {
result.output shouldContain "SUCCESSFUL"
// E.g., "Configuration cache entry discarded with 4 problems."
result.output shouldContain "Configuration cache entry discarded with"
result.output shouldContain "Configuration cache entry stored."
}
}
}
Expand Down