Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-files-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-android-gradle-plugin": minor
---

Configurable executable and env for CLI task
13 changes: 13 additions & 0 deletions posthog-android-gradle-plugin/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ plugins {
kotlin("android")
id("com.posthog.android") // <- add this plugin
}

// Optional configuration
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be cool to amend this section of the docs as well https://posthog.com/docs/error-tracking/upload-mappings/android#inject-and-upload

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tasks.withType<PostHogCliExecTask> {
// Custom CLI location
// defaults to globally installed posthog-cli on the PATH
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just export the env variables as well
https://posthog.com/docs/error-tracking/upload-mappings/android#authenticate
you dont need to manually do posthog-cli login

postHogExecutable = "/path/to/posthog-cli"

// Authentication parameters
// otherwise the CLI must be pre-authenticated
postHogHost = "https://eu.posthog.com"
postHogProjectId = "my-project-id"
postHogApiKey = "my-personal-api-key"
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "abstract task, should not be used directly")
internal abstract class DirectoryOutputTask : DefaultTask() {
@get:OutputDirectory abstract val output: DirectoryProperty
public abstract class DirectoryOutputTask : DefaultTask() {
@get:OutputDirectory
public abstract val output: DirectoryProperty
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import java.io.File
import java.util.Properties

@CacheableTask
internal abstract class InjectPostHogMetaPropertiesIntoAssetsTask : DefaultTask() {
public abstract class InjectPostHogMetaPropertiesIntoAssetsTask : DefaultTask() {
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:InputFiles
abstract val inputDir: DirectoryProperty
public abstract val inputDir: DirectoryProperty

@get:OutputDirectory
val outputDir: DirectoryProperty = project.objects.directoryProperty()
public val outputDir: DirectoryProperty = project.objects.directoryProperty()
get() {
// AGP < 8.3 sets an output folder which contains the input folder
// input: app/intermediates/assets/release/mergeReleaseAssets
Expand All @@ -48,10 +48,10 @@ internal abstract class InjectPostHogMetaPropertiesIntoAssetsTask : DefaultTask(
// we only care about file contents
@get:PathSensitive(PathSensitivity.NONE)
@get:InputFiles
abstract val inputPropertyFiles: ConfigurableFileCollection
public abstract val inputPropertyFiles: ConfigurableFileCollection

@TaskAction
fun taskAction() {
protected fun taskAction() {
val input = inputDir.get().asFile
val output = outputDir.getAndDelete()
output.mkdirs()
Expand All @@ -69,8 +69,8 @@ internal abstract class InjectPostHogMetaPropertiesIntoAssetsTask : DefaultTask(
propsFile.writer().use { props.store(it, "Generated by com.posthog.android") }
}

companion object {
internal const val POSTHOG_META_PROPERTIES_OUTPUT = "posthog-meta.properties"
internal companion object {
const val POSTHOG_META_PROPERTIES_OUTPUT = "posthog-meta.properties"

fun register(
project: Project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,57 @@
package com.posthog.android

import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "abstract task, should not be used directly")
internal abstract class PostHogCliExecTask : Exec() {
public abstract class PostHogCliExecTask : Exec() {
@get:Input
public abstract val postHogExecutable: Property<String>

@get:Input
@get:Optional
public abstract val postHogHost: Property<String>

@get:Input
@get:Optional
public abstract val postHogProjectId: Property<String>

@get:Input
@get:Optional
public abstract val postHogApiKey: Property<String>

init {
postHogExecutable.convention("posthog-cli")
}

override fun exec() {
computeCommandLineArgs().let {
commandLine(it)
logger.info("cli args: $it")
executable = postHogExecutable.get()

val args =
computeCommandLineArgs().also {
logger.info("cli args: $it")
}
args(args)

// Setup environment variables for authentication etc
postHogHost.orNull?.let {
environment("POSTHOG_CLI_HOST", it)
}
postHogProjectId.orNull?.let {
environment("POSTHOG_CLI_PROJECT_ID", it)
}
postHogApiKey.orNull?.let {
environment("POSTHOG_CLI_API_KEY", it)
}

super.exec()
}

abstract fun getArguments(args: MutableList<String>)
protected abstract fun getArguments(args: MutableList<String>)

/** Computes the full list of arguments for the task */
private fun computeCommandLineArgs(): List<String> {
Expand All @@ -26,9 +63,6 @@ internal abstract class PostHogCliExecTask : Exec() {
args.add(1, "/c")
}

// TODO: CLI path config
args.add("posthog-cli")

getArguments(args)

return args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.gradle.work.DisableCachingByDefault
import java.util.UUID

@DisableCachingByDefault
internal abstract class PostHogGenerateMapIdTask : PropertiesFileOutputTask() {
public abstract class PostHogGenerateMapIdTask : PropertiesFileOutputTask() {
init {
description =
"Generates a unique build ID to be used when uploading the PostHog mapping file"
Expand All @@ -25,10 +25,11 @@ internal abstract class PostHogGenerateMapIdTask : PropertiesFileOutputTask() {
override val outputFile: Provider<RegularFile>
get() = output.file(POSTHOG_MAP_ID_OUTPUT)

@get:Internal abstract val proguardMappingFiles: ConfigurableFileCollection
@get:Internal
public abstract val proguardMappingFiles: ConfigurableFileCollection

@TaskAction
fun generateProperties() {
protected fun generateProperties() {
val outputDir = output.get().asFile
outputDir.mkdirs()

Expand All @@ -42,9 +43,9 @@ internal abstract class PostHogGenerateMapIdTask : PropertiesFileOutputTask() {
logger.info("PostHogGenerateMapIdTask - outputFile: $outputFile, uuid: $uuid")
}

companion object {
internal const val STATIC_HASH = "<hash>"
internal const val POSTHOG_MAP_ID_OUTPUT = "posthog-proguard-map-id.properties"
internal companion object {
const val STATIC_HASH = "<hash>"
const val POSTHOG_MAP_ID_OUTPUT = "posthog-proguard-map-id.properties"
const val POSTHOG_PROGUARD_MAPPING_MAP_ID_PROPERTY = "io.posthog.proguard.mapid"

fun register(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.gradle.api.tasks.TaskProvider
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "Uploads should not be cached")
internal abstract class PostHogUploadProguardMappingsTask : PostHogCliExecTask() {
public abstract class PostHogUploadProguardMappingsTask : PostHogCliExecTask() {
init {
description = "Uploads the proguard mappings file to PostHog"

Expand All @@ -30,11 +30,11 @@ internal abstract class PostHogUploadProguardMappingsTask : PostHogCliExecTask()

@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
abstract val mapIdFile: RegularFileProperty
public abstract val mapIdFile: RegularFileProperty

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract var mappingsFiles: Provider<FileCollection>
public abstract var mappingsFiles: Provider<FileCollection>

override fun exec() {
if (!mappingsFiles.isPresent || mappingsFiles.get().isEmpty) {
Expand Down Expand Up @@ -67,7 +67,7 @@ internal abstract class PostHogUploadProguardMappingsTask : PostHogCliExecTask()
args.add(uuid)
}

companion object {
internal companion object {
fun register(
project: Project,
generateMapIdTask: Provider<PostHogGenerateMapIdTask>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.tasks.Internal
import org.gradle.work.DisableCachingByDefault

@DisableCachingByDefault(because = "abstract task, should not be used directly")
internal abstract class PropertiesFileOutputTask : DirectoryOutputTask() {
@get:Internal abstract val outputFile: Provider<RegularFile>
public abstract class PropertiesFileOutputTask : DirectoryOutputTask() {
@get:Internal
public abstract val outputFile: Provider<RegularFile>
}
Loading