forked from Javey/m3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm3d.php
More file actions
executable file
·157 lines (136 loc) · 4.13 KB
/
Copy pathm3d.php
File metadata and controls
executable file
·157 lines (136 loc) · 4.13 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Created by JetBrains PhpStorm. M3D安装脚本
* User: zoujiawei
* Date: 14-3-21
* Time: 下午9:50
* To change this template use File | Settings | File Templates.
*/
process($argv);
function process($argv) {
if (!isset($argv[1])) {
displayHelp();
exit();
}
switch ($argv[1]) {
case 'install':
install();
break;
case 'add':
add($argv);
break;
case 'rm':
remove($argv);
break;
case 'help':
default:
displayHelp();
break;
}
}
function install() {
exec('pwd', $info);
$curPath = $info[0];
$parentPath = dirname($curPath);
// 判断是否已安装
if (!file_exists("${parentPath}/conf/config.php")) {
shell_exec("cp -rf ${curPath}/Install/global/* ${parentPath}");
out('m3d安装完成, 接下来,请添加工程');
} else {
out('m3d已安装,重新安装请删除配置文件', 'error');
}
}
function add($argv) {
if (!isset($argv[2])) {
displayHelp();
exit();
}
$projectName = $argv[2];
exec('pwd', $info);
$curPath = $info[0];
$curDir = basename($curPath);
$parentPath = dirname($curPath);
$projectPath = $parentPath.'/project/'.$projectName;
mkdir($projectPath, 0777, true);
shell_exec("cp -rf ${curPath}/Install/project/* ${projectPath}");
// 修改index.php
$indexPath = $projectPath.'/index.php';
$contents = file_get_contents($indexPath);
$contents = str_replace('{M3D_CORE}', $curDir, $contents);
file_put_contents($indexPath, $contents);
// 修改site-template lighttpd.conf
$lighttpdTemplate = $projectPath.'/site/site-template/lighttpd.conf';
$contents = str_replace(
array('{project_name}', '{project_path}'),
array($projectName, $projectPath),
file_get_contents($lighttpdTemplate)
);
file_put_contents($lighttpdTemplate, $contents);
// 建立静态文件软链
shell_exec("ln -snf ${curPath}/Ui/static ${projectPath}/static");
shell_exec("ln -snf ${curPath}/Tool/ProLanguage/prolang.php ${projectPath}/site/site-template/wwwdata.test/webroot/prolang.php");
// 修改lighttpd配置文件
$lighttpdConfPath = $projectPath.'/lighttpd.conf';
$contents = file_get_contents($lighttpdConfPath);
$contents = str_replace(
array('{project_name}', '{project_path}'),
array($projectName, $projectPath),
$contents
);
file_put_contents($lighttpdConfPath, $contents);
$text = "${projectName}添加完成";
$text .= PHP_EOL."vHost: ${projectName}.m3d.com";
$text .= PHP_EOL."Have fun!";
out($text, 'success');
}
function remove($argv) {
if (!isset($argv[2])) {
displayHelp();
exit();
}
$name = $argv[2];
exec('pwd', $info);
$curPath = $info[0];
$parentPath = dirname($curPath);
$projectPath = $parentPath.'/project/'.$name;
out("确定删除${name}项目(y/n)", 'info');
$line = trim(fgets(STDIN));
if ($line === 'y') {
if (file_exists($projectPath)) {
exec("rm -rf ${projectPath}", $info, $ret);
if (!$ret) {
out('删除成功', 'success');
} else {
out('删除失败', 'error');
}
} else {
out("${name}不存在", 'error');
}
}
}
function displayHelp() {
echo <<<EOF
M3D Usage
-----------------------
Options
help this help
install deploy m3d
add name add a project
rm name remove a project
EOF;
}
function out($text, $color=null, $newLine=true) {
$styles = array(
'success' => "\033[0;32m%s\033[0m",
'error' => "\033[31;31m%s\033[0m",
'info' => "\033[33;33m%s\033[0m"
);
$format = '%s';
if (isset($styles[$color])) {
$format = $styles[$color];
}
if ($newLine) {
$format .= PHP_EOL;
}
printf($format, $text);
}