|
| 1 | +package com.telerik.metadata.analytics.impl; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.telerik.metadata.analytics.AnalyticsCollector; |
| 8 | + |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | + |
| 14 | +public final class EnabledAnalyticsCollectorImpl implements AnalyticsCollector { |
| 15 | + |
| 16 | + private static final String HAS_KOTLIN_RUNTIME_CLASSES_JSON_PROPERTY_NAME = "hasKotlinRuntimeClasses"; |
| 17 | + private static final String HAS_USE_KOTLIN_PROPERTY_IN_APP_JSON_PROPERTY_NAME = "hasUseKotlinPropertyInApp"; |
| 18 | + private static final String KOTLIN_USAGE_JSON_PROPERTY_NAME = "kotlinUsage"; |
| 19 | + |
| 20 | + |
| 21 | + private final String analyticsFilePath; |
| 22 | + private final Gson gson; |
| 23 | + private boolean hasMarked; |
| 24 | + |
| 25 | + public static void main(String... args) { |
| 26 | + EnabledAnalyticsCollectorImpl a = new EnabledAnalyticsCollectorImpl("/Users/vmutafov/work/android_runtime_release/android-runtime/test-app/analytics/build-statistics.json"); |
| 27 | + a.markHasKotlinRuntimeClassesIfNotMarkedAlready(); |
| 28 | + } |
| 29 | + |
| 30 | + public EnabledAnalyticsCollectorImpl(String analyticsFilePath) { |
| 31 | + this.analyticsFilePath = analyticsFilePath; |
| 32 | + this.gson = new GsonBuilder().setPrettyPrinting().create(); |
| 33 | + this.hasMarked = false; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void markHasKotlinRuntimeClassesIfNotMarkedAlready() { |
| 38 | + if (!hasMarked) { |
| 39 | + |
| 40 | + try { |
| 41 | + Path statisticsFilePath = Paths.get(analyticsFilePath); |
| 42 | + String json; |
| 43 | + |
| 44 | + if (Files.notExists(statisticsFilePath)) { |
| 45 | + Files.createDirectories(statisticsFilePath.getParent()); |
| 46 | + Files.createFile(statisticsFilePath); |
| 47 | + |
| 48 | + JsonObject kotlinUsageObject = createNewKotlinUsageJsonObject(true); |
| 49 | + json = gson.toJson(kotlinUsageObject); |
| 50 | + } else { |
| 51 | + byte[] fileContent = Files.readAllBytes(statisticsFilePath); |
| 52 | + String statisticsJson = new String(fileContent, StandardCharsets.UTF_8); |
| 53 | + JsonElement modifiedStatisticsJson = modifyExistingAnalyticsJsonObject(statisticsJson, true); |
| 54 | + json = gson.toJson(modifiedStatisticsJson); |
| 55 | + } |
| 56 | + |
| 57 | + Files.write(statisticsFilePath, json.getBytes(StandardCharsets.UTF_8)); |
| 58 | + hasMarked = true; |
| 59 | + } catch (Exception e) { |
| 60 | + System.out.println(e.getMessage()); // do not fail the build if analytics collection fails |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private JsonObject createNewKotlinUsageJsonObject(boolean hasKotlinRuntimeClasses) { |
| 66 | + JsonObject jsonObject = new JsonObject(); |
| 67 | + |
| 68 | + JsonObject kotlinUsageObject = new JsonObject(); |
| 69 | + kotlinUsageObject.addProperty(HAS_KOTLIN_RUNTIME_CLASSES_JSON_PROPERTY_NAME, hasKotlinRuntimeClasses); |
| 70 | + |
| 71 | + jsonObject.add(KOTLIN_USAGE_JSON_PROPERTY_NAME, kotlinUsageObject); |
| 72 | + |
| 73 | + return jsonObject; |
| 74 | + } |
| 75 | + |
| 76 | + private JsonElement modifyExistingAnalyticsJsonObject(String json, boolean hasKotlinRuntimeClasses) { |
| 77 | + JsonElement jsonElement = new Gson().fromJson(json, JsonElement.class); |
| 78 | + JsonObject jsonObject = jsonElement.getAsJsonObject(); |
| 79 | + |
| 80 | + if (jsonObject.has(KOTLIN_USAGE_JSON_PROPERTY_NAME)) { |
| 81 | + JsonObject kotlinUsageObject = jsonObject.getAsJsonObject(KOTLIN_USAGE_JSON_PROPERTY_NAME); |
| 82 | + kotlinUsageObject.addProperty(HAS_KOTLIN_RUNTIME_CLASSES_JSON_PROPERTY_NAME, hasKotlinRuntimeClasses); |
| 83 | + |
| 84 | + if(!kotlinUsageObject.has(HAS_USE_KOTLIN_PROPERTY_IN_APP_JSON_PROPERTY_NAME)){ |
| 85 | + kotlinUsageObject.addProperty(HAS_USE_KOTLIN_PROPERTY_IN_APP_JSON_PROPERTY_NAME, false); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return jsonElement; |
| 90 | + } |
| 91 | +} |
0 commit comments