1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jamal\JenkinsArduino; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Jamal\JenkinsArduino\Jenkins\Client; |
7
|
|
|
use Jamal\JenkinsArduino\Jenkins\JobConfig; |
8
|
|
|
use Jamal\JenkinsArduino\Jenkins\Credentials; |
9
|
|
|
use Jamal\JenkinsArduino\Serial\Writer as SerialWriter; |
10
|
|
|
use Jamal\JenkinsArduino\Serial\Detector; |
11
|
|
|
use Jamal\JenkinsArduino\Jenkins\ArduinoFormatter; |
12
|
|
|
use Jamal\JenkinsArduino\Http\Request; |
13
|
|
|
use Jamal\JenkinsArduino\Validation\NotEmpty; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Caio Almeida <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Runner |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Roda a aplicação |
22
|
|
|
*/ |
23
|
|
|
public static function run() |
24
|
|
|
{ |
25
|
|
|
$request = new Request(); |
26
|
|
|
|
27
|
|
|
$job = $request->get('job'); |
28
|
|
|
|
29
|
|
|
$validator = new NotEmpty('job', $job); |
30
|
|
|
|
31
|
|
|
if (!$validator->isValid()) { |
32
|
|
|
header("HTTP/1.1 422 Unprocessable Entity", true, 422); |
33
|
|
|
echo $validator->getMessage(); |
34
|
|
|
self::stop(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$config = self::getConfig(); |
38
|
|
|
|
39
|
|
|
$jobConfig = new JobConfig($job, $config['server'], $config['port']); |
40
|
|
|
$jobConfig->setCredentials(new Credentials()); |
41
|
|
|
|
42
|
|
|
$client = new Client(); |
43
|
|
|
|
44
|
|
|
$build = $client->doRequest($jobConfig); |
45
|
|
|
|
46
|
|
|
$formatter = new ArduinoFormatter(); |
47
|
|
|
$data = $formatter->format($build); |
48
|
|
|
|
49
|
|
|
$usbDetector = new Detector(); |
50
|
|
|
$usbArduino = $usbDetector->detect($config['serialType'], $config['serialCount']); |
51
|
|
|
|
52
|
|
|
$serialReader = new SerialWriter(); |
53
|
|
|
$serialReader->write($usbArduino, $data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public static function getConfig() |
60
|
|
|
{ |
61
|
|
|
$path = __DIR__ . '/../../config/production.php'; |
62
|
|
|
|
63
|
|
|
if (!is_readable($path)) { |
64
|
|
|
throw new Exception('O arquivo de configuração production.php não existe!'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$config = include $path; |
68
|
|
|
return $config; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function stop() |
72
|
|
|
{ |
73
|
|
|
die; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.