2017-03-07 22:24:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
const fs = require('fs-extra');
|
2017-03-07 22:24:30 +00:00
|
|
|
const path = require('path');
|
2017-07-12 00:11:49 +00:00
|
|
|
const os = require('os');
|
2017-09-26 13:08:26 +00:00
|
|
|
const child_process = require('child_process');
|
2017-03-07 22:24:30 +00:00
|
|
|
const fetch = require('node-fetch');
|
2017-10-24 01:26:06 +00:00
|
|
|
const ini = require('ini');
|
2017-07-05 14:37:06 +00:00
|
|
|
const decompress = require('decompress');
|
2017-09-26 13:08:26 +00:00
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
const LOCKFILE_NAME = 'download-realm.lock';
|
2017-10-24 01:26:06 +00:00
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
function exec() {
|
|
|
|
const args = Array.from(arguments);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function callback(error, stdout, stderr) {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
2017-10-12 17:19:06 +00:00
|
|
|
resolve(stdout.trim());
|
2017-09-26 13:08:26 +00:00
|
|
|
}
|
2017-07-12 00:11:49 +00:00
|
|
|
}
|
2017-09-26 13:08:26 +00:00
|
|
|
args.push(callback);
|
|
|
|
child_process.exec.apply(null, args);
|
|
|
|
});
|
2017-07-12 00:11:49 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
function printProgress(input, totalBytes, archive) {
|
2017-07-12 00:11:49 +00:00
|
|
|
const ProgressBar = require('progress');
|
|
|
|
const StreamCounter = require('stream-counter');
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
const message = `Downloading ${archive} [:bar] (:ratek)`;
|
2017-07-12 00:11:49 +00:00
|
|
|
const bar = new ProgressBar(message, {
|
|
|
|
complete: '=',
|
|
|
|
incomplete: ' ',
|
|
|
|
width: process.stdout.columns - message.length,
|
|
|
|
total: totalBytes / 1024
|
|
|
|
});
|
|
|
|
|
|
|
|
input.pipe(new StreamCounter()).on('progress', function() {
|
|
|
|
bar.tick((this.bytes / 1024) - bar.curr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
function download(serverFolder, archive, destination) {
|
|
|
|
const url = `https://static.realm.io/downloads/${serverFolder}/${archive}`;
|
2017-03-07 22:24:30 +00:00
|
|
|
return fetch(url).then((response) => {
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw new Error(`Error downloading ${url} - received status ${response.status} ${response.statusText}`);
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
const lastModified = new Date(response.headers.get('Last-Modified'));
|
|
|
|
return fs.exists(destination)
|
|
|
|
.then(exists => {
|
|
|
|
if (!exists) {
|
|
|
|
return saveFile();
|
|
|
|
} else {
|
|
|
|
return fs.stat(destination)
|
|
|
|
.then(stat => {
|
|
|
|
if (stat.mtime.getTime() !== lastModified.getTime()) {
|
|
|
|
return saveFile();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-03-07 22:24:30 +00:00
|
|
|
});
|
2017-09-26 13:08:26 +00:00
|
|
|
|
|
|
|
function saveFile() {
|
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
printProgress(response.body, parseInt(response.headers.get('Content-Length')), archive);
|
|
|
|
} else {
|
|
|
|
console.log(`Downloading ${archive}`);
|
|
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const file = fs.createWriteStream(destination);
|
|
|
|
response.body.pipe(file).once('finish', () => file.close(resolve));
|
|
|
|
}).then(() => fs.utimes(destination, lastModified, lastModified));
|
|
|
|
}
|
2017-03-07 22:24:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
function extract(downloadedArchive, targetFolder, archiveRootFolder) {
|
|
|
|
console.log(`Extracting ${path.basename(downloadedArchive)} => ${targetFolder}`);
|
2017-09-30 14:35:46 +00:00
|
|
|
const decompressOptions = /tar\.xz$/.test(downloadedArchive) ? { plugins: [ require('decompress-tarxz')() ] } : undefined;
|
2017-09-26 13:08:26 +00:00
|
|
|
if (!archiveRootFolder) {
|
|
|
|
return decompress(downloadedArchive, targetFolder, decompressOptions);
|
|
|
|
} else {
|
|
|
|
const tempExtractLocation = path.resolve(os.tmpdir(), path.basename(downloadedArchive, path.extname(downloadedArchive)));
|
|
|
|
return decompress(downloadedArchive, tempExtractLocation, decompressOptions)
|
|
|
|
.then(() => fs.readdir(path.resolve(tempExtractLocation, archiveRootFolder)))
|
|
|
|
.then(items => Promise.all(items.map(item => {
|
|
|
|
const source = path.resolve(tempExtractLocation, archiveRootFolder, item);
|
|
|
|
const target = path.resolve(targetFolder, item);
|
|
|
|
return fs.copy(source, target, { filter: n => path.extname(n) !== '.so' });
|
|
|
|
})))
|
|
|
|
.then(() => fs.remove(tempExtractLocation))
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 14:37:06 +00:00
|
|
|
|
2017-10-24 01:26:06 +00:00
|
|
|
function acquire(desired, target) {
|
|
|
|
const corePath = desired.CORE_ARCHIVE && path.resolve(os.tmpdir(), desired.CORE_ARCHIVE);
|
|
|
|
const syncPath = desired.SYNC_ARCHIVE && path.resolve(os.tmpdir(), desired.SYNC_ARCHIVE);
|
2017-09-26 13:08:26 +00:00
|
|
|
|
2017-10-24 01:26:06 +00:00
|
|
|
return fs.emptyDir(target)
|
|
|
|
.then(() => corePath && download(desired.CORE_SERVER_FOLDER, desired.CORE_ARCHIVE, corePath))
|
|
|
|
.then(() => corePath && extract(corePath, target, desired.CORE_ARCHIVE_ROOT))
|
|
|
|
.then(() => syncPath && download(desired.SYNC_SERVER_FOLDER, desired.SYNC_ARCHIVE, syncPath))
|
|
|
|
.then(() => syncPath && extract(syncPath, target, desired.SYNC_ARCHIVE_ROOT))
|
2017-10-24 12:29:04 +00:00
|
|
|
.then(() => writeLockfile(target, desired))
|
2017-09-26 13:08:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 17:19:06 +00:00
|
|
|
function getSyncCommitSha(version) {
|
|
|
|
return exec(`git ls-remote git@github.com:realm/realm-sync.git --tags "v${version}^{}"`)
|
|
|
|
.then(stdout => {
|
2017-10-30 19:55:50 +00:00
|
|
|
if (!stdout) {
|
2017-10-12 17:19:06 +00:00
|
|
|
return exec(`git ls-remote git@github.com:realm/realm-sync.git --tags "v${version}"`)
|
|
|
|
} else {
|
|
|
|
return stdout;
|
|
|
|
}
|
|
|
|
}).then(stdout => /([^\t]+)/.exec(stdout)[0]);
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
function getCoreRequirements(dependencies, options, required = {}) {
|
|
|
|
required.CORE_SERVER_FOLDER = `core/v${dependencies.REALM_CORE_VERSION}`;
|
2017-10-24 01:26:06 +00:00
|
|
|
let flavor = options.debug ? 'Debug' : 'Release';
|
|
|
|
|
|
|
|
switch (options.platform) {
|
|
|
|
case 'mac':
|
2017-10-24 12:29:04 +00:00
|
|
|
required.CORE_SERVER_FOLDER += `/macos/${flavor}`;
|
|
|
|
required.CORE_ARCHIVE = `realm-core-${flavor}-v${dependencies.REALM_CORE_VERSION}-Darwin-devel.tar.gz`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-24 01:26:06 +00:00
|
|
|
case 'ios':
|
|
|
|
flavor = flavor === 'Debug' ? 'MinSizeDebug' : flavor;
|
2017-10-24 12:29:04 +00:00
|
|
|
required.CORE_SERVER_FOLDER += `/ios/${flavor}`;
|
|
|
|
required.CORE_ARCHIVE = `realm-core-${flavor}-v${dependencies.REALM_CORE_VERSION}-iphoneos.tar.gz`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-30 19:55:50 +00:00
|
|
|
case 'win': {
|
2017-10-24 01:26:06 +00:00
|
|
|
if (!options.arch) throw new Error(`Specifying '--arch' is required for platform 'win'`);
|
|
|
|
const arch = options.arch === 'ia32' ? 'Win32' : options.arch;
|
2017-10-24 12:29:04 +00:00
|
|
|
required.CORE_SERVER_FOLDER += `/windows/${arch}/nouwp/${flavor}`;
|
|
|
|
required.CORE_ARCHIVE = `realm-core-${flavor}-v${dependencies.REALM_CORE_VERSION}-Windows-${arch}-devel.tar.gz`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-30 19:55:50 +00:00
|
|
|
}
|
2017-10-24 01:26:06 +00:00
|
|
|
case 'linux':
|
2017-10-24 12:29:04 +00:00
|
|
|
required.CORE_SERVER_FOLDER = 'core';
|
|
|
|
required.CORE_ARCHIVE = `realm-core-${dependencies.REALM_CORE_VERSION}.tgz`;
|
|
|
|
required.CORE_ARCHIVE_ROOT = `realm-core-${dependencies.REALM_CORE_VERSION}`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-24 01:26:06 +00:00
|
|
|
default:
|
2017-10-24 12:29:04 +00:00
|
|
|
return Promise.reject(new Error(`Unsupported core platform '${options.platform}'`));
|
2017-10-24 01:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
function getSyncRequirements(dependencies, options, required = {}) {
|
|
|
|
required.SYNC_SERVER_FOLDER = 'sync';
|
2017-09-26 13:08:26 +00:00
|
|
|
let flavor = options.debug ? 'Debug' : 'Release';
|
|
|
|
|
2017-07-05 14:37:06 +00:00
|
|
|
switch (options.platform) {
|
2017-10-24 01:26:06 +00:00
|
|
|
case 'mac':
|
2017-10-24 12:29:04 +00:00
|
|
|
required.SYNC_ARCHIVE = `realm-sync-node-cocoa-${dependencies.REALM_SYNC_VERSION}.tar.gz`;
|
|
|
|
required.SYNC_ARCHIVE_ROOT = `realm-sync-node-cocoa-${dependencies.REALM_SYNC_VERSION}`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-24 01:26:06 +00:00
|
|
|
case 'ios':
|
2017-10-24 12:29:04 +00:00
|
|
|
required.SYNC_ARCHIVE = `realm-sync-cocoa-${dependencies.REALM_SYNC_VERSION}.tar.xz`;
|
|
|
|
required.SYNC_ARCHIVE_ROOT = `core`;
|
|
|
|
return Promise.resolve(required);
|
2017-10-30 19:55:50 +00:00
|
|
|
case 'win': {
|
2017-10-24 01:26:06 +00:00
|
|
|
const arch = options.arch === 'ia32' ? 'Win32' : options.arch;
|
2017-10-24 12:29:04 +00:00
|
|
|
required.SYNC_ARCHIVE = `realm-sync-${flavor}-v${dependencies.REALM_SYNC_VERSION}-Windows-${arch}-devel.tar.gz`;
|
|
|
|
return getCoreRequirements(dependencies, options, required)
|
2017-10-24 01:26:06 +00:00
|
|
|
.then(() => getSyncCommitSha(dependencies.REALM_SYNC_VERSION))
|
2017-10-24 12:29:04 +00:00
|
|
|
.then(sha => {
|
|
|
|
required.SYNC_SERVER_FOLDER += `/sha-version/${sha}`;
|
|
|
|
return required;
|
|
|
|
});
|
2017-10-30 19:55:50 +00:00
|
|
|
}
|
2017-10-24 01:26:06 +00:00
|
|
|
default:
|
|
|
|
return Promise.reject(new Error(`Unsupported sync platform '${options.platform}'`));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
function writeLockfile(target, contents) {
|
|
|
|
return fs.writeFile(path.resolve(target, LOCKFILE_NAME), ini.encode(contents));
|
2017-10-24 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
function readLockfile(target) {
|
2017-10-24 01:26:06 +00:00
|
|
|
try {
|
2017-10-24 12:29:04 +00:00
|
|
|
return ini.parse(fs.readFileSync(path.resolve(target, LOCKFILE_NAME), 'utf8'));
|
2017-10-24 01:26:06 +00:00
|
|
|
} catch (e) {
|
2017-10-24 12:05:42 +00:00
|
|
|
return null;
|
2017-07-05 14:37:06 +00:00
|
|
|
}
|
2017-10-24 01:26:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
function shouldSkipAcquire(target, requirements, force) {
|
2017-10-24 12:05:42 +00:00
|
|
|
if (force) {
|
2017-10-24 12:29:04 +00:00
|
|
|
console.log('Skipping lockfile check as --force is enabled');
|
2017-10-24 12:05:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
const existingLockfile = readLockfile(target);
|
2017-09-26 13:08:26 +00:00
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
if (!existingLockfile) {
|
|
|
|
console.log('No lockfile found at the target, proceeding.');
|
2017-10-24 12:05:42 +00:00
|
|
|
return false;
|
2017-10-24 01:26:06 +00:00
|
|
|
}
|
2017-10-24 12:05:42 +00:00
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
if (!Object.keys(requirements).every(key => existingLockfile[key] === requirements[key])) {
|
|
|
|
console.log('Target directory has a differing lockfile, overwriting.');
|
2017-10-24 12:05:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
console.log('Matching lockfile already exists at target - nothing to do (use --force to override)');
|
2017-10-24 12:05:42 +00:00
|
|
|
return true;
|
2017-07-05 14:37:06 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 13:08:26 +00:00
|
|
|
const optionDefinitions = [
|
|
|
|
{ name: 'platform', type: String, defaultOption: true },
|
|
|
|
{ name: 'arch', type: String },
|
|
|
|
{ name: 'sync', type: Boolean },
|
|
|
|
{ name: 'debug', type: Boolean },
|
2017-10-24 01:26:06 +00:00
|
|
|
{ name: 'force', type: Boolean },
|
2017-09-26 13:08:26 +00:00
|
|
|
];
|
|
|
|
const options = require('command-line-args')(optionDefinitions);
|
|
|
|
if (options.platform === '..\\win') {
|
|
|
|
options.platform = 'win'; // handle gyp idiocy
|
2017-07-12 00:11:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 22:24:30 +00:00
|
|
|
const vendorDir = path.resolve(__dirname, '../vendor');
|
2017-07-12 00:11:49 +00:00
|
|
|
|
|
|
|
let realmDir = path.resolve(vendorDir, `realm-${options.platform}`);
|
|
|
|
if (options.arch) {
|
|
|
|
realmDir += `-${options.arch}`;
|
|
|
|
}
|
|
|
|
if (options.debug) {
|
|
|
|
realmDir += '-dbg'
|
|
|
|
}
|
2017-03-07 22:24:30 +00:00
|
|
|
|
2017-10-24 01:26:06 +00:00
|
|
|
const dependencies = ini.parse(fs.readFileSync(path.resolve(__dirname, '../dependencies.list'), 'utf8'));
|
2017-07-12 00:11:49 +00:00
|
|
|
|
2017-10-24 12:29:04 +00:00
|
|
|
(options.sync ? getSyncRequirements : getCoreRequirements)(dependencies, options)
|
|
|
|
.then(requirements => {
|
|
|
|
console.log('Resolved requirements:', requirements);
|
|
|
|
if (!shouldSkipAcquire(realmDir, requirements, options.force)) {
|
|
|
|
return acquire(requirements, realmDir)
|
2017-10-24 01:26:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => console.log('Success'))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|