-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateTags.php
executable file
·121 lines (100 loc) · 2.81 KB
/
updateTags.php
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
#!/usr/bin/env php
<?php
define('CTAG_FILENAME', 'ctags');
class LineParser
{
protected $line;
public function __construct($line)
{
$this->line = $line;
}
public function extractTag()
{
$info = explode("\t", $this->line);
return $info[0];
}
public function extractPath()
{
$info = explode("\t", $this->line);
return $info[1];
}
public function extractDeclaration()
{
$regex = '#/^(.*)\$/;"#';
preg_match($regex, $this->line, $matches);
return isset($matches[1]) ? trim( $matches[1] ) : '';
}
public function extractType()
{
$regex = '#\$/;"(.*)$#';
preg_match($regex, $this->line, $matches);
return isset($matches[1]) ? trim($matches[1]) : '';
}
public function extractCleanTag()
{
$tag = $this->extractTag();
$split = explode('_', $tag);
return count($split) ? $split[count($split) - 1] : '';
}
public function extractAll()
{
return array(
'tag' => $this->extractTag(),
'path' => $this->extractPath(),
'declaration' => $this->extractDeclaration(),
'type' => $this->extractType(),
'cleanTag' => $this->extractCleanTag(),
);
}
}
// Get current working directory
$currDir = getcwd();
// The CWD is our GIT repo so we can use the alias '.' to specify current dir
echo "Creating tags...\n";
$cmd = 'ctags '.$currDir.' 2> /dev/null'; // Buy using getcwd(), tags paths will be absolute
exec($cmd);
$ctagsPath = $currDir.'/ctags';
if(file_exists($ctagsPath))
{
echo "Parsing tag file\n";
$file = new SplFileObject($ctagsPath, 'a+');
$newFileStr = '';
while(!$file->eof())
{
// Get current line
$line = $file->fgets();
// Check empty lines
if(!isset($line[0]))
continue;
// Add comment
if($line[0] === '!')
{
$newFileStr .= $line;
continue;
}
// Parse current line
$parser = new LineParser($line);
$info = $parser->extractAll();
// Class mapping
if($info['type'] == 'c')
{
// Check PHP extension
$pathRegex = '#(?<!\.old[0-9]|\.old)\.php$#';
if(!preg_match($pathRegex, $info['path']))
continue;
// Comparing filename and tag name
$filename = basename($info['path']);
if($filename === $info['cleanTag'].'.php')
$newFileStr .= $line;
}
else
$newFileStr .= $line;
}
$file = null;
// Write final file
$file = new SplFileObject($ctagsPath, 'w');
$file->fwrite($newFileStr);
$file = null;
}
else
echo "No tag file to parse\n";