|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const platform = process.env.CAPACITOR_PLATFORM_NAME; |
| 5 | +console.log("\OneSignal plugin - running hook after update - for " + platform); |
| 6 | +const projectDirPath = process.env.CAPACITOR_ROOT_DIR; |
| 7 | + |
| 8 | +if (platform == 'android') { |
| 9 | + fixAndroidAzureRepository(); |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Add the azure repository (where OneSignal native Android library is housed) to project root's build.gradle |
| 14 | + * Because capacitor plugins are injected as separate gradle modules, this is necessary for release builds lintVital gradle tasks to pass. |
| 15 | + */ |
| 16 | +function fixAndroidAzureRepository() { |
| 17 | + const gradleFilePath = path.resolve(projectDirPath, 'android/build.gradle'); |
| 18 | + const azureUrl = 'https://pkgs.dev.azure.com/OutSystemsRD/9e79bc5b-69b2-4476-9ca5-d67594972a52/_packaging/PublicArtifactRepository/maven/v1'; |
| 19 | + const mavenBlock = ` maven { |
| 20 | + url "${azureUrl}" |
| 21 | + }`; |
| 22 | + |
| 23 | + let gradleContent = fs.readFileSync(gradleFilePath, 'utf8'); |
| 24 | + |
| 25 | + if (gradleContent.includes(azureUrl)) { |
| 26 | + console.log('\t[SKIPPED] Azure repository already in root build.gradle.'); |
| 27 | + } else { |
| 28 | + const allprojectsStart = gradleContent.indexOf('allprojects {'); |
| 29 | + if (allprojectsStart === -1) { |
| 30 | + console.warn('\t[WARNING] Could not find allprojects { ... } block. Unable to add Azure Repository'); |
| 31 | + return; |
| 32 | + } |
| 33 | + const repositoriesStart = gradleContent.indexOf('repositories {', allprojectsStart); |
| 34 | + if (repositoriesStart === -1) { |
| 35 | + console.warn('\t[WARNING] Could not find allprojects { repositories { ... } } block. Unable to add Azure Repository'); |
| 36 | + return; |
| 37 | + } |
| 38 | + // Track braces to find end of repositories block |
| 39 | + let braceCount = 0; |
| 40 | + let i = repositoriesStart + 'repositories {'.length - 1; |
| 41 | + let endIndex = -1; |
| 42 | + while (i < gradleContent.length) { |
| 43 | + if (gradleContent[i] === '{') braceCount++; |
| 44 | + else if (gradleContent[i] === '}') braceCount--; |
| 45 | + |
| 46 | + if (braceCount === 0) { |
| 47 | + endIndex = i; |
| 48 | + break; |
| 49 | + } |
| 50 | + i++; |
| 51 | + } |
| 52 | + if (endIndex === -1) { |
| 53 | + console.warn('\t[WARNING] Could not find allprojects { repositories { ... } } block. Unable to add Azure Repository'); |
| 54 | + return; |
| 55 | + } |
| 56 | + const closingBraceLineStartIndex = gradleContent.lastIndexOf('\n', endIndex); |
| 57 | + // Insert the maven block at the end of the repositories block (before closing brace), because gradle searches repositories by order. |
| 58 | + // The Azure repo should be the last one since it will only apply for a few dependencies. |
| 59 | + // Otherwise this could slow down gradle build. |
| 60 | + const updatedContent = gradleContent.slice(0, closingBraceLineStartIndex) + '\n' + mavenBlock + gradleContent.slice(closingBraceLineStartIndex); |
| 61 | + fs.writeFileSync(gradleFilePath, updatedContent, 'utf8'); |
| 62 | + console.log('\t[SUCCESS] Added Azure repository maven block to the root build.gradle.'); |
| 63 | + } |
| 64 | +} |
0 commit comments