Skip to content

Commit

Permalink
Better handle dryRun (#229)
Browse files Browse the repository at this point in the history
* Better handle dryRun

* Adjustments

* onSuccessfulUpload
  • Loading branch information
rbro112 authored Aug 8, 2024
1 parent a0e83ee commit 16a2223
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,13 @@ abstract class UploadPerfBundle : BaseUploadTask() {
proguardMappingsZipPath = "$artifactName/${UploadAAB.AAB_PROGUARD_PATH}",
)

val response = upload(artifactMetadata)
checkNotNull(response) {
"Upload failed, please check your network connection and try again."
upload(artifactMetadata) { response ->
logger.lifecycle(
"Performance bundle upload successful! " +
"View Emerge's performance analysis at the following url:"
)
logger.lifecycle("https://emergetools.com/performance/compare/${response.uploadId}")
logger.lifecycle("Performance testing usually takes around 30 minutes or less.")
}

logger.lifecycle(
"Performance bundle upload successful! " +
"View Emerge's performance analysis at the following url:"
)
logger.lifecycle("https://emergetools.com/build/${response.uploadId}?buildContent=comparison")
logger.lifecycle("Performance testing usually takes around 30 minutes or less.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ abstract class InitializeReaper : BaseUploadTask() {
proguardMappingsZipPath = "$artifactName/$AAB_PROGUARD_PATH",
)

val response = upload(artifactMetadata)
checkNotNull(response) {
"Upload failed, please check your network connection and try again. ${response.toString()}"
upload(artifactMetadata) { response ->
logger.lifecycle("Reaper initialized! View Reaper reports for this version at the following url:")
logger.lifecycle("https://emergetools.com/reaper/${response.uploadId}")
logger.lifecycle("Note: Initial Reaper processing can take up to 10 minutes.")
}
logger.lifecycle("Reaper initialized! View Reaper reports at the url:")
logger.lifecycle("https://emergetools.com/reaper/${response.uploadId}")
logger.lifecycle("Initial processing can take up to 10 minutes.")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ abstract class UploadAAB : BaseUploadTask() {
proguardMappingsZipPath = "$artifactName/$AAB_PROGUARD_PATH",
)

val response = upload(artifactMetadata)
checkNotNull(response) {
"Upload failed, please check your network connection and try again. ${response.toString()}"
upload(artifactMetadata) { response ->
logger.lifecycle("AAB Upload successful! View Emerge's size analysis at the following url:")
logger.lifecycle("https://emergetools.com/build/${response.uploadId}")
logger.lifecycle("Size processing can take up to 10 minutes.")
}

logger.lifecycle("AAB Upload successful! View Emerge's size analysis at the following url:")
logger.lifecycle("https://emergetools.com/build/${response.uploadId}")
logger.lifecycle("Size processing can take up to 10 minutes.")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,11 @@ abstract class UploadAPK : BaseUploadTask() {
proguardMappingsZipPath = proguardMappingName,
)

val response = upload(artifactMetadata)
checkNotNull(response) {
"Upload failed, please check your network connection and try again."
upload(artifactMetadata) { response ->
logger.lifecycle("APK Upload successful! View Emerge's size analysis at the following url:")
logger.lifecycle("https://emergetools.com/build/${response.uploadId}")
logger.lifecycle("Size processing can take up to 10 minutes.")
}

logger.lifecycle("APK Upload successful! View Emerge's size analysis at the following url:")
logger.lifecycle("https://emergetools.com/build/${response.uploadId}")
logger.lifecycle("Size processing can take up to 10 minutes.")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@ abstract class UploadSnapshotBundle : BaseUploadTask() {
}
val artifactMetadata: ArtifactMetadata = Json.decodeFromString(artifactMetadataFilePath.readText())

val response = upload(
upload(
artifactMetadata = artifactMetadata.copy(
created = Clock.System.now()
)
)
checkNotNull(response) {
"Upload failed, please check your network connection and try again."
) { response ->
logger.lifecycle(
"Snapshot bundle upload successful! View snapshots at the following url:"
)
logger.lifecycle("https://emergetools.com/snapshot/${response.uploadId}")
logger.lifecycle("Snapshot generations usually take ~10 minutes or less.")
}

logger.lifecycle(
"Snapshot bundle upload successful! View snapshots at the following url:"
)
logger.lifecycle("https://emergetools.com/snapshot/${response.uploadId}")
logger.lifecycle("Snapshot generations usually take ~10 minutes or less.")
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ abstract class BaseUploadTask : DefaultTask() {
@get:Optional
abstract val baseUrl: Property<String>

protected fun upload(artifactMetadata: ArtifactMetadata): EmergeUploadResponse? {
protected fun upload(
artifactMetadata: ArtifactMetadata,
onSuccessfulUpload: (EmergeUploadResponse) -> Unit,
) {
check(!apiToken.getOrElse(System.getenv(DEFAULT_API_TOKEN_ENV_KEY)).isNullOrBlank()) {
"Missing API token. Please set the 'apiToken' property in the emerge {} extension block or" +
" ensure an 'EMERGE_API_TOKEN' environment variable is set with a valid API token." +
Expand Down Expand Up @@ -171,15 +174,21 @@ abstract class BaseUploadTask : DefaultTask() {
}
}

if (dryRun.getOrElse(false)) {
val outputFilePath = File(outputDir, zipFile.name)
zipFile.copyTo(outputFilePath, overwrite = true)
logger.lifecycle("Dry run complete. Zip file created at: ${outputFilePath.path}")
return
}

val response = uploadFile(zipFile)
// Write the upload response to a file so it can be read by clients should they want to ingest
// the buildId or url
val response = uploadFile(zipFile, outputDir)
// the uploadId or url
File(outputDir, UPLOAD_RESPONSE_FILE_NAME).also {
it.createNewFile()
it.writeText(Json.encodeToString(response))
}

return response
onSuccessfulUpload(response)
}

/**
Expand Down Expand Up @@ -208,15 +217,7 @@ abstract class BaseUploadTask : DefaultTask() {
)
}

private fun uploadFile(
file: File,
outputDir: File,
): EmergeUploadResponse? {
if (dryRun.get()) {
file.copyTo(File(outputDir, "/${file.name}"), overwrite = true)
return null
}

private fun uploadFile(file: File): EmergeUploadResponse {
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
Expand Down Expand Up @@ -258,7 +259,8 @@ abstract class BaseUploadTask : DefaultTask() {
project: Project,
variant: Variant,
) {
val emergeOutputDir = File(project.buildDir, ARTIFACT_OUTPUT_DIR).also(File::mkdirs)
val emergeOutputDir =
File(project.layout.buildDirectory.asFile.get(), ARTIFACT_OUTPUT_DIR).also(File::mkdirs)
dryRun.set(extension.dryRun)
apiToken.set(extension.apiToken)
agpVersion.set(AgpVersions.CURRENT.toString())
Expand Down

0 comments on commit 16a2223

Please sign in to comment.