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
225 lines (191 loc) · 6.73 KB
/
BuildSdk.php
File metadata and controls
225 lines (191 loc) · 6.73 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
<?php
namespace ProcessMaker;
use \Exception;
use \ZipArchive;
use function GuzzleHttp\json_decode;
class BuildSdk {
private $rebuild = false;
private $debug = false;
private $image = "openapitools/openapi-generator-cli";
private $tag = "v4.2.2";
private $lang = null;
private $outputPath = null;
private $jsonPath = null;
private $tmpfile = null;
public function __construct($jsonPath, $outputPath, $debug = false, $rebuild = false) {
$this->jsonPath = $jsonPath;
$this->outputPath = rtrim($outputPath, "/");
$this->debug = $debug;
$this->rebuild = $rebuild;
}
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()}";
}
private function cp($from, $to)
{
$this->runCmd("docker cp " . $from . " " . $to);
}
private function imageWithTag()
{
return $this->image . ":" . $this->tag;
}
private function startContainer()
{
$this->runCmd("docker run -t -d --entrypoint '/bin/sh' --name generator " . $this->imageWithTag());
}
private function stopContainer()
{
$this->runCmd("docker kill generator || true");
$this->runCmd("docker rm generator || 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 run ' . $this->imageWithTag() . ' config-help -g ' . $this->lang);
}
public function getAvailableLanguages()
{
$result = $this->runCmd('docker 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_dir($this->outputDir())) {
// throw new Exception("Folder exists: {$this->outputDir()}. You must manually remove the destination folder before running this script.");
// }
if (!is_file($this->jsonPath) || !is_readable($this->jsonPath)) {
throw new Exception("Json file does not exist or can not be read: " . $this->jsonPath);
}
// if (json_decode($this->apiJsonRaw()) === null) {
// throw new Exception("File is not valid json: " . $this->jsonPath);
// }
}
private function outputDir()
{
return $this->outputPath;
}
private function generator($cmd)
{
return $this->runCmd('docker 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" => "info@processmaker.com",
];
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)
{
$this->log("Running: $cmd");
exec($cmd . " 2>&1", $output, $returnVal);
$output = implode("\n", $output);
if ($returnVal) {
$this->stopContainer();
throw new Exception("Cmd returned: $returnVal " . $output);
}
$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");
}
}
}