|
| 1 | +import fs from 'fs'; |
| 2 | +import { globSync } from 'glob'; |
| 3 | +import { Constants } from '../constants'; |
| 4 | +import { |
| 5 | + AndroidCodeType, |
| 6 | + BlockContentType, |
| 7 | + MainActivityTaskType, |
| 8 | +} from '../types/mod.types'; |
| 9 | +import { applyContentModification } from '../utils/applyContentModification'; |
| 10 | +import { findClosingTagIndex } from '../utils/findClosingTagIndex'; |
| 11 | +import { getErrMessage } from '../utils/getErrMessage'; |
| 12 | +import { getProjectPath } from '../utils/getProjectPath'; |
| 13 | +import { satisfies } from '../utils/satisfies'; |
| 14 | +import { setState } from '../utils/setState'; |
| 15 | +import { stringSplice } from '../utils/stringSplice'; |
| 16 | +import { variables } from '../variables'; |
| 17 | + |
| 18 | +export async function mainActivityTask(args: { |
| 19 | + configPath: string; |
| 20 | + packageName: string; |
| 21 | + content: string; |
| 22 | + task: MainActivityTaskType; |
| 23 | +}): Promise<string> { |
| 24 | + let { content } = args; |
| 25 | + const { task, configPath, packageName } = args; |
| 26 | + |
| 27 | + for (const action of task.actions) { |
| 28 | + variables.set('CONTENT', content); |
| 29 | + if (action.when && !satisfies(variables.getStore(), action.when)) { |
| 30 | + setState(action.name, { |
| 31 | + state: 'skipped', |
| 32 | + reason: 'when', |
| 33 | + error: false, |
| 34 | + }); |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + setState(action.name, { |
| 39 | + state: 'progress', |
| 40 | + error: false, |
| 41 | + }); |
| 42 | + try { |
| 43 | + content = await applyContentModification({ |
| 44 | + action, |
| 45 | + findOrCreateBlock, |
| 46 | + configPath, |
| 47 | + packageName, |
| 48 | + content, |
| 49 | + indentation: 2, |
| 50 | + }); |
| 51 | + |
| 52 | + setState(action.name, { |
| 53 | + state: 'done', |
| 54 | + error: false, |
| 55 | + }); |
| 56 | + } catch (e) { |
| 57 | + setState(action.name, { |
| 58 | + state: 'error', |
| 59 | + reason: getErrMessage(e), |
| 60 | + error: true, |
| 61 | + }); |
| 62 | + throw e; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return content; |
| 67 | +} |
| 68 | + |
| 69 | +function findOrCreateBlock( |
| 70 | + content: string, |
| 71 | + block: string |
| 72 | +): { |
| 73 | + blockContent: BlockContentType; |
| 74 | + content: string; |
| 75 | +} { |
| 76 | + let blockContent = { |
| 77 | + start: 0, |
| 78 | + end: content.length, |
| 79 | + match: content, |
| 80 | + space: '', |
| 81 | + justCreated: false, |
| 82 | + }; |
| 83 | + |
| 84 | + const blockPath = block.split('.'); |
| 85 | + let contentOffset = 0; |
| 86 | + |
| 87 | + for (let i = 0; i < blockPath.length; i++) { |
| 88 | + const matcherRegex = new RegExp(`^((\\s+)?)${blockPath[i]}\\s+\\{`, 'ms'); |
| 89 | + let blockStart = matcherRegex.exec(blockContent.match); |
| 90 | + |
| 91 | + const justCreated = !blockStart; |
| 92 | + if (!blockStart) { |
| 93 | + const blockName = blockPath[i]; |
| 94 | + // create block in block |
| 95 | + const space = ' '.repeat(2 * i); |
| 96 | + const previousSpace = ' '.repeat(Math.max(0, 2 * (i - 1))); |
| 97 | + const newBlock = `${space}${blockName} {}`; |
| 98 | + const codeToInsert = ` |
| 99 | +${newBlock} |
| 100 | +${previousSpace}`; |
| 101 | + const contentLengthBeforeInsert = content.length; |
| 102 | + content = stringSplice(content, blockContent.end, 0, codeToInsert); |
| 103 | + if (codeToInsert.length && contentLengthBeforeInsert < content.length) { |
| 104 | + blockContent.match += codeToInsert; |
| 105 | + blockContent.end += codeToInsert.length; |
| 106 | + blockStart = matcherRegex.exec(blockContent.match); |
| 107 | + } |
| 108 | + } |
| 109 | + if (!blockStart) { |
| 110 | + throw new Error('block could not be inserted, something wrong?'); |
| 111 | + } |
| 112 | + |
| 113 | + const blockEndIndex = findClosingTagIndex( |
| 114 | + content, |
| 115 | + contentOffset + blockStart.index + blockStart[0].length |
| 116 | + ); |
| 117 | + const blockBody = content.substring( |
| 118 | + contentOffset + blockStart.index + blockStart[0].length, |
| 119 | + blockEndIndex |
| 120 | + ); |
| 121 | + blockContent = { |
| 122 | + start: contentOffset + blockStart.index + blockStart[0].length, |
| 123 | + end: blockEndIndex, |
| 124 | + match: blockBody, |
| 125 | + justCreated, |
| 126 | + space: ' '.repeat(2 * i), |
| 127 | + }; |
| 128 | + contentOffset += blockStart.index + blockStart[0].length; |
| 129 | + } |
| 130 | + |
| 131 | + return { |
| 132 | + blockContent, |
| 133 | + content, |
| 134 | + }; |
| 135 | +} |
| 136 | + |
| 137 | +function getMainActivityPath(lang?: AndroidCodeType) { |
| 138 | + const projectPath = getProjectPath(); |
| 139 | + |
| 140 | + const mainActivityPath = globSync( |
| 141 | + [ |
| 142 | + projectPath, |
| 143 | + 'android', |
| 144 | + 'app', |
| 145 | + 'src', |
| 146 | + 'main', |
| 147 | + 'java', |
| 148 | + '**', |
| 149 | + lang === 'kotlin' |
| 150 | + ? Constants.MAIN_ACTIVITY_KT_FILE_NAME |
| 151 | + : Constants.MAIN_ACTIVITY_JAVA_FILE_NAME, |
| 152 | + ].join('/'), |
| 153 | + { nodir: true } |
| 154 | + )[0]; |
| 155 | + if (!mainActivityPath) |
| 156 | + throw new Error( |
| 157 | + `MainActivity.${lang === 'kotlin' ? 'kt' : 'java'} file not found` |
| 158 | + ); |
| 159 | + return mainActivityPath; |
| 160 | +} |
| 161 | + |
| 162 | +function readMainActivityContent(lang?: AndroidCodeType) { |
| 163 | + const mainActivityPath = getMainActivityPath(lang); |
| 164 | + return fs.readFileSync(mainActivityPath, 'utf-8'); |
| 165 | +} |
| 166 | + |
| 167 | +function writeAppDelegateContent( |
| 168 | + content: string, |
| 169 | + lang?: AndroidCodeType |
| 170 | +): void { |
| 171 | + const mainActivityPath = getMainActivityPath(lang); |
| 172 | + return fs.writeFileSync(mainActivityPath, content, 'utf-8'); |
| 173 | +} |
| 174 | + |
| 175 | +export async function runTask(args: { |
| 176 | + configPath: string; |
| 177 | + packageName: string; |
| 178 | + task: MainActivityTaskType; |
| 179 | +}): Promise<void> { |
| 180 | + let content = readMainActivityContent(args.task.lang); |
| 181 | + |
| 182 | + content = await mainActivityTask({ |
| 183 | + ...args, |
| 184 | + content, |
| 185 | + }); |
| 186 | + |
| 187 | + writeAppDelegateContent(content, args.task.lang); |
| 188 | +} |
| 189 | + |
| 190 | +export const summary = 'MainActivity modification'; |
0 commit comments