1const { withDangerousMod } = require('@expo/config-plugins');
2const assert = require('assert');
3const fs = require('fs-extra');
4const path = require('path');
5
6// Use this improve gradle builds
7module.exports = (config, options) => {
8  assert(options, 'gradle.properties must be defined');
9  return withDangerousMod(config, [
10    'android',
11    async (config) => {
12      const filePath = path.join(config.modRequest.projectRoot, 'android', 'gradle.properties');
13      const contents = await fs.readFile(filePath, 'utf-8');
14      const results = [];
15      const lines = contents.split('\n');
16      for (let i = 0; i < lines.length; i++) {
17        const line = lines[i].trim();
18        if (line && !line.startsWith('#')) {
19          const eok = line.indexOf('=');
20          const keyName = line.slice(0, eok);
21          let value;
22          if (keyName in options) {
23            value = options[keyName];
24            delete options[keyName];
25          } else {
26            value = line.slice(eok + 1, line.length);
27          }
28          results.push(`${keyName}=${value}`);
29        } else {
30          results.push(line);
31        }
32      }
33
34      // Add the remaining options
35      for (const [key, value] of Object.entries(options)) {
36        results.push(`${key}=${value}`);
37      }
38      await fs.writeFile(filePath, results.join('\n'));
39      return config;
40    },
41  ]);
42};
43