-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e98ce57
commit c6ea5cf
Showing
8 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import fs from 'fs-extra'; | ||
import del from 'del'; | ||
import androidAssemble from '../utils/androidAssemble'; | ||
import { Input } from '../types'; | ||
import jetifier from '../utils/jetifier'; | ||
|
||
type TargetOptions = { | ||
androidPath: string, | ||
reverseJetify: boolean | ||
}; | ||
|
||
const defaultOptions: TargetOptions = { | ||
androidPath: "android", | ||
reverseJetify: false | ||
}; | ||
|
||
type Options = Input & { | ||
options?: Partial<TargetOptions>; | ||
}; | ||
|
||
async function createGradleFile(file: string) { | ||
await fs.createFile(file); | ||
await fs.writeFile(file, 'configurations.maybeCreate("default")\nartifacts.add("default", file(\'android.aar\'))') | ||
} | ||
|
||
export default async function build({ | ||
root, | ||
output, | ||
options, | ||
report, | ||
}: Options) { | ||
const targetOptions = { | ||
...defaultOptions, | ||
...options | ||
}; | ||
|
||
report.info( | ||
`Cleaning up previous build at ${chalk.blue(path.relative(root, output))}` | ||
); | ||
|
||
await del([output]); | ||
|
||
await androidAssemble({ root, androidPath: targetOptions.androidPath, report }); | ||
|
||
report.info( | ||
`Creating new output directory at ${chalk.blue(path.relative(root, output))}` | ||
); | ||
await fs.mkdir(output); | ||
|
||
const sourceAar = path.join(targetOptions.androidPath, 'build', 'outputs', 'aar', 'android.aar'); | ||
const targetAar = path.join(output, 'android.aar'); | ||
|
||
report.info( | ||
`Copying AAR from ${chalk.blue(path.relative(root, sourceAar))} to ${chalk.blue(path.relative(root, targetAar))}` | ||
); | ||
await fs.copyFile(sourceAar, targetAar); | ||
|
||
const gradleFile = path.join(output, 'build.gradle'); | ||
report.info( | ||
`Creating AAR Gradle file at ${chalk.blue(path.relative(root, gradleFile))}` | ||
); | ||
await createGradleFile(gradleFile); | ||
|
||
if (targetOptions.reverseJetify) { | ||
const supportOutputPath = path.join(output, 'support'); | ||
report.info( | ||
`Creating new support output directory at ${chalk.blue(path.relative(root, supportOutputPath))}` | ||
); | ||
await fs.mkdir(supportOutputPath); | ||
|
||
const supportAar = path.join(supportOutputPath, 'android.aar'); | ||
report.info( | ||
`Using Jetifier to convert AAR from AndroidX to Support AAR at ${chalk.blue(path.relative(root, supportAar))}` | ||
); | ||
|
||
await jetifier({ | ||
root, | ||
report, | ||
input: targetAar, | ||
output: supportAar, | ||
reverse: true | ||
}); | ||
|
||
const supportGradleFile = path.join(supportOutputPath, 'build.gradle'); | ||
report.info( | ||
`Creating Support AAR Gradle file at ${chalk.blue(path.relative(root, supportGradleFile))}` | ||
); | ||
await createGradleFile(supportGradleFile); | ||
} | ||
|
||
report.success( | ||
`Wrote files to ${chalk.blue(path.relative(root, output))}` | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import fs from 'fs-extra'; | ||
import { execFileSync } from 'child_process'; | ||
import { platform } from 'os'; | ||
import { Report } from '../types'; | ||
|
||
type Options = { | ||
root: string; | ||
androidPath: string; | ||
report: Report; | ||
}; | ||
|
||
export default async function androidAssemble({ root, androidPath, report }: Options) { | ||
const cwd = path.relative(root, androidPath) | ||
|
||
report.info( | ||
`Assembling Android project in ${chalk.blue(cwd)} with ${chalk.blue('gradle')}` | ||
); | ||
|
||
const gradleWrapper = './gradlew' + ((platform() === "win32") ? './gradlew.bat' : ''); | ||
if (await fs.pathExists(path.join(androidPath, gradleWrapper))) { | ||
execFileSync(gradleWrapper, ['assemble'], { cwd: androidPath }); | ||
} else { | ||
throw new Error( | ||
`The ${chalk.blue( | ||
'gradlew' | ||
)} script doesn't seem to present in ${chalk.blue( | ||
androidPath | ||
)}. Make sure you have added it by running ${chalk.blue( | ||
'gradle wrapper' | ||
)} in that directory.` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import path from 'path'; | ||
import chalk from 'chalk'; | ||
import { execFileSync } from 'child_process'; | ||
import fs from 'fs-extra'; | ||
import { Report } from '../types'; | ||
|
||
type Options = { | ||
root: string; | ||
input: string; | ||
output: string; | ||
reverse: boolean; | ||
report: Report; | ||
}; | ||
|
||
export default async function jetifier({ root, input, output, reverse }: Options) { | ||
const jetifierStandalone = path.join(root, 'node_modules', '.bin', 'jetifier-standalone') | ||
|
||
if (await fs.pathExists(jetifierStandalone)) { | ||
const args = ['-i', input, '-o', output]; | ||
if (reverse) { | ||
args.push("-r"); | ||
} | ||
|
||
execFileSync(jetifierStandalone, args); | ||
} else { | ||
throw new Error( | ||
`The ${chalk.blue( | ||
'jetifier' | ||
)} binary doesn't seem to be installed under ${chalk.blue( | ||
'node_modules' | ||
)}. Make sure you have added ${chalk.blue( | ||
'jetifier' | ||
)} to your ${chalk.blue('devDependencies')}.` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters