66
77import org .gradle .api .DefaultTask ;
88import org .gradle .api .Project ;
9+ import org .gradle .api .file .ArchiveOperations ;
910import org .gradle .api .file .ConfigurableFileCollection ;
11+ import org .gradle .api .file .DirectoryProperty ;
12+ import org .gradle .api .file .FileSystemOperations ;
1013import org .gradle .api .file .RegularFileProperty ;
14+ import org .gradle .api .tasks .CacheableTask ;
1115import org .gradle .api .tasks .InputFiles ;
16+ import org .gradle .api .tasks .OutputDirectory ;
1217import org .gradle .api .tasks .OutputFile ;
18+ import org .gradle .api .tasks .PathSensitive ;
19+ import org .gradle .api .tasks .PathSensitivity ;
1320import org .gradle .api .tasks .TaskAction ;
1421import org .gradle .api .tasks .TaskProvider ;
1522
1623import javax .inject .Inject ;
17- import java .io .FileInputStream ;
1824import java .io .IOException ;
1925import java .nio .charset .StandardCharsets ;
2026import java .nio .file .Files ;
21- import java .nio .file .StandardCopyOption ;
22- import java .util .zip .ZipEntry ;
23- import java .util .zip .ZipInputStream ;
2427
28+ @ CacheableTask
2529abstract class SlimeLauncherMetadata extends DefaultTask implements ForgeGradleTask {
2630 static TaskProvider <SlimeLauncherMetadata > register (Project project , MinecraftDependencyInternal mcdep ) {
2731 var taskName = "slimeLauncherMetadataFor" + Util .dependencyToCamelCase (mcdep .getModule ());
@@ -31,35 +35,41 @@ static TaskProvider<SlimeLauncherMetadata> register(Project project, MinecraftDe
3135 });
3236 }
3337
38+ @ PathSensitive (PathSensitivity .NONE )
3439 protected abstract @ InputFiles ConfigurableFileCollection getMetadata ();
3540
41+ protected abstract @ OutputDirectory DirectoryProperty getOutputDirectory ();
42+
3643 protected abstract @ OutputFile RegularFileProperty getRunsJson ();
3744
45+ protected abstract @ Inject ArchiveOperations getArchiveOperations ();
46+
47+ protected abstract @ Inject FileSystemOperations getFileSystemOperations ();
48+
3849 @ Inject
3950 public SlimeLauncherMetadata () {
40- this .getRunsJson ().convention (this .getDefaultOutputDirectory ().map (d -> d .file ("runs.json" )));
51+ this .getOutputDirectory ().convention (this .getDefaultOutputDirectory ());
52+ this .getRunsJson ().convention (
53+ this .getOutputDirectory ().map (d -> d .dir ("launcher" ).file ("runs.json" ))
54+ );
4155 }
4256
4357 @ TaskAction
4458 protected void exec () throws IOException {
4559 var archive = this .getMetadata ().getSingleFile ();
46- var json = this .getRunsJson ().getAsFile (). get (). toPath ();
60+ var outputDir = this .getOutputDirectory ().get ();
4761
48- boolean foundRuns = false ;
49- try (var zin = new ZipInputStream (new FileInputStream (archive ))) {
50- for (ZipEntry entry ; ((entry = zin .getNextEntry ()) != null ); ) {
51- if (!entry .getName ().startsWith ("launcher/" ))
52- continue ;
53- if (entry .getName ().equals ("launcher/runs.json" )) {
54- Files .copy (zin , json , StandardCopyOption .REPLACE_EXISTING );
55- foundRuns = true ;
56- }
57- }
58- }
62+ this .getFileSystemOperations ().sync (spec -> {
63+ spec .from (this .getArchiveOperations ().zipTree (archive ));
64+ spec .into (outputDir );
65+ });
5966
60- // If we don't find a metadata file, write an empty runs
61- // This happens when using a 'vanilla' minecraft dependency
62- if (!foundRuns )
67+ // Write an empty runs.json if it doesn't exist
68+ // This happens when using a 'vanilla' Minecraft dependency
69+ var json = this .getRunsJson ().getAsFile ().get ().toPath ();
70+ if (!Files .exists (json )) {
71+ Files .createDirectories (json .getParent ());
6372 Files .writeString (json , "{}" , StandardCharsets .UTF_8 );
73+ }
6474 }
6575}
0 commit comments