This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
/
helpers.js
196 lines (173 loc) · 4.67 KB
/
helpers.js
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
/** @babel */
/**
* Copyright (с) 2016-present PlatformIO <contact@platformio.org>
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import * as config from '../config';
import * as utils from '../utils';
import fs from 'fs-plus';
import path from 'path';
import request from 'request';
import tar from 'tar';
import zlib from 'zlib';
export function getCacheDir() {
if (!fs.isDirectorySync(config.CACHE_DIR)) {
fs.makeTreeSync(config.CACHE_DIR);
}
return config.CACHE_DIR;
}
export async function download(source, target, retries = 3) {
const contentLength = await getContentLength(source);
if (fileExistsAndSizeMatches(target, contentLength)) {
return target;
}
let lastError = '';
while (retries >= 0) {
try {
await _download(source, target);
if (fileExistsAndSizeMatches(target, contentLength)) {
return target;
}
} catch (error) {
lastError = error;
console.error(error);
}
retries--;
}
throw new Error(`Failed to download file ${source}: ${lastError}`);
}
function fileExistsAndSizeMatches(target, contentLength) {
if (fs.isFileSync(target)) {
if (contentLength > 0 && contentLength == fs.getSizeSync(target)) {
return true;
}
try {
fs.removeSync(target);
} catch (err) {
console.error(err);
}
}
return false;
}
async function _download(source, target) {
const proxy = await new Promise((resolve, reject) => {
utils.runAPMCommand(
['config', 'get', 'https-proxy'],
(code, stdout) => {
if (code !== 0) {
return reject(null);
}
resolve(stdout.trim() === 'null' ? null : stdout.trim());
},
{
cacheValid: '1h'
}
);
});
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(target);
const options = {
url: source
};
if (proxy) {
options.proxy = proxy;
}
request.get(options)
.on('error', err => reject(err))
.pipe(file);
file.on('error', err => reject(err));
file.on('finish', () => resolve(target));
});
}
export function getContentLength(url) {
return new Promise(resolve => {
request.head({
url
}, (err, response) => {
if (err || response.statusCode !== 200 || !response.headers.hasOwnProperty('content-length')) {
resolve(-1);
}
resolve(parseInt(response.headers['content-length']));
});
});
}
export function extractTarGz(source, destination) {
return new Promise((resolve, reject) => {
fs.createReadStream(source)
.pipe(zlib.createGunzip())
.on('error', err => reject(err))
.pipe(tar.extract({
cwd: destination
}))
.on('error', err => reject(err))
.on('end', () => resolve(destination));
});
}
export async function getPythonExecutable(customDirs = null) {
const candidates = new Set();
const defaultName = config.IS_WINDOWS ? 'python.exe' : 'python';
if (customDirs) {
customDirs.forEach(dir => candidates.add(path.join(dir, defaultName)));
}
if (atom.config.get('platformio-ide.useBuiltinPIOCore')) {
candidates.add(path.join(config.ENV_BIN_DIR, defaultName));
}
if (config.IS_WINDOWS) {
candidates.add(defaultName);
candidates.add('C:\\Python27\\' + defaultName);
} else {
candidates.add('python2.7');
candidates.add(defaultName);
}
for (const item of process.env.PATH.split(path.delimiter)) {
if (fs.isFileSync(path.join(item, defaultName))) {
candidates.add(path.join(item, defaultName));
}
}
for (const executable of candidates.values()) {
if (!executable.includes('msys') && (await isPython2(executable))) {
return executable;
}
}
return null;
}
function isPython2(executable) {
const args = ['-c', 'import sys; print \'.\'.join(str(v) for v in sys.version_info[:2])'];
return new Promise(resolve => {
utils.runCommand(
executable,
args,
(code, stdout) => {
resolve(code === 0 && stdout.startsWith('2.7'));
}
);
});
}
export function PEPverToSemver(pepver) {
return pepver.replace(/(\.\d+)\.?(dev|a|b|rc|post)/, '$1-$2.');
}
export function reinstallPIOCore() {
if (fs.isDirectorySync(config.ENV_DIR)) {
try {
fs.removeSync(config.ENV_DIR);
} catch (err) {
console.error(err);
}
}
atom.notifications.addWarning(
'PlatformIO Core has been uninstalled!',
{
detail: 'Please restart Atom to install appropriate version',
buttons: [
{
text: 'Restart',
onDidClick: () => atom.restartApplication()
}
],
dismissable: true
}
);
}