17c10e2d9SEvan Baconconst { withDangerousMod } = require('@expo/config-plugins');
27c10e2d9SEvan Baconconst assert = require('assert');
37c10e2d9SEvan Baconconst fs = require('fs-extra');
47c10e2d9SEvan Baconconst path = require('path');
57c10e2d9SEvan Bacon
67c10e2d9SEvan Bacon// Use this improve gradle builds
77c10e2d9SEvan Baconmodule.exports = (config, options) => {
87c10e2d9SEvan Bacon  assert(options, 'gradle.properties must be defined');
97c10e2d9SEvan Bacon  return withDangerousMod(config, [
107c10e2d9SEvan Bacon    'android',
11*35f78160SBartosz Kaszubowski    async (config) => {
127c10e2d9SEvan Bacon      const filePath = path.join(config.modRequest.projectRoot, 'android', 'gradle.properties');
137c10e2d9SEvan Bacon      const contents = await fs.readFile(filePath, 'utf-8');
147c10e2d9SEvan Bacon      const results = [];
157c10e2d9SEvan Bacon      const lines = contents.split('\n');
167c10e2d9SEvan Bacon      for (let i = 0; i < lines.length; i++) {
177c10e2d9SEvan Bacon        const line = lines[i].trim();
187c10e2d9SEvan Bacon        if (line && !line.startsWith('#')) {
197c10e2d9SEvan Bacon          const eok = line.indexOf('=');
207c10e2d9SEvan Bacon          const keyName = line.slice(0, eok);
217c10e2d9SEvan Bacon          let value;
227c10e2d9SEvan Bacon          if (keyName in options) {
237c10e2d9SEvan Bacon            value = options[keyName];
247c10e2d9SEvan Bacon            delete options[keyName];
257c10e2d9SEvan Bacon          } else {
267c10e2d9SEvan Bacon            value = line.slice(eok + 1, line.length);
277c10e2d9SEvan Bacon          }
287c10e2d9SEvan Bacon          results.push(`${keyName}=${value}`);
297c10e2d9SEvan Bacon        } else {
307c10e2d9SEvan Bacon          results.push(line);
317c10e2d9SEvan Bacon        }
327c10e2d9SEvan Bacon      }
337c10e2d9SEvan Bacon
347c10e2d9SEvan Bacon      // Add the remaining options
357c10e2d9SEvan Bacon      for (const [key, value] of Object.entries(options)) {
367c10e2d9SEvan Bacon        results.push(`${key}=${value}`);
377c10e2d9SEvan Bacon      }
387c10e2d9SEvan Bacon      await fs.writeFile(filePath, results.join('\n'));
397c10e2d9SEvan Bacon      return config;
407c10e2d9SEvan Bacon    },
417c10e2d9SEvan Bacon  ]);
427c10e2d9SEvan Bacon};
43