隨著公司的并行項目越來越多,Android新項目初始化的變成一個問題,一開始是每個人建立項目不一樣,為此設計了清單列表。后來發(fā)現(xiàn)項目是一致了,但是每次都要手動執(zhí)行列表,大部分都是簡單地新建文件,很無聊,而且偶爾思想開個小差,還出錯,又要找問題、修bug,煩~。
都說懶惰是程序員的優(yōu)良品質(zhì),所以決定研究一下怎么自動化整個過程。
需求
經(jīng)過分析,發(fā)現(xiàn)這里面最核心需求有兩個:
- 根據(jù)模板java文件生成最終的java文件,主要是替換
類名
、包名
和app包名
; - 根據(jù)包名規(guī)則生成相應的文件夾。
其中涉及的流程有:
- 讀取、解析配置文件;
- 根據(jù)名稱,生成相應的文件夾;
- 根據(jù)模板文件,生成文件夾下相應的文件;
涉及的技術點有:
- 配置文件的格式用什么好?
- 怎么解析配置文件?
技術點1:配置文件的格式用什么好?
經(jīng)過分析,考慮擴展性和簡潔程序,決定json來寫配置。
方案一:json和文件(夾)的層級結(jié)構(gòu)保持一致。
- type:這一層級的類型:
-
dir
: 表示文件夾 -
file
: 表示文件
- 如果 type為dir,則需要定義:
-
name
:文件夾的名字 -
list
:子文件(夾)列表 -
package_name
:包名。默認為 父級文件夾名字和自身文件夾的名字的疊加。
- 如果 type為file,則需要定義:
-
name
:文件的名字 -
template
:模板文件的名字
這樣一個配置兩個文件的json如下:
{
"type":"dir",
"name":"baby",
"list":[
{
"type":"dir",
"name":"view",
"list":[
{
"type":"file",
"name":"BabyFragment",
"template":"BaseFragment.java.tpl"
},
{
"type":"file",
"name":"MorningCheckFragment",
"template":"BaseFragment.java.tpl"
}
]
}
]
}
才兩個文件,就有三層嵌套,就算是增加同包文件,都要增加三行,太繁瑣了。換一種思路:
方案二:根據(jù)模板、包名,把同一個包名下的同一個模板的文件匯總在一起
- 第一層:
-
app_id
: 應用包名 -
list
: 模板新建列表
- 第二層:模板新建列表項
-
package_name
: 對應的包名 -
template
: 采用的模板 -
fileList
: 需要新建的文件列表
這樣json變成如下:
{
"app_id":"panda.android.test",
"list":[
{
"package_name":"panda.android.test",
"template":"BaseFragment.java.tpl",
"fileList":[
"BabyFragment",
"MorningCheckFragment"
]
}
]
}
簡單直觀很多。
怎么解析配置文件?
這里當然選擇強大易用的Python了。
Python字符串替換的3種方法:
1. 字符串替換
將需要替換的內(nèi)容使用格式化符替代,后續(xù)補上替換內(nèi)容;
template = "hello %s , your website is %s " % ("簡書","http://www.lxweimin.com/")
print(template)
也可使用format函數(shù)完成:
template = "hello {0} , your website is {1} ".format("簡書","http://www.lxweimin.com/")
print(template)
2. 字符串命名格式化符替換
使用命名格式化符,這樣,對于多個相同變量的引用,在后續(xù)替換只用申明一次即可;
template = "hello %(name)s ,your name is %(name), your website is %(message)s" %{"name":"簡書","message":"http://www.lxweimin.com/"}
print(template)
使用format函數(shù)的語法方式:
template = "hello {name} , your name is {name}, your website is {message} ".format(name="大CC",message="http://blog.me115.com")
print(template)
3. 模版方法替換
使用string中的Template方法;
from string import Template
tempTemplate = string.Template("Hello $name ,your website is $message")
print(tempTemplate.substitute(name='大CC',message='http://blog.me115.com'))
有了模版方法后,就可以將模版保存到文件單獨編輯,在生成的地方替換為需要的變量。
樣例代碼
- BaseFragment.java.tpl如下:
package ${PACKAGE_NAME};
import ${ANDROID_APP_ID}.R;
import panda.android.lib.base.ui.fragment.BaseFragment;
/**
* Created on ${DATA}.
*/
public class ${CLASS_NAME} extends BaseFragment{
@Override
public int getLayoutId() {
return R.layout.fragment_net;
}
}
- 配置文件如下:
{
"app_id":"panda.android.test",
"list":[
{
"package_name":"panda.android.test",
"template":"BaseFragment.java.tpl",
"fileList":[
"BabyFragment",
"MorningCheckFragment"
]
}
]
}
- 腳本如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys,re,traceback
from datetime import datetime
from string import Template
import json
def generateAndroidJavaCode(app_id, package_name, template, fileName):
className = fileName;
path = "./" + package_name.replace('.','/');
filePath = r'%s/%s.java' % (path,className)
if os.path.exists(path) == False:
os.makedirs(path)
class_file = open(filePath,'w')
lines = []
#模版文件
template_file = open(r'BaseFragment.java.tpl','r')
tmpl = Template(template_file.read())
#模版替換
lines.append(tmpl.substitute(
PACKAGE_NAME = package_name,
ANDROID_APP_ID = app_id,
DATA = datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
CLASS_NAME = className))
# 0.將生成的代碼寫入文件
class_file.writelines(lines)
class_file.close()
print 'generate %s over. ~ ~' % filePath
## 1. 解析配置文件
configuration = """
{
"app_id":"panda.android.test",
"list":[
{
"package_name":"panda.android.test",
"template":"BaseFragment.java.tpl",
"fileList":[
"BabyFragment",
"MorningCheckFragment"
]
}
]
}
"""
configurationJson = json.loads(configuration);
## 2. 按照模板生成對應的文件
app_id = configurationJson["app_id"]
for filesInfo in configurationJson["list"]:
package_name = filesInfo["package_name"]
template = filesInfo["template"]
for fileName in filesInfo["fileList"]:
generateAndroidJavaCode(app_id, package_name, template, fileName);
-
最后生成的文件如下:
引用
Panda
2016-07-04