Skip to content

Instantly share code, notes, and snippets.

@Humoud
Last active August 29, 2015 14:17
Show Gist options
  • Save Humoud/e0dac44388c0c99d3351 to your computer and use it in GitHub Desktop.
Save Humoud/e0dac44388c0c99d3351 to your computer and use it in GitHub Desktop.
Edits / Inserts code into Java files
import glob
import re
import fileinput
# will insert/inject this
code = "\n\t\t\t\tgetSupportActionBar().setDisplayHomeAsUpEnabled(true);\n"
#code += "\t\t\t\tgetSupportActionBar().setCustomView(R.layout.custom_actionbar);\n"
#code += "\t\t\t\tActionBar actionBar = getSupportActionBar();\n"
#code += "\t\t\t\tactionBar.setHomeButtonEnabled(true);\n"
# will search for this
insertAfterThis = 'actionBar.setHomeButtonEnabled(true);'
def doesLineContain(str, line):
if str in line:
return True
else:
return False
def insertCode(lines, code):
lines += code
return lines
def writeToFile(file, code):
with open(file, 'w+') as fw:
fw.write(code)
# interate over file in folder "code" with extention ".java"
for file in glob.glob('code/*.java'):
print "Working on: "+file
modified = "" # prep for new file
for line in fileinput.input(file):
modified += line
if doesLineContain(insertAfterThis, line): # got a hit
print 'got a hit... inserting code...'
modified = insertCode(modified, code)
# write result in new file in folder "modified"
newFile = 'modified/'+file.replace('code/', '')
print 'writing new code to file: '+newFile
writeToFile(newFile, modified)
print "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment