<commit_before>const fs = require('fs-extra');
const efs = require('../utils/fs');
const path = require('path');
const compose = require('lodash.flowright');
const utils = require('../utils');

const SETTINGS_PATCH_PATTERN = `include ':app'`;
const BUILD_PATCH_PATTERN = `dependencies {`;
const MAIN_ACTIVITY_IMPORT_PATTERN = `import android.app.Activity;`;
const MAIN_ACTIVITY_PACKAGE_PATTERN = `.addPackage(new MainReactPackage())`;

module.exports = function registerNativeAndroidModule(name, dependencyConfig, projectConfig) {
  const BUILD_PATCH = `    compile project(':${name}')`;
  const SETTINGS_PATCH = `include ':${name}'
project(':${name}').projectDir = new File(rootProject.projectDir, \
'../node_modules/${name}/android')`;

  const getImportPatch = (importPath) => `import ${importPath}`;

  const getPackagePatch = (instance) =>
    `                .addPackage(${instance})`;

  /**
   * Replace SETTINGS_PATCH_PATTERN by patch in the passed content
   * @param  {String} content Content of the Settings.gradle file
   * @return {String}         Patched content of Settings.gradle
   */
  const patchProjectSettings = (content) =>
    utils.replace(content, SETTINGS_PATCH_PATTERN, SETTINGS_PATCH);

  /**
   * Replace BUILD_PATCH_PATTERN by patch in the passed content
   * @param  {String} content Content of the Build.gradle file
   * @return {String}         Patched content of Build.gradle
   */
  const patchProjectBuild = (content) =>
    utils.replace(content, BUILD_PATCH_PATTERN, BUILD_PATCH);

  /**
   * Make a MainActivity.java program patcher
   * @param  {String}   importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
   * @param  {String}   instance   Code to instance a package, e.g. new VectorIconsPackage();
   * @return {Function}            Patcher function
   */
  const makeMainActivityPatcher = (importPath, instance) =>
    (content) =>
      utils.replace(content, MAIN_ACTIVITY_IMPORT_PATTERN, getImportPatch(importPath)) &&
      utils.replace(content, MAIN_ACTIVITY_PACKAGE_PATTERN, getPackagePatch(instance));

  /**
   * @param  {String}   importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
   * @param  {String}   instance   Code to instance a package, e.g. new VectorIconsPackage();
   * @return {Function}            Patcher function
   */
  const applyMainActivityPatch = (importPath, instance) =>
    compose(
      efs.writeFile.bind(null, projectConfig.mainActivity),
      makeMainActivityPatcher(importPath, instance),
      efs.loadFile.bind(null, projectConfig.mainActivity)
    );

  /**
   * Copy assets from dependencyConfig.folder to projectConfig.assetsFolder
   * @param  {Array} assets Array of assets specified in config
   * @return {Function}
   */
  const copyAssets = (assets) => {
    return () => (assets || []).forEach((asset) =>
      fs.copySync(
        path.join(dependencyConfig._folder, asset),
        path.join(projectConfig.assetsFolder, asset)
      )
    );
  };

  const applySettingsGradlePatch = compose(
    efs.writeFile.bind(null, projectConfig.settings),
    patchProjectSettings,
    efs.loadFile.bind(null, projectConfig.settings)
  );

  const applyBuildGradlePatch = compose(
    efs.writeFile.bind(null, projectConfig.project),
    patchProjectBuild,
    efs.loadFile.bind(null, projectConfig.project)
  );

  compose(
    copyAssets(dependencyConfig.assets),
    applySettingsGradlePatch,
    applyBuildGradlePatch,
    applyMainActivityPatch(
      dependencyConfig.packageImportPath,<commit_msg>Move pure functions to the root scope, rename local variables to camelCase because they are not constants and make it confusing to decide whether they are coming from local scope or from the root one - personal, feel free to rollback<commit_after>const fs = require('fs-extra');
const efs = require('../utils/fs');
const path = require('path');
const compose = require('lodash.flowright');
const utils = require('../utils');

const SETTINGS_PATCH_PATTERN = `include ':app'`;
const BUILD_PATCH_PATTERN = `dependencies {`;
const MAIN_ACTIVITY_IMPORT_PATTERN = `import android.app.Activity;`;
const MAIN_ACTIVITY_PACKAGE_PATTERN = `.addPackage(new MainReactPackage())`;

/**
 * Given package name, returns Java import declaration to be
 * inserted in the file
 */
const getImportPatch = (importPath) => `import ${importPath}`;

/**
 * Given package initialisation, returns Java .addPackage function call
 * to be attached in the MainActivity.java
 */
const getPackagePatch = (instance) => `                .addPackage(${instance})`;

module.exports = function registerNativeAndroidModule(name, dependencyConfig, projectConfig) {
  const buildPatch = `    compile project(':${name}')`;
  const settingsPatch = `include ':${name}'
project(':${name}').projectDir = new File(rootProject.projectDir, \
'../node_modules/${name}/android')`;

  // Replace SETTINGS_PATCH_PATTERN by patch in the passed content
  const patchProjectSettings = (content) =>
    utils.replace(content, SETTINGS_PATCH_PATTERN, settingsPatch);

  // Replace BUILD_PATCH_PATTERN by patch in the passed content
  const patchProjectBuild = (content) =>
    utils.replace(content, BUILD_PATCH_PATTERN, buildPatch);

  /**
   * Make a MainActivity.java program patcher
   * @param  {String}   importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
   * @param  {String}   instance   Code to instance a package, e.g. new VectorIconsPackage();
   * @return {Function}            Patcher function
   */
  const makeMainActivityPatcher = (importPath, instance) =>
    (content) =>
      utils.replace(content, MAIN_ACTIVITY_IMPORT_PATTERN, getImportPatch(importPath)) &&
      utils.replace(content, MAIN_ACTIVITY_PACKAGE_PATTERN, getPackagePatch(instance));

  /**
   * @param  {String}   importPath Import path, e.g. com.oblador.vectoricons.VectorIconsPackage;
   * @param  {String}   instance   Code to instance a package, e.g. new VectorIconsPackage();
   * @return {Function}            Patcher function
   */
  const applyMainActivityPatch = (importPath, instance) =>
    compose(
      efs.writeFile.bind(null, projectConfig.mainActivity),
      makeMainActivityPatcher(importPath, instance),
      efs.loadFile.bind(null, projectConfig.mainActivity)
    );

  // Copy assets from dependencyConfig.folder to projectConfig.assetsFolder
  const copyAssets = (assets) => {
    return () => (assets || []).forEach((asset) =>
      fs.copySync(
        path.join(dependencyConfig._folder, asset),
        path.join(projectConfig.assetsFolder, asset)
      )
    );
  };

  const applySettingsGradlePatch = compose(
    efs.writeFile.bind(null, projectConfig.settings),
    patchProjectSettings,
    efs.loadFile.bind(null, projectConfig.settings)
  );

  const applyBuildGradlePatch = compose(
    efs.writeFile.bind(null, projectConfig.project),
    patchProjectBuild,
    efs.loadFile.bind(null, projectConfig.project)
  );

  compose(
    copyAssets(dependencyConfig.assets),
    applySettingsGradlePatch,
    applyBuildGradlePatch,
    applyMainActivityPatch(
      dependencyConfig.packageImportPath,
{'fraction_non_alphanumeric': 0.11530898876404494, 'fraction_numerical': 0.0, 'mean_word_length': 4.0900643316654754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '347962', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2079, 'n_words': 492}