-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpcc
130 lines (118 loc) · 3.81 KB
/
phpcc
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
#!/usr/bin/env php
<?php
if (count($argv) < 2) {
print<<<EOT
install: install php-cc,add pre-commit to .git/hooks,and rename the old pre-commit file to pre-commit.bak
remove: remove php-cc,remove .git/hooks/pre-commit,and mv pre-commit.bak to pre-commit
config: config php-cc,[phpcc config key value] is to set config item [key] to value [value]
See 'composer exec phpcc <command>' [install|remove|config] key value
EOT;
exit(1);
}
switch ($argv[1]) {
case 'install':
install();
break;
case 'remove':
remove();
break;
# case 'help':
# print<<<EOT
#install: install php-cc,add pre-commit to .git/hooks,and rename the old pre-commit file to pre-commit.bak
#remove: remove php-cc,remove .git/hooks/pre-commit,and mv pre-commit.bak to pre-commit
#config: config php-cc,[phpcc config key value] is to set config item [key] to value [value]
#See 'composer exec phpcc <command>' [install|remove|config] key value
#EOT;
# break;
case 'remove':
remove();
break;
default:
echo 'No such command...' . "\n";
break;
}
print <<<EOT
install: install php-cc,add pre-commit to .git/hooks,and rename the old pre-commit file to pre-commit.bak
remove: remove php-cc,remove .git/hooks/pre-commit,and mv pre-commit.bak to pre-commit
config: config item-key [set-value]
EOT;
exit(0);
function install()
{
# check git
echo "Checking git repository...\n";
if (!is_dir("./.git")) {
echo "Your project has not been init by git! Please check it...\n";
exit(1);
}
# check phplint
echo "Checking phplint install...\n";
exec('phplint --version', $phplint_check_rs, $return_var);
// exec('./vendor/bin/phplint --version', $phplint_check_rs, $return_var);
if ($return_var) {
echo "Checking phplint failed! Please install phplint first!";
exit(1);
} else {
echo "Checking phplint success!\n";
echo $phplint_check_rs[0] . "\n";
}
# check phpcs
echo "Checking phpcs install...\n";
exec('phpcs --version', $phpcs_check_rs, $return_var);
if ($return_var) {
echo "Checking phpcs failed! Please install phpcs first!";
exit(1);
} else {
echo "Checking phpcs success!\n";
echo $phpcs_check_rs[0] . "\n";
}
if (is_file('./.git/hooks/pre-commit')) {
exec('mv ./.git/hooks/pre-commit ./.git/hooks/pre-commit.bak.' . time());
}
exec('cp ./vendor/zhenggui/php-cc/pre-commit ./.git/hooks');
echo "php-cc install success!\n";
exit(0);
}
function remove()
{
# delete phpcc file
echo "Delete pre-commit file...\n";
if (is_file('.git/hooks/pre-commit')) {
exec('rm -f .git/hooks/pre-commit');
}
# check old file
echo "Retrieve the old pre-commit file...\n";
if (is_file('.git/hooks/pre-commit.bak')) {
exec('mv .git/hooks/pre-commit.bak .git/hooks/pre-commit');
}
echo "Remove phpcc success!\n";
exit(0);
}
function get_ini_file()
{
$file_name = './vendor/zhenggui/php-cc/phpcc.ini';
$str = file_get_contents($file_name);
$ini_list = explode("\n", $str);
$ini_items = array();
foreach ($ini_list as $item) {
$one_item = explode("=", $item);
if (isset($one_item[0]) && isset($one_item[1])) {
$ini_items[trim($one_item[0])] = trim($one_item[1]);
}
}
return $ini_items;
}
function write_ini_file($key, $value)
{
$config_file_path = './vendor/zhenggui/php-cc/phpcc.ini';
$content = file_get_contents($config_file_path);
if (strpos($content, "$key=")) {
$content = preg_replace("/$key=.*/", "$key=$value", $content);
} else {
$content .= "\n";
$content .= "$key=$value";
}
$file = fopen($config_file_path, 'w');
fwrite($file, $content);
return true;
}