-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublime_to_emacs.php
More file actions
296 lines (235 loc) · 7.58 KB
/
sublime_to_emacs.php
File metadata and controls
296 lines (235 loc) · 7.58 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
// Usage: sublime_to_emacs.php /path/to/my/src_project /path/to/my/cleaned_project
// This script will copy files from src_project/ to cleaned_project/, and re-indenting the ones you need
// ---------------------------- BEG. SETTINGS ----------------------------------------------
// You need to set the files you want to re-indent, and the ones you want to copy, to your cleaned directory
// Files containing these strings in their names will be re-indented
$toIndent = ['.c', '.h'];
// Examples:
// $toIndent = ['.cpp', '.h'];
// $toIndent = ['main.c', 'george.c']; // "I only want to reindent precise files!"
// If null, the script will copy EVERY file from source to destination
// If [...], the script will only copy files containing the array strings in their names
$copyOnlyTheseFiles = null;
// Examples:
// $copyOnlyTheseFiles = ['Makefile', '.c', '.h'];
// $copyOnlyTheseFiles = ['.cpp', '.h'];
// $copyOnlyTheseFiles = ['.php'];
// If you dont get this option, just let it at null
// Ps : .svn .git .DS_STORE are NOT copied by default (line 174)
// ---------------------------- END SETTINGS -------------------------------------------------
// nothing to configure from there
$usage = 'Usage: php sublime_to_emacs.php /path/to/my/project /path/to/my/cleaned_project' . PHP_EOL;
$selectionMask = $toIndent;
$preciseCopy = (is_array($copyOnlyTheseFiles) and count($copyOnlyTheseFiles));
$preciseCopyMask = $preciseCopy ? $copyOnlyTheseFiles : null;
if ($argc != 3)
die($usage);
$baseDirectory = substr($argv[1], -1) !== '/' ? $argv[1] . '/' : $argv[1];
$finalDirectory = substr($argv[2], -1) !== '/' ? $argv[2] . '/' : $argv[2];
if ($baseDirectory === $finalDirectory) {
die("L'overwriting n'est pas autorisé et vraiment pas recommandé" . PHP_EOL);
}
$confirm = function ($message) {
$handle = fopen ("php://stdin","r");
while ($resp !== 'y' and 'n' !== $resp) {
echo $message . ' (y/n)' . PHP_EOL . '> ';
$line = fgets($handle);
$resp = substr($line, 0, strlen($line) - 1);
}
fclose($handle);
return $resp === 'y';
};
$removeFinalDir = false;
if (file_exists($finalDirectory)) {
$resp = $confirm(realpath($finalDirectory) . ' already exists. Remove ?');
if (! $resp) {
die('Pussy' . PHP_EOL);
}
$removeFinalDir = true;
}
echo '---------------------------------------------------------------------------';
echo PHP_EOL . 'Sublime to emacs ---> ' . realpath($baseDirectory) . ' copied TO ' . $finalDirectory . PHP_EOL;
echo PHP_EOL . 'Files copied -------> ';
if ($preciseCopy) {
foreach ($preciseCopyMask as $idx => $mask) {
if ($idx) echo ', ';
echo $mask;
} echo PHP_EOL;
} else echo 'Every file' . PHP_EOL;
echo PHP_EOL . 'Files re-indented --> ';
foreach ($selectionMask as $idx => $mask) {
if ($idx) echo ', ';
echo $mask;
} echo PHP_EOL . PHP_EOL;
if (! $confirm('GO?')) die ('tchiiiiiiiiiiiiiiip' . PHP_EOL);
if ($removeFinalDir) recursiveRemoveDirectory($finalDirectory);
mkdir($finalDirectory, 0777, true);
main($baseDirectory, $finalDirectory, $selectionMask, $preciseCopy, $preciseCopyMask);
echo "Success" . PHP_EOL;
/**
* @param string $baseDirectory
* @param string $finalDirectory
* @param array $selectionMask
* @param bool $preciseCopy
* @param array $preciseCopyMask
* @throws Exception
*/
function main($baseDirectory, $finalDirectory, $selectionMask, $preciseCopy, $preciseCopyMask)
{
foreach (buildEmacsFilesArray($baseDirectory, $preciseCopy, $preciseCopyMask) as $file) {
//$path = $finalDirectory . substr($file, 3);
$path = $finalDirectory . substr($file, strlen($baseDirectory) + 1);
mkdir(dirname($path), 0777, true);
foreach ($selectionMask as $masque) {
if (strrpos($file, $masque)) {
file_put_contents($path, toEmacs($file));
continue 2;
}
}
file_put_contents($path, file_get_contents($file));
}
}
/**
* @param string $baseDirectory
* @param bool $preciseCopy
* @param array $preciseCopyMask
* @return array
*/
function buildEmacsFilesArray($baseDirectory, $preciseCopy, $preciseCopyMask)
{
$files = dirToArray($baseDirectory);
if (! $preciseCopy)
return $files;
$res = [];
foreach ($files as $file) {
foreach ($preciseCopyMask as $masque) {
if (strrpos($file, $masque)) {
$res[] = $file;
continue;
}
}
}
return $res;
}
/**
* @param string $directory
* @return array
*/
function dirToArray($directory) {
$arrayItems = array();
$skipByExclude = false;
$handle = opendir($directory);
if ($handle) {
while (false !== ($file = readdir($handle))) {
preg_match("/(^(([\.]){1,2})$|(\.(svn|git))|(Thumbs\.db|\.DS_STORE))$/iu", $file, $skip);
if (!$skip && !$skipByExclude) {
if (is_dir($directory. DIRECTORY_SEPARATOR . $file)) {
$arrayItems = array_merge($arrayItems, dirToArray($directory. DIRECTORY_SEPARATOR . $file));
} else {
$file = $directory . DIRECTORY_SEPARATOR . $file;
$arrayItems[] = $file;
}
}
}
closedir($handle);
}
return $arrayItems;
}
/**
* @param string|null $arg
* @return string
* @throws Exception
*/
function toEmacs($arg = null)
{
if (! $arg)
throw new \Exception('PAS DE ARG');
$fileContent = file_get_contents($arg);
$result = [];
$blanks = [];
$blankGroupIndex = 0;
$newLine = true;
$onBlank = false;
$pos = 0;
foreach (str_split($fileContent) as $char) {
if ($newLine) {
$blanks = [];
$blankGroupIndex = 0;
$newLine = false;
$pos = 0;
$onBlank = false;
}
if ($char === ' ' || chr(9) === $char) {
if (! $onBlank)
$blanks[$blankGroupIndex] = 0;
$actualBlankLen = getLen($pos, $char);
$blanks[$blankGroupIndex] += $actualBlankLen;
$pos += $actualBlankLen;
$onBlank = true;
continue;
} elseif ($onBlank) {
$onBlank = false;
$result[] = [' ', $blanks[$blankGroupIndex++]];
}
if ($char === PHP_EOL)
$newLine = true;
$result[] = [$char, 1];
$pos++;
}
$emacsed = '';
foreach ($result as $charData) {
$emacsed .= getRepresentation(0, $charData);
}
return $emacsed;
}
/**
* @param int $pos
* @param [] $charData
* @return string
*/
function getRepresentation($pos, $charData) // -- TODO Prendre en compte les columns de emacs
{
// 0 : char
// 1 : len
if ($charData[0] !== ' ')
return $charData[0];
$res = '';
if ($charData[1] > 7) {
while (($charData[1] -= 8) > 0) {
$res .= chr(9);
}
$charData[1] += 8;
}
while ($charData[1] -- > 0)
$res .= ' ';
return $res;
}
/**
* @param int $pos
* @param string $char
* @return int
*/
function getLen($pos, $char)
{
$poss = [4,3,2,1];
if ($pos > 3)
while (($pos -= 4) > 3) ;
if ($char != chr(9))
return (1);
return $poss[$pos];
}
//http://stackoverflow.com/questions/11267086/php-unlink-all-files-within-a-directory-and-then-deleting-that-directory
function recursiveRemoveDirectory($directory)
{
foreach(glob("{$directory}/*") as $file)
{
if(is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}
?>