Skip to content

Commit 12ba3e5

Browse files
author
simonh5
committed
implementation of script to modify gradle.build files
1 parent 8843b43 commit 12ba3e5

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

gradle-modify/modify-project.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
ARTIFACT_VERSION="2.1.1"
4+
5+
if [[ $1 == "" ]]; then
6+
echo "arg1 - the path to the project, where high-level pom.xml is"
7+
echo "arg2 - (Optional) Custom version for the artifact (e.g., 1.1.0, 2.1). Default is $ARTIFACT_VERSION"
8+
exit
9+
fi
10+
11+
12+
if [[ ! $2 == "" ]]; then
13+
ARTIFACT_VERSION=$2
14+
fi
15+
16+
crnt=`pwd`
17+
working_dir=`dirname $0`
18+
project_path=$1
19+
20+
cd ${project_path}
21+
project_path=`pwd`
22+
cd - > /dev/null
23+
24+
cd ${working_dir}
25+
26+
subProjects=`find ${project_path} -mindepth 2 -name build.gradle | wc -l`
27+
28+
find ${project_path} -maxdepth 1 -name build.gradle | python3 modify_gradle.py ${ARTIFACT_VERSION} ${subProjects}
29+
30+
cd ${crnt}

gradle-modify/modify_gradle.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import sys
2+
3+
4+
def modify(path, version, subModules):
5+
f = open(path, "r")
6+
f2 = open(path, "r")
7+
inBuildScript = False
8+
inRepositories = False
9+
inDependency = False
10+
dependencySet = False
11+
repositorySet = False
12+
addedToSubproject = False
13+
result = ""
14+
cnt = 0
15+
inBuildScriptBraceCnt = 1
16+
17+
dependencies = f"""
18+
classpath("edu.illinois:plugin:{version}")
19+
"""
20+
21+
full_buildscript = """buildscript {
22+
repositories {
23+
maven {
24+
url = uri('https://plugins.gradle.org/m2/')
25+
}
26+
}
27+
dependencies {
28+
classpath("edu.illinois:plugin:%s")
29+
}
30+
}
31+
32+
""" % version
33+
34+
mavenString = """
35+
maven {
36+
url = uri('https://plugins.gradle.org/m2/')
37+
}
38+
"""
39+
40+
41+
lines = f2.read()
42+
lines = lines.replace(" ", "")
43+
if "buildscript{" not in lines:
44+
result += full_buildscript
45+
cnt = 3
46+
47+
subprojectsInFile = False
48+
if "subprojects{" in lines:
49+
subprojectsInFile = True
50+
51+
stack = []
52+
isInSubproject = False
53+
# add line to output by finding the location of repositories{}, buildscript{}
54+
for line in f.readlines():
55+
if cnt < 3:
56+
# add data
57+
if inDependency and not dependencySet:
58+
result += dependencies
59+
dependencySet = True
60+
cnt += 1
61+
62+
if inRepositories and not repositorySet:
63+
result += mavenString
64+
repositorySet = True
65+
cnt += 1
66+
67+
# update state variables
68+
if inBuildScript and "}" in line:
69+
inBuildScriptBraceCnt -= line.count("}")
70+
if inBuildScript and "{" in line:
71+
inBuildScriptBraceCnt += line.count("{")
72+
if inBuildScript and "repositories {" in line:
73+
inRepositories = True
74+
inDependency = False
75+
if "buildscript {" in line or "buildscript{" in line:
76+
inBuildScript = True
77+
if inBuildScript and "dependencies {" in line:
78+
inDependency = True
79+
inRepositories = False
80+
if inBuildScript and "}" in line and inBuildScriptBraceCnt == 0:
81+
if not dependencySet:
82+
result += "\tdependencies {" + dependencies + "}\n"
83+
dependencySet = True
84+
cnt += 1
85+
if not repositorySet:
86+
result += "\trepositories {" + mavenString + "}\n"
87+
repositorySet = True
88+
cnt += 1
89+
90+
91+
line_no_space = line.replace(" ", "")
92+
if subprojectsInFile and "subprojects{" in line_no_space.strip():
93+
isInSubproject = True
94+
if isInSubproject:
95+
for char in line_no_space.strip():
96+
if char == "{":
97+
stack.append(char)
98+
elif char == "}" and stack[-1] == "{":
99+
stack.pop()
100+
elif char == "}":
101+
stack.append(char)
102+
if len(stack) == 0:
103+
result += "\tapply plugin: 'edu.illinois.nondex' \n"
104+
isInSubproject = False
105+
addedToSubproject = True
106+
107+
if not isInSubproject and ("test {" in line or "test{" in line):
108+
result += "tasks.withType(Test) {\n"
109+
continue
110+
111+
result += line
112+
113+
# add lines at the end of the file
114+
result += "\napply plugin: 'edu.illinois.nondex'\n"
115+
116+
if subModules > 0 and not addedToSubproject:
117+
result += """subprojects {
118+
apply plugin: 'edu.illinois.nondex'
119+
}
120+
"""
121+
122+
output = open(path, "w")
123+
output.write(result)
124+
output.close()
125+
126+
127+
if __name__ == "__main__":
128+
version = sys.argv[1]
129+
numberOfSubModules = sys.argv[2]
130+
131+
for path in sys.stdin:
132+
path = path.strip()
133+
print("modify: " + path)
134+
modify(path, version, int(numberOfSubModules))
135+

0 commit comments

Comments
 (0)