<commit_before>///<reference path="../.d.ts"/>
"use strict";

import path = require("path");
import unzip = require("unzip");
import options = require("../options");
import util = require("../helpers");
import Future = require("fibers/future");
var _ = <UnderscoreStatic> require("underscore");

export class SimulateCommand implements ICommand {
	private PLUGINS_PACKAGE_IDENTIFIER: string = "Plugins";
	private PLUGINS_API_CONTRACT: string = "/api/cordova/plugins/package";
	private MACOS_COMING_SOON = [
		"For Mac OS, the device simulator will be shipped with Telerik AppBuilder 2.1.",
		"For more information about the latest available version,",
		"go to https://www.npmjs.org/package/appbuilder or",
		"http://docs.telerik.com/platform/appbuilder/release-notes"].join("\n");

	private projectData;
	private cacheDir: string;
	private simulatorPath: string;
	private pluginsPath: string;
	private serverVersion: string;

	constructor(private $logger: ILogger,
		private $httpClient: Server.IHttpClient,
		private $fs: IFileSystem,
		private $config: IConfiguration,
		private $serverConfiguration: IServerConfiguration,
		private $server: Server.IServer,
		private $project: Project.IProject,
		private $loginManager: ILoginManager,
		private $platformServices: ISimulatorPlatformServices) {
		this.projectData = $project.projectData;
	}

	public execute(args: string[]): IFuture<void> {
		return (() => {
			// we are shipping our Mac OS simulator very soon
			// for now, just stop executing on Mac OS
			if (process.platform !== "win32") {
				this.$logger.fatal(this.MACOS_COMING_SOON);
				return;
			}

			this.$project.ensureProject();

			this.cacheDir = path.join(options["profile-dir"], "Cache");
			this.serverVersion = this.$serverConfiguration.assemblyVersion.wait();
			this.$logger.debug("Server version: %s", this.serverVersion);

			this.$loginManager.ensureLoggedIn().wait();

			this.prepareSimulator().wait();
			this.prepareCordovaPlugins().wait();

			this.runSimulator();
		}).future<void>()();
	}

	private prepareSimulator(): IFuture<void> {
		return ((): void => {
			this.simulatorPath = path.join(this.cacheDir, this.$platformServices.getPackageName());
			this.$fs.createDirectory(this.simulatorPath).wait();

			var serverVersionFile = path.join(this.cacheDir, "server-version.json");

			this.$logger.trace("Simulator path: %s", this.simulatorPath);

			var cachedVersion  = "0.0.0.0";

			if (this.$fs.exists(serverVersionFile).wait()) {
				cachedVersion = this.$fs.readJson(serverVersionFile).wait().version;
				this.$logger.debug("Cached version is: %s", cachedVersion);
			}

			if (this.versionCompare(cachedVersion, this.serverVersion) < 0) {
				var downloadUri = this.getSimulatorDownloadUri().wait();
				this.$logger.info("Updating simulator package...");
				this.$logger.debug("Downloading simulator from %s", downloadUri);

				var extractor = unzip.Extract({path: this.simulatorPath});
				var request = this.$httpClient.httpRequest({
					url: downloadUri,
					pipeTo: extractor,
					headers: { Accept: "application/octet-stream, application/x-silverlight-app" }
				});

				this.$fs.futureFromEvent(extractor, "finish").wait();
				request.wait(); // silence the assert that all fibers are waited

				this.$platformServices.preparePackage(this.simulatorPath);

				this.$fs.writeJson(serverVersionFile, { version : this.serverVersion }).wait();

				this.$logger.info("Finished updating simulator package.");
			}
		}).future<void>()();
	}

	private getSimulatorDownloadUri(): IFuture<string> {
		return (() => {
			var serverUri = this.$config.AB_SERVER_PROTO + "://" + this.$config.AB_SERVER;
			var downloadUri: string;

			if (this.$config.USE_CDN_FOR_SIMULATOR_DOWNLOAD) {
				var servicesExtensionsUri =  serverUri + "/services/extensions";

				this.$logger.trace("Getting extensions from %s", servicesExtensionsUri);

				var extensions = JSON.parse(this.$httpClient.httpRequest(servicesExtensionsUri).wait().body);
				downloadUri = (<any>_.findWhere(extensions["$values"],
						{ Identifier : this.$platformServices.getPackageName() })).DownloadUri;
			} else {
				downloadUri = serverUri + "/ClientBin/" + this.$platformServices.getPackageName() + '.xap';
			}

			return downloadUri;
		}).future<string>()();
	}<commit_msg>Fixed random truncation of unzipped files<commit_after>///<reference path="../.d.ts"/>
"use strict";

import path = require("path");
import unzip = require("unzip");
import options = require("../options");
import util = require("../helpers");
import Future = require("fibers/future");
var _ = <UnderscoreStatic> require("underscore");

export class SimulateCommand implements ICommand {
	private PLUGINS_PACKAGE_IDENTIFIER: string = "Plugins";
	private PLUGINS_API_CONTRACT: string = "/api/cordova/plugins/package";
	private MACOS_COMING_SOON = [
		"For Mac OS, the device simulator will be shipped with Telerik AppBuilder 2.1.",
		"For more information about the latest available version,",
		"go to https://www.npmjs.org/package/appbuilder or",
		"http://docs.telerik.com/platform/appbuilder/release-notes"].join("\n");

