|
| 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