55package net .minecraftforge .forgedev .legacy .values ;
66
77import net .minecraftforge .forgedev .legacy .tasks .Util ;
8+ import org .gradle .api .Action ;
89import org .gradle .api .Project ;
10+ import org .gradle .api .Transformer ;
911import org .gradle .api .artifacts .Configuration ;
1012import org .gradle .api .provider .Provider ;
1113import org .gradle .api .tasks .TaskProvider ;
1214import org .gradle .api .tasks .bundling .AbstractArchiveTask ;
15+ import org .gradle .plugins .ide .eclipse .model .Library ;
1316
1417import java .io .File ;
1518import java .io .Serializable ;
1619import java .util .Collection ;
1720import java .util .HashMap ;
21+ import java .util .LinkedHashMap ;
22+ import java .util .List ;
1823import java .util .Map ;
1924import java .util .concurrent .Semaphore ;
2025
2126public record LibraryInfo (String name , Downloads downloads ) implements Serializable {
2227 public record Downloads (ArtifactInfo artifact ) implements Serializable {
23- public record ArtifactInfo (String path , String url , String sha1 , long size ) implements Serializable {
28+ public static class ArtifactInfo implements Serializable {
29+ public String path ;
30+ public String url ;
31+ public String sha1 ;
32+ public long size ;
33+
34+ public ArtifactInfo (String path , String url , String sha1 , long size ) {
35+ this .path = path ;
36+ this .url = url ;
37+ this .sha1 = sha1 ;
38+ this .size = size ;
39+ }
40+
2441 public ArtifactInfo validateUrl (boolean offline ) {
2542 if (offline || !url .startsWith ("https://libraries.minecraft.net/" ))
2643 return this ;
@@ -57,41 +74,48 @@ public static Map<String, LibraryInfo> from(Collection<MinimalResolvedArtifact>
5774 return from (dependencies , Boolean .valueOf (validateUrl ));
5875 }
5976
77+ public static LibraryInfo from (MinimalResolvedArtifact dependency ) {
78+ var info = dependency .info ();
79+ var url = "https://libraries.minecraft.net/" + info .path ();
80+ if (!Util .checkExists (url ))
81+ url = "https://maven.minecraftforge.net/" + info .path ();
82+
83+ var file = dependency .file ();
84+ var sha1 = Util .sha1 (dependency .file ());
85+
86+ return new LibraryInfo (
87+ info .name (),
88+ info .path (),
89+ url ,
90+ sha1 ,
91+ file .length ()
92+ );
93+ }
94+
6095 private static Map <String , LibraryInfo > from (Collection <MinimalResolvedArtifact > dependencies , Boolean offline ) {
61- var ret = new HashMap <String , LibraryInfo >(dependencies .size ());
96+ var ret = new LinkedHashMap <String , LibraryInfo >(dependencies .size ());
6297 var semaphore = new Semaphore (1 , true );
6398 dependencies .parallelStream ().forEachOrdered (dependency -> {
64- var info = dependency .info ();
65- var url = "https://libraries.minecraft.net/" + info .path ();
66- if (!Util .checkExists (url ))
67- url = "https://maven.minecraftforge.net/" + info .path ();
68-
69- var file = dependency .file ();
70- var sha1 = Util .sha1 (dependency .file ());
99+ var library = from (dependency );
100+ if (offline != null )
101+ library = library .validateUrl (offline );
71102
72103 try {
73104 semaphore .acquire ();
105+ ret .put (dependency .info ().key (), library );
106+ semaphore .release ();
74107 } catch (InterruptedException e ) {
75108 throw new RuntimeException ("Interrupted while trying to get library info for " + dependency .info (), e );
76109 }
77-
78- var library = new LibraryInfo (
79- info .name (),
80- info .path (),
81- url ,
82- sha1 ,
83- file .length ()
84- );
85- if (offline != null )
86- library = library .validateUrl (offline );
87- ret .put (info .key (), library );
88-
89- semaphore .release ();
90110 });
91111
92112 return ret ;
93113 }
94114
115+ public static Provider <LibraryInfo > from (Project project , TaskProvider <? extends AbstractArchiveTask > task ) {
116+ return MinimalResolvedArtifact .from (project , task ).map (LibraryInfo ::from );
117+ }
118+
95119 @ SafeVarargs
96120 public static Provider <Map <String , LibraryInfo >> from (Project project , TaskProvider <? extends AbstractArchiveTask >... tasks ) {
97121 var dependencies = project .getObjects ().listProperty (MinimalResolvedArtifact .class );
@@ -100,17 +124,21 @@ public static Provider<Map<String, LibraryInfo>> from(Project project, TaskProvi
100124 }
101125
102126 var ret = project .getObjects ().mapProperty (String .class , LibraryInfo .class ).value (dependencies .map (LibraryInfo ::from ));
103-
104- ret .disallowChanges ();
105- ret .finalizeValueOnRead ();
106- if (project .getState ().getExecuted ()) {
107- ret .finalizeValue ();
108- }
109-
110- return ret ;
127+ return Util .finalize (project , ret );
111128 }
112129
113130 public static Provider <Map <String , LibraryInfo >> from (Project project , Configuration configuration ) {
114131 return MinimalResolvedArtifact .from (project , configuration ).map (LibraryInfo ::from );
115132 }
133+
134+ public static List <LibraryInfo > toList (List <MinimalResolvedArtifact > list ) {
135+ return list .stream ().map (LibraryInfo ::from ).toList ();
136+ }
137+
138+ public static Transformer <LibraryInfo , LibraryInfo > apply (Action <LibraryInfo > action ) {
139+ return info -> {
140+ action .execute (info );
141+ return info ;
142+ };
143+ }
116144}
0 commit comments