forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildSdk.php
More file actions
268 lines (215 loc) · 7.36 KB
/
BuildSdk.php
File metadata and controls
268 lines (215 loc) · 7.36 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
<?php
namespace ProcessMaker;
use Exception;
use function GuzzleHttp\json_decode;
use ProcessMaker\Events\BuildScriptExecutor;
use ProcessMaker\Facades\Docker;
use ZipArchive;
class BuildSdk
{
private $debug = true;
private $image = 'openapitools/openapi-generator-cli';
private $tag = 'v5.1.1';
private $lang = null;
private $outputPath = null;
private $jsonPath = null;
private $tmpfile = null;
private $userId = null;
public function __construct($jsonPath, $outputPath)
{
$this->jsonPath = $jsonPath;
$this->outputPath = rtrim($outputPath, '/');
}
public function run()
{
$folder = '/tmp/sdk-' . $this->lang;
$this->runCmd('rm -rf ' . $folder);
$this->writeOptionsToTmpFile();
$this->startContainer();
$this->cp($this->jsonPath, 'generator:/api-docs.json');
$this->cp($this->tmpfile, 'generator:/config.json');
$this->generator('validate -i /api-docs.json');
$this->generator('generate -g ' . $this->lang . ' -i /api-docs.json -c /config.json -o /sdk');
$this->cp('generator:/sdk', $folder);
$this->stopContainer();
$this->fixErroneousCode($folder); // lua and python
$this->addMissingDependency($folder); // java
$this->removeDateTime($folder); // csharp
$this->runCmd("cp -rf {$folder}/. {$this->outputDir()}");
return "DONE. Api is at {$this->outputDir()}";
}
public function setUserId($userId)
{
if (!is_numeric($userId)) {
throw new \Exception('User id must be a number');
}
$this->userId = $userId;
}
private function cp($from, $to)
{
$this->runCmd(Docker::command() . ' cp ' . $from . ' ' . $to);
}
private function imageWithTag()
{
return $this->image . ':' . $this->tag;
}
private function startContainer()
{
$this->runCmd(Docker::command() . " run -t -d --entrypoint '/bin/sh' --name generator " . $this->imageWithTag());
}
private function stopContainer()
{
$this->runCmd(Docker::command() . ' kill generator 2>&1 || true');
$this->runCmd(Docker::command() . ' rm generator 2>&1 || true');
}
public function setLang($value)
{
$langs = $this->getAvailableLanguages();
if (!in_array($value, $langs)) {
throw new Exception("$value language is not supported. Must be one of these: " . implode(',', $langs));
}
$this->lang = $value;
}
public function getOptions()
{
if (!$this->lang) {
throw new Exception('Language must be specified using setLang()');
}
return $this->runCmd(Docker::command() . ' run ' . $this->imageWithTag() . ' config-help -g ' . $this->lang);
}
public function getAvailableLanguages()
{
$result = $this->runCmd(Docker::command() . ' run ' . $this->imageWithTag() . ' list -s');
return explode(',', $result);
}
private function runChecks()
{
if (!$this->lang) {
throw new Exception('Language must be specified using setLang()');
}
if (!is_dir($this->outputPath)) {
throw new Exception("{$this->outputPath} is not a valid directory");
}
if (!is_writable($this->outputPath)) {
throw new Exception('Folder is not writeable: ' . $this->outputPath);
}
if (!is_file($this->jsonPath) || !is_readable($this->jsonPath)) {
throw new Exception('Json file does not exist or can not be read: ' . $this->jsonPath);
}
}
private function outputDir()
{
return $this->outputPath;
}
private function generator($cmd)
{
return $this->runCmd(Docker::command() . ' exec generator docker-entrypoint.sh ' . $cmd);
}
private function writeOptionsToTmpFile()
{
$this->tmpfile = tempnam('/tmp', 'json');
$handle = fopen($this->tmpfile, 'w');
fwrite(
$handle,
json_encode($this->getConfig())
);
fclose($handle);
}
private function getConfig()
{
// get all available options with curl http://127.0.0.1:8080/api/gen/clients/php
$options = [
'gitUserId' => 'processmaker',
'gitRepoId' => 'sdk-' . $this->lang,
'packageName' => 'pmsdk',
'appDescription' => 'SDK Client for the ProcessMaker App',
'infoUrl' => 'https://github.com/ProcessMaker/processmaker',
'infoEmail' => '[email protected]',
];
if (isset($this->config()['options'])) {
$options = array_merge($options, $this->config()['options']);
}
return $options;
}
private function config()
{
return config('script-runners.' . $this->lang);
}
private function runCmd($cmd)
{
if ($this->userId) {
event(new BuildScriptExecutor("Running: $cmd\n", $this->userId, 'running'));
} else {
$this->log("Running: $cmd");
}
$dsc = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']];
$process = proc_open("($cmd) 2>&1", $dsc, $pipes);
$output = '';
while (!feof($pipes[1])) {
$line = fgets($pipes[1]);
if ($this->userId) {
event(new BuildScriptExecutor($line, $this->userId, 'running'));
}
$output .= $line;
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$returnVal = proc_close($process);
if ($returnVal) {
$this->stopContainer();
$message = "Cmd returned: $returnVal " . $output;
if ($this->userId) {
event(new BuildScriptExecutor($message, $this->userId, 'error'));
}
throw new Exception($message);
}
$this->log("Got: '$output'");
return $output;
}
private function log($message)
{
if ($this->debug) {
echo "$message\n";
}
}
private function fixErroneousCode($folder)
{
if ($this->lang === 'lua') {
$this->runCmd("find {$folder} -name '*.lua' -exec sed -i -E 's/(req\.readers:upsert.*)/-- \\1/g' {} \;");
}
if ($this->lang === 'python') {
// Replace \User with \\User since slash \U is unicode in python. Major slashitis.
$this->runCmd(
"find {$folder} -name '*.py' -exec " .
"sed -i -E 's/ProcessMaker\\\Models\\\/ProcessMaker\\\\\\\Models\\\\\\\/g' {} \;"
);
}
}
private function addMissingDependency($folder)
{
if ($this->lang !== 'java') {
return;
}
$file = "{$folder}/pom.xml";
$dom = new \DOMDocument();
$dom->load($file);
$deps = $dom->getElementsByTagName('dependencies')[0];
$dep = $dom->createDocumentFragment();
$dep->appendXML('
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
');
$deps->appendChild($dep);
file_put_contents($file, $dom->saveXml());
}
private function removeDateTime($folder)
{
if ($this->lang === 'csharp') {
unlink("{$folder}/src/ProcessMakerSDK/Model/DateTime.cs");
}
}
}