22
33import io .github .intisy .gradle .github .impl .GitHub ;
44import io .github .intisy .gradle .github .impl .Gradle ;
5- import io .github .intisy .gradle .github .utils .FileUtils ;
65import io .github .intisy .gradle .github .utils .GradleUtils ;
76import org .eclipse .jgit .api .errors .GitAPIException ;
87import org .gradle .api .Action ;
@@ -45,45 +44,52 @@ public void apply(Project project) {
4544 GitHub gitHub = new GitHub (logger , resourcesExtension , githubExtension );
4645
4746 Task processGitHubResources = project .getTasks ().create ("processGitHubResources" , task -> task .doLast (t -> {
48- logger .debug ("Process resource event called on " + project .getName ());
49- if (resourcesExtension .getRepoUrl () != null ) {
50- logger .debug ("Found an repository in the resource extension" );
47+ logger .debug ("Process resource event called on " + project .getName ());
48+ if (resourcesExtension .getRepoUrl () != null ) {
49+ logger .debug ("Found a repository in the resource extension" );
5150
52- File path = GradleUtils .getGradleHome ().resolve ("resources" ).resolve (gitHub .getResourceRepoOwner () + "-" + gitHub .getResourceRepoName ()).toFile ();
51+ File sourceRepoPath = GradleUtils .getGradleHome ().resolve ("resources" ).resolve (gitHub .getResourceRepoOwner () + "-" + gitHub .getResourceRepoName ()).toFile ();
5352
54- for (File dir : resourceDirs ) {
55- try {
56- gitHub .cloneOrPullRepository (path , resourcesExtension .getBranch ());
53+ try {
54+ gitHub .cloneOrPullRepository (sourceRepoPath , resourcesExtension .getBranch ());
5755
58- if (resourcesExtension .isBuildOnly ()) {
59- dir = project .getBuildDir ().toPath ().resolve ("resources" ).resolve (dir .getParentFile ().getName ()).toFile ();
56+ for (File destinationDir : resourceDirs ) {
57+ File finalDestination ;
58+ if (resourcesExtension .isBuildOnly ()) {
59+ finalDestination = project .getBuildDir ().toPath ().resolve ("resources" ).resolve (destinationDir .getParentFile ().getName ()).toFile ();
60+ } else {
61+ finalDestination = destinationDir ;
6062 }
6163
62- FileUtils . deleteDirectory ( dir . toPath () );
64+ File sourceResourcePath = sourceRepoPath . toPath (). resolve ( resourcesExtension . getPath ()). toFile ( );
6365
64- if (dir .mkdirs ()) {
65- logger .debug ("Copying resources from " + path + " to: " + dir );
66- FileUtils .copyDirectory (path .toPath ().resolve (resourcesExtension .getPath ()), dir .toPath ());
67- } else {
68- logger .error ("Failed to create directory: " + dir );
69- }
70- } catch (GitAPIException | IOException e ) {
71- throw new RuntimeException (e );
72- }
73- }
74- }
75- }));
66+ if (!sourceResourcePath .exists ()) {
67+ logger .warn ("Source resource path does not exist, skipping sync: " + sourceResourcePath );
68+ continue ;
69+ }
70+
71+ logger .debug ("Syncing resources from " + sourceResourcePath + " to: " + finalDestination );
72+
73+ project .sync (copySpec -> {
74+ copySpec .from (sourceResourcePath );
75+ copySpec .into (finalDestination );
76+ });
77+ }
78+ } catch (GitAPIException | IOException e ) {
79+ throw new RuntimeException (e );
80+ }
81+ }
82+ }));
7683
7784 project .getPlugins ().withType (JavaPlugin .class , (Action <? super JavaPlugin >) plugin -> project .getTasks ().named ("processResources" , Copy .class , processResources -> {
78- logger .debug ("Process resource event found on " + project .getName ());
79- processResources .dependsOn (processGitHubResources );
80- }));
85+ logger .debug ("Process resource event found on " + project .getName ());
86+ processResources .dependsOn (processGitHubResources );
87+ }));
8188
8289 project .afterEvaluate (proj -> githubImplementation .getDependencies ().all (dependency -> {
8390 File jar = gitHub .getAsset (dependency .getGroup (), dependency .getName (), dependency .getVersion ());
84-
85- project .getDependencies ().add ("implementation" , project .files (jar ));
86- }));
91+ project .getDependencies ().add ("implementation" , project .files (jar ));
92+ }));
8793
8894 project .getTasks ().register ("printGithubDependencies" , task -> {
8995 task .setGroup ("github" );
@@ -124,8 +130,8 @@ public void apply(Project project) {
124130 if (refresh )
125131 Gradle .safeSoftRefreshGradle (project );
126132 });
127- });
128- }
133+ });
134+ }
129135
130136 public Set <Dependency > getDependencies (Project project ) {
131137 Set <Dependency > dependencyList = new HashSet <>();
@@ -137,4 +143,4 @@ public Set<Dependency> getDependencies(Project project) {
137143 dependencyList .addAll (project .getConfigurations ().getByName ("githubImplementation" ).getAllDependencies ());
138144 return dependencyList ;
139145 }
140- }
146+ }
0 commit comments