Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ The NonDex Maven plugin also offers additional options; to see them all, run:
Use (Gradle):
============

To use NonDex in Gradle, add the following content into your build.gradle:
You can use the following command, to set up the NonDex Gradle plugin in your project:
```shell
./gradle-modify/modify-project.sh <path to your project>
```

If you want to do it manually, add the following content into your build.gradle:

```groovy
buildscript {
Expand Down
30 changes: 30 additions & 0 deletions gradle-modify/modify-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

ARTIFACT_VERSION="2.1.1"

if [[ $1 == "" ]]; then
echo "arg1 - the path to the project, where high-level pom.xml is"
echo "arg2 - (Optional) Custom version for the artifact (e.g., 1.1.0, 2.1). Default is $ARTIFACT_VERSION"
exit
fi


if [[ ! $2 == "" ]]; then
ARTIFACT_VERSION=$2
fi

crnt=`pwd`
working_dir=`dirname $0`
project_path=$1

cd ${project_path}
project_path=`pwd`
cd - > /dev/null

cd ${working_dir}

subProjects=`find ${project_path} -mindepth 2 -name build.gradle | wc -l`

find ${project_path} -maxdepth 1 -name build.gradle | python3 modify_gradle.py ${ARTIFACT_VERSION} ${subProjects}

cd ${crnt}
135 changes: 135 additions & 0 deletions gradle-modify/modify_gradle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import sys


def modify(path, version, subModules):
f = open(path, "r")
f2 = open(path, "r")
inBuildScript = False
inRepositories = False
inDependency = False
dependencySet = False
repositorySet = False
addedToSubproject = False
result = ""
cnt = 0
inBuildScriptBraceCnt = 1

dependencies = f"""
classpath("edu.illinois:plugin:{version}")
"""

full_buildscript = """buildscript {
repositories {
maven {
url = uri('https://plugins.gradle.org/m2/')
}
}
dependencies {
classpath("edu.illinois:plugin:%s")
}
}

""" % version

mavenString = """
maven {
url = uri('https://plugins.gradle.org/m2/')
}
"""


lines = f2.read()
lines = lines.replace(" ", "")
if "buildscript{" not in lines:
result += full_buildscript
cnt = 3

subprojectsInFile = False
if "subprojects{" in lines:
subprojectsInFile = True

stack = []
isInSubproject = False
# add line to output by finding the location of repositories{}, buildscript{}
for line in f.readlines():
if cnt < 3:
# add data
if inDependency and not dependencySet:
result += dependencies
dependencySet = True
cnt += 1

if inRepositories and not repositorySet:
result += mavenString
repositorySet = True
cnt += 1

# update state variables
if inBuildScript and "}" in line:
inBuildScriptBraceCnt -= line.count("}")
if inBuildScript and "{" in line:
inBuildScriptBraceCnt += line.count("{")
if inBuildScript and "repositories {" in line:
inRepositories = True
inDependency = False
if "buildscript {" in line or "buildscript{" in line:
inBuildScript = True
if inBuildScript and "dependencies {" in line:
inDependency = True
inRepositories = False
if inBuildScript and "}" in line and inBuildScriptBraceCnt == 0:
if not dependencySet:
result += "\tdependencies {" + dependencies + "}\n"
dependencySet = True
cnt += 1
if not repositorySet:
result += "\trepositories {" + mavenString + "}\n"
repositorySet = True
cnt += 1


line_no_space = line.replace(" ", "")
if subprojectsInFile and "subprojects{" in line_no_space.strip():
isInSubproject = True
if isInSubproject:
for char in line_no_space.strip():
if char == "{":
stack.append(char)
elif char == "}" and stack[-1] == "{":
stack.pop()
elif char == "}":
stack.append(char)
if len(stack) == 0:
result += "\tapply plugin: 'edu.illinois.nondex' \n"
isInSubproject = False
addedToSubproject = True

if not isInSubproject and ("test {" in line or "test{" in line):
result += "tasks.withType(Test) {\n"
continue

result += line

# add lines at the end of the file
result += "\napply plugin: 'edu.illinois.nondex'\n"

if subModules > 0 and not addedToSubproject:
result += """subprojects {
apply plugin: 'edu.illinois.nondex'
}
"""

output = open(path, "w")
output.write(result)
output.close()


if __name__ == "__main__":
version = sys.argv[1]
numberOfSubModules = sys.argv[2]

for path in sys.stdin:
path = path.strip()
print("modify: " + path)
modify(path, version, int(numberOfSubModules))