|
| 1 | +import path from 'path'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import del from 'del'; |
| 5 | +import androidAssemble from '../utils/androidAssemble'; |
| 6 | +import { Input } from '../types'; |
| 7 | +import jetifier from '../utils/jetifier'; |
| 8 | + |
| 9 | +type TargetOptions = { |
| 10 | + androidPath: string, |
| 11 | + reverseJetify: boolean |
| 12 | +}; |
| 13 | + |
| 14 | +const defaultOptions: TargetOptions = { |
| 15 | + androidPath: "android", |
| 16 | + reverseJetify: false |
| 17 | +}; |
| 18 | + |
| 19 | +type Options = Input & { |
| 20 | + options?: Partial<TargetOptions>; |
| 21 | +}; |
| 22 | + |
| 23 | +async function createGradleFile(file: string) { |
| 24 | + await fs.createFile(file); |
| 25 | + await fs.writeFile(file, 'configurations.maybeCreate("default")\nartifacts.add("default", file(\'android.aar\'))') |
| 26 | +} |
| 27 | + |
| 28 | +export default async function build({ |
| 29 | + root, |
| 30 | + output, |
| 31 | + options, |
| 32 | + report, |
| 33 | +}: Options) { |
| 34 | + const targetOptions = { |
| 35 | + ...defaultOptions, |
| 36 | + ...options |
| 37 | + }; |
| 38 | + |
| 39 | + report.info( |
| 40 | + `Cleaning up previous build at ${chalk.blue(path.relative(root, output))}` |
| 41 | + ); |
| 42 | + |
| 43 | + await del([output]); |
| 44 | + |
| 45 | + await androidAssemble({ root, androidPath: targetOptions.androidPath, report }); |
| 46 | + |
| 47 | + report.info( |
| 48 | + `Creating new output directory at ${chalk.blue(path.relative(root, output))}` |
| 49 | + ); |
| 50 | + await fs.mkdir(output); |
| 51 | + |
| 52 | + const sourceAar = path.join(targetOptions.androidPath, 'build', 'outputs', 'aar', 'android.aar'); |
| 53 | + const targetAar = path.join(output, 'android.aar'); |
| 54 | + |
| 55 | + report.info( |
| 56 | + `Copying AAR from ${chalk.blue(path.relative(root, sourceAar))} to ${chalk.blue(path.relative(root, targetAar))}` |
| 57 | + ); |
| 58 | + await fs.copyFile(sourceAar, targetAar); |
| 59 | + |
| 60 | + const gradleFile = path.join(output, 'build.gradle'); |
| 61 | + report.info( |
| 62 | + `Creating AAR Gradle file at ${chalk.blue(path.relative(root, gradleFile))}` |
| 63 | + ); |
| 64 | + await createGradleFile(gradleFile); |
| 65 | + |
| 66 | + if (targetOptions.reverseJetify) { |
| 67 | + const supportOutputPath = path.join(output, 'support'); |
| 68 | + report.info( |
| 69 | + `Creating new support output directory at ${chalk.blue(path.relative(root, supportOutputPath))}` |
| 70 | + ); |
| 71 | + await fs.mkdir(supportOutputPath); |
| 72 | + |
| 73 | + const supportAar = path.join(supportOutputPath, 'android.aar'); |
| 74 | + report.info( |
| 75 | + `Using Jetifier to convert AAR from AndroidX to Support AAR at ${chalk.blue(path.relative(root, supportAar))}` |
| 76 | + ); |
| 77 | + |
| 78 | + await jetifier({ |
| 79 | + root, |
| 80 | + report, |
| 81 | + input: targetAar, |
| 82 | + output: supportAar, |
| 83 | + reverse: true |
| 84 | + }); |
| 85 | + |
| 86 | + const supportGradleFile = path.join(supportOutputPath, 'build.gradle'); |
| 87 | + report.info( |
| 88 | + `Creating Support AAR Gradle file at ${chalk.blue(path.relative(root, supportGradleFile))}` |
| 89 | + ); |
| 90 | + await createGradleFile(supportGradleFile); |
| 91 | + } |
| 92 | + |
| 93 | + report.success( |
| 94 | + `Wrote files to ${chalk.blue(path.relative(root, output))}` |
| 95 | + ); |
| 96 | +} |
0 commit comments