22 * SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
33 * SPDX-License-Identifier: GPL-3.0-or-later
44 */
5+ import me.champeau.gradle.igp.gitRepositories
6+ import org.eclipse.jgit.api.Git
7+ import java.io.FileInputStream
8+ import java.util.Properties
59
610pluginManagement {
711 repositories {
@@ -10,6 +14,20 @@ pluginManagement {
1014 mavenCentral()
1115 }
1216}
17+
18+ plugins {
19+ // need to manually read version catalog because it is not available in settings.gradle.kts
20+ // this code is duplicate with the below but there is no way to avoid it...
21+ fun findInVersionCatalog (versionIdentifier : String ): String {
22+ val regex = " ^.*$versionIdentifier *= *\" ([^\" ]+)\" .*$" .toRegex()
23+ return File (" gradle/libs.versions.toml" )
24+ .readLines()
25+ .firstNotNullOf { regex.find(it)?.groupValues?.get(1 ) }
26+ }
27+
28+ id(" me.champeau.includegit" ) version findInVersionCatalog(" includegitPlugin" )
29+ }
30+
1331dependencyResolutionManagement {
1432 repositoriesMode.set(RepositoriesMode .FAIL_ON_PROJECT_REPOS )
1533 repositories {
@@ -21,13 +39,85 @@ dependencyResolutionManagement {
2139}
2240include (" :app" )
2341
24- // Use a local copy of NewPipe Extractor by uncommenting the lines below.
25- // We assume, that NewPipe and NewPipe Extractor have the same parent directory.
26- // If this is not the case, please change the path in includeBuild().
2742
28- // includeBuild("../NewPipeExtractor") {
29- // dependencySubstitution {
30- // substitute(module("com.github.TeamNewPipe:NewPipeExtractor"))
31- // .using(project(":extractor"))
32- // }
33- // }
43+ // All of the code below handles depending on libraries from git repos, in particular
44+ // NewPipeExtractor. The git commit to checkout can be updated in libs.versions.toml.
45+ // If you want to use a local copy of NewPipeExtractor (provided that you have cloned it in
46+ // `../NewPipeExtractor`), you can add `useLocalNewPipeExtractor=true` to `local.properties`.
47+ // Or you use a commit you pushed to GitHub by just replacing TeamNewPipe with your GitHub
48+ // name below here and update the commit hash in libs.versions.toml with the commit hash of the
49+ // (pushed) commit you want to test.
50+
51+ data class IncludeGitRepo (
52+ val name : String ,
53+ val uri : String ,
54+ val projectPath : String ,
55+ val commit : String ,
56+ )
57+
58+ // need to manually read version catalog because it is not available in settings.gradle.kts
59+ // this code is duplicate with the above but there is no way to avoid it...
60+ fun findInVersionCatalog (versionIdentifier : String ): String {
61+ val regex = " ^.*$versionIdentifier *= *\" ([^\" ]+)\" .*$" .toRegex()
62+ return File (" gradle/libs.versions.toml" )
63+ .readLines()
64+ .firstNotNullOf { regex.find(it)?.groupValues?.get(1 ) }
65+ }
66+
67+ val newPipeExtractor = IncludeGitRepo (
68+ name = " NewPipeExtractor" ,
69+ uri = " https://github.com/TeamNewPipe/NewPipeExtractor" ,
70+ projectPath = " :extractor" ,
71+ commit = findInVersionCatalog(" teamnewpipe-newpipeextractor" ),
72+ )
73+
74+ val localProperties = Properties ().apply {
75+ try {
76+ load(FileInputStream (File (rootDir, " local.properties" )))
77+ } catch (e: Throwable ) {
78+ println (" Warning: can't read local.properties: $e " )
79+ }
80+ }
81+
82+ if (localProperties.getOrDefault(" useLocalNewPipeExtractor" , " " ) == " true" ) {
83+ includeBuild(" ../${newPipeExtractor.name} " ) {
84+ dependencySubstitution {
85+ substitute(module(" git.included.build:${newPipeExtractor.name} " ))
86+ .using(project(newPipeExtractor.projectPath))
87+ }
88+ }
89+
90+ } else {
91+ // if the repo has already been cloned, the gitRepositories plugin is buggy and doesn't
92+ // fetch the remote repo before trying to checkout the commit (in case the commit has changed),
93+ // and doesn't clone the repo again if the remote changed, so we need to do it manually
94+ val repo = newPipeExtractor
95+ val file = File (" $rootDir /checkouts/${repo.name} " )
96+ if (file.isDirectory) {
97+ val git = Git .open(file)
98+ val sameRemote = git.remoteList().call()
99+ .any { rem -> rem.urIs.any { uri -> uri.toString() == repo.uri } }
100+ if (sameRemote) {
101+ // the commit may have changed, fetch again
102+ git.fetch().call()
103+ } else {
104+ // the remote changed, delete the repository and start from scratch
105+ println (" Git: remote for ${repo.name} changed, deleting the current folder" )
106+ file.deleteRecursively()
107+ }
108+ }
109+
110+ gitRepositories {
111+ include(repo.name) {
112+ uri.set(repo.uri)
113+ commit.set(repo.commit)
114+ autoInclude.set(false )
115+ includeBuild(" " ) {
116+ dependencySubstitution {
117+ substitute(module(" git.included.build:${repo.name} " ))
118+ .using(project(repo.projectPath))
119+ }
120+ }
121+ }
122+ }
123+ }
0 commit comments