Skip to content

Commit

Permalink
🔨 Light refactoring of android extra config
Browse files Browse the repository at this point in the history
  • Loading branch information
belemaire committed Apr 12, 2019
1 parent 8756615 commit ee6d21a
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 59 deletions.
35 changes: 4 additions & 31 deletions ern-container-gen-android/src/AndroidGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class AndroidGenerator implements ContainerGenerator {
throw new Error('react-native plugin does not have a version !')
}

const mustacheView: any = {}
let mustacheView: any = {}
injectReactNativeVersionKeysInObject(
mustacheView,
reactNativePlugin.version
Expand All @@ -91,8 +91,9 @@ export default class AndroidGenerator implements ContainerGenerator {
.task('Adding Native Dependencies Hooks')
.run(this.addAndroidPluginHookClasses(config.plugins, config.outDir))

kax.info('Configure android config hooks')
this.configureAndroidBuildOptions(config, mustacheView)
kax.info('Setting Android tools and libraries versions')
const versions = android.resolveAndroidVersions(config.androidConfig)
mustacheView = Object.assign(mustacheView, versions)

const injectPluginsTaskMsg = 'Injecting Native Dependencies'
const injectPluginsKaxTask = kax.task(injectPluginsTaskMsg)
Expand Down Expand Up @@ -336,32 +337,4 @@ export default class AndroidGenerator implements ContainerGenerator {
mustacheView.isCodePushPluginIncluded = true
}
}

public configureAndroidBuildOptions(
config: ContainerGeneratorConfig,
mustacheView: any
) {
const androidBuildOptions = (config && config.androidConfig) || {}
mustacheView.androidGradlePlugin =
(androidBuildOptions && androidBuildOptions.androidGradlePlugin) ||
android.DEFAULT_ANDROID_GRADLE_PLUGIN_VERSION
mustacheView.buildToolsVersion =
(androidBuildOptions && androidBuildOptions.buildToolsVersion) ||
android.DEFAULT_BUILD_TOOLS_VERSION
mustacheView.compileSdkVersion =
(androidBuildOptions && androidBuildOptions.compileSdkVersion) ||
android.DEFAULT_COMPILE_SDK_VERSION
mustacheView.gradleDistributionVersion =
(androidBuildOptions && androidBuildOptions.gradleDistributionVersion) ||
android.DEFAULT_GRADLE_DISTRIBUTION_VERSION
mustacheView.minSdkVersion =
(androidBuildOptions && androidBuildOptions.minSdkVersion) ||
android.DEFAULT_MIN_SDK_VERSION
mustacheView.supportLibraryVersion =
(androidBuildOptions && androidBuildOptions.supportLibraryVersion) ||
android.DEFAULT_SUPPORT_LIBRARY
mustacheView.targetSdkVersion =
(androidBuildOptions && androidBuildOptions.targetSdkVersion) ||
android.DEFAULT_TARGET_SDK_VERSION
}
}
32 changes: 31 additions & 1 deletion ern-core/src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,39 @@ export const DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
export const DEFAULT_COMPILE_SDK_VERSION = '28'
export const DEFAULT_GRADLE_DISTRIBUTION_VERSION = '4.6'
export const DEFAULT_MIN_SDK_VERSION = '19'
export const DEFAULT_SUPPORT_LIBRARY = '28.0.0'
export const DEFAULT_SUPPORT_LIBRARY_VERSION = '28.0.0'
export const DEFAULT_TARGET_SDK_VERSION = '28'

export interface AndroidResolvedVersions {
androidGradlePlugin: string
buildToolsVersion: string
compileSdkVersion: string
gradleDistributionVersion: string
minSdkVersion: string
supportLibraryVersion: string
targetSdkVersion: string
}

export function resolveAndroidVersions({
androidGradlePlugin = DEFAULT_ANDROID_GRADLE_PLUGIN_VERSION,
buildToolsVersion = DEFAULT_BUILD_TOOLS_VERSION,
compileSdkVersion = DEFAULT_COMPILE_SDK_VERSION,
gradleDistributionVersion = DEFAULT_GRADLE_DISTRIBUTION_VERSION,
minSdkVersion = DEFAULT_MIN_SDK_VERSION,
supportLibraryVersion = DEFAULT_SUPPORT_LIBRARY_VERSION,
targetSdkVersion = DEFAULT_TARGET_SDK_VERSION,
} = {}): AndroidResolvedVersions {
return {
androidGradlePlugin,
buildToolsVersion,
compileSdkVersion,
gradleDistributionVersion,
minSdkVersion,
supportLibraryVersion,
targetSdkVersion,
}
}

// ==============================================================================
// Misc utilities
// ==============================================================================
Expand Down
1 change: 1 addition & 0 deletions ern-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export { isPackagePublished } from './isPackagePublished'
export { getDefaultMavenLocalDirectory } from './getDefaultMavenLocalDirectory'
export { gitCli } from './gitCli'
export { BaseMiniApp } from './BaseMiniApp'
export { AndroidResolvedVersions } from './android'

export const config = _config
export const Platform = _Platform
Expand Down
55 changes: 55 additions & 0 deletions ern-core/test/android-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,59 @@ describe('android.js', () => {
expect(await android.androidGetBootAnimProp()).to.eql('Stopped')
})
})

