-
Notifications
You must be signed in to change notification settings - Fork 1
/
composer_wp_plugins_to_git_ignore.php
62 lines (52 loc) · 1.32 KB
/
composer_wp_plugins_to_git_ignore.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
<?php
define('IGNORE_FILE', __DIR__ . '/.gitignore');
define('COMPOSER_FILE', __DIR__ . '/composer.json');
define('PLUGIN_PATH', 'content/plugins/');
define('PLUGIN_DIR', __DIR__ . '/' . PLUGIN_PATH);
$ignored = [];
$new_ignores = [];
$end_with_empty_row = TRUE;
$composer_json = json_decode(file_get_contents(COMPOSER_FILE));
if(!$composer_json)
{
die('Error reading composer.json' . PHP_EOL);
}
if(file_exists(IGNORE_FILE))
{
foreach(explode(PHP_EOL, file_get_contents(IGNORE_FILE)) as $row)
{
$end_with_empty_row = empty($row);
if($end_with_empty_row)
{
continue;
}
if(substr($row, 0, strlen(PLUGIN_PATH)))
{
$ignored[] = trim(substr($row, strlen(PLUGIN_PATH)), '/');
}
}
$ignored = array_combine($ignored, $ignored);
}
foreach($composer_json->require as $name => $version_string)
{
$name_parts = explode('/', $name, 2);
if(isset($ignored[$name_parts[1]]))
{
continue;
}
if(is_dir(PLUGIN_DIR . $name_parts[1] . '/'))
{
$new_ignores[] = PLUGIN_PATH . $name_parts[1] . '/';
}
}
if($new_ignores)
{
if(file_exists(IGNORE_FILE))
{
file_put_contents(IGNORE_FILE, ($end_with_empty_row ? '' : PHP_EOL) . implode(PHP_EOL, $new_ignores) . PHP_EOL, FILE_APPEND);
}
else
{
file_put_contents(IGNORE_FILE, implode(PHP_EOL, $new_ignores) . PHP_EOL);
}
}