Skip to content

Commit c3cc62c

Browse files
committed
downloader ip
1 parent 6a3759e commit c3cc62c

File tree

3 files changed

+113
-83
lines changed

3 files changed

+113
-83
lines changed

lib/boostDownloader.js

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
var _ = require("lodash");
2-
var IntLog = require("./intLog");
2+
var cmakejs = require("cmake-js");
3+
var CMLog = cmakejs.CMLog;
34
var Bluebird = require("bluebird");
45
var fs = Bluebird.promisifyAll(require("fs-extra"));
56
var semver = require("semver");
67
var path = require("path");
8+
var environment = cmakejs.environment;
9+
var cli = require("cli");
10+
var zlib = require("zlib");
11+
var tar = require("tar");
12+
var request = require("request");
13+
14+
function downloadTo(url, result) {
15+
return new Bluebird(function (resolve, reject) {
16+
request
17+
.get(url)
18+
.on('error', function (err) { reject(err); })
19+
.pipe(result);
20+
21+
result.once("finish", function () { resolve(); });
22+
});
23+
}
724

825
function BoostDownloader(options) {
926
this.options = options;
10-
this.dir = options.directory;
27+
this.dir = path.join(environment.home, ".cmake-js", "boost");
1128
this.version = options.version;
12-
this.log = new IntLog(options);
29+
this.log = new CMLog(this.options);
1330
}
1431

1532
BoostDownloader.prototype.ensureDownloaded = function() {
@@ -36,6 +53,7 @@ BoostDownloader.prototype.ensureDownloaded = function() {
3653
})
3754
.then(function(foundDir) {
3855
if (foundDir) {
56+
self.log.info("BOOST", "Boost found in '" + foundDir + "'.");
3957
return foundDir;
4058
}
4159

@@ -44,5 +62,92 @@ BoostDownloader.prototype.ensureDownloaded = function() {
4462
};
4563

4664
BoostDownloader.prototype._download = function() {
47-
// git ls-remote --tags https://github.com/boostorg/boost.git - list tags
65+
var self = this;
66+
self.log.verbose("BOOST", "Getting Boost releases.");
67+
var command = "git ls-remote --tags https://github.com/boostorg/boost.git";
68+
return new Bluebird(function(resolve, reject) {
69+
cli.exec(command,
70+
function (output) {
71+
var downloadVersion = null;
72+
if (output && output.length) {
73+
output.forEach(function(line) {
74+
var parts = line.split(/\s+/);
75+
if (parts.length === 2) {
76+
var relVersion = parts[1].substr("refs/tags/boost-".length);
77+
self.log.verbose("BOOST", "Comparing version: " + relVersion);
78+
if (semver.satisfies(relVersion, self.version)) {
79+
self.log.verbose("Version OK.");
80+
downloadVersion = relVersion;
81+
return false;
82+
}
83+
}
84+
});
85+
}
86+
if (downloadVersion) {
87+
return self._downloadVersion(downloadVersion);
88+
}
89+
else {
90+
reject(new Error("No releases found."));
91+
}
92+
},
93+
function (err, output) {
94+
if (err instanceof Error) {
95+
reject(new Error(err.message + (output ? ("\n" + output) : "")));
96+
return;
97+
}
98+
if (_.isArray(output) && output.length || err && err.message) {
99+
reject(new Error("Git exec error: " + err.message || err));
100+
return;
101+
}
102+
reject(new Error("Git exec error."));
103+
});
104+
});
105+
};
106+
107+
BoostDownloader.prototype._downloadVersion = function(version) {
108+
var self = this;
109+
var downloadUrl = "https://github.com/boostorg/boost/archive/boost-" + version + ".tar.gz";
110+
var internalPath = path.join(this.dir, version);
111+
self.log.http("BOOST", "Downloading Boost main package: " + downloadUrl);
112+
113+
var gunzip = zlib.createGunzip();
114+
var extracter = new tar.Extract({
115+
path: internalPath,
116+
strip: 1
117+
});
118+
119+
var main = new Bluebird(function (resolve, reject) {
120+
extracter.once("end", function () {
121+
resolve();
122+
});
123+
extracter.once("error", function (err) { reject(err); });
124+
request
125+
.get(tarUrl)
126+
.on('error', function (err) { reject(err); })
127+
.pipe(gunzip)
128+
.pipe(extracter);
129+
});
130+
131+
var submodules = main.then(function() {
132+
var task = [];
133+
task.push(self._downloadSubmo(version, path.join(internalPath, "tools"), "build"));
134+
task.push(self._downloadSubmo(version, path.join(internalPath, "tools"), "inspect"));
135+
var libsPath = path.join(internalPath, "libs");
136+
task.push(
137+
fs.readdirAsync(libsPath)
138+
.then(function(libs) {
139+
var libTasks = [];
140+
libs.forEach(function (lib) {
141+
var fullLibPath = path.join(libsPath, lib);
142+
var stat = fs.lstatSync(fullLibPath);
143+
if (stat.isDirectory()) {
144+
libTasks.push(self._downloadSubmo(version, fullLibPath, lib));
145+
}
146+
});
147+
return Bluebird.all(libTasks);
148+
}));
149+
return Bluebird.all(task);
150+
});
151+
152+
return submodules;
48153
};

lib/intLog.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
}
2424
},
2525
"dependencies": {
26-
"semver": "^4.3.4",
2726
"bluebird": "^2.9.15",
2827
"cli": "^0.6.5",
28+
"cmake-js": "^0.10.0",
2929
"debug": "^2.1.3",
3030
"fs-extra": "^0.16.5",
3131
"lodash": "^3.6.0",
32+
"memory-stream": "0.0.3",
3233
"npmlog": "^1.2.0",
3334
"request": "^2.54.0",
35+
"semver": "^4.3.4",
3436
"tar": "^1.0.3",
3537
"url-join": "0.0.1",
36-
"yargs": "^3.6.0",
37-
"memory-stream": "0.0.3"
38+
"yargs": "^3.6.0"
3839
}
3940
}

0 commit comments

Comments
 (0)