// ==========================================================
// resolveAndroidVersions
// ==========================================================
describe('resolveAndroidVersions', () => {
it('should return all default versions if no versions are provided', () => {
const versions = android.resolveAndroidVersions()
expect(versions).deep.equal({
androidGradlePlugin: android.DEFAULT_ANDROID_GRADLE_PLUGIN_VERSION,
buildToolsVersion: android.DEFAULT_BUILD_TOOLS_VERSION,
compileSdkVersion: android.DEFAULT_COMPILE_SDK_VERSION,
gradleDistributionVersion: android.DEFAULT_GRADLE_DISTRIBUTION_VERSION,
minSdkVersion: android.DEFAULT_MIN_SDK_VERSION,
supportLibraryVersion: android.DEFAULT_SUPPORT_LIBRARY_VERSION,
targetSdkVersion: android.DEFAULT_TARGET_SDK_VERSION,
})
})

it('should return default versions along with user provided versions', () => {
const versions = android.resolveAndroidVersions({
androidGradlePlugin: '3.0.0',
minSdkVersion: '15',
})
expect(versions).deep.equal({
androidGradlePlugin: '3.0.0',
buildToolsVersion: android.DEFAULT_BUILD_TOOLS_VERSION,
compileSdkVersion: android.DEFAULT_COMPILE_SDK_VERSION,
gradleDistributionVersion: android.DEFAULT_GRADLE_DISTRIBUTION_VERSION,
minSdkVersion: '15',
supportLibraryVersion: android.DEFAULT_SUPPORT_LIBRARY_VERSION,
targetSdkVersion: android.DEFAULT_TARGET_SDK_VERSION,
})
})

it('should return only user provided versions', () => {
const versions = android.resolveAndroidVersions({
androidGradlePlugin: '3.0.0',
buildToolsVersion: '27.0.0',
compileSdkVersion: '27',
gradleDistributionVersion: '4.5',
minSdkVersion: '15',
supportLibraryVersion: '27.0.0',
targetSdkVersion: '27',
})
expect(versions).deep.equal({
androidGradlePlugin: '3.0.0',
buildToolsVersion: '27.0.0',
compileSdkVersion: '27',
gradleDistributionVersion: '4.5',
minSdkVersion: '15',
supportLibraryVersion: '27.0.0',
targetSdkVersion: '27',
})
})
})
})
38 changes: 11 additions & 27 deletions ern-runner-gen-android/src/AndroidRunnerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default class AndroidRunnerGenerator implements RunnerGenerator {
}

public async generate(config: RunnerGeneratorConfig): Promise<void> {
const mustacheView: any = {}
configureMustacheView(config, mustacheView)
let mustacheView: any = {}
mustacheView = configureMustacheView(config, mustacheView)

shell.cp('-R', path.join(runnerHullPath, '*'), config.outDir)
const files = readDir(
Expand All @@ -34,8 +34,8 @@ export default class AndroidRunnerGenerator implements RunnerGenerator {
public async regenerateRunnerConfig(
config: RunnerGeneratorConfig
): Promise<void> {
const mustacheView: any = {}
configureMustacheView(config, mustacheView)
let mustacheView: any = {}
mustacheView = configureMustacheView(config, mustacheView)

const subPathToRunnerConfig = path.join(
'app',
Expand Down Expand Up @@ -70,29 +70,11 @@ function configureMustacheView(
config: RunnerGeneratorConfig,
mustacheView: any
) {
const androidBuildOptions =
(config && config.extra && config.extra.androidConfig) || {}
mustacheView.androidGradlePlugin =
(androidBuildOptions && androidBuildOptions.androidGradlePlugin) ||
android.DEFAULT_ANDROID_GRADLE_PLUGIN_VERSION
mustacheView.buildToolsVersion =
(androidBuildOptions && androidBuildOptions.buildToolsVersion) ||
android.DEFAULT_BUILD_TOOLS_VERSION
mustacheView.compileSdkVersion =
(androidBuildOptions && androidBuildOptions.compileSdkVersion) ||
android.DEFAULT_COMPILE_SDK_VERSION
mustacheView.gradleDistributionVersion =
(androidBuildOptions && androidBuildOptions.gradleDistributionVersion) ||
android.DEFAULT_GRADLE_DISTRIBUTION_VERSION
mustacheView.minSdkVersion =
(androidBuildOptions && androidBuildOptions.minSdkVersion) ||
android.DEFAULT_MIN_SDK_VERSION
mustacheView.supportLibraryVersion =
(androidBuildOptions && androidBuildOptions.supportLibraryVersion) ||
android.DEFAULT_SUPPORT_LIBRARY
mustacheView.targetSdkVersion =
(androidBuildOptions && androidBuildOptions.targetSdkVersion) ||
android.DEFAULT_TARGET_SDK_VERSION
const versions = android.resolveAndroidVersions(
config.extra && config.extra.androidConfig
)
mustacheView = Object.assign(mustacheView, versions)

mustacheView.isReactNativeDevSupportEnabled =
config.reactNativeDevSupportEnabled === true ? 'true' : 'false'
mustacheView.miniAppName = config.mainMiniAppName
Expand All @@ -101,4 +83,6 @@ function configureMustacheView(
mustacheView.packagerPort =
config.reactNativePackagerPort || defaultReactNativePackagerPort
mustacheView.pascalCaseMiniAppName = pascalCase(config.mainMiniAppName)

return mustacheView
}

0 comments on commit ee6d21a

Please sign in to comment.