|
| 1 | +/* Kotlin Multiplatform (KMP) wizard using posthog-agent with PostHog MCP */ |
| 2 | +import type { FrameworkConfig } from '../../lib/framework-config'; |
| 3 | +import { Integration } from '../../lib/constants'; |
| 4 | +import { gradlePackageManager } from '../../lib/detection/package-manager'; |
| 5 | +import fg from 'fast-glob'; |
| 6 | +import * as fs from 'node:fs'; |
| 7 | +import * as path from 'node:path'; |
| 8 | + |
| 9 | +export const KMP_AGENT_CONFIG: FrameworkConfig = { |
| 10 | + metadata: { |
| 11 | + name: 'Kotlin Multiplatform', |
| 12 | + integration: Integration.kmp, |
| 13 | + beta: true, |
| 14 | + docsUrl: 'https://posthog.com/docs/libraries/kmp', |
| 15 | + preRunNotice: |
| 16 | + 'The PostHog Kotlin Multiplatform SDK is in early access (0.x pre-release). The API may change between minor versions.', |
| 17 | + }, |
| 18 | + |
| 19 | + detection: { |
| 20 | + packageName: 'posthog-kmp', |
| 21 | + packageDisplayName: 'Kotlin Multiplatform', |
| 22 | + usesPackageJson: false, |
| 23 | + getVersion: () => undefined, |
| 24 | + detectPackageManager: gradlePackageManager, |
| 25 | + // KMP is detected before Android/Swift because a KMP project also looks like |
| 26 | + // an Android and/or Swift project. Detection therefore requires the Kotlin |
| 27 | + // Multiplatform Gradle plugin or a `commonMain` source set — signals that a |
| 28 | + // plain Android or Swift project does not have. |
| 29 | + detect: async (options) => { |
| 30 | + const { installDir } = options; |
| 31 | + |
| 32 | + // Strategy 1: a Gradle build file that applies the Kotlin Multiplatform plugin. |
| 33 | + const buildFiles = await fg(['**/build.gradle', '**/build.gradle.kts'], { |
| 34 | + cwd: installDir, |
| 35 | + ignore: ['**/build/**', '**/node_modules/**', '**/.gradle/**'], |
| 36 | + }); |
| 37 | + |
| 38 | + for (const file of buildFiles) { |
| 39 | + const content = fs.readFileSync(path.join(installDir, file), 'utf-8'); |
| 40 | + if ( |
| 41 | + content.includes('kotlin("multiplatform")') || |
| 42 | + content.includes('org.jetbrains.kotlin.multiplatform') || |
| 43 | + content.includes('kotlin-multiplatform') |
| 44 | + ) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Strategy 2: a KMP `commonMain` source set exists. |
| 50 | + const commonMain = await fg('**/src/commonMain', { |
| 51 | + cwd: installDir, |
| 52 | + onlyDirectories: true, |
| 53 | + ignore: ['**/build/**', '**/node_modules/**', '**/.gradle/**'], |
| 54 | + }); |
| 55 | + |
| 56 | + return commonMain.length > 0; |
| 57 | + }, |
| 58 | + }, |
| 59 | + |
| 60 | + environment: { |
| 61 | + uploadToHosting: false, |
| 62 | + getEnvVars: (apiKey: string, host: string) => ({ |
| 63 | + POSTHOG_PROJECT_TOKEN: apiKey, |
| 64 | + POSTHOG_HOST: host, |
| 65 | + }), |
| 66 | + }, |
| 67 | + |
| 68 | + analytics: { |
| 69 | + getTags: () => ({}), |
| 70 | + }, |
| 71 | + |
| 72 | + prompts: { |
| 73 | + projectTypeDetection: |
| 74 | + 'This is a Kotlin Multiplatform (KMP) project. Look for a build.gradle.kts (or build.gradle) that applies the Kotlin Multiplatform plugin (kotlin("multiplatform") or org.jetbrains.kotlin.multiplatform) and a shared module with a src/commonMain source set.', |
| 75 | + packageInstallation: |
| 76 | + 'Add the PostHog KMP SDK to the shared module\'s commonMain dependencies in build.gradle.kts: within the kotlin { sourceSets { ... } } block, add implementation("com.posthog:posthog-kmp:<VERSION>") to commonMain.dependencies. Match the existing dependency format (Groovy vs Kotlin DSL).', |
| 77 | + getAdditionalContextLines: () => [ |
| 78 | + 'Framework docs ID: kmp (use posthog://docs/frameworks/kmp for documentation)', |
| 79 | + 'Initialize once, early in the app lifecycle, from shared code. All PostHog APIs live in the com.posthog.kmp package.', |
| 80 | + 'Setup call: PostHog.setup(config = PostHogConfig(apiKey = "<POSTHOG_PROJECT_TOKEN>", host = "<POSTHOG_HOST>"), context = PostHogContext())', |
| 81 | + 'PostHogContext is platform-specific: on Android pass the Application via PostHogContext(application); on iOS and Web use the no-argument PostHogContext().', |
| 82 | + 'Imports: com.posthog.kmp.PostHog, com.posthog.kmp.PostHogConfig, com.posthog.kmp.PostHogContext.', |
| 83 | + ], |
| 84 | + }, |
| 85 | + |
| 86 | + ui: { |
| 87 | + successMessage: 'PostHog integration complete', |
| 88 | + estimatedDurationMinutes: 5, |
| 89 | + getOutroChanges: () => [ |
| 90 | + 'Analyzed your Kotlin Multiplatform project structure', |
| 91 | + 'Added the PostHog KMP SDK to your shared module', |
| 92 | + 'Configured PostHog initialization in your shared code', |
| 93 | + 'Added event capture and user identification', |
| 94 | + ], |
| 95 | + getOutroNextSteps: () => [ |
| 96 | + 'Wire up the platform-specific PostHogContext for each target (Android/iOS/Web)', |
| 97 | + 'Build and run your app to see PostHog in action', |
| 98 | + 'Visit your PostHog dashboard to see incoming events', |
| 99 | + 'Check out the PostHog KMP docs for feature flags, session replay, and more', |
| 100 | + ], |
| 101 | + }, |
| 102 | +}; |
0 commit comments