one py file for one template
- in template,
'type':'create'
- prompt user to input args
- replace args in path and code
- create new file by
path
, insert text to new file bycode
xxx.py
template = {
'type': 'create',
'args': ['path', 'className', 'arg2'],
'files': [
{
'path': '{path}/{className}.kt',
'code': '''class {className}(){
val name : String = ''
fun test(n:String) {
name = n
}
}
'''
}
]
}
yyy.py
template = {
'type':'create',
'args':['path','className','interfaceName', 'arg2'],
'files':[
{
'path':'{path}/{interfaceName}.kt',
'code':'''interface {interfaceName}(){
fun test()
}
'''
},
{
'path':'{path}/{className}.kt',
'code':'''class {className}() : {interfaceName}{
val name : String = ''
override fun test() {
name = 'hello world'
}
}
'''
},
]
}
- in template,
'type':'modify',
- prompt user to input args
- replace args in path, patt and code
- modify file by
path
, iftype == replace
, replacepatt
tocode
- modify file by
path
, iftype == insertAfter
, insertcode
afterpatt
- modify file by
path
, iftype == insertBefore
, insertcode
beforepatt
xxx.py
'''
examples:
1. insert a new function to class
2. insert a new val before class
3. replace code
4. modify fun
'''
template = {
'type': 'modify',
'args': ['filePath', 'className', 'newFun', 'funName'],
'files': [
{
'path': '{filePath}',
'operator': [
{
'type': 'insertAfter',
'patt': '''(class\s*?{className}[\s\S]*?{[\s\S]*?)\n}''',
'code': '''
fun {newFun}() {
// This is new fun
}\n'''
},
{
'type': 'insertBefore',
'patt': '''(class\s*?{className}[\s\S]*?{[\s\S]*?)\n}''',
'code': '''val TAG = "{className}"\n\n'''
},
{
'type': 'replace',
'patt': '''val name : String''',
'code': '''val newName : String'''
},
{
'type': 'insertAfter',
'patt': '''(fun\s*?{funName}[\s\S]*?{[\s\S]*?)\n}''',
'code': '''\n val rs = "hello world"
return rs
'''
},
]
}
]
}