<commit_before>const path = require('path');
const xml = require('xmldoc');
const fs = require('fs');
const efs = require('../utils/fs');
const glob = require('glob');

// Default android source directory to look for files
const BASE_DIR = './android';
const GLOB_PATTERN = '**/*.java';

const getSourceDirectory = (folder) => {
  var src = path.join(folder, BASE_DIR);

  if (!fs.existsSync(src)) {
    return null;
  }

  if (fs.existsSync(path.join(src, 'app'))) {
    src = path.join(src, 'app');
  }

  return src;
};

const getPackageName = (src) => {
  const manifest = new xml.XmlDocument(
    efs.loadFile(path.join(src, 'src/main/AndroidManifest.xml'))
  );

  return manifest.attr.package;
};

const getPackageFolder = (packageName) => packageName.replace('.', path.sep);

const getPackageInstance = (src) => {
  const files = glob.sync(GLOB_PATTERN, {
    cwd: src,
  });

  const packages = files
    .map(filePath => efs.loadFile(path.join(src, filePath)))
    .map(file => file.match(/class (.*) implements ReactPackage/))
    .filter(match => match);

  // No package exported, ignore
  if (packages.length === 0) {
    return null;
  }

  return packages[0][1];
};

exports.projectConfig = function projectConfigAndroid(folder, userConfig) {
  const src = userConfig.sourceDir || getSourceDirectory(folder);

  if (!src) {
    return null;
  }

  const packageName = userConfig.packageName || getPackageName(src);
  const packageFolder = userConfig.packageFolder || getPackageFolder(packageName);

  return {
    src: src,
    folder: folder,
    project: path.join(src, 'build.gradle'),
    settings: path.join(BASE_DIR, 'settings.gradle'),
    mainActivity: path.join(src, `src/main/java/${packageFolder}/MainActivity.java`),
  };
};

exports.dependencyConfig = function dependencyConfigAndroid(folder, userConfig) {
  const src = userConfig.sourceDir || getSourceDirectory(folder);

  if (!src) {
    return null;
  }

  const packageName = userConfig.packageName || getPackageName(src);
  const packageInstance = userConfig.packageInstance || getPackageInstance(src);

  // This module has no package to export
  if (!packageInstance) {
    return null;
  }

  return {
    src: src,
    folder: folder,
    packageImportPath: `import ${packageName}.${packageInstance}`,
    packageInstance: ` new ${packageInstance}()`,
  };
};<commit_msg>Add few more bits to Android<commit_after>const path = require('path');
const xml = require('xmldoc');
const fs = require('fs');
const efs = require('../utils/fs');
const glob = require('glob');

// Default android source directory to look for files
const BASE_DIR = './android';
const GLOB_PATTERN = '**/*.java';

const getSourceDirectory = (folder) => {
  var src = path.join(folder, BASE_DIR);

  if (!fs.existsSync(src)) {
    return null;
  }

  if (fs.existsSync(path.join(src, 'app'))) {
    src = path.join(src, 'app');
  }

  return src;
};

const getPackageName = (src) => {
  const manifest = new xml.XmlDocument(
    efs.loadFile(path.join(src, 'src/main/AndroidManifest.xml'))
  );

  return manifest.attr.package;
};

const getPackageFolder = (packageName) => packageName.replace('.', path.sep);

const getPackageInstance = (src) => {
  const files = glob.sync(GLOB_PATTERN, {
    cwd: src,
  });

  const packages = files
    .map(filePath => efs.loadFile(path.join(src, filePath)))
    .map(file => file.match(/class (.*) implements ReactPackage/))
    .filter(match => match);

  // No package exported, ignore
  if (packages.length === 0) {
    return null;
  }

  return packages[0][1];
};

exports.projectConfig = function projectConfigAndroid(folder, userConfig) {
  const src = userConfig.sourceDir || getSourceDirectory(folder);

  if (!src) {
    return null;
  }

  const packageName = userConfig.packageName || getPackageName(src);
  const packageFolder = userConfig.packageFolder || getPackageFolder(packageName);

  return {
    src: src,
    folder: folder,
    project: userConfig.project || path.join(src, 'build.gradle'),
    settings: userConfig.settings || path.join(BASE_DIR, 'settings.gradle'),
    mainActivity: userConfig.mainActivity || path.join(src, `src/main/java/${packageFolder}/MainActivity.java`),
  };
};

exports.dependencyConfig = function dependencyConfigAndroid(folder, userConfig) {
  const src = userConfig.sourceDir || getSourceDirectory(folder);

  if (!src) {
    return null;
  }

  const packageName = userConfig.packageName || getPackageName(src);
  const packageInstance = userConfig.packageInstance || getPackageInstance(src);

  // This module has no package to export
  if (!packageInstance) {
    return null;
  }

  return {
    src: src,
    folder: folder,
    packageImportPath: `import ${packageName}.${packageInstance}`,
    packageInstance: `new ${packageInstance}()`,
  };
};
{'fraction_non_alphanumeric': 0.1495424292845258, 'fraction_numerical': 0.0012479201331114808, 'mean_word_length': 4.051470588235294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1033209', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1475, 'n_words': 350}