-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyaf_cg
executable file
·124 lines (105 loc) · 3.27 KB
/
yaf_cg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env php
<?php
if ($argc < 2) {
die(<<<USAGE
Yaf Code Generetor Version 1.0.0
Usage:
{$argv[0]} ApplicationName [ApplicationPath]
USAGE
);
}
$app_name = $argv[1];
if (empty($argv[2])) {
$app_path = dirname(__FILE__) . "/output/{$app_name}";
} else {
$app_path = rtrim($argv[2], '/\\');
}
$author = trim(`whoami`);
$hostname = trim(`hostname`);
$conf = array(
'AUTHOR' => $author,//开发人员
'APP_NAME' => $app_name,//app名字,请确保这也是一个合法的文件夹名
'DEV_PC' => $author . '@' . $hostname,//开发机,用于生产makefile自动部署
);
define('INPUT_DIR', dirname(__FILE__) . '/templates');
$strOutputRoot = $app_path;
if (file_exists($strOutputRoot)) {
rename($strOutputRoot, $strOutputRoot . date('Ymd-His', time()));
}
$arrTpls = getAllTpls();
foreach ($arrTpls as $strFultTplPath) {
$strContent = processTemplates($strFultTplPath);
$strRelativeTplPath = substr($strFultTplPath, strlen(INPUT_DIR) + 1);
$strOutputRelativePath = convertPath($strRelativeTplPath);
$strOutputPath = $strOutputRoot . '/' . $strOutputRelativePath;
$strOutputDir = dirname($strOutputPath);
if (!file_exists($strOutputDir)) {
mkdir($strOutputDir, 0777, true);
}
file_put_contents($strOutputPath, $strContent);
}
echo "DONE\n";
//获取所有的代码模板文件
function getAllTpls()
{
$intFirst = 0;
$intLast = 1;
$arrQueue = array(INPUT_DIR);
$arrFiles = array();
while ($intFirst < $intLast) {
$strPath = $arrQueue[$intFirst++];
if (!is_dir($strPath)) {
if (file_exists($strPath)) {
$arrSep = explode('.', $strPath);
//只取.tpl文件
if ($arrSep[count($arrSep) - 1] == 'tpl') {
array_push($arrFiles, $strPath);
}
}
} else {
$arrPaths = scandir($strPath);
if (count($arrPaths) == 0) {
continue;
}
foreach ($arrPaths as $strSubPath) {
if ($strSubPath === '.' || $strSubPath === '..') {
continue;
}
$strCurPath = $strPath . '/' . $strSubPath;
$arrQueue[$intLast++] = $strCurPath;
}
}
}
return $arrFiles;
}
//将模板文件名转换成输出的文件名
function convertPath($strPath)
{
global $conf;
//去掉模板后缀
$strPath = str_replace('.tpl', '', $strPath);
$arrSep = explode('.', $strPath);
$bolIsPhp = false;
if ($arrSep[count($arrSep) - 1] == 'php') {
$bolIsPhp = true;
}
//替换文件名中的模板变量
$strToFind = '$APP_NAME$';
$strToRelace = $bolIsPhp ? ucfirst($conf['APP_NAME']) : $conf['APP_NAME'];
$strPath = str_replace($strToFind, $strToRelace, $strPath);
return $strPath;
}
//以后如果需要,可以用smarty等复杂的模板引擎来处理
function processTemplates($strTpl)
{
$strContent = file_get_contents($strTpl);
$arrSearch = array();
$arrReplace = array();
global $conf;
foreach ($conf as $strKey => $strValue) {
$arrSearch[] = '{&$' . $strKey . '&}';
$arrReplace[] = $strValue;
}
$strResult = str_replace($arrSearch, $arrReplace, $strContent);
return $strResult;
}