Skip to content

Commit

Permalink
Add source set configuration (#110)
Browse files Browse the repository at this point in the history
Решил вмёрджить, чтобы @ussernamenikita попал в контрибьюторы проекта. Спасибо за помощь 🙏
  • Loading branch information
ussernamenikita committed Jun 16, 2024
1 parent 8542879 commit 02bf395
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 48 deletions.
4 changes: 4 additions & 0 deletions plugins/hh-geminio/docs/en/RECIPE_CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ optionalParams:
predefinedFeatures:
- enableModuleCreationParams:
defaultPackageNamePrefix: ru.hh.test
# optional
defaultSourceCodeFolderName: java
# optional
defaultSourceSetName: main

widgets:
- stringParameter:
Expand Down
10 changes: 10 additions & 0 deletions plugins/hh-geminio/docs/en/recipe_content/PREDEFINED_FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ predefinedFeatures:

Here, `ru.hh.test` - new value of your custom package name.

Similarly, you can set the `sourceSet` and the folder name (usually kotlin/java) inside the sourceSet:

```yaml
predefinedFeatures:
- enableModuleCreationParams:
defaultPackageNamePrefix: ru.hh.test
defaultSourceCodeFolderName: java
defaultSourceSetName: main
```

---

[Back to `recipe.yaml` content](../RECIPE_CONTENT.md)
4 changes: 4 additions & 0 deletions plugins/hh-geminio/docs/ru/RECIPE_CONTENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ optionalParams:
predefinedFeatures:
- enableModuleCreationParams:
defaultPackageNamePrefix: ru.hh.test
# optional
defaultSourceCodeFolderName: java
# optional
defaultSourceSetName: main

widgets:
- stringParameter:
Expand Down
10 changes: 10 additions & 0 deletions plugins/hh-geminio/docs/ru/recipe_content/PREDEFINED_FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ predefinedFeatures:

Здесь `ru.hh.test` - значение нужного вам custom-пакета.

Аналогично можно задать `sourceSet` и имя папки ( обычно kotlin/java ) внутри sourceSet:

```yaml
predefinedFeatures:
- enableModuleCreationParams:
defaultPackageNamePrefix: ru.hh.test
defaultSourceSetName: main
defaultSourceCodeFolderName: java
```

---

[Обратно к устройству "рецептов"](../RECIPE_CONTENT.md)
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ import java.io.File
*/
class GeminioAndroidModulePaths(
private val basePath: String,
private val moduleName: String
private val moduleName: String,
sourceSetConfig: GeminioSourceSetConfig,
) : AndroidModulePaths {

private companion object {
const val LOG_TAG = "GeminioAndroidModulePaths"

const val MANIFEST_FOLDER_PATH = "src/main"
const val UI_TESTS_FOLDER_PATH = "src/androidTest"
const val UNIT_TESTS_FOLDER_PATH = "src/test"
const val RES_FOLDER_PATH = "src/main/res"
const val AIDL_FOLDER_PATH = "src/main/aidl"
const val SOURCES_FOLDER_PATH = "src/main/java"
}

private val sourceSet = sourceSetConfig.sourceSet
private val sourceCodeFolderName = sourceSetConfig.sourceCodeFolderName

private val manifestFolderPath = "src/$sourceSet"
private val uiTestsFolderPath = "src/androidTest"
private val unitTestsFolderPath = "src/test"
private val resFolderPath = "src/$sourceSet/res"
private val aidlFolderPath = "src/$sourceSet/aidl"
private val sourcesFolderPath = "src/$sourceSet/$sourceCodeFolderName"
private val moduleRootPath: String get() = "$basePath/$moduleName"

override val manifestDirectory: File
get() {
val manifestDirectoryPath = "$moduleRootPath/$MANIFEST_FOLDER_PATH"
val manifestDirectoryPath = "$moduleRootPath/$manifestFolderPath"

log("getter for manifestDirectory | path: $manifestDirectoryPath")
return File(manifestDirectoryPath)
Expand All @@ -45,36 +48,36 @@ class GeminioAndroidModulePaths(

override val resDirectories: List<File>
get() {
val resDirectoryPath = "$moduleRootPath/$RES_FOLDER_PATH"
val resDirectoryPath = "$moduleRootPath/$resFolderPath"

log("getter for resDirectories | path: $resDirectoryPath")
return listOf(File(resDirectoryPath))
}

override fun getAidlDirectory(packageName: String?): File {
val aidlDirectoryPath = "$moduleRootPath/$AIDL_FOLDER_PATH" + packageName?.toSlashedFilePath().orEmpty()
val aidlDirectoryPath = "$moduleRootPath/$aidlFolderPath" + packageName?.toSlashedFilePath().orEmpty()

log("getter for getAidlDirectory(packageName: $packageName) | path: $aidlDirectoryPath")
return File(aidlDirectoryPath)
}

override fun getSrcDirectory(packageName: String?): File {
val srcDirectoryPath = "$moduleRootPath/$SOURCES_FOLDER_PATH" + packageName?.toSlashedFilePath().orEmpty()
val srcDirectoryPath = "$moduleRootPath/$sourcesFolderPath" + packageName?.toSlashedFilePath().orEmpty()

log("getter for getSrcDirectory(packageName: $packageName) | path: $srcDirectoryPath")
return File(srcDirectoryPath)
}

override fun getTestDirectory(packageName: String?): File {
val testDirectoryPath = "$moduleRootPath/$UI_TESTS_FOLDER_PATH" + packageName?.toSlashedFilePath().orEmpty()
val testDirectoryPath = "$moduleRootPath/$uiTestsFolderPath" + packageName?.toSlashedFilePath().orEmpty()

log("getter for getTestDirectory(packageName: $packageName) | path: $testDirectoryPath")
return File(testDirectoryPath)
}

override fun getUnitTestDirectory(packageName: String?): File {
val unitTestDirectoryPath =
"$moduleRootPath/$UNIT_TESTS_FOLDER_PATH" + packageName?.toSlashedFilePath().orEmpty()
"$moduleRootPath/$unitTestsFolderPath" + packageName?.toSlashedFilePath().orEmpty()

log("getter for getUnitTestDirectory(packageName: $packageName) | path: $unitTestDirectoryPath")
return File(unitTestDirectoryPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.hh.plugins.geminio.models

data class GeminioSourceSetConfig(
val sourceSet: String,
val sourceCodeFolderName: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.jetbrains.android.facet.AndroidFacet
import ru.hh.plugins.extensions.toSlashedFilePath
import ru.hh.plugins.geminio.models.GeminioAndroidModulePaths
import ru.hh.plugins.geminio.models.GeminioConfigureTemplateStepModel
import ru.hh.plugins.geminio.models.GeminioSourceSetConfig
import ru.hh.plugins.geminio.services.StubProjectSyncInvoker
import ru.hh.plugins.logger.HHLogger
import java.io.File
Expand All @@ -30,6 +31,8 @@ class ConfigureTemplateParametersStepFactory(

const val STUB_MODULE_NAME = "stub_module_name"
const val STUB_PARENT_MODULE_NAME = "stub_parent_module_name"
const val STUB_SOURCE_SET = "stub_source_set"
const val STUB_SOURCE_CODE_FOLDER_NAME = "stub_source_code_folder_name"
}

fun createFromAndroidFacet(
Expand Down Expand Up @@ -90,7 +93,11 @@ class ConfigureTemplateParametersStepFactory(
name = NAMED_MODULE_TEMPLATE_NAME,
paths = GeminioAndroidModulePaths(
basePath = directoryPath,
moduleName = STUB_MODULE_NAME
moduleName = STUB_MODULE_NAME,
sourceSetConfig = GeminioSourceSetConfig(
sourceSet = STUB_SOURCE_SET,
sourceCodeFolderName = STUB_SOURCE_CODE_FOLDER_NAME,
),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.android.tools.idea.wizard.template.ViewBindingSupport
import com.intellij.openapi.project.Project
import ru.hh.plugins.geminio.models.GeminioAndroidModulePaths
import ru.hh.plugins.geminio.models.GeminioRecipeExecutorModel
import ru.hh.plugins.geminio.models.GeminioSourceSetConfig
import ru.hh.plugins.geminio.sdk.models.GeminioTemplateData
import java.io.File

Expand All @@ -41,12 +42,18 @@ class GeminioRecipeExecutorFactoryService(
): GeminioRecipeExecutorModel {
val moduleName = geminioTemplateData.getModuleName()
val packageName = geminioTemplateData.getPackageName()
val sourceSet = geminioTemplateData.getSourceSet()
val sourceCodeFolderName = geminioTemplateData.getSourceCodeFolderName()

val moduleTemplateData = createModuleTemplateData(
project = project,
directoryPath = newModuleRootDirectoryPath,
moduleName = moduleName,
packageName = packageName
packageName = packageName,
sourceSetConfig = GeminioSourceSetConfig(
sourceSet = sourceSet,
sourceCodeFolderName = sourceCodeFolderName,
),
)

val renderingContext = RenderingContext(
Expand All @@ -71,28 +78,22 @@ class GeminioRecipeExecutorFactoryService(
project: Project,
directoryPath: String,
moduleName: String,
packageName: String
packageName: String,
sourceSetConfig: GeminioSourceSetConfig,
): ModuleTemplateData {
val projectTemplateDataBuilder = ProjectTemplateDataBuilder(isNewProject = false)
.also { builder ->
builder.applicationPackage = packageName
builder.language = Language.Kotlin

// Starting from Android Studio Iguana this property is required
builder.agpVersion = AgpVersions.latestKnown

builder.setProjectDefaults(project)
}

return ModuleTemplateDataBuilder(
projectTemplateDataBuilder = projectTemplateDataBuilder,
projectTemplateDataBuilder = newProjectTemplateDataBuilder(
project,
packageName
),
isNewModule = true,
viewBindingSupport = ViewBindingSupport.NOT_SUPPORTED
).also { builder ->
builder.setModuleRoots(
paths = GeminioAndroidModulePaths(
basePath = directoryPath,
moduleName = moduleName
moduleName = moduleName,
sourceSetConfig = sourceSetConfig,
),
projectPath = project.basePath!!,
moduleName = ":$moduleName",
Expand All @@ -119,6 +120,20 @@ class GeminioRecipeExecutorFactoryService(
}.build()
}

private fun newProjectTemplateDataBuilder(
project: Project,
packageName: String
): ProjectTemplateDataBuilder = ProjectTemplateDataBuilder(isNewProject = false)
.also { builder ->
builder.applicationPackage = packageName
builder.language = Language.Kotlin

// Starting from Android Studio Iguana this property is required
builder.agpVersion = AgpVersions.latestKnown

builder.setProjectDefaults(project)
}

private fun createStubApiVersion(): ApiVersion {
return ApiVersion(STUB_API_VERSION, STUB_API_VERSION_STRING)
}
Expand All @@ -130,4 +145,12 @@ class GeminioRecipeExecutorFactoryService(
private fun GeminioTemplateData.getPackageName(): String {
return existingParametersMap[geminioIds.newModulePackageNameParameterId]!!.value as String
}

private fun GeminioTemplateData.getSourceCodeFolderName(): String {
return existingParametersMap[geminioIds.newModuleSourceCodeFolderParameterId]!!.value as String
}

private fun GeminioTemplateData.getSourceSet(): String {
return existingParametersMap[geminioIds.newModuleSourceSetParameterId]!!.value as String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ internal object GeminioSdkConstants {
const val FEATURE_FORMATTED_MODULE_NAME_PARAMETER_ID = "__formattedModuleName"
const val FEATURE_PACKAGE_NAME_PARAMETER_ID = "__packageName"
const val FEATURE_APPLICATIONS_MODULES_PARAMETER_ID = "__applicationsModules"
const val FEATURE_SOURCE_SET_PARAMETER_ID = "__sourceSet"
const val FEATURE_DEFAULT_SOURCE_CODE_FOLDER_PARAMETER_ID = "__srcCodeFolder"

const val GLOBALS_SHOW_HIDDEN_VALUES_ID = "__showHiddenGlobals"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ data class GeminioTemplateParametersIds(
val newModuleNameParameterId: String,
val newModulePackageNameParameterId: String,
val newApplicationModulesParameterId: String,
val newModuleSourceSetParameterId: String,
val newModuleSourceCodeFolderParameterId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ package ru.hh.plugins.geminio.sdk.recipe.models.predefined

sealed class PredefinedFeatureParameter {
data class ModuleCreationParameter(
val defaultPackageNamePrefix: String = "ru.hh"
) : PredefinedFeatureParameter()
val defaultPackageNamePrefix: String = DEFAULT_PACKAGE_NAME_PREFIX,
val defaultSourceSet: String = DEFAULT_SOURCE_SET_NAME,
val defaultSourceCodeFolderName: String = DEFAULT_SOURCE_CODE_FOLDER_NAME,
) : PredefinedFeatureParameter() {
companion object {
const val DEFAULT_PACKAGE_NAME_PREFIX = "ru.hh"
const val DEFAULT_SOURCE_SET_NAME = "main"
const val DEFAULT_SOURCE_CODE_FOLDER_NAME = "java"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ package ru.hh.plugins.geminio.sdk.recipe.parsers.predefined

import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeature
import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeatureParameter
import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeatureParameter.ModuleCreationParameter.Companion.DEFAULT_PACKAGE_NAME_PREFIX
import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeatureParameter.ModuleCreationParameter.Companion.DEFAULT_SOURCE_CODE_FOLDER_NAME
import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeatureParameter.ModuleCreationParameter.Companion.DEFAULT_SOURCE_SET_NAME
import ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeaturesSection
import ru.hh.plugins.geminio.sdk.recipe.parsers.ParsersErrorsFactory.sectionUnknownEnumKeyErrorMessage

private const val KEY_PREDEFINED_FEATURES_SECTION = "predefinedFeatures"
private const val KEY_PARAMETER_PREDEFINE_PACKAGE_NAME = "defaultPackageNamePrefix"
private const val KEY_PARAMETER_SOURCE_NAME = "defaultSourceSetName"
private const val KEY_PARAMETER_SOURCE_CODE_FOLDER_NAME = "defaultSourceCodeFolderName"

/**
* Parser from YAML to [ru.hh.plugins.geminio.sdk.recipe.models.predefined.PredefinedFeaturesSection].
Expand Down Expand Up @@ -62,13 +67,13 @@ private fun Map<String, Any>.parseParameter(
return when (featureType) {
PredefinedFeature.ENABLE_MODULE_CREATION_PARAMS -> {
val defaultPackageNamePrefix = this[KEY_PARAMETER_PREDEFINE_PACKAGE_NAME] as? String
if (defaultPackageNamePrefix == null) {
PredefinedFeatureParameter.ModuleCreationParameter()
} else {
PredefinedFeatureParameter.ModuleCreationParameter(
defaultPackageNamePrefix = defaultPackageNamePrefix
)
}
val defaultSourceSet = this[KEY_PARAMETER_SOURCE_NAME] as? String
val codeFolderName = this[KEY_PARAMETER_SOURCE_CODE_FOLDER_NAME] as? String
PredefinedFeatureParameter.ModuleCreationParameter(
defaultPackageNamePrefix = defaultPackageNamePrefix ?: DEFAULT_PACKAGE_NAME_PREFIX,
defaultSourceSet = defaultSourceSet ?: DEFAULT_SOURCE_SET_NAME,
defaultSourceCodeFolderName = codeFolderName ?: DEFAULT_SOURCE_CODE_FOLDER_NAME,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ internal fun GeminioRecipe.toGeminioTemplateData(project: Project, targetDirecto
newModuleNameParameterId = GeminioSdkConstants.FEATURE_MODULE_NAME_PARAMETER_ID,
newModulePackageNameParameterId = GeminioSdkConstants.FEATURE_PACKAGE_NAME_PARAMETER_ID,
newApplicationModulesParameterId = GeminioSdkConstants.FEATURE_APPLICATIONS_MODULES_PARAMETER_ID,
newModuleSourceSetParameterId = GeminioSdkConstants.FEATURE_SOURCE_SET_PARAMETER_ID,
newModuleSourceCodeFolderParameterId = GeminioSdkConstants.FEATURE_DEFAULT_SOURCE_CODE_FOLDER_PARAMETER_ID,
),
paramsStore = paramsStore
)
Expand Down
Loading

0 comments on commit 02bf395

Please sign in to comment.