2525import net .raphimc .javadowngrader .impl .classtransform .util .ClassNameUtil ;
2626import net .raphimc .javadowngrader .runtime .RuntimeRoot ;
2727import org .gradle .api .DefaultTask ;
28- import org .gradle .api .file .FileCollection ;
29- import org .gradle .api .tasks .Internal ;
28+ import org .gradle .api .file .ConfigurableFileCollection ;
29+ import org .gradle .api .file .RegularFileProperty ;
30+ import org .gradle .api .provider .Property ;
31+ import org .gradle .api .tasks .Input ;
32+ import org .gradle .api .tasks .InputFile ;
33+ import org .gradle .api .tasks .InputFiles ;
3034import org .gradle .api .tasks .TaskAction ;
3135import org .objectweb .asm .Opcodes ;
3236
3640import java .io .UncheckedIOException ;
3741import java .net .URI ;
3842import java .net .URISyntaxException ;
39- import java .nio .file .FileSystem ;
40- import java .nio .file .FileSystems ;
41- import java .nio .file .Files ;
42- import java .nio .file .Path ;
43+ import java .nio .file .*;
4344import java .util .Collection ;
4445import java .util .Collections ;
4546import java .util .HashSet ;
46- import java .util .Objects ;
4747import java .util .stream .Stream ;
4848
49- public class DowngradeJarTask extends DefaultTask {
49+ public abstract class DowngradeJarTask extends DefaultTask {
5050
51- @ Internal
52- private File input ;
51+ @ InputFile
52+ public abstract RegularFileProperty getInput () ;
5353
54- @ Internal
55- private String outputSuffix = "-downgraded" ;
54+ @ Input
55+ public abstract Property < String > getOutputSuffix () ;
5656
57- @ Internal
58- private FileCollection compileClassPath ;
57+ @ InputFiles
58+ public abstract ConfigurableFileCollection getCompileClassPath () ;
5959
60- @ Internal
61- private int targetVersion = Opcodes . V1_8 ;
60+ @ Input
61+ public abstract Property < Integer > getTargetVersion () ;
6262
63- @ Internal
64- private boolean copyRuntimeClasses = true ;
63+ @ Input
64+ public abstract Property <Boolean > getCopyRuntimeClasses ();
65+
66+ public DowngradeJarTask () {
67+ getOutputSuffix ().convention ("-downgraded" );
68+ getTargetVersion ().convention (Opcodes .V1_8 );
69+ getCopyRuntimeClasses ().convention (true );
70+ }
6571
6672 @ TaskAction
6773 public void run () throws IOException , URISyntaxException {
68- Objects .requireNonNull (this .input , "input must be set" );
69- Objects .requireNonNull (this .outputSuffix , "outputSuffix must be set" );
70- Objects .requireNonNull (this .compileClassPath , "compileClassPath must be set" );
71- if (!this .input .exists ()) throw new IllegalArgumentException ("input does not exist" );
72- if (!this .input .isFile () || !this .input .getName ().endsWith (".jar" )) throw new IllegalArgumentException ("input is not a jar file" );
73-
74- System .out .println ("Downgrading jar: " + this .input .getName ());
75- try (FileSystem inFs = FileSystems .newFileSystem (this .input .toPath (), null )) {
74+ final File inputFile = getInput ().getAsFile ().get ();
75+ System .out .println ("Downgrading jar: " + inputFile );
76+
77+ try (FileSystem inFs = FileSystems .newFileSystem (inputFile .toPath (), null )) {
7678 final Path inRoot = inFs .getRootDirectories ().iterator ().next ();
7779
7880 final Collection <String > runtimeDeps = new HashSet <>();
7981 final TransformerManager transformerManager = new TransformerManager (
80- new PathClassProvider (inRoot , new LazyFileClassProvider (this . compileClassPath .getFiles (), new BasicClassProvider ()))
82+ new PathClassProvider (inRoot , new LazyFileClassProvider (getCompileClassPath () .getFiles (), new BasicClassProvider ()))
8183 );
8284 transformerManager .addBytecodeTransformer (
8385 JavaDowngraderTransformer .builder (transformerManager )
84- .targetVersion (targetVersion )
86+ .targetVersion (getTargetVersion (). get () )
8587 .classFilter (c -> Files .isRegularFile (inRoot .resolve (ClassNameUtil .toClassFilename (c ))))
8688 .depCollector (runtimeDeps ::add )
8789 .build ()
8890 );
8991
90- final String outputName = this . input . getName ().substring (0 , this . input . getName ().length () - 4 ) + this . outputSuffix ;
91- final File outputFile = new File (this . input .getParentFile (), outputName + ".jar" );
92+ final String outputName = inputFile . getName ().substring (0 , inputFile . getName ().length () - 4 ) + getOutputSuffix (). get () ;
93+ final File outputFile = new File (inputFile .getParentFile (), outputName + ".jar" );
9294
9395 try (FileSystem outFs = FileSystems .newFileSystem (new URI ("jar:" + outputFile .toURI ()), Collections .singletonMap ("create" , "true" ))) {
9496 final Path outRoot = outFs .getRootDirectories ().iterator ().next ();
@@ -108,7 +110,7 @@ public void run() throws IOException, URISyntaxException {
108110 Files .createDirectories (parent );
109111 }
110112 if (!relative .endsWith (".class" ) || relative .contains ("META-INF/versions/" )) {
111- Files .copy (path , dest );
113+ Files .copy (path , dest , StandardCopyOption . REPLACE_EXISTING );
112114 return ;
113115 }
114116 final String className = ClassNameUtil .toClassName (relative );
@@ -127,7 +129,7 @@ public void run() throws IOException, URISyntaxException {
127129 }
128130
129131 // Copy runtime classes
130- if (this . copyRuntimeClasses ) {
132+ if (getCopyRuntimeClasses (). get () ) {
131133 for (final String runtimeDep : runtimeDeps ) {
132134 final String classPath = runtimeDep .concat (".class" );
133135 try (InputStream is = RuntimeRoot .class .getResourceAsStream ("/" + classPath )) {
@@ -137,52 +139,12 @@ public void run() throws IOException, URISyntaxException {
137139 if (parent != null ) {
138140 Files .createDirectories (parent );
139141 }
140- Files .copy (is , dest );
142+ Files .copy (is , dest , StandardCopyOption . REPLACE_EXISTING );
141143 }
142144 }
143145 }
144146 }
145147 }
146148 }
147149
148- public File getInput () {
149- return this .input ;
150- }
151-
152- public String getOutputSuffix () {
153- return this .outputSuffix ;
154- }
155-
156- public FileCollection getCompileClassPath () {
157- return this .compileClassPath ;
158- }
159-
160- public int getTargetVersion () {
161- return this .targetVersion ;
162- }
163-
164- public boolean getCopyRuntimeClasses () {
165- return this .copyRuntimeClasses ;
166- }
167-
168- public void setInput (final File input ) {
169- this .input = input ;
170- }
171-
172- public void setOutputSuffix (final String outputSuffix ) {
173- this .outputSuffix = outputSuffix ;
174- }
175-
176- public void setCompileClassPath (final FileCollection compileClassPath ) {
177- this .compileClassPath = compileClassPath ;
178- }
179-
180- public void setTargetVersion (final int targetVersion ) {
181- this .targetVersion = targetVersion ;
182- }
183-
184- public void setCopyRuntimeClasses (final boolean copyRuntimeClasses ) {
185- this .copyRuntimeClasses = copyRuntimeClasses ;
186- }
187-
188150}
0 commit comments