-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathUtilities.php
More file actions
170 lines (147 loc) · 4.53 KB
/
Utilities.php
File metadata and controls
170 lines (147 loc) · 4.53 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
<?php
namespace EditorconfigChecker;
class Utilities
{
/**
* Returns the architecture of the current machine
*/
public static function getCurrentArch(): string
{
$arch = php_uname('m');
switch ($arch) {
case 'x86_64':
case 'AMD64':
return "amd64";
case 'i386':
return "386";
case 'arm64':
case 'aarch64':
return "arm64";
default:
printf('ERROR: Unexpected, please contact the maintainer or provide a pull request :)%s', PHP_EOL);
exit(1);
}
}
/**
* Returns the operating system of the current machine
*/
public static function getCurrentOs(): string
{
$os = strtolower(php_uname('s'));
if ($os === 'windows nt') {
$os = 'windows';
}
return $os;
}
/**
* Returns the releasename needed for this machine
*/
public static function getReleaseName(): string
{
return sprintf('editorconfig-checker-%s-%s', Utilities::getCurrentOs(), Utilities::getCurrentArch());
}
/**
* Returns the root path of this library
*/
public static function getBasePath(): string
{
$basePath = sprintf("%s/../..", dirname(__FILE__));
return $basePath;
}
/**
* returns the binary name
*/
public static function getBinaryPath(): string
{
$binaryName = Utilities::getReleaseName();
if (self::getCurrentOs() === 'windows') {
$binaryName .= '.exe';
}
$binaryPath = sprintf('%s/bin/%s', Utilities::getBasePath(), $binaryName);
return $binaryPath;
}
/**
* Downloads the release from the release page
*/
public static function downloadReleaseArchive(string $releaseName, string $version): bool
{
$archivePath = sprintf('%s/%s.tar.gz', Utilities::getBasePath(), $releaseName);
$releaseSuffix = '.tar.gz';
if (self::getCurrentOs() === 'windows') {
$releaseSuffix = '.exe.tar.gz';
}
$releaseUrl = sprintf(
'https://github.com/editorconfig-checker/editorconfig-checker/releases/download/v%s/%s',
$version,
$releaseName . $releaseSuffix
);
$result = file_put_contents($archivePath, fopen($releaseUrl, 'r'));
return $result > 0;
}
/**
* decompresses and extracts the release archive
*/
public static function extractReleaseArchive(string $releaseName): bool
{
return Utilities::decompress($releaseName) && Utilities::unpack($releaseName);
}
/**
* decompresses the release archive
*/
public static function decompress(string $releaseName): bool
{
try {
$p = new \PharData(sprintf("%s/%s.tar.gz", Utilities::getBasePath(), $releaseName));
$p->decompress();
} catch (\Exception $e) {
printf('ERROR: Can not decompress the archive%s%s', PHP_EOL, $e);
return false;
}
return true;
}
/**
* unpacks the release archive
*/
public static function unpack(string $releaseName): bool
{
try {
$p = new \PharData(sprintf("%s/%s.tar", Utilities::getBasePath(), $releaseName));
$p->extractTo(Utilities::getBasePath());
if (!unlink(sprintf("%s/%s.tar", Utilities::getBasePath(), $releaseName))) {
printf('ERROR: Can not remove the decompressed archive%s', PHP_EOL);
return false;
}
} catch (\PharException $e) {
printf('ERROR: Can not unpack the archive%s%s', PHP_EOL, $e);
return false;
}
return true;
}
/**
* Constructs the arguments the binary needs to be called by
* the arguments providedunline
*
* @param string[] $arguments
*/
public static function constructStringFromArguments(array $arguments): string
{
$result = '';
foreach ($arguments as $argument) {
$result .= ' ' . $argument;
}
return $result;
}
/**
* Removes all intermediate files
*/
public static function cleanup(): void
{
$releaseName = sprintf("%s/%s", Utilities::getBasePath(), Utilities::getReleaseName());
if (is_file($releaseName . '.tar.gz')) {
unlink($releaseName . '.tar.gz');
}
if (is_file($releaseName . '.tar')) {
unlink($releaseName . '.tar');
}
}
}