Extract addSnapshotModule from buildSnapshot

Bug: 186290299
Test: m nothing
Change-Id: I297d63366d71f692572e8e40f1e510b6e41859e6
diff --git a/sdk/update.go b/sdk/update.go
index da76fb4..ef6a7b0 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -288,7 +288,64 @@
 		bpFile.AddModule(unversioned)
 	}
 
-	// Create the snapshot module.
+	// Add the sdk/module_exports_snapshot module to the bp file.
+	s.addSnapshotModule(ctx, builder, sdkVariants, memberRefs)
+
+	// generate Android.bp
+	bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
+	generateBpContents(&bp.generatedContents, bpFile)
+
+	contents := bp.content.String()
+	syntaxCheckSnapshotBpFile(ctx, contents)
+
+	bp.build(pctx, ctx, nil)
+
+	filesToZip := builder.filesToZip
+
+	// zip them all
+	outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
+	outputDesc := "Building snapshot for " + ctx.ModuleName()
+
+	// If there are no zips to merge then generate the output zip directly.
+	// Otherwise, generate an intermediate zip file into which other zips can be
+	// merged.
+	var zipFile android.OutputPath
+	var desc string
+	if len(builder.zipsToMerge) == 0 {
+		zipFile = outputZipFile
+		desc = outputDesc
+	} else {
+		zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
+		desc = "Building intermediate snapshot for " + ctx.ModuleName()
+	}
+
+	ctx.Build(pctx, android.BuildParams{
+		Description: desc,
+		Rule:        zipFiles,
+		Inputs:      filesToZip,
+		Output:      zipFile,
+		Args: map[string]string{
+			"basedir": builder.snapshotDir.String(),
+		},
+	})
+
+	if len(builder.zipsToMerge) != 0 {
+		ctx.Build(pctx, android.BuildParams{
+			Description: outputDesc,
+			Rule:        mergeZips,
+			Input:       zipFile,
+			Inputs:      builder.zipsToMerge,
+			Output:      outputZipFile,
+		})
+	}
+
+	return outputZipFile
+}
+
+// addSnapshotModule adds the sdk_snapshot/module_exports_snapshot module to the builder.
+func (s *sdk) addSnapshotModule(ctx android.ModuleContext, builder *snapshotBuilder, sdkVariants []*sdk, memberRefs []sdkMemberRef) {
+	bpFile := builder.bpFile
+
 	snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
 	var snapshotModuleType string
 	if s.properties.Module_exports {
@@ -390,56 +447,6 @@
 	snapshotModule.transform(pruneEmptySetTransformer{})
 
 	bpFile.AddModule(snapshotModule)
-
-	// generate Android.bp
-	bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
-	generateBpContents(&bp.generatedContents, bpFile)
-
-	contents := bp.content.String()
-	syntaxCheckSnapshotBpFile(ctx, contents)
-
-	bp.build(pctx, ctx, nil)
-
-	filesToZip := builder.filesToZip
-
-	// zip them all
-	outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
-	outputDesc := "Building snapshot for " + ctx.ModuleName()
-
-	// If there are no zips to merge then generate the output zip directly.
-	// Otherwise, generate an intermediate zip file into which other zips can be
-	// merged.
-	var zipFile android.OutputPath
-	var desc string
-	if len(builder.zipsToMerge) == 0 {
-		zipFile = outputZipFile
-		desc = outputDesc
-	} else {
-		zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
-		desc = "Building intermediate snapshot for " + ctx.ModuleName()
-	}
-
-	ctx.Build(pctx, android.BuildParams{
-		Description: desc,
-		Rule:        zipFiles,
-		Inputs:      filesToZip,
-		Output:      zipFile,
-		Args: map[string]string{
-			"basedir": builder.snapshotDir.String(),
-		},
-	})
-
-	if len(builder.zipsToMerge) != 0 {
-		ctx.Build(pctx, android.BuildParams{
-			Description: outputDesc,
-			Rule:        mergeZips,
-			Input:       zipFile,
-			Inputs:      builder.zipsToMerge,
-			Output:      outputZipFile,
-		})
-	}
-
-	return outputZipFile
 }
 
 // Check the syntax of the generated Android.bp file contents and if they are