forked from isitnikov/m2-convert-patch-for-composer-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-for-composer.php
executable file
·168 lines (136 loc) · 4.87 KB
/
convert-for-composer.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
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
#!/usr/bin/env php
<?php
class Converter
{
const MODULE = 'Module';
const ADMINHTML_DESIGN = 'AdminhtmlDesign';
const FRONTEND_DESIGN = 'FrontendDesign';
const LIBRARY = 'Library';
protected $nonComposerPath = array(
self::MODULE => 'app/code/Magento/',
self::ADMINHTML_DESIGN => 'app/design/adminhtml/Magento/',
self::FRONTEND_DESIGN => 'app/design/frontend/Magento/',
self::LIBRARY => 'lib/internal/Magento/'
);
protected $composerPath = array(
self::MODULE => 'vendor/magento/module-',
self::ADMINHTML_DESIGN => 'vendor/magento/theme-adminhtml-',
self::FRONTEND_DESIGN => 'vendor/magento/theme-frontend-',
self::LIBRARY => 'vendor/magento/'
);
protected $options;
public function __construct($params = array())
{
$filename = $this->parseArgs($params);
$this->validateFile($filename);
$this->convert($filename);
}
protected function showHelp()
{
echo <<<HELP_TEXT
Usage: php -f converter-for-composer.php [options] file [> new-file]
converter-for-composer.php [options] file [> new-file]
file path to source PATCH file
[options]
-h, --help Show help
-r Reverse mode. Convert composer format back to git
HELP_TEXT;
exit(0);
}
protected function parseArgs($params)
{
if (count($params) < 2) {
$this->showHelp();
}
array_shift($params);
$this->options = getopt('rh', array('help'));
if (isset($this->options['h']) || isset($this->options['help'])) {
$this->showHelp();
}
$filename = array_pop($params);
return $filename;
}
protected function validateFile($filename)
{
if (!file_exists($filename) || is_dir($filename)) {
printf("Error! File %s does not exist.\n", $filename);
exit(1);
}
if (!is_readable($filename)) {
printf("Error! Can not read file %s.\n", $filename);
exit(2);
}
}
public function camelCaseStringCallback($value)
{
return trim(preg_replace_callback('/((?:^|[A-Z])[a-z]+)/',
array($this, 'splitCamelCaseByDashes'), $value[1]), '-') . '/';
}
public function camelCaseStringCallbackModule($value)
{
return $this->composerPath[self::MODULE] . $this->camelCaseStringCallback($value);
}
public function camelCaseStringCallbackAdminhtmlDesign($value)
{
return $this->composerPath[self::ADMINHTML_DESIGN] . $this->camelCaseStringCallback($value);
}
public function camelCaseStringCallbackFrontendDesign($value)
{
return $this->composerPath[self::FRONTEND_DESIGN] . $this->camelCaseStringCallback($value);
}
public function camelCaseStringCallbackLibrary($value)
{
return $this->composerPath[self::LIBRARY] . $this->camelCaseStringCallback($value);
}
public function splitCamelCaseByDashes($value)
{
return '-' . strtolower($value[0]);
}
protected function convertGitToComposer(&$content)
{
foreach ($this->nonComposerPath as $type => $path) {
$content = preg_replace_callback('/' . addcslashes($path, '/') . '([A-z0-9\-]+)?\//',
array($this, 'camelCaseStringCallback' . $type), $content);
}
return $content;
}
protected function convertDashedStringToCamelCase($string)
{
return str_replace('-', '', ucwords($string, '-'));
}
protected function dashedStringCallbackModule($matches)
{
return $this->nonComposerPath[self::MODULE] . $this->convertDashedStringToCamelCase($matches[1]);
}
protected function dashedStringCallbackAdminhtmlDesign($matches)
{
return $this->nonComposerPath[self::ADMINHTML_DESIGN] . $matches[1];
}
protected function dashedStringCallbackFrontendDesign($matches)
{
return $this->nonComposerPath[self::FRONTEND_DESIGN] . $matches[1];
}
protected function dashedStringCallbackLibrary($matches)
{
return $this->nonComposerPath[self::LIBRARY] . $this->convertDashedStringToCamelCase($matches[1]);
}
protected function convertComposerToGit(&$content)
{
foreach ($this->composerPath as $type => $path) {
$content = preg_replace_callback('~' . $path . '([-\w]+)~',
array($this, 'dashedStringCallback' . $type), $content);
}
return $content;
}
protected function convert($filename)
{
$content = file_get_contents($filename);
if (!isset($this->options['r'])) {
echo $this->convertGitToComposer($content);
} else {
echo $this->convertComposerToGit($content);
}
exit(0);
}
}
new Converter($argv);