|
| 1 | +// Copyright (c) 2026. Tony Robalik. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.autonomousapps.jvm.projects |
| 4 | + |
| 5 | +import com.autonomousapps.AbstractProject |
| 6 | +import com.autonomousapps.kit.GradleProject |
| 7 | +import com.autonomousapps.kit.Source |
| 8 | +import com.autonomousapps.kit.SourceType |
| 9 | +import com.autonomousapps.model.ProjectTypeUsage |
| 10 | + |
| 11 | +import static com.autonomousapps.kit.gradle.Dependency.project |
| 12 | +import static com.autonomousapps.kit.gradle.dependencies.Dependencies.* |
| 13 | + |
| 14 | +final class TypeUsageMultiModuleProject extends AbstractProject { |
| 15 | + |
| 16 | + final GradleProject gradleProject |
| 17 | + |
| 18 | + TypeUsageMultiModuleProject() { |
| 19 | + this.gradleProject = build() |
| 20 | + } |
| 21 | + |
| 22 | + private GradleProject build() { |
| 23 | + return newGradleProjectBuilder() |
| 24 | + .withSubproject('app') { s -> |
| 25 | + s.sources = appSources |
| 26 | + s.withBuildScript { bs -> |
| 27 | + bs.plugins = kotlin |
| 28 | + bs.dependencies = [ |
| 29 | + project('implementation', ':core'), |
| 30 | + project('implementation', ':utils'), |
| 31 | + commonsCollections('implementation'), |
| 32 | + kotlinStdLib('implementation') |
| 33 | + ] |
| 34 | + } |
| 35 | + } |
| 36 | + .withSubproject('core') { s -> |
| 37 | + s.sources = coreSources |
| 38 | + s.withBuildScript { bs -> |
| 39 | + bs.plugins = kotlin |
| 40 | + bs.dependencies = [ |
| 41 | + project('implementation', ':utils'), |
| 42 | + kotlinStdLib('implementation') |
| 43 | + ] |
| 44 | + } |
| 45 | + } |
| 46 | + .withSubproject('utils') { s -> |
| 47 | + s.sources = utilsSources |
| 48 | + s.withBuildScript { bs -> |
| 49 | + bs.plugins = kotlin |
| 50 | + bs.dependencies = [ |
| 51 | + commonsIO('implementation'), |
| 52 | + kotlinStdLib('implementation') |
| 53 | + ] |
| 54 | + } |
| 55 | + } |
| 56 | + .write() |
| 57 | + } |
| 58 | + |
| 59 | + private appSources = [ |
| 60 | + new Source( |
| 61 | + SourceType.KOTLIN, "MainActivity", "com/example/app", |
| 62 | + """\ |
| 63 | + package com.example.app |
| 64 | +
|
| 65 | + import com.example.core.UserRepository |
| 66 | + import com.example.core.User |
| 67 | + import com.example.utils.Logger |
| 68 | + import org.apache.commons.collections4.bag.HashBag |
| 69 | +
|
| 70 | + class MainActivity { |
| 71 | + private val repository = UserRepository() |
| 72 | + private val logger = Logger() |
| 73 | + private val cache = HashBag<String>() |
| 74 | +
|
| 75 | + fun loadUsers() { |
| 76 | + val users = repository.getUsers() |
| 77 | + logger.log("Loaded \${users.size} users") |
| 78 | + users.forEach { cache.add(it.name) } |
| 79 | + } |
| 80 | + } |
| 81 | + """.stripIndent() |
| 82 | + ), |
| 83 | + new Source( |
| 84 | + SourceType.KOTLIN, "AppHelper", "com/example/app", |
| 85 | + """\ |
| 86 | + package com.example.app |
| 87 | +
|
| 88 | + internal class AppHelper { |
| 89 | + fun getMainActivity() = MainActivity() |
| 90 | + } |
| 91 | + """.stripIndent() |
| 92 | + ) |
| 93 | + ] |
| 94 | + |
| 95 | + private coreSources = [ |
| 96 | + new Source( |
| 97 | + SourceType.KOTLIN, "UserRepository", "com/example/core", |
| 98 | + """\ |
| 99 | + package com.example.core |
| 100 | +
|
| 101 | + import com.example.utils.Logger |
| 102 | +
|
| 103 | + class UserRepository { |
| 104 | + private val logger = Logger() |
| 105 | +
|
| 106 | + fun getUsers(): List<User> { |
| 107 | + logger.log("Fetching users") |
| 108 | + return listOf(User("Alice"), User("Bob")) |
| 109 | + } |
| 110 | + } |
| 111 | + """.stripIndent() |
| 112 | + ), |
| 113 | + new Source( |
| 114 | + SourceType.KOTLIN, "User", "com/example/core", |
| 115 | + """\ |
| 116 | + package com.example.core |
| 117 | +
|
| 118 | + data class User(val name: String) |
| 119 | + """.stripIndent() |
| 120 | + ), |
| 121 | + new Source( |
| 122 | + SourceType.KOTLIN, "CoreInternal", "com/example/core", |
| 123 | + """\ |
| 124 | + package com.example.core |
| 125 | +
|
| 126 | + internal class CoreInternal { |
| 127 | + fun createUser(name: String) = User(name) |
| 128 | + } |
| 129 | + """.stripIndent() |
| 130 | + ) |
| 131 | + ] |
| 132 | + |
| 133 | + private utilsSources = [ |
| 134 | + new Source( |
| 135 | + SourceType.KOTLIN, "Logger", "com/example/utils", |
| 136 | + """\ |
| 137 | + package com.example.utils |
| 138 | +
|
| 139 | + import org.apache.commons.io.FileUtils |
| 140 | + import java.io.File |
| 141 | +
|
| 142 | + class Logger { |
| 143 | + fun log(message: String) { |
| 144 | + FileUtils.write(File("log.txt"), message, "UTF-8", true) |
| 145 | + } |
| 146 | + } |
| 147 | + """.stripIndent() |
| 148 | + ), |
| 149 | + new Source( |
| 150 | + SourceType.KOTLIN, "UtilsHelper", "com/example/utils", |
| 151 | + """\ |
| 152 | + package com.example.utils |
| 153 | +
|
| 154 | + internal class UtilsHelper { |
| 155 | + fun createLogger() = Logger() |
| 156 | + } |
| 157 | + """.stripIndent() |
| 158 | + ) |
| 159 | + ] |
| 160 | + |
| 161 | + ProjectTypeUsage actualTypeUsageFor(String projectPath) { |
| 162 | + def typeUsage = gradleProject.singleArtifact(projectPath, |
| 163 | + com.autonomousapps.internal.OutputPathsKt.getTypeUsagePath('main')) |
| 164 | + def adapter = com.autonomousapps.internal.utils.MoshiUtils.MOSHI |
| 165 | + .adapter(com.autonomousapps.model.ProjectTypeUsage) |
| 166 | + return adapter.fromJson(typeUsage.asPath.text) |
| 167 | + } |
| 168 | +} |
0 commit comments