Skip to content

Commit

Permalink
Added mirroring handling
Browse files Browse the repository at this point in the history
  • Loading branch information
debdasha committed Jun 20, 2019
1 parent 619e3a0 commit 726eecd
Show file tree
Hide file tree
Showing 86 changed files with 171 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ru.surfstudio.android.build.ConfigInfoProvider

apply from: 'buildTasks.gradle'
apply from: 'buildSrc/buildTasks.gradle'

allprojects {
repositories {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/androidModule.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ android {
minSdkVersion project.ext.libMinSdkVersion
targetSdkVersion project.ext.targetSdkVersion
versionCode project.ext.moduleVersionCode
versionName Components.getModuleVersion(project)
versionName Components.getModuleVersion(project.name)
testBuildType "debug"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/androidSample.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
minSdkVersion project.ext.sampleMinSdkVersion
targetSdkVersion project.ext.targetSdkVersion
versionCode project.ext.moduleVersionCode
versionName Components.getModuleVersion(project)
versionName Components.getModuleVersion(project.name)
multiDexEnabled true
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions buildSrc/deploy.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ext.configureDeploy = { context ->

def artifactGroupId = ArtifactoryConfig.ANDROID_STANDARD_GROUP_ID
def artifactName = Components.getArtifactName(project.name)
def artifactVersion = Components.getModuleVersion(project)
def artifactVersion = Components.getModuleVersion(project.name)

pom.groupId = artifactGroupId
pom.artifactId = artifactName
Expand All @@ -57,7 +57,7 @@ ext.configureDeploy = { context ->
dependencies {
Components.getAndroidStandardDependencies(project.name).each {
def depArtifactName = it.getArtifactName()
def depArtifactVersion = Components.getModuleVersion(project.rootProject.project(it.getName()))
def depArtifactVersion = Components.getModuleVersion(it.getName())
dependency {
groupId artifactGroupId
artifactId depArtifactName
Expand Down
File renamed without changes.
23 changes: 18 additions & 5 deletions buildSrc/src/main/kotlin/ru/surfstudio/android/build/Components.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ru.surfstudio.android.build

import org.gradle.api.Project
import org.gradle.api.GradleException
import ru.surfstudio.android.build.exceptions.ComponentNotFoundForStandardDependencyException
import ru.surfstudio.android.build.exceptions.LibraryNotFoundException
import ru.surfstudio.android.build.model.Component
import ru.surfstudio.android.build.model.dependency.Dependency
import ru.surfstudio.android.build.model.json.ComponentJson
import ru.surfstudio.android.build.model.module.Library
import ru.surfstudio.android.build.model.module.Module
import ru.surfstudio.android.build.model.json.ComponentJson
import ru.surfstudio.android.build.utils.EMPTY_STRING
import ru.surfstudio.android.build.utils.createCompositeVersion

Expand Down Expand Up @@ -36,7 +36,15 @@ object Components {
* Get project's module
*/
@JvmStatic
fun getModules(): List<Module> = value.flatMap(Component::getModules)
fun getModules(): List<Module> {
val mirrorComponentName = GradlePropertiesManager.getMirrorComponentName()
return if (!GradlePropertiesManager.isCurrentComponentAMirror()) {
value.flatMap(Component::getModules)
} else {
val mirrorComponent = getMirrorComponentByName(mirrorComponentName)
mirrorComponent.libraries + mirrorComponent.samples
}
}

/**
* Get moduleVersionName
Expand All @@ -48,11 +56,10 @@ object Components {
* 4. X.Y.Z-alpha.unstable_version-projectPostfix.projectVersion - component is unstable, projectPostfix isn't empty
*/
@JvmStatic
fun getModuleVersion(gradleProject: Project): String {
fun getModuleVersion(moduleName: String): String {
if (value.isEmpty()) return EMPTY_STRING
if (value.any { it.projectVersion.isEmpty() }) configModuleVersions()

val moduleName = gradleProject.name
val component = value.find { it.getModules().map(Module::name).contains(moduleName) }

return component?.projectVersion ?: EMPTY_STRING
Expand Down Expand Up @@ -130,4 +137,10 @@ object Components {
)
}
}

private fun getMirrorComponentByName(mirrorComponentName: String): Component {
val mirrorComponent = value.firstOrNull { it.name == mirrorComponentName }

return mirrorComponent ?: throw GradleException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ru.surfstudio.android.build.model.json.ConfigInfoJson
import ru.surfstudio.android.build.utils.JsonHelper
import java.io.File

private const val CONFIG_INFO_JSON_FILE_PATH = "configInfo.json"
private const val CONFIG_INFO_JSON_FILE_PATH = "buildSrc/projectConfiguration.json"

/**
* Class to work with project configInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.surfstudio.android.build

import org.gradle.api.Project
import ru.surfstudio.android.build.artifactory.ArtifactoryConfig
import ru.surfstudio.android.build.model.dependency.Dependency

private const val LIBRARY_VERSIONS_KEY = "libraryVersions"
Expand All @@ -24,7 +25,6 @@ object DependencyConfigurator {
if (libraryVersions.isEmpty()) {
libraryVersions = project.property(LIBRARY_VERSIONS_KEY) as Map<String, String>
}

Components.value.forEach { component ->
component.libraries
.find { it.name == project.name }
Expand All @@ -37,13 +37,20 @@ object DependencyConfigurator {

/**
* Add dependency to project with "implementation"
* If current project is not mirror and dependency is not contained in current project then
* add in with artifactory name else add locally
*
* @param project - project
* @param dep - dependency name
* @param dependencyName - dependency name
*/
@JvmStatic
fun projectImplementation(project: Project, gradleProjectName: String) {
addDependency(project, IMPLEMENTATION_DEP_TYPE, project.rootProject.project(gradleProjectName))
fun projectImplementation(project: Project, dependencyName: String) {
project.properties.get("")
if (GradlePropertiesManager.isCurrentComponentAMirror() && !isProjectIncluded(project, dependencyName)) {
addDependency(project, IMPLEMENTATION_DEP_TYPE, getDependencyArtifactoryName(dependencyName))
} else {
addDependency(project, IMPLEMENTATION_DEP_TYPE, project.rootProject.project(dependencyName))
}
}

/**
Expand Down Expand Up @@ -76,7 +83,11 @@ object DependencyConfigurator {

private fun addAndroidStandardDependencies(project: Project, dependencies: List<Dependency>) {
dependencies.forEach {
addDependency(project, it.type, project.rootProject.project(":${it.name}"))
if (GradlePropertiesManager.isCurrentComponentAMirror() && !isProjectIncluded(project, it.name)) {
addDependency(project, IMPLEMENTATION_DEP_TYPE, getDependencyArtifactoryName(it.name))
} else {
addDependency(project, IMPLEMENTATION_DEP_TYPE, project.rootProject.project(":${it.name}"))
}
}
}

Expand All @@ -97,4 +108,26 @@ object DependencyConfigurator {
private fun getDependencyNameWithVersion(shortDependencyName: String): String {
return "$shortDependencyName:${libraryVersions[shortDependencyName]}"
}

/**
* @param dependencyName dependency name for creating artifactory name
*
* @return artifactory name for dependency
*/
private fun getDependencyArtifactoryName(dependencyName: String): String {
val group = ArtifactoryConfig.ANDROID_STANDARD_GROUP_ID
val name = Components.getArtifactName(dependencyName)
val version = Components.getModuleVersion(dependencyName)
return "$group:$name:$version"
}

/**
* checks if project with [gradleProjectName] included in current project [project]
*
* @return true if included
*/
private fun isProjectIncluded(project: Project, gradleProjectName: String): Boolean {
val allProjects = project.rootProject.allprojects
return allProjects.map { it.name }.contains(gradleProjectName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.surfstudio.android.build

import org.gradle.api.GradleException
import ru.surfstudio.android.build.exceptions.CantReadFileException
import ru.surfstudio.android.build.exceptions.NoPropertyDefinedInFileException
import ru.surfstudio.android.build.utils.EMPTY_STRING
import java.io.File
import java.io.FileInputStream
import java.util.*

const val GRADLE_PROPERTIES_FILE_PATH = "mirror.properties"
const val MIRROR_COMPONENT_NAME = "surf.mirrorComponentName"

/**
* Loading properties and caching them from [GRADLE_PROPERTIES_FILE_PATH]
* Created for loading only once during settings.gradle execute and then use everywhere
*/
object GradlePropertiesManager {
private var componentMirrorName: String = EMPTY_STRING

init {
loadMirrorComponentName()
}

/**
* get current mirror component name
*
* @return component mirror name
*/
fun getMirrorComponentName(): String {
return componentMirrorName
}

/**
* check if current component is mirror
*
* @return true if mirror
*/
fun isCurrentComponentAMirror() = componentMirrorName != EMPTY_STRING

/**
* gets component mirror name as property from file [GRADLE_PROPERTIES_FILE_PATH]
* if there is no such file then current repository is not mirror
*/
private fun loadMirrorComponentName() {
val props = Properties()
val propFile = File(GRADLE_PROPERTIES_FILE_PATH)
if (!propFile.exists()) return
if (propFile.canRead()) {
props.load(FileInputStream(propFile))
if (props.containsKey(MIRROR_COMPONENT_NAME)) {
componentMirrorName = props[MIRROR_COMPONENT_NAME] as String
} else {
throw NoPropertyDefinedInFileException(MIRROR_COMPONENT_NAME, GRADLE_PROPERTIES_FILE_PATH)
}
} else {
throw CantReadFileException(GRADLE_PROPERTIES_FILE_PATH)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ru.surfstudio.android.build

import ru.surfstudio.android.build.exceptions.ComponentDirectoryNotExistException
import ru.surfstudio.android.build.exceptions.ComponentNotFoundException
import ru.surfstudio.android.build.exceptions.LibraryDirectoryNotExistException
import ru.surfstudio.android.build.exceptions.SampleDirectoryNotExistException
import ru.surfstudio.android.build.model.json.ComponentJson
import ru.surfstudio.android.build.utils.COMPONENTS_JSON_FILE_PATH
import java.io.File
import ru.surfstudio.android.build.utils.JsonHelper
import java.io.File

object Initializator {

Expand All @@ -16,33 +17,47 @@ object Initializator {
@JvmStatic
fun init() {
val jsonComponents = JsonHelper.parseComponentsJson(COMPONENTS_JSON_FILE_PATH)
checkComponentsFolders(jsonComponents)
if (GradlePropertiesManager.isCurrentComponentAMirror()) {
checkOnlyMirrorComponentFolder(jsonComponents)
} else {
checkAllComponentsFolders(jsonComponents)
}
Components.init(jsonComponents)
}

private fun checkOnlyMirrorComponentFolder(jsonComponents: List<ComponentJson>) {
val componentMirrorName = GradlePropertiesManager.getMirrorComponentName()
val component = jsonComponents.firstOrNull { it.id == componentMirrorName }
?: throw ComponentNotFoundException(componentMirrorName)
checkComponentFolders(component)
}

/**
* Check value directories for exist
*/
private fun checkComponentsFolders(componentJsons: List<ComponentJson>) {
private fun checkAllComponentsFolders(componentJsons: List<ComponentJson>) {
componentJsons.forEach { component ->
checkComponentFolders(component)
}
}

//check component "dir"
if (!File(component.dir).exists()) {
throw ComponentDirectoryNotExistException(component.id)
}
private fun checkComponentFolders(component: ComponentJson) {
//check component "dir"
if (!File(component.dir).exists()) {
throw ComponentDirectoryNotExistException(component.id)
}

//check libs
component.libs.forEach { lib ->
if (!File("${component.dir}/${lib.dir}").exists()) {
throw LibraryDirectoryNotExistException(component.id, lib.name, lib.dir)
}
//check libs
component.libs.forEach { lib ->
if (!File("${component.dir}/${lib.dir}").exists()) {
throw LibraryDirectoryNotExistException(component.id, lib.name, lib.dir)
}
}

//check samples
component.samples.forEach { sample ->
if (!File("${component.dir}/${sample.dir}").exists()) {
throw SampleDirectoryNotExistException(component.id, sample.name)
}
//check samples
component.samples.forEach { sample ->
if (!File("${component.dir}/${sample.dir}").exists()) {
throw SampleDirectoryNotExistException(component.id, sample.name)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.surfstudio.android.build.exceptions

import org.gradle.api.GradleException

class CantReadFileException(fileName: String) : GradleException(
"Can`t read file $fileName"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.surfstudio.android.build.exceptions

import org.gradle.api.GradleException

class NoPropertyDefinedInFileException (
propertyName: String,
fileName: String
) : GradleException(
"No property $propertyName found in file $fileName"
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 726eecd

Please sign in to comment.