<commit_before>'use strict';

const Blueprint = require('../models/blueprint');
const Task = require('../models/task');
const RSVP = require('rsvp');
const isGitRepo = require('is-git-url');
const temp = require('temp');
const path = require('path');
const merge = require('ember-cli-lodash-subset').merge;
const execa = require('execa');

const logger = require('heimdalljs-logger')('ember-cli:tasks:install-blueprint');

// Automatically track and cleanup temp files at exit
temp.track();

let mkdirTemp = RSVP.denodeify(temp.mkdir);

class InstallBlueprintTask extends Task {
  run(options) {
    let cwd = process.cwd();
    let name = options.rawName;
    let blueprintOption = options.blueprint;
    // If we're in a dry run, pretend we changed directories.
    // Pretending we cd'd avoids prompts in the actual current directory.
    let fakeCwd = path.join(cwd, name);
    let target = options.dryRun ? fakeCwd : cwd;

    let installOptions = {
      target,
      entity: { name },
      ui: this.ui,
      analytics: this.analytics,
      project: this.project,
      dryRun: options.dryRun,
      targetFiles: options.targetFiles,
      rawArgs: options.rawArgs,
    };

    installOptions = merge(installOptions, options || {});

    return this._resolveBlueprint(blueprintOption).then(blueprint => {
      logger.info(`Installing blueprint into "${target}" ...`);
      return blueprint.install(installOptions);
    });
  }

  _resolveBlueprint(name) {
    name = name || 'app';
    logger.info(`Resolving blueprint "${name}" ...`);

    if (!isGitRepo(name)) {
      logger.info(`Looking up blueprint "${name}" ...`);
      return this._lookupBlueprint(name);
    }

    return this._createTempFolder().then(pathName => {
      logger.info(`Cloning blueprint from git (${name}) into "${pathName}" ...`);
      return this._gitClone(name, pathName).then(() => {

        logger.info(`Running "npm install" in "${pathName}" ...`);
        return this._npmInstall(pathName);

      }).then(() => {
        logger.info(`Loading blueprint from "${pathName}" ...`);
        return Blueprint.load(pathName);
      });
    });
  }

  _lookupBlueprint(name) {
    return RSVP.resolve().then(() => Blueprint.lookup(name, {
      paths: this.project.blueprintLookupPaths(),
    }));
  }

  _createTempFolder() {
    return mkdirTemp('ember-cli');
  }

  _gitClone(source, destination) {
    return execa('git', ['clone', source, destination]);
  }

  _npmInstall(cwd) {
    return execa('npm', ['install'], { cwd });
  }
}

module.exports = InstallBlueprintTask;<commit_msg>tasks/install-blueprint: Extract _loadBlueprintFromPath() method<commit_after>'use strict';

const Blueprint = require('../models/blueprint');
const Task = require('../models/task');
const RSVP = require('rsvp');
const isGitRepo = require('is-git-url');
const temp = require('temp');
const path = require('path');
const merge = require('ember-cli-lodash-subset').merge;
const execa = require('execa');

const logger = require('heimdalljs-logger')('ember-cli:tasks:install-blueprint');

// Automatically track and cleanup temp files at exit
temp.track();

let mkdirTemp = RSVP.denodeify(temp.mkdir);

class InstallBlueprintTask extends Task {
  run(options) {
    let cwd = process.cwd();
    let name = options.rawName;
    let blueprintOption = options.blueprint;
    // If we're in a dry run, pretend we changed directories.
    // Pretending we cd'd avoids prompts in the actual current directory.
    let fakeCwd = path.join(cwd, name);
    let target = options.dryRun ? fakeCwd : cwd;

    let installOptions = {
      target,
      entity: { name },
      ui: this.ui,
      analytics: this.analytics,
      project: this.project,
      dryRun: options.dryRun,
      targetFiles: options.targetFiles,
      rawArgs: options.rawArgs,
    };

    installOptions = merge(installOptions, options || {});

    return this._resolveBlueprint(blueprintOption).then(blueprint => {
      logger.info(`Installing blueprint into "${target}" ...`);
      return blueprint.install(installOptions);
    });
  }

  _resolveBlueprint(name) {
    name = name || 'app';
    logger.info(`Resolving blueprint "${name}" ...`);

    if (!isGitRepo(name)) {
      logger.info(`Looking up blueprint "${name}" ...`);
      return this._lookupBlueprint(name);
    }

    return this._createTempFolder().then(pathName => {
      logger.info(`Cloning blueprint from git (${name}) into "${pathName}" ...`);
      return this._gitClone(name, pathName).then(() => {

        logger.info(`Running "npm install" in "${pathName}" ...`);
        return this._npmInstall(pathName);

      }).then(() => {
        logger.info(`Loading blueprint from "${pathName}" ...`);
        return this._loadBlueprintFromPath(pathName);
      });
    });
  }

  _lookupBlueprint(name) {
    return RSVP.resolve().then(() => Blueprint.lookup(name, {
      paths: this.project.blueprintLookupPaths(),
    }));
  }

  _createTempFolder() {
    return mkdirTemp('ember-cli');
  }

  _gitClone(source, destination) {
    return execa('git', ['clone', source, destination]);
  }

  _npmInstall(cwd) {
    return execa('npm', ['install'], { cwd });
  }

  _loadBlueprintFromPath(path) {
    return RSVP.resolve().then(() => Blueprint.load(path));
  }
}

module.exports = InstallBlueprintTask;
{'fraction_non_alphanumeric': 0.1588345864661654, 'fraction_numerical': 0.0, 'mean_word_length': 3.393889347646573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988964', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1598, 'n_words': 358}