diff --git a/src/main/java/com/android/tools/metalava/Baseline.kt b/src/main/java/com/android/tools/metalava/Baseline.kt index f4ea6ee26fd88c56e9a6f8dd6ea3b66c22abc759..02c4f4c04d74137940b24cd928300373533bb9dc 100644 --- a/src/main/java/com/android/tools/metalava/Baseline.kt +++ b/src/main/java/com/android/tools/metalava/Baseline.kt @@ -219,7 +219,7 @@ class Baseline( private fun write() { val updateFile = this.updateFile ?: return - if (!map.isEmpty()) { + if (!map.isEmpty() || !options.deleteEmptyBaselines) { val sb = StringBuilder() sb.append(format.header()) sb.append(headerComment) @@ -235,6 +235,11 @@ class Baseline( } sb.append("\n\n") } + + if (sb.endsWith("\n\n")) { + sb.setLength(sb.length - 2) + } + updateFile.parentFile?.mkdirs() updateFile.writeText(sb.toString(), UTF_8) } else { diff --git a/src/main/java/com/android/tools/metalava/Options.kt b/src/main/java/com/android/tools/metalava/Options.kt index a0e301fc85108a1cce46b703480335d4a472dd1d..91a6adbfc709270b62303f2a134f18cfc2667981 100644 --- a/src/main/java/com/android/tools/metalava/Options.kt +++ b/src/main/java/com/android/tools/metalava/Options.kt @@ -145,6 +145,7 @@ const val ARG_UPDATE_BASELINE = "--update-baseline" const val ARG_MERGE_BASELINE = "--merge-baseline" const val ARG_STUB_PACKAGES = "--stub-packages" const val ARG_STUB_IMPORT_PACKAGES = "--stub-import-packages" +const val ARG_DELETE_EMPTY_BASELINES = "--delete-empty-baselines" class Options( args: Array<String>, @@ -507,6 +508,9 @@ class Options( /** If updating baselines, don't fail the build */ var passBaselineUpdates = false + /** If updating baselines and the baseline is empty, delete the file */ + var deleteEmptyBaselines = false + /** Whether the baseline should only contain errors */ var baselineErrorsOnly = false @@ -756,6 +760,7 @@ class Options( } } ARG_PASS_BASELINE_UPDATES -> passBaselineUpdates = true + ARG_DELETE_EMPTY_BASELINES -> deleteEmptyBaselines = true ARG_PUBLIC, "-public" -> docLevel = DocLevel.PUBLIC ARG_PROTECTED, "-protected" -> docLevel = DocLevel.PROTECTED @@ -1901,6 +1906,8 @@ class Options( ARG_PASS_BASELINE_UPDATES, "Normally, encountering error will fail the build, even when updating " + "baselines. This flag allows you to tell $PROGRAM_NAME to continue without errors, such that " + "all the baselines in the source tree can be updated in one go.", + ARG_DELETE_EMPTY_BASELINES, "Whether to delete baseline files if they are updated and there is nothing " + + "to include.", "", "\nJDiff:", "$ARG_XML_API <file>", "Like $ARG_API, but emits the API in the JDiff XML format instead", diff --git a/src/test/java/com/android/tools/metalava/OptionsTest.kt b/src/test/java/com/android/tools/metalava/OptionsTest.kt index 83726e2d756f2e723b9db03e688c2961052f5006..36d02bf03f2735ac1277cb4e3293ab13f91f6a2f 100644 --- a/src/test/java/com/android/tools/metalava/OptionsTest.kt +++ b/src/test/java/com/android/tools/metalava/OptionsTest.kt @@ -246,6 +246,8 @@ Diffs and Checks: allows you to tell metalava to continue without errors, such that all the baselines in the source tree can be updated in one go. +--delete-empty-baselines Whether to delete baseline files if they are + updated and there is nothing to include. JDiff: --api-xml <file> Like --api, but emits the API in the JDiff XML