	private projectData;
	private cacheDir: string;
	private simulatorPath: string;
	private pluginsPath: string;
	private serverVersion: string;

	constructor(private $logger: ILogger,
		private $httpClient: Server.IHttpClient,
		private $fs: IFileSystem,
		private $config: IConfiguration,
		private $serverConfiguration: IServerConfiguration,
		private $server: Server.IServer,
		private $project: Project.IProject,
		private $loginManager: ILoginManager,
		private $platformServices: ISimulatorPlatformServices) {
		this.projectData = $project.projectData;
	}

	public execute(args: string[]): IFuture<void> {
		return (() => {
			// we are shipping our Mac OS simulator very soon
			// for now, just stop executing on Mac OS
			if (process.platform !== "win32") {
				this.$logger.fatal(this.MACOS_COMING_SOON);
				return;
			}

			this.$project.ensureProject();

			this.cacheDir = path.join(options["profile-dir"], "Cache");
			this.serverVersion = this.$serverConfiguration.assemblyVersion.wait();
			this.$logger.debug("Server version: %s", this.serverVersion);

			this.$loginManager.ensureLoggedIn().wait();

			this.prepareSimulator().wait();
			this.prepareCordovaPlugins().wait();

			this.runSimulator();
		}).future<void>()();
	}

	private prepareSimulator(): IFuture<void> {
		return ((): void => {
			this.simulatorPath = path.join(this.cacheDir, this.$platformServices.getPackageName());
			this.$fs.createDirectory(this.simulatorPath).wait();

			var serverVersionFile = path.join(this.cacheDir, "server-version.json");

			this.$logger.trace("Simulator path: %s", this.simulatorPath);

			var cachedVersion  = "0.0.0.0";

			if (this.$fs.exists(serverVersionFile).wait()) {
				cachedVersion = this.$fs.readJson(serverVersionFile).wait().version;
				this.$logger.debug("Cached version is: %s", cachedVersion);
			}

			if (this.versionCompare(cachedVersion, this.serverVersion) < 0) {
				var downloadUri = this.getSimulatorDownloadUri().wait();
				this.$logger.info("Updating simulator package...");
				this.$logger.debug("Downloading simulator from %s", downloadUri);

				var extractor = unzip.Extract({path: this.simulatorPath});
				var request = this.$httpClient.httpRequest({
					url: downloadUri,
					pipeTo: extractor,
					headers: { Accept: "application/octet-stream, application/x-silverlight-app" }
				});

				this.$fs.futureFromEvent(extractor, "close").wait();
				request.wait();

				this.$platformServices.preparePackage(this.simulatorPath);

				this.$fs.writeJson(serverVersionFile, { version : this.serverVersion }).wait();

				this.$logger.info("Finished updating simulator package.");
			}
		}).future<void>()();
	}

	private getSimulatorDownloadUri(): IFuture<string> {
		return (() => {
			var serverUri = this.$config.AB_SERVER_PROTO + "://" + this.$config.AB_SERVER;
			var downloadUri: string;

			if (this.$config.USE_CDN_FOR_SIMULATOR_DOWNLOAD) {
				var servicesExtensionsUri =  serverUri + "/services/extensions";

				this.$logger.trace("Getting extensions from %s", servicesExtensionsUri);

				var extensions = JSON.parse(this.$httpClient.httpRequest(servicesExtensionsUri).wait().body);
				downloadUri = (<any>_.findWhere(extensions["$values"],
						{ Identifier : this.$platformServices.getPackageName() })).DownloadUri;
			} else {
				downloadUri = serverUri + "/ClientBin/" + this.$platformServices.getPackageName() + '.xap';
			}

			return downloadUri;
		}).future<string>()();
	}
{'fraction_non_alphanumeric': 0.14889148191365228, 'fraction_numerical': 0.0021003500583430573, 'mean_word_length': 4.6056245912361025, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2065756', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2513, 'n_words': 531}