Add flag to allow baseline files to be empty

Normally baselines are deleted if they are empty. This
CL adds a flag to let you leave baselines in place even
if they are empty.

--delete-empty-baselines  Whether to delete baseline files if they are
                          updated and there is nothing to include.

Also tweak baselines to not end with two blank lines.

Test: make update-api
Change-Id: I7c24d6ac531ad32dfd0e45e2ebd3f2d4c7591338
diff --git a/src/main/java/com/android/tools/metalava/Baseline.kt b/src/main/java/com/android/tools/metalava/Baseline.kt
index f4ea6ee..02c4f4c 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 @@
 
     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 @@
                 }
                 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 a0e301f..91a6adb 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_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 @@
     /** 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 @@
                     }
                 }
                 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 @@
             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 83726e2..36d02bf 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 @@
                                           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