Code coverage report for lib/services/android-project-service.js

Statements: 27.43% (79 / 288)      Branches: 9.89% (9 / 91)      Functions: 9.23% (6 / 65)      Lines: 27.3% (77 / 282)      Ignored: none     

All files » lib/services/ » android-project-service.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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425    1 1 1 1   1 1 1 1 1 1 1 1 1 1 11 11 11 11 11 11 11 11 11 11 11 11 11   1   11 11 11 11                                         11         1                 1                 1                                                     1       1                                         1                     1               1 11 11 11   11   1     1     1                       1                                                                         1     1                         1     1     1                     1             1                 1                                   1                                 1     1                   1                         1     1       1             1                                     1               1               1                               1                     1                                 1 1 1 1 1   1 1  
///<reference path="../.d.ts"/>
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) Iif (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var path = require("path");
var shell = require("shelljs");
var Future = require("fibers/future");
var constants = require("../constants");
var semver = require("semver");
var projectServiceBaseLib = require("./platform-project-service-base");
var androidDebugBridgePath = require("../common/mobile/android/android-debug-bridge");
var AndroidProjectService = (function (_super) {
    __extends(AndroidProjectService, _super);
    function AndroidProjectService($androidEmulatorServices, $androidToolsInfo, $childProcess, $errors, $fs, $hostInfo, $logger, $options, $projectData, $projectDataService, $sysInfo, $mobileHelper, $injector) {
        _super.call(this, $fs, $projectData, $projectDataService);
        this.$androidEmulatorServices = $androidEmulatorServices;
        this.$androidToolsInfo = $androidToolsInfo;
        this.$childProcess = $childProcess;
        this.$errors = $errors;
        this.$hostInfo = $hostInfo;
        this.$logger = $logger;
        this.$options = $options;
        this.$sysInfo = $sysInfo;
        this.$mobileHelper = $mobileHelper;
        this.$injector = $injector;
        this._platformData = null;
        this._androidProjectPropertiesManagers = Object.create(null);
    }
    Object.defineProperty(AndroidProjectService.prototype, "platformData", {
        get: function () {
            Eif (!this._platformData) {
                var projectRoot = path.join(this.$projectData.platformsDir, "android");
                var packageName = this.getProjectNameFromId();
                this._platformData = {
                    frameworkPackageName: "tns-android",
                    normalizedPlatformName: "Android",
                    appDestinationDirectoryPath: path.join(projectRoot, "src", "main", "assets"),
                    platformProjectService: this,
                    emulatorServices: this.$androidEmulatorServices,
                    projectRoot: projectRoot,
                    deviceBuildOutputPath: path.join(projectRoot, "build", "outputs", "apk"),
                    validPackageNamesForDevice: [
                        (packageName + "-debug.apk"),
                        (packageName + "-release.apk"),
                        (this.$projectData.projectName + "-debug.apk"),
                        (this.$projectData.projectName + "-release.apk")
                    ],
                    frameworkFilesExtensions: [".jar", ".dat", ".so"],
                    configurationFileName: "AndroidManifest.xml",
                    configurationFilePath: path.join(projectRoot, "src", "main", "AndroidManifest.xml"),
                    relativeToFrameworkConfigurationFilePath: path.join("src", "main", "AndroidManifest.xml"),
                    mergeXmlConfig: [{ "nodename": "manifest", "attrname": "*" }, { "nodename": "application", "attrname": "*" }]
                };
            }
            return this._platformData;
        },
        enumerable: true,
        configurable: true
    });
    AndroidProjectService.prototype.getAppResourcesDestinationDirectoryPath = function (frameworkVersion) {
        var _this = this;
        return (function () {
            if (_this.canUseGradle(frameworkVersion).wait()) {
                return path.join(_this.platformData.projectRoot, "src", "main", "res");
            }
            return path.join(_this.platformData.projectRoot, "res");
        }).future()();
    };
    AndroidProjectService.prototype.validate = function () {
        var _this = this;
        return (function () {
            _this.validatePackageName(_this.$projectData.projectId);
            _this.validateProjectName(_this.$projectData.projectName);
            _this.$androidToolsInfo.getPathToAndroidExecutable().wait();
            _this.$androidToolsInfo.validateJavacVersion(_this.$sysInfo.getSysInfo().wait().javacVersion, { showWarningsAsErrors: true }).wait();
        }).future()();
    };
    AndroidProjectService.prototype.createProject = function (frameworkDir, frameworkVersion) {
        var _this = this;
        return (function () {
            if (semver.lt(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE)) {
                _this.$errors.failWithoutHelp("The NativeScript CLI requires Android runtime " + AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE + " or later to work properly.");
            }
            _this.$fs.ensureDirectoryExists(_this.platformData.projectRoot).wait();
            _this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true }).wait();
            var androidToolsInfo = _this.$androidToolsInfo.getToolsInfo().wait();
            var targetSdkVersion = androidToolsInfo.targetSdkVersion;
            _this.$logger.trace("Using Android SDK '" + targetSdkVersion + "'.");
            if (_this.$options.symlink) {
                _this.symlinkDirectory("build-tools", _this.platformData.projectRoot, frameworkDir).wait();
                _this.symlinkDirectory("libs", _this.platformData.projectRoot, frameworkDir).wait();
            }
            else {
                _this.copy(_this.platformData.projectRoot, frameworkDir, "build-tools libs", "-R");
            }
            _this.copy(_this.platformData.projectRoot, frameworkDir, "src", "-R");
            _this.copy(_this.platformData.projectRoot, frameworkDir, "build.gradle settings.gradle gradle.properties", "-f");
            if (_this.useGradleWrapper(frameworkDir)) {
                _this.copy(_this.platformData.projectRoot, frameworkDir, "gradle", "-R");
                _this.copy(_this.platformData.projectRoot, frameworkDir, "gradlew gradlew.bat", "-f");
            }
            _this.cleanResValues(targetSdkVersion, frameworkVersion).wait();
        }).future()();
    };
    AndroidProjectService.prototype.useGradleWrapper = function (frameworkDir) {
        var gradlew = path.join(frameworkDir, "gradlew");
        return this.$fs.exists(gradlew).wait();
    };
    AndroidProjectService.prototype.cleanResValues = function (targetSdkVersion, frameworkVersion) {
        var _this = this;
        return (function () {
            var resDestinationDir = _this.getAppResourcesDestinationDirectoryPath(frameworkVersion).wait();
            var directoriesInResFolder = _this.$fs.readDirectory(resDestinationDir).wait();
            var directoriesToClean = directoriesInResFolder
                .map(function (dir) {
                return {
                    dirName: dir,
                    sdkNum: parseInt(dir.substr(AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX.length))
                };
            })
                .filter(function (dir) { return dir.dirName.match(AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX)
                && dir.sdkNum
                && (!targetSdkVersion || (targetSdkVersion < dir.sdkNum)); })
                .map(function (dir) { return path.join(resDestinationDir, dir.dirName); });
            _this.$logger.trace("Directories to clean:");
            _this.$logger.trace(directoriesToClean);
            Future.wait(_.map(directoriesToClean, function (dir) { return _this.$fs.deleteDirectory(dir); }));
        }).future()();
    };
    AndroidProjectService.prototype.interpolateData = function () {
        var _this = this;
        return (function () {
            _this.interpolateConfigurationFile().wait();
            var stringsFilePath = path.join(_this.getAppResourcesDestinationDirectoryPath().wait(), 'values', 'strings.xml');
            shell.sed('-i', /__NAME__/, _this.$projectData.projectName, stringsFilePath);
            shell.sed('-i', /__TITLE_ACTIVITY__/, _this.$projectData.projectName, stringsFilePath);
            var gradleSettingsFilePath = path.join(_this.platformData.projectRoot, "settings.gradle");
            shell.sed('-i', /__PROJECT_NAME__/, _this.getProjectNameFromId(), gradleSettingsFilePath);
        }).future()();
    };
    AndroidProjectService.prototype.interpolateConfigurationFile = function () {
        var _this = this;
        return (function () {
            var manifestPath = _this.platformData.configurationFilePath;
            shell.sed('-i', /__PACKAGE__/, _this.$projectData.projectId, manifestPath);
            shell.sed('-i', /__APILEVEL__/, _this.$options.sdk || _this.$androidToolsInfo.getToolsInfo().wait().compileSdkVersion.toString(), manifestPath);
        }).future()();
    };
    AndroidProjectService.prototype.getProjectNameFromId = function () {
        var id;
        Eif (this.$projectData && this.$projectData.projectId) {
            id = this.$projectData.projectId.split(".")[2];
        }
        return id;
    };
    AndroidProjectService.prototype.afterCreateProject = function (projectRoot) {
        return Future.fromResult();
    };
    AndroidProjectService.prototype.canUpdatePlatform = function (currentVersion, newVersion) {
        return Future.fromResult(true);
    };
    AndroidProjectService.prototype.updatePlatform = function (currentVersion, newVersion, canUpdate, addPlatform, removePlatforms) {
        var _this = this;
        return (function () {
            if (semver.eq(newVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE)) {
                var platformLowercase = _this.platformData.normalizedPlatformName.toLowerCase();
                removePlatforms([platformLowercase.split("@")[0]]).wait();
                addPlatform(platformLowercase).wait();
                return false;
            }
            return true;
        }).future()();
    };
    AndroidProjectService.prototype.buildProject = function (projectRoot, buildConfig) {
        var _this = this;
        return (function () {
            if (_this.canUseGradle().wait()) {
                _this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true }).wait();
                var androidToolsInfo = _this.$androidToolsInfo.getToolsInfo().wait();
                var compileSdk = androidToolsInfo.compileSdkVersion;
                var targetSdk = _this.getTargetFromAndroidManifest().wait() || compileSdk;
                var buildToolsVersion = androidToolsInfo.buildToolsVersion;
                var appCompatVersion = androidToolsInfo.supportRepositoryVersion;
                var buildOptions = ["buildapk",
                    ("-PcompileSdk=android-" + compileSdk),
                    ("-PtargetSdk=" + targetSdk),
                    ("-PbuildToolsVersion=" + buildToolsVersion),
                    ("-PsupportVersion=" + appCompatVersion),
                ];
                if (_this.$options.release) {
                    buildOptions.push("-Prelease");
                    buildOptions.push("-PksPath=" + path.resolve(_this.$options.keyStorePath));
                    buildOptions.push("-Palias=" + _this.$options.keyStoreAlias);
                    buildOptions.push("-Ppassword=" + _this.$options.keyStoreAliasPassword);
                    buildOptions.push("-PksPassword=" + _this.$options.keyStorePassword);
                }
                if (buildConfig && buildConfig.runSbGenerator) {
                    buildOptions.push("-PrunSBGenerator");
                }
                var gradleBin = _this.useGradleWrapper(projectRoot) ? path.join(projectRoot, "gradlew") : "gradle";
                _this.spawn(gradleBin, buildOptions, { stdio: "inherit", cwd: _this.platformData.projectRoot }).wait();
            }
            else {
                _this.checkAnt().wait();
                var args = _this.getAntArgs(_this.$options.release ? "release" : "debug", projectRoot);
                _this.spawn('ant', args).wait();
                _this.platformData.deviceBuildOutputPath = path.join(_this.platformData.projectRoot, "bin");
            }
        }).future()();
    };
    AndroidProjectService.prototype.isPlatformPrepared = function (projectRoot) {
        return this.$fs.exists(path.join(this.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME));
    };
    AndroidProjectService.prototype.addLibrary = function (libraryPath) {
        var _this = this;
        return (function () {
            if (_this.$fs.exists(path.join(libraryPath, "project.properties")).wait()) {
                _this.$errors.failWithoutHelp("Unable to add android library. You can use `library add` command only with path to folder containing one or more .jar files.");
            }
            var name = path.basename(libraryPath);
            var targetLibPath = _this.getLibraryPath(name);
            var targetPath = path.dirname(targetLibPath);
            _this.$fs.ensureDirectoryExists(targetPath).wait();
            shell.cp("-f", path.join(libraryPath, "*.jar"), targetPath);
        }).future()();
    };
    AndroidProjectService.prototype.getFrameworkFilesExtensions = function () {
        return [".jar", ".dat"];
    };
    AndroidProjectService.prototype.prepareProject = function () {
        return Future.fromResult();
    };
    AndroidProjectService.prototype.prepareAppResources = function (appResourcesDirectoryPath) {
        var _this = this;
        return (function () {
            var resourcesDirPath = path.join(appResourcesDirectoryPath, _this.platformData.normalizedPlatformName);
            var valuesDirRegExp = /^values/;
            var resourcesDirs = _this.$fs.readDirectory(resourcesDirPath).wait().filter(function (resDir) { return !resDir.match(valuesDirRegExp); });
            _.each(resourcesDirs, function (resourceDir) {
                _this.$fs.deleteDirectory(path.join(_this.getAppResourcesDestinationDirectoryPath().wait(), resourceDir)).wait();
            });
        }).future()();
    };
    AndroidProjectService.prototype.preparePluginNativeCode = function (pluginData) {
        var _this = this;
        return (function () {
            var pluginPlatformsFolderPath = _this.getPluginPlatformsFolderPath(pluginData, AndroidProjectService.ANDROID_PLATFORM_NAME);
            _this.processResourcesFromPlugin(pluginData.name, pluginPlatformsFolderPath).wait();
        }).future()();
    };
    AndroidProjectService.prototype.processConfigurationFilesFromAppResources = function () {
        var _this = this;
        return (function () {
            var manifestFilePath = path.join(_this.$projectData.appResourcesDirectoryPath, _this.platformData.normalizedPlatformName, _this.platformData.configurationFileName);
            if (_this.$fs.exists(manifestFilePath).wait()) {
                _this.processResourcesFromPlugin("NativescriptAppResources", path.dirname(manifestFilePath)).wait();
            }
        }).future()();
    };
    AndroidProjectService.prototype.processResourcesFromPlugin = function (pluginName, pluginPlatformsFolderPath) {
        var _this = this;
        return (function () {
            var configurationsDirectoryPath = path.join(_this.platformData.projectRoot, "configurations");
            _this.$fs.ensureDirectoryExists(configurationsDirectoryPath).wait();
            var pluginConfigurationDirectoryPath = path.join(configurationsDirectoryPath, pluginName);
            if (_this.$fs.exists(pluginPlatformsFolderPath).wait()) {
                _this.$fs.ensureDirectoryExists(pluginConfigurationDirectoryPath).wait();
                var resourcesDestinationDirectoryPath = path.join(_this.platformData.projectRoot, "src", pluginName);
                _this.$fs.ensureDirectoryExists(resourcesDestinationDirectoryPath).wait();
                shell.cp("-Rf", path.join(pluginPlatformsFolderPath, "*"), resourcesDestinationDirectoryPath);
            }
            var includeGradleFilePath = path.join(pluginPlatformsFolderPath, "include.gradle");
            if (_this.$fs.exists(includeGradleFilePath).wait()) {
                shell.cp("-f", includeGradleFilePath, pluginConfigurationDirectoryPath);
            }
        }).future()();
    };
    AndroidProjectService.prototype.removePluginNativeCode = function (pluginData) {
        var _this = this;
        return (function () {
            try {
                _this.$fs.deleteDirectory(path.join(_this.platformData.projectRoot, "configurations", pluginData.name)).wait();
                _this.$fs.deleteDirectory(path.join(_this.platformData.projectRoot, "src", pluginData.name)).wait();
            }
            catch (e) {
                if (e.code === "ENOENT") {
                    _this.$logger.debug("No native code jars found: " + e.message);
                }
                else {
                    throw e;
                }
            }
        }).future()();
    };
    AndroidProjectService.prototype.afterPrepareAllPlugins = function () {
        return Future.fromResult();
    };
    AndroidProjectService.prototype.deploy = function (deviceIdentifier) {
        var _this = this;
        return (function () {
            var adb = _this.$injector.resolve(androidDebugBridgePath.AndroidDebugBridge, { identifier: deviceIdentifier });
            var deviceRootPath = "/data/local/tmp/" + _this.$projectData.projectId;
            adb.executeShellCommand(["rm", "-rf", _this.$mobileHelper.buildDevicePath(deviceRootPath, "fullsync"),
                _this.$mobileHelper.buildDevicePath(deviceRootPath, "sync"),
                _this.$mobileHelper.buildDevicePath(deviceRootPath, "removedsync")]).wait();
        }).future()();
    };
    AndroidProjectService.prototype.canUseGradle = function (frameworkVersion) {
        var _this = this;
        return (function () {
            if (!_this._canUseGradle) {
                if (!frameworkVersion) {
                    _this.$projectDataService.initialize(_this.$projectData.projectDir);
                    frameworkVersion = _this.$projectDataService.getValue(_this.platformData.frameworkPackageName).wait().version;
                }
                _this._canUseGradle = semver.gte(frameworkVersion, AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE);
            }
            return _this._canUseGradle;
        }).future()();
    };
    AndroidProjectService.prototype.getLibraryPath = function (libraryName) {
        return path.join(this.$projectData.projectDir, "lib", this.platformData.normalizedPlatformName, libraryName);
    };
    AndroidProjectService.prototype.copy = function (projectRoot, frameworkDir, files, cpArg) {
        var paths = files.split(' ').map(function (p) { return path.join(frameworkDir, p); });
        shell.cp(cpArg, paths, projectRoot);
    };
    AndroidProjectService.prototype.spawn = function (command, args, opts) {
        if (this.$hostInfo.isWindows) {
            args.unshift('/s', '/c', command);
            command = process.env.COMSPEC || 'cmd.exe';
        }
        return this.$childProcess.spawnFromEvent(command, args, "close", opts || { stdio: "inherit" });
    };
    AndroidProjectService.prototype.getAntArgs = function (configuration, projectRoot) {
        var args = [configuration, "-f", path.join(projectRoot, "build.xml")];
        if (configuration === "release") {
            if (this.$options.keyStorePath) {
                args = args.concat(["-Dkey.store", path.resolve(this.$options.keyStorePath)]);
            }
            if (this.$options.keyStorePassword) {
                args = args.concat(["-Dkey.store.password", this.$options.keyStorePassword]);
            }
            if (this.$options.keyStoreAlias) {
                args = args.concat(["-Dkey.alias", this.$options.keyStoreAlias]);
            }
            if (this.$options.keyStoreAliasPassword) {
                args = args.concat(["-Dkey.alias.password", this.$options.keyStoreAliasPassword]);
            }
        }
        args = args.concat(["-Dns.resources", path.join(__dirname, "../../resources/tools")]);
        return args;
    };
    AndroidProjectService.prototype.validatePackageName = function (packageName) {
        if (!/^[a-zA-Z]+(\.[a-zA-Z0-9][a-zA-Z0-9_]*)+$/.test(packageName)) {
            this.$errors.fail("Package name must look like: com.company.Name");
        }
        if (/\b[Cc]lass\b/.test(packageName)) {
            this.$errors.fail("class is a reserved word");
        }
    };
    AndroidProjectService.prototype.validateProjectName = function (projectName) {
        if (projectName === '') {
            this.$errors.fail("Project name cannot be empty");
        }
        if (/^[0-9]/.test(projectName)) {
            this.$errors.fail("Project name must not begin with a number");
        }
    };
    AndroidProjectService.prototype.getTargetFromAndroidManifest = function () {
        var _this = this;
        return (function () {
            var versionInManifest;
            if (_this.$fs.exists(_this.platformData.configurationFilePath).wait()) {
                var targetFromAndroidManifest = _this.$fs.readText(_this.platformData.configurationFilePath).wait();
                if (targetFromAndroidManifest) {
                    var match = targetFromAndroidManifest.match(/.*?android:targetSdkVersion=\"(.*?)\"/);
                    if (match && match[1]) {
                        versionInManifest = match[1];
                    }
                }
            }
            return versionInManifest;
        }).future()();
    };
    AndroidProjectService.prototype.checkAnt = function () {
        var _this = this;
        return (function () {
            try {
                _this.$childProcess.exec("ant -version").wait();
            }
            catch (error) {
                _this.$errors.fail("Error executing commands 'ant', make sure you have ant installed and added to your PATH.");
            }
        }).future()();
    };
    AndroidProjectService.prototype.symlinkDirectory = function (directoryName, projectRoot, frameworkDir) {
        var _this = this;
        return (function () {
            _this.$fs.createDirectory(path.join(projectRoot, directoryName)).wait();
            var directoryContent = _this.$fs.readDirectory(path.join(frameworkDir, directoryName)).wait();
            _.each(directoryContent, function (file) {
                var sourceFilePath = path.join(frameworkDir, directoryName, file);
                var destinationFilePath = path.join(projectRoot, directoryName, file);
                if (_this.$fs.getFsStats(sourceFilePath).wait().isFile()) {
                    _this.$fs.symlink(sourceFilePath, destinationFilePath).wait();
                }
                else {
                    _this.$fs.symlink(sourceFilePath, destinationFilePath, "dir").wait();
                }
            });
        }).future()();
    };
    AndroidProjectService.VALUES_DIRNAME = "values";
    AndroidProjectService.VALUES_VERSION_DIRNAME_PREFIX = AndroidProjectService.VALUES_DIRNAME + "-v";
    AndroidProjectService.ANDROID_PLATFORM_NAME = "android";
    AndroidProjectService.MIN_RUNTIME_VERSION_WITH_GRADLE = "1.3.0";
    return AndroidProjectService;
})(projectServiceBaseLib.PlatformProjectServiceBase);
exports.AndroidProjectService = AndroidProjectService;
$injector.register("androidProjectService", AndroidProjectService);