diff --git a/checkstyle/configuration/google_checks.xml b/checkstyle/configuration/google_checks.xml deleted file mode 100755 index 441f1af..0000000 --- a/checkstyle/configuration/google_checks.xml +++ /dev/null @@ -1,210 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/checkstyle/configuration/touchin_checkstyle.xml b/checkstyle/configuration/touchin_checkstyle.xml deleted file mode 100755 index b36d47f..0000000 --- a/checkstyle/configuration/touchin_checkstyle.xml +++ /dev/null @@ -1,339 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/findbugs/filters/findbugs-filter.xml b/findbugs/filters/findbugs-filter.xml deleted file mode 100644 index bb462be..0000000 --- a/findbugs/filters/findbugs-filter.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/gradle/apiGenerator.gradle b/gradle/apiGenerator.gradle deleted file mode 100644 index 9564f5b..0000000 --- a/gradle/apiGenerator.gradle +++ /dev/null @@ -1,46 +0,0 @@ -repositories { - maven { - url 'https://dl.bintray.com/touchin/touchin-tools' - metadataSources { - artifact() - } - } -} - -configurations { - apigenerator -} - -dependencies { - apigenerator 'ru.touchin:api-generator:1.4.0-beta9' -} - -android.libraryVariants.all { variant -> - final File generatedModelsDirectory = new File("${project.buildDir}/generated/source/models/${variant.dirName}") - - def generateJsonModelsTask = tasks.create("apiGenerator${variant.name}") doLast { - javaexec { - main = "-jar" - workingDir = file("${rootDir}") - args = [ - configurations.apigenerator.asPath, - "generate-client-code", - "--output-language", - "KOTLIN", - "--android-calendar-library", - "JAVA_TIME", - "--specification-path", - rootProject.extensions.findByName("pathToApiSchemes"), - "--kotlin-methods-generation-mode", - "COROUTINE", - "--output-path", - "${generatedModelsDirectory.path}", - "--package-name", - "${rootProject.extensions.findByName("applicationId") ?: applicationId}" - ] - } - } - - generateJsonModelsTask.description = 'Generates Java classes for JSON models' - variant.registerJavaGeneratingTask generateJsonModelsTask, generatedModelsDirectory -} diff --git a/gradle/apiGeneratorKotlinServer.gradle b/gradle/apiGeneratorKotlinServer.gradle deleted file mode 100644 index 66676b5..0000000 --- a/gradle/apiGeneratorKotlinServer.gradle +++ /dev/null @@ -1,38 +0,0 @@ -repositories { - maven { - url "https://dl.bintray.com/touchin/touchin-tools" - metadataSources { - artifact() - } - } -} - -configurations { - apigeneratorKotlinServer -} - -dependencies { - apigeneratorKotlinServer 'ru.touchin:api-generator:1.4.0-beta1' -} - -task generateApiModelsKotlinServer doLast { - javaexec { - main = "-jar" - workingDir = file("${rootDir}") - args = [ - configurations.apigeneratorKotlinServer.asPath, - "generate-client-code", - "--output-language", - "KOTLIN_SERVER", - "--specification-path", - rootProject.extensions.findByName("pathToApiSchemes"), - "--output-path", - "${rootDir}/src/main/kotlin", - "--package-name", - rootProject.extensions.findByName("apiPackageName"), - "--recreate_output_dirs", - false - ] - } -} - diff --git a/gradle/commonStaticAnalysis.gradle b/gradle/commonStaticAnalysis.gradle deleted file mode 100644 index b929257..0000000 --- a/gradle/commonStaticAnalysis.gradle +++ /dev/null @@ -1,233 +0,0 @@ -apply plugin: 'cpd' -apply plugin: 'io.gitlab.arturbosch.detekt' - -def getCpdTask -def getLintTask -def getKotlinDetektTasks - -def appendError -def appendCpdErrors -def appendKotlinErrors -def appendLintErrors - -repositories { - maven { url "http://dl.bintray.com/touchin/touchin-tools" } -} - -configurations { - pngtastic -} - -cpd { - skipLexicalErrors = true -} - -import org.apache.tools.ant.taskdefs.condition.Os - -ext.getIdeaFormatTask = { isAndroidProject, sources -> - def ideaPath = System.getenv("IDEA_HOME") - if (ideaPath == null) { - return tasks.create((isAndroidProject ? "android" : "server") + "donothing") - } - return tasks.create((isAndroidProject ? "android" : "server") + "IdeaFormat_$project.name", Exec) { - def inspectionPath - def params = ["-r", "-mask", "*.java,*.kt,*.xml"] - for (String source : sources) { - params.add(source) - } - - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - inspectionPath = ['cmd', '/c', "\"${ideaPath}\\bin\\format.bat\" ${params.join(" ")}"] - } else { - inspectionPath = ["$ideaPath/bin/format.sh"] - } - commandLine inspectionPath - if (!Os.isFamily(Os.FAMILY_WINDOWS)) { - args = params - } - } -} - -ext.getStaticAnalysisTaskNames = { isAndroidProject, sources, buildVariant -> - def tasksNames = new ArrayList() - try { - tasksNames.add(getCpdTask(isAndroidProject, sources)) - tasksNames.addAll(getKotlinDetektTasks()) - if (isAndroidProject) { - tasksNames.add(getLintTask(buildVariant)) - } - } catch (Exception exception) { - println(exception.toString()) - } - return tasksNames -} - -ext.generateReport = { isAndroidProject -> - StringBuilder consoleReport = new StringBuilder() - consoleReport.append("STATIC ANALYSIS RESULTS:") - def count = 0 - - def previousCount = count - count = appendCpdErrors(count, new File("${project.buildDir}/reports/cpd.xml")) - if (count - previousCount > 0) { - consoleReport.append("\nCPD: FAILED (" + (count - previousCount) + " errors)") - } else { - consoleReport.append("\nCPD: PASSED") - } - - previousCount = count - subprojects.forEach { subproject -> - def reportFile = new File("${rootProject.buildDir}/reports/kotlin-detekt-${subproject.name}.xml") - if (reportFile.exists()) { - count = appendKotlinErrors(count, reportFile).toInteger() - } - } - if (count - previousCount > 0) { - consoleReport.append("\nKotlin-detekt: FAILED (" + (count - previousCount) + " errors)") - } else { - consoleReport.append("\nKotlin-detekt: PASSED") - } - - if (isAndroidProject) { - previousCount = count - count = appendLintErrors(count, new File("${rootProject.buildDir}/reports/lint_report.xml")) - if (count - previousCount > 0) { - consoleReport.append("\nLint: FAILED (" + (count - previousCount) + " errors)") - } else { - consoleReport.append("\nLint: PASSED") - } - } - - if (count > 0) { - consoleReport.append("\nOverall: FAILED (" + count + " errors)") - throw new Exception(consoleReport.toString()) - } else { - consoleReport.append("\nOverall: PASSED") - println(consoleReport.toString()) - } -} - -appendError = { number, analyzer, file, line, errorId, errorLink, description -> - println("$number. $analyzer : $description ($errorId)\n\tat $file: $line") -} - -appendKotlinErrors = { count, checkstyleFile -> - def rootNode = new XmlParser().parse(checkstyleFile) - for (def fileNode : rootNode.children()) { - if (!fileNode.name().equals("file")) { - continue - } - - for (def errorNode : fileNode.children()) { - if (!errorNode.name().equals("error")) { - continue - } - count++ - - appendError(count, "Detekt", fileNode.attribute("name"), errorNode.attribute("line"), errorNode.attribute("source"), "", errorNode.attribute("message")) - } - } - return count -} - -appendCpdErrors = { count, cpdFile -> - def rootNode = new XmlParser().parse(cpdFile) - for (def duplicationNode : rootNode.children()) { - if (!duplicationNode.name().equals("duplication")) { - continue - } - count++ - - def duplicationIndex = 0 - - String duplicationPoints = "" - for (def filePointNode : duplicationNode.children()) { - if (filePointNode.name().equals("file")) { - def file = filePointNode.attribute("path") - def line = filePointNode.attribute("line") - duplicationPoints += "\n " + file + ":" + line - duplicationIndex++ - } - } - println("$count CPD: code duplication $duplicationPoints") - } - return count -} - -appendLintErrors = { count, lintFile -> - def rootNode = new XmlParser().parse(lintFile) - for (def issueNode : rootNode.children()) { - if (!issueNode.name().equals("issue") - || !issueNode.attribute("severity").equals("Error")) { - continue - } - for (def locationNode : issueNode.children()) { - if (!locationNode.name().equals("location")) { - continue - } - count++ - appendError(count, "Lint", locationNode.attribute("file"), locationNode.attribute("line"), - issueNode.attribute("id"), issueNode.attribute("explanation"), issueNode.attribute("message")) - } - } - return count -} - -getCpdTask = { isAndroidProject, sources -> - def taskName = (isAndroidProject ? "android" : "server") + "cpd_${rootProject.name}" - def task = tasks.findByName(taskName) - if (task == null) { - task = tasks.create(taskName, tasks.findByName('cpdCheck').getClass().getSuperclass()) { - minimumTokenCount = 60 - source = files(sources) - ignoreFailures = true - reports { - xml { - enabled = true - destination = file("${rootProject.buildDir}/reports/cpd.xml") - } - } - } - } - return task.path -} - -getLintTask = { buildVariant -> - def appProject = subprojects.find { it.plugins.hasPlugin("com.android.application") } - def lintTaskPath - if (buildVariant != null) { - lintTaskPath = ":${appProject.name}:lint${buildVariant.name.capitalize()}" - } else { - def lintDebugTasks = appProject.tasks.matching { it.getName().contains("lint") && it.getName().contains("Debug") } - lintTaskPath = lintDebugTasks.first().path - } - if (lintTaskPath == null) { - throw IllegalStateException("Unable to find lint debug task for build variant: ${buildVariant}") - } - return lintTaskPath -} - -getKotlinDetektTasks = { - subprojects - .findResults { it.tasks.findByName("detekt")?.path } - .findAll { !it.contains(":libs") } -} - -task optimizePng { - doFirst { - def jarArgs = new ArrayList() - jarArgs.add(configurations.pngtastic.asPath) - def relatedPathIndex = "${rootDir}".length() + 1 - for (def file : fileTree(dir: "${rootDir}", include: '**/src/**/res/drawable**/*.png')) { - jarArgs.add(file.absolutePath.substring(relatedPathIndex)) - } - for (def file : fileTree(dir: "${rootDir}", include: '**/src/**/res/mipmap**/*.png')) { - jarArgs.add(file.absolutePath.substring(relatedPathIndex)) - } - javaexec { main = "-jar"; args = jarArgs; workingDir = file("${rootDir}") } - } -} - -dependencies { - pngtastic 'com.github.depsypher:pngtastic:1.2' -} diff --git a/gradle/plugins/.gitignore b/gradle/plugins/.gitignore new file mode 100644 index 0000000..39d12d8 --- /dev/null +++ b/gradle/plugins/.gitignore @@ -0,0 +1,20 @@ +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ +/*/build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Log Files +*.log + +.gradle +.idea +.DS_Store +/captures +*.iml diff --git a/gradle/plugins/build.gradle.kts b/gradle/plugins/build.gradle.kts new file mode 100644 index 0000000..3611bcb --- /dev/null +++ b/gradle/plugins/build.gradle.kts @@ -0,0 +1,52 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + `java-gradle-plugin` + `kotlin-dsl` +} + +// The kotlin-dsl plugin requires a repository to be declared +repositories { + jcenter() + google() +} + +dependencies { + // android gradle plugin, required by custom plugin + implementation("com.android.tools.build:gradle:4.0.1") + + implementation("io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.10.0") + implementation("de.aaschmid:gradle-cpd-plugin:3.1") + + // kotlin plugin, required by custom plugin + implementation(kotlin("gradle-plugin", embeddedKotlinVersion)) + + gradleKotlinDsl() + implementation(kotlin("stdlib-jdk8")) +} + +val compileKotlin: KotlinCompile by tasks +compileKotlin.kotlinOptions { + jvmTarget = "1.8" +} + +gradlePlugin { + plugins { + create("api-generator-android") { + id = "api-generator-android" + implementationClass = "apigen.ApiGeneratorAndroidPlugin" + } + create("api-generator-backend") { + id = "api-generator-backend" + implementationClass = "apigen.ApiGeneratorBackendPlugin" + } + create("static-analysis-android") { + id = "static-analysis-android" + implementationClass = "static_analysis.plugins.StaticAnalysisAndroidPlugin" + } + create("static-analysis-backend") { + id = "static-analysis-backend" + implementationClass = "static_analysis.plugins.StaticAnalysisBackendPlugin" + } + } +} diff --git a/gradle/plugins/settings.gradle.kts b/gradle/plugins/settings.gradle.kts new file mode 100644 index 0000000..e69de29 diff --git a/gradle/plugins/src/main/java/apigen/ApiGeneratorAndroidPlugin.kt b/gradle/plugins/src/main/java/apigen/ApiGeneratorAndroidPlugin.kt new file mode 100644 index 0000000..ea06f40 --- /dev/null +++ b/gradle/plugins/src/main/java/apigen/ApiGeneratorAndroidPlugin.kt @@ -0,0 +1,46 @@ +package apigen + +import com.android.build.gradle.AppExtension +import com.android.build.gradle.LibraryExtension +import org.gradle.api.Project +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.kotlin.dsl.findByType +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +class ApiGeneratorAndroidPlugin : ApiGeneratorPlugin() { + + override fun apply(target: Project) { + super.apply(target) + + with(target) { + val extension = getExtension() + val outputDir = getDirectoryForGeneration() + + extension.outputDirPath = outputDir.path + extension.recreateOutputDir = true + + afterEvaluate { + extensions.findByType()?.apply { + sourceSets.getByName("main") + .java + .srcDir(outputDir) + } + extensions.findByType()?.apply { + sourceSets.getByName("main") + .java + .srcDir(outputDir) + } + tasks + .filterIsInstance() + .forEach { it.source(outputDir) } + + tasks + .filterIsInstance() + .forEach { it.source(outputDir) } + } + } + } + + private fun Project.getDirectoryForGeneration() = file("$buildDir/generated/api") + +} diff --git a/gradle/plugins/src/main/java/apigen/ApiGeneratorBackendPlugin.kt b/gradle/plugins/src/main/java/apigen/ApiGeneratorBackendPlugin.kt new file mode 100644 index 0000000..b4a8723 --- /dev/null +++ b/gradle/plugins/src/main/java/apigen/ApiGeneratorBackendPlugin.kt @@ -0,0 +1,17 @@ +package apigen + +import org.gradle.api.Project + +class ApiGeneratorBackendPlugin : ApiGeneratorPlugin() { + + override fun apply(target: Project) { + super.apply(target) + + val extension = target.getExtension() + + extension.outputDirPath = target.file("src/main/kotlin").path + extension.recreateOutputDir = false + extension.outputLanguage = OutputLanguage.KotlinServer + + } +} diff --git a/gradle/plugins/src/main/java/apigen/ApiGeneratorExtension.kt b/gradle/plugins/src/main/java/apigen/ApiGeneratorExtension.kt new file mode 100644 index 0000000..76e0ed9 --- /dev/null +++ b/gradle/plugins/src/main/java/apigen/ApiGeneratorExtension.kt @@ -0,0 +1,22 @@ +package apigen + +open class ApiGeneratorExtension( + var pathToApiSchemes: String? = null, + var outputPackageName: String = "", + var outputDirPath: String = "", + var recreateOutputDir: Boolean = false, + var outputLanguage: OutputLanguage? = null +) + +sealed class OutputLanguage(val argName: String, val methodOutputType: MethodOutputType? = null) { + object KotlinServer : OutputLanguage("KOTLIN_SERVER") + class KotlinAndroid(methodOutputType: MethodOutputType = MethodOutputType.Rx) : OutputLanguage("KOTLIN", methodOutputType) + object Java : OutputLanguage("JAVA") + object Swift : OutputLanguage("SWIFT") +} + +sealed class MethodOutputType(val argName: String) { + object Rx : MethodOutputType("REACTIVE") + object RetrofitCall : MethodOutputType("CALL") + object Coroutine : MethodOutputType("COROUTINE") +} diff --git a/gradle/plugins/src/main/java/apigen/ApiGeneratorPlugin.kt b/gradle/plugins/src/main/java/apigen/ApiGeneratorPlugin.kt new file mode 100644 index 0000000..0294f5e --- /dev/null +++ b/gradle/plugins/src/main/java/apigen/ApiGeneratorPlugin.kt @@ -0,0 +1,78 @@ +package apigen + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.kotlin.dsl.create +import org.gradle.kotlin.dsl.dependencies +import org.gradle.kotlin.dsl.repositories + +abstract class ApiGeneratorPlugin : Plugin { + + companion object { + const val API_GENERATOR_CONFIG = "apiGenerator" + const val API_GENERATOR_EXT_NAME = "apiGenerator" + const val API_GENERATOR_DEFAULT_VERSION = "1.4.0-beta9" + } + + override fun apply(target: Project) { + with(target) { + repositories { + maven { + url = uri("https://dl.bintray.com/touchin/touchin-tools") + metadataSources { + artifact() + } + } + } + + configurations.create(API_GENERATOR_CONFIG) + + dependencies { + add(API_GENERATOR_CONFIG, "ru.touchin:api-generator:$API_GENERATOR_DEFAULT_VERSION") + } + + extensions.create(API_GENERATOR_EXT_NAME) + + val apiGenTask = createApiGeneratorTask() + + gradle.projectsEvaluated { + tasks.getByName("preBuild").dependsOn(apiGenTask) + } + } + } + + protected fun Project.getExtension(): ApiGeneratorExtension = extensions.getByName(API_GENERATOR_EXT_NAME) as ApiGeneratorExtension + + private fun Project.createApiGeneratorTask(): Task = tasks.create(API_GENERATOR_CONFIG).doLast { + + val extension = getExtension() + + val pathToApiSchemes = extension.pathToApiSchemes ?: throw IllegalStateException("Configure path to api schemes for api generator plugin") + val outputLanguage = extension.outputLanguage ?: throw IllegalStateException("Configure output language code for api generator plugin") + + javaexec { + main = "-jar" + workingDir = rootDir + args = listOfNotNull( + configurations.getByName("apiGenerator").asPath, + "generate-client-code", + "--output-language", + outputLanguage.argName, + "--specification-path", + pathToApiSchemes, + "--android-calendar-library", + "JAVA_TIME", + "--kotlin-methods-generation-mode".takeIf { outputLanguage.methodOutputType != null }, + outputLanguage.methodOutputType?.argName, + "--output-path", + extension.outputDirPath, + "--package-name", + extension.outputPackageName, + "--recreate_output_dirs", + extension.recreateOutputDir.toString() + ) + } + } + +} diff --git a/gradle/plugins/src/main/java/static_analysis/errors/AndroidLintError.kt b/gradle/plugins/src/main/java/static_analysis/errors/AndroidLintError.kt new file mode 100644 index 0000000..6b87d81 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/errors/AndroidLintError.kt @@ -0,0 +1,15 @@ +package static_analysis.errors + +class AndroidLintError( + private val filePath: String, + private val fileLine: String?, + private val errorId: String, + private val description: String +) : StaticAnalysisError { + + override fun print(count: Int): String = "\n$count. Android Lint. $description ($errorId)\n\tat [$filePath$fileLinePrefix]" + + private val fileLinePrefix: String + get() = fileLine?.let { ":$it" }.orEmpty() + +} diff --git a/gradle/plugins/src/main/java/static_analysis/errors/CpdError.kt b/gradle/plugins/src/main/java/static_analysis/errors/CpdError.kt new file mode 100644 index 0000000..cca1c8a --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/errors/CpdError.kt @@ -0,0 +1,12 @@ +package static_analysis.errors + +class CpdError( + private val duplications: List>, + private val codeFragment: String +) : StaticAnalysisError { + + override fun print(count: Int): String = "\n$count. CPD. Code duplication in files: " + + duplications.joinToString(separator = "") { (file, line) -> "\n\t[$file:$line]" } + + "\n\n Duplicated code:\n\n$codeFragment\n" + +} diff --git a/gradle/plugins/src/main/java/static_analysis/errors/DetektError.kt b/gradle/plugins/src/main/java/static_analysis/errors/DetektError.kt new file mode 100644 index 0000000..3aa9c09 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/errors/DetektError.kt @@ -0,0 +1,12 @@ +package static_analysis.errors + +class DetektError( + private val filePath: String, + private val fileLine: String, + private val errorId: String, + private val description: String +) : StaticAnalysisError { + + override fun print(count: Int): String = "\n$count. Detekt. $description ($errorId)\n\tat [$filePath:$fileLine]" + +} diff --git a/gradle/plugins/src/main/java/static_analysis/errors/StaticAnalysisError.kt b/gradle/plugins/src/main/java/static_analysis/errors/StaticAnalysisError.kt new file mode 100644 index 0000000..f932aca --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/errors/StaticAnalysisError.kt @@ -0,0 +1,5 @@ +package static_analysis.errors + +interface StaticAnalysisError { + fun print(count: Int): String +} diff --git a/gradle/plugins/src/main/java/static_analysis/linters/AndroidLinter.kt b/gradle/plugins/src/main/java/static_analysis/linters/AndroidLinter.kt new file mode 100644 index 0000000..8550e04 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/linters/AndroidLinter.kt @@ -0,0 +1,72 @@ +package static_analysis.linters + +import com.android.build.gradle.AppExtension +import com.android.build.gradle.AppPlugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.findByType +import static_analysis.errors.AndroidLintError +import static_analysis.errors.StaticAnalysisError +import static_analysis.plugins.StaticAnalysisExtension +import static_analysis.utils.typedChildren +import static_analysis.utils.xmlParser + +class AndroidLinter : Linter { + + override val name: String = "Android lint" + + override fun getErrors(project: Project): List = xmlParser(project.getLintReportFile()) + .typedChildren() + .filter { it.name() == "issue" && (it.attribute("severity") as String) == "Error" } + .map { errorNode -> + errorNode + .typedChildren() + .filter { it.name() == "location" } + .map { locationNode -> + AndroidLintError( + filePath = locationNode.attribute("file") as String, + fileLine = locationNode.attribute("line") as String?, + errorId = errorNode.attribute("id") as String, + description = errorNode.attribute("message") as String + ) + } + } + .flatten() + + override fun setupForProject(project: Project, extension: StaticAnalysisExtension) { + project.gradle.projectsEvaluated { + project.subprojects + .mapNotNull { it.extensions.findByType() } + .first() + .lintOptions.apply { + isAbortOnError = false + isCheckAllWarnings = true + isWarningsAsErrors = false + xmlReport = true + htmlReport = false + isCheckDependencies = true + disable("MissingConstraints", "VectorRaster") + xmlOutput = project.getLintReportFile() + lintConfig = project.file("${extension.buildScriptDir}/static_analysis_configs/lint.xml") + } + } + } + + override fun getTaskNames(project: Project, buildType: String?): List { + if (buildType == null) { + throw IllegalStateException("Build type must not be null in android linter") + } + + return project + .subprojects + .filter { it.plugins.hasPlugin(AppPlugin::class.java) } + .mapNotNull { subproject: Project -> + subproject + .tasks + .find { task -> task.name.contains(buildType, ignoreCase = true) && task.name.contains("lint") } + ?.path + } + } + + private fun Project.getLintReportFile() = file("${rootProject.buildDir}/reports/lint-report.xml") + +} diff --git a/gradle/plugins/src/main/java/static_analysis/linters/CpdLinter.kt b/gradle/plugins/src/main/java/static_analysis/linters/CpdLinter.kt new file mode 100644 index 0000000..5e14d6b --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/linters/CpdLinter.kt @@ -0,0 +1,58 @@ +package static_analysis.linters + +import de.aaschmid.gradle.plugins.cpd.Cpd +import de.aaschmid.gradle.plugins.cpd.CpdExtension +import org.gradle.api.Project +import org.gradle.kotlin.dsl.findByType +import org.gradle.kotlin.dsl.withType +import static_analysis.errors.CpdError +import static_analysis.errors.StaticAnalysisError +import static_analysis.plugins.StaticAnalysisExtension +import static_analysis.utils.getSources +import static_analysis.utils.typedChildren +import static_analysis.utils.xmlParser + +class CpdLinter : Linter { + + override val name: String = "CPD" + + override fun getErrors(project: Project): List = xmlParser(project.getCpdReportFile()) + .typedChildren() + .filter { it.name() == "duplication" } + .map { duplicationNode -> + + val children = duplicationNode + .typedChildren() + + CpdError( + duplications = children + .filter { it.name() == "file" } + .map { fileNode -> fileNode.attribute("path") as String to fileNode.attribute("line") as String }, + codeFragment = children.findLast { it.name() == "codefragment" }!!.text() + ) + + } + + override fun setupForProject(project: Project, extension: StaticAnalysisExtension) { + project.extensions.findByType()!!.apply { + isSkipLexicalErrors = true + language = "kotlin" + encoding = "UTF-8" + minimumTokenCount = 60 + } + project.tasks.withType { + reports.xml.destination = project.getCpdReportFile() + ignoreFailures = true + source = project.getSources(extension.excludes) + } + } + + override fun getTaskNames(project: Project, buildType: String?): List = project + .rootProject + .tasks + .withType() + .map(Cpd::getPath) + + private fun Project.getCpdReportFile() = file("${rootProject.buildDir}/reports/cpd.xml") + +} diff --git a/gradle/plugins/src/main/java/static_analysis/linters/DetektLinter.kt b/gradle/plugins/src/main/java/static_analysis/linters/DetektLinter.kt new file mode 100644 index 0000000..0aafb22 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/linters/DetektLinter.kt @@ -0,0 +1,62 @@ +package static_analysis.linters + +import io.gitlab.arturbosch.detekt.Detekt +import org.gradle.api.Project +import static_analysis.errors.DetektError +import static_analysis.errors.StaticAnalysisError +import static_analysis.plugins.StaticAnalysisExtension +import static_analysis.utils.getSources +import static_analysis.utils.typedChildren +import static_analysis.utils.xmlParser + +class DetektLinter : Linter { + + override val name: String = "Detekt" + + override fun getErrors(project: Project): List = xmlParser(project.getDetektReportFile()) + .typedChildren() + .filter { fileNode -> fileNode.name() == "file" } + .map { fileNode -> + fileNode + .typedChildren() + .filter { it.name() == "error" } + .map { errorNode -> + DetektError( + filePath = fileNode.attribute("name") as String, + fileLine = errorNode.attribute("line") as String, + errorId = errorNode.attribute("source") as String, + description = errorNode.attribute("message") as String + ) + } + } + .flatten() + + override fun setupForProject(project: Project, extension: StaticAnalysisExtension) { + project + .tasks + .withType(Detekt::class.java) { + exclude("**/test/**") + exclude("resources/") + exclude("build/") + exclude("tmp/") + jvmTarget = "1.8" + + config.setFrom(project.files("${extension.buildScriptDir!!}/static_analysis_configs/detekt-config.yml")) + reports { + txt.enabled = false + html.enabled = false + xml { + enabled = true + destination = project.getDetektReportFile() + } + } + + source = project.getSources(extension.excludes) + } + } + + override fun getTaskNames(project: Project, buildType: String?): List = listOf(":detekt") + + private fun Project.getDetektReportFile() = file("${rootProject.buildDir}/reports/detekt.xml") + +} diff --git a/gradle/plugins/src/main/java/static_analysis/linters/Linter.kt b/gradle/plugins/src/main/java/static_analysis/linters/Linter.kt new file mode 100644 index 0000000..65421a1 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/linters/Linter.kt @@ -0,0 +1,12 @@ +package static_analysis.linters + +import org.gradle.api.Project +import static_analysis.errors.StaticAnalysisError +import static_analysis.plugins.StaticAnalysisExtension + +interface Linter { + val name: String + fun getErrors(project: Project): List + fun setupForProject(project: Project, extension: StaticAnalysisExtension) + fun getTaskNames(project: Project, buildType: String? = null): List +} diff --git a/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisAndroidPlugin.kt b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisAndroidPlugin.kt new file mode 100644 index 0000000..c433e81 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisAndroidPlugin.kt @@ -0,0 +1,43 @@ +package static_analysis.plugins + +import com.android.build.gradle.AppExtension +import org.gradle.api.Project +import org.gradle.kotlin.dsl.getByType +import static_analysis.linters.AndroidLinter +import static_analysis.linters.CpdLinter +import static_analysis.linters.DetektLinter +import static_analysis.linters.Linter + +class StaticAnalysisAndroidPlugin : StaticAnalysisPlugin() { + + override fun createStaticAnalysisTasks(project: Project, linters: List) { + project.subprojects { + if (plugins.hasPlugin("com.android.application")) { + + extensions.getByType().apply { + applicationVariants.forEach { variant -> + project.tasks.register("staticAnalysis${variant.name.capitalize()}") { + setupStaticAnalysisTask(linters, variant.name) + } + } + + project.tasks.register("staticAnalysis") { + setupStaticAnalysisTask( + linters = linters, + buildVariant = applicationVariants.first { it.name.contains("Debug") }.name + ) + } + } + + + } + } + } + + override fun createLinters(): List = listOf( + DetektLinter(), + CpdLinter(), + AndroidLinter() + ) + +} diff --git a/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisBackendPlugin.kt b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisBackendPlugin.kt new file mode 100644 index 0000000..fa35f1d --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisBackendPlugin.kt @@ -0,0 +1,21 @@ +package static_analysis.plugins + +import org.gradle.api.Project +import static_analysis.linters.CpdLinter +import static_analysis.linters.DetektLinter +import static_analysis.linters.Linter + +class StaticAnalysisBackendPlugin : StaticAnalysisPlugin() { + + override fun createStaticAnalysisTasks(project: Project, linters: List) { + project.tasks.register("staticAnalysis") { + setupStaticAnalysisTask(linters) + } + } + + override fun createLinters(): List = listOf( + CpdLinter(), + DetektLinter() + ) + +} diff --git a/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisExtension.kt b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisExtension.kt new file mode 100644 index 0000000..dfa698d --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisExtension.kt @@ -0,0 +1,6 @@ +package static_analysis.plugins + +open class StaticAnalysisExtension( + var excludes: String = "", + var buildScriptDir: String? = null +) diff --git a/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisPlugin.kt b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisPlugin.kt new file mode 100644 index 0000000..38ded00 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/plugins/StaticAnalysisPlugin.kt @@ -0,0 +1,47 @@ +package static_analysis.plugins + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.kotlin.dsl.create +import org.gradle.kotlin.dsl.getByType +import static_analysis.linters.Linter +import static_analysis.utils.ReportGenerator + +abstract class StaticAnalysisPlugin : Plugin { + + companion object { + const val DETEKT_ID = "io.gitlab.arturbosch.detekt" + const val CPD_ID = "de.aaschmid.cpd" + const val STATIC_ANALYSIS_EXT_NAME = "staticAnalysis" + } + + override fun apply(target: Project) { + + with(target) { + pluginManager.apply(CPD_ID) + pluginManager.apply(DETEKT_ID) + + extensions.create(STATIC_ANALYSIS_EXT_NAME) + + val linters = createLinters() + + afterEvaluate { + linters.forEach { it.setupForProject(target, extensions.getByType()) } + } + + gradle.projectsEvaluated { + createStaticAnalysisTasks(target, linters) + } + } + } + + fun Task.setupStaticAnalysisTask(linters: List, buildVariant: String? = null) { + doFirst { ReportGenerator.generate(linters, project) } + dependsOn(*(linters.map { it.getTaskNames(project, buildVariant) }.flatten().toTypedArray())) + } + + abstract fun createLinters(): List + abstract fun createStaticAnalysisTasks(project: Project, linters: List) + +} diff --git a/gradle/plugins/src/main/java/static_analysis/utils/Node.kt b/gradle/plugins/src/main/java/static_analysis/utils/Node.kt new file mode 100644 index 0000000..e8618e2 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/utils/Node.kt @@ -0,0 +1,5 @@ +package static_analysis.utils + +import groovy.util.Node + +fun Node.typedChildren() = children() as List diff --git a/gradle/plugins/src/main/java/static_analysis/utils/Project.kt b/gradle/plugins/src/main/java/static_analysis/utils/Project.kt new file mode 100644 index 0000000..f1c46cf --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/utils/Project.kt @@ -0,0 +1,27 @@ +package static_analysis.utils + +import org.gradle.api.Project +import org.gradle.api.file.FileTree +import java.io.File + +fun Project.getSources(excludes: String): FileTree = files( + project + .rootProject + .subprojects + .filter { subproject -> subproject.subprojects.isEmpty() && !excludes.contains(subproject.path) } + .map { subproject -> subproject.file("${subproject.projectDir.path}/src/main") } + .filter { it.exists() && it.isDirectory } + .flatMap { srcDir -> + srcDir + .listFiles() + .orEmpty() + .flatMap { + listOf( + File(srcDir.path, "java"), + File(srcDir.path, "kotlin") + ) + } + } + .filter { it.exists() && it.isDirectory } + .map { it.path } +).asFileTree diff --git a/gradle/plugins/src/main/java/static_analysis/utils/ReportGenerator.kt b/gradle/plugins/src/main/java/static_analysis/utils/ReportGenerator.kt new file mode 100644 index 0000000..b18f8f4 --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/utils/ReportGenerator.kt @@ -0,0 +1,52 @@ +package static_analysis.utils + +import org.gradle.api.Project +import static_analysis.errors.StaticAnalysisError +import static_analysis.linters.Linter + +object ReportGenerator { + + fun generate(linters: List, project: Project) { + + val groupedErrors = linters + .map { linter -> linter to linter.getErrors(project) } + + val lintersResults = groupedErrors + .map { (linter, linterErrors) -> linter.name to linterErrors.size } + + val allErrors = groupedErrors + .map(Pair>::second) + .flatten() + + val consoleReport = StringBuilder("\nSTATIC ANALYSIS ERRORS:").apply { + appendAllErrors(allErrors) + append("\nREPORT:\n") + appendReportsSummary(lintersResults) + appendOverallSummary(allErrors) + } + + if (allErrors.isEmpty()) { + println(consoleReport) + } else { + throw Exception(consoleReport.toString()) + } + + } + + private fun StringBuilder.appendAllErrors(errors: List) = errors + .mapIndexed { index, staticAnalysisError -> staticAnalysisError.print(index + 1) } + .forEach { error -> append(error) } + + private fun StringBuilder.appendReportsSummary(lintersResults: List>) = lintersResults + .forEach { this.appendSummary(it.first, it.second) } + + private fun StringBuilder.appendOverallSummary(errors: List) = appendSummary("Overall", errors.size) + + private fun StringBuilder.appendSummary(header: String, quantityOfErrors: Int) { + assert(quantityOfErrors < 0) + + append("\n$header: ") + append(if (quantityOfErrors == 0) "PASSED" else "FAILED ($quantityOfErrors errors)") + } + +} diff --git a/gradle/plugins/src/main/java/static_analysis/utils/XmlUtils.kt b/gradle/plugins/src/main/java/static_analysis/utils/XmlUtils.kt new file mode 100644 index 0000000..1754f4c --- /dev/null +++ b/gradle/plugins/src/main/java/static_analysis/utils/XmlUtils.kt @@ -0,0 +1,6 @@ +package static_analysis.utils + +import groovy.util.XmlParser +import java.io.File + +fun xmlParser(file: File) = XmlParser().parse(file) diff --git a/gradle/applicationFileNaming.gradle b/gradle/scripts/applicationFileNaming.gradle similarity index 100% rename from gradle/applicationFileNaming.gradle rename to gradle/scripts/applicationFileNaming.gradle diff --git a/gradle/stringGenerator.gradle b/gradle/scripts/stringGenerator.gradle similarity index 82% rename from gradle/stringGenerator.gradle rename to gradle/scripts/stringGenerator.gradle index 374ab12..9c47e90 100644 --- a/gradle/stringGenerator.gradle +++ b/gradle/scripts/stringGenerator.gradle @@ -2,11 +2,11 @@ import groovy.json.JsonSlurper import groovy.xml.MarkupBuilder task stringGenerator { - generate(android.languageMap, android.rootPath ?: "app") + generate(android.languageMap, project) println("Strings generated!") } -private def generate(Map sources, String rootPath) { +private def generate(Map sources, Project project) { if (sources == null || sources.isEmpty()) { throw new IOException("languageMap can't be null or empty") } @@ -31,7 +31,7 @@ private def generate(Map sources, String rootPath) { } } - def stringsFile = getFile(key, key == defaultLang, rootPath) + def stringsFile = getFile(key, key == defaultLang, project) stringsFile.write(sw.toString(), "UTF-8") } } @@ -69,15 +69,15 @@ private static Map getJsonsMap(Map sources) { } } -private static File getFile(String key, boolean defaultLang, String rootPath) { +private static File getFile(String key, boolean defaultLang, Project project) { if (defaultLang) { - return new File("${rootPath}/src/main/res/values/strings.xml") + return project.file("src/main/res/values/strings.xml") } else { - def directory = new File("${rootPath}/src/main/res/values-$key") + def directory = project.file("src/main/res/values-$key") if (!directory.exists()) { directory.mkdir() } - return new File("${rootPath}/src/main/res/values-$key/strings.xml") + return project.file("src/main/res/values-$key/strings.xml") } } diff --git a/gradle/staticAnalysis.gradle b/gradle/staticAnalysis.gradle deleted file mode 100644 index b952ec9..0000000 --- a/gradle/staticAnalysis.gradle +++ /dev/null @@ -1,166 +0,0 @@ -buildscript { - repositories { - maven { url "https://plugins.gradle.org/m2" } - } - dependencies { - classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.5.1" - } -} - -def getServerProjectSources -def getAndroidProjectSources - -apply from: "$buildScriptsDir/gradle/commonStaticAnalysis.gradle" - -gradle.projectsEvaluated { - - tasks.withType(JavaCompile) { - options.compilerArgs << - "-Xlint:cast" << - "-Xlint:divzero" << - "-Xlint:empty" << - "-Xlint:deprecation" << - "-Xlint:finally" << - "-Xlint:overrides" << - "-Xlint:path" << - "-Werror" - } - - def excludes = rootProject.extensions.findByName("staticAnalysisExcludes") - - def androidSources = getAndroidProjectSources(excludes) - def androidStaticAnalysisTasks = getStaticAnalysisTaskNames(true, androidSources, null) - def androidIdeaFormatTask = getIdeaFormatTask(true, androidSources) - - task staticAnalysisWithFormatting { - androidStaticAnalysisTasks.each { task -> - tasks.findByName(task)?.mustRunAfter(androidIdeaFormatTask) - } - dependsOn androidIdeaFormatTask - dependsOn androidStaticAnalysisTasks - doFirst { - generateReport(true) - } - } - - task staticAnalysis { - dependsOn androidStaticAnalysisTasks - doFirst { - generateReport(true) - } - } - - def serverStaticAnalysisTasks = getStaticAnalysisTaskNames(false, getServerProjectSources(excludes), null) - def serverIdeaFormatTask = getIdeaFormatTask(false, getServerProjectSources(excludes)) - - task serverStaticAnalysisWithFormatting { - serverStaticAnalysisTasks.each { task -> - tasks.findByName(task)?.mustRunAfter(serverIdeaFormatTask) - } - dependsOn serverIdeaFormatTask - dependsOn serverStaticAnalysisTasks - doFirst { - generateReport(false) - } - } - - task serverStaticAnalysis { - dependsOn serverStaticAnalysisTasks - doFirst { - generateReport(false) - } - } - - subprojects { subproject -> - if (subproject.plugins.hasPlugin("com.android.application")) { - subproject.android { - lintOptions.abortOnError = false - lintOptions.checkAllWarnings = true - lintOptions.warningsAsErrors = false - lintOptions.xmlReport = true - lintOptions.xmlOutput = file "$rootProject.buildDir/reports/lint_report.xml" - lintOptions.htmlReport = false - lintOptions.lintConfig = file "$buildScriptsDir/lint/lint.xml" - lintOptions.checkDependencies true - lintOptions.disable 'MissingConstraints', 'VectorRaster' - - applicationVariants.all { variant -> - task("staticAnalysis${variant.name.capitalize()}") { - dependsOn getStaticAnalysisTaskNames(true, androidSources, variant) - doFirst { generateReport(true) } - } - } - } - } - - def regex = ~':detekt$' - tasks.forEach { task -> - if (!task.name.contains(":libs") && task.path =~ regex) { - task.exclude '**/test/**' - task.exclude 'resources/' - task.exclude 'build/' - task.exclude 'tmp/' - - task.jvmTarget = "1.8" - } - } - - detekt { - config = files("$buildScriptsDir/kotlin/detekt-config.yml") - - reports { - txt.enabled = false - html.enabled = false - xml { - enabled = true - destination = file("${rootProject.buildDir}/reports/kotlin-detekt-${subproject.name}.xml") - } - } - } - } -} - -getServerProjectSources = { excludes -> - def sources = new ArrayList() - def sourcesDirectory = new File(project.projectDir.path, 'src') - - for (def sourceFlavorDirectory : sourcesDirectory.listFiles()) { - def javaSourceDirectory = new File(sourceFlavorDirectory.path, 'java') - def kotlinSourceDirectory = new File(sourceFlavorDirectory.path, 'kotlin') - - if (javaSourceDirectory.exists() && javaSourceDirectory.isDirectory()) { - sources.add(javaSourceDirectory.absolutePath) - } - if (kotlinSourceDirectory.exists() && kotlinSourceDirectory.isDirectory()) { - sources.add(kotlinSourceDirectory.absolutePath) - } - } - return sources -} - -getAndroidProjectSources = { excludes -> - def sources = new ArrayList() - for (def project : rootProject.subprojects) { - if (!project.subprojects.isEmpty() || (excludes != null && excludes.contains(project.path))) { - continue - } - - def sourcesDirectory = new File(project.projectDir.path, 'src') - if (!sourcesDirectory.exists() || !sourcesDirectory.isDirectory()) { - continue - } - - for (def sourceFlavorDirectory : sourcesDirectory.listFiles()) { - def javaSourceDirectory = new File(sourceFlavorDirectory.path, 'java') - def kotlinSourceDirectory = new File(sourceFlavorDirectory.path, 'kotlin') - - if (javaSourceDirectory.exists() && javaSourceDirectory.isDirectory()) { - sources.add(javaSourceDirectory.absolutePath) - } - if (kotlinSourceDirectory.exists() && kotlinSourceDirectory.isDirectory()) { - sources.add(kotlinSourceDirectory.absolutePath) - } - } - } - return sources -} diff --git a/lint/lint.xml b/lint/lint.xml deleted file mode 100644 index 57a80e9..0000000 --- a/lint/lint.xml +++ /dev/null @@ -1,272 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pmd/rulesets/java/android.xml b/pmd/rulesets/java/android.xml deleted file mode 100644 index debe6f4..0000000 --- a/pmd/rulesets/java/android.xml +++ /dev/null @@ -1,131 +0,0 @@ - - - - Every Java Rule in PMD - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/proguard/common.pro b/proguard/common.pro index 2e69ab7..d7f2023 100644 --- a/proguard/common.pro +++ b/proguard/common.pro @@ -1,5 +1,4 @@ -include rules/components.pro - -include rules/okhttp.pro -include rules/retrofit.pro -include rules/logansquare.pro @@ -8,4 +7,3 @@ -include rules/kaspersky.pro -include rules/appsflyer.pro -include rules/moshi.pro --include rules/androidx_security.pro diff --git a/export_src.sh b/scripts/export_src.sh similarity index 100% rename from export_src.sh rename to scripts/export_src.sh diff --git a/kotlin/detekt-config.yml b/static_analysis_configs/detekt-config.yml similarity index 99% rename from kotlin/detekt-config.yml rename to static_analysis_configs/detekt-config.yml index 3afb6fa..5ea4cce 100644 --- a/kotlin/detekt-config.yml +++ b/static_analysis_configs/detekt-config.yml @@ -23,7 +23,7 @@ formatting: active: true console-reports: - active: true + active: false exclude: # - 'ProjectStatisticsReport' # - 'ComplexityReport' diff --git a/static_analysis_configs/lint.xml b/static_analysis_configs/lint.xml new file mode 100644 index 0000000..98227d4 --- /dev/null +++ b/static_analysis_configs/lint.xml @@ -0,0 +1,273 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/xcode/.swiftlint.yml b/xcode/.swiftlint.yml index d502145..a8c88c7 100644 --- a/xcode/.swiftlint.yml +++ b/xcode/.swiftlint.yml @@ -241,7 +241,7 @@ custom_rules: redundant_type_annotation_bool: name: "Redundant type annotation for Bool" - regex: '((var|let)) *\w+ *((: *Bool *=)|((\w| |<|>|:)*= *BehaviorRelay\( *value *:)) *((true)|(false))' + regex: '\s((var|let))\s{1,}\w+ *((: *Bool *=)|((\w| |<|>|:)*= *BehaviorRelay\( *value *:)) *((true)|(false))' message: "Using a type annotation for Bool is redundant." severity: error @@ -251,6 +251,12 @@ custom_rules: message: "The parameter name is actually used in the function name. Use _ instead." severity: error + parameter_closure: + name: "Parameter closure" + regex: '\w*Closure<[^\r\n\t\f\v]*, Void[^\r\n\t\f\v]*>' + message: "Use `ParameterClosure` instead of declaring an explicit return value of `Void`." + severity: error + # Rx unused_map_parameter: @@ -259,6 +265,12 @@ custom_rules: message: "Replace Rx.map operator with replace(with:) or asVoid(). For Sequence.map consider using forEach." severity: warning + disposable_nil: + name: "Disposable nil" + regex: ' *\S*(d|D)isposable\?? *= *nil' + message: "nil assigning doesn't dispose subscription. Call `dispose()` instead." + severity: error + # LeadKit multiple_add_subview: diff --git a/xcode/build_phases/common/localization_check b/xcode/build_phases/common/localization_check new file mode 100755 index 0000000..e1d1078 Binary files /dev/null and b/xcode/build_phases/common/localization_check differ diff --git a/xcode/build_phases/documentation_generator.sh b/xcode/build_phases/documentation_generator.sh index a5449b7..ffd4828 100644 --- a/xcode/build_phases/documentation_generator.sh +++ b/xcode/build_phases/documentation_generator.sh @@ -1 +1,2 @@ -. build-scripts/xcode/aux_scripts/certificates_readme_generator.sh > $PROJECT_DIR/Certificates/README.md \ No newline at end of file +readonly BUILD_SCRIPTS_DIR=${1:-${PROJECT_DIR}} # first argument or PROJECT_DIR +. $BUILD_SCRIPTS_DIR/build-scripts/xcode/aux_scripts/certificates_readme_generator.sh > $PROJECT_DIR/Certificates/README.md diff --git a/xcode/build_phases/localization_check.sh b/xcode/build_phases/localization_check.sh new file mode 100755 index 0000000..afb80eb --- /dev/null +++ b/xcode/build_phases/localization_check.sh @@ -0,0 +1,9 @@ +# source: https://github.com/iKenndac/verify-string-files + +# first argument set base localization strings path +readonly LOCALIZATION_PATH=${1:-${PRODUCT_NAME}/Resources/Localization/Base.lproj/Localizable.strings} + +# second argument set check script path +readonly CHECK_SCRIPT=${2:-${PROJECT_DIR}/build-scripts/xcode/build_phases/common/localization_check} + +${CHECK_SCRIPT} -master ${LOCALIZATION_PATH} diff --git a/xcode/build_phases/multiple_swiftlint/array_extension.rb b/xcode/build_phases/multiple_swiftlint/array_extension.rb new file mode 100644 index 0000000..e5bebf6 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/array_extension.rb @@ -0,0 +1,5 @@ +class Array + def nilOrEmpty? + self.nil? or self.empty? + end +end diff --git a/xcode/build_phases/multiple_swiftlint/command_utils.rb b/xcode/build_phases/multiple_swiftlint/command_utils.rb new file mode 100644 index 0000000..cf12d07 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/command_utils.rb @@ -0,0 +1,6 @@ +class CommandUtils + def self.make_command(command) + command = command.to_s + return `#{command}` + end +end \ No newline at end of file diff --git a/xcode/build_phases/multiple_swiftlint/git_caretaker.rb b/xcode/build_phases/multiple_swiftlint/git_caretaker.rb new file mode 100644 index 0000000..323464a --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/git_caretaker.rb @@ -0,0 +1,35 @@ +require_relative 'array_extension.rb' +require_relative 'command_utils.rb' +require_relative 'string_extension.rb' + +class GitСaretaker < CommandUtils + def self.get_modified_files + non_indexed_files = get_files_from('git diff --name-only | sed s/.*/"&,"/ ') + indexed_files = get_files_from('git diff --cached --name-only | sed s/.*/"&,"/ ') + + modified_files = non_indexed_files + indexed_files + unique_modified_files = modified_files.uniq + + unique_modified_swift_files = [] + if not unique_modified_files.nilOrEmpty? + unique_modified_swift_files = unique_modified_files.select { |file_path| + file_path.to_s.filter_allowed_symbol_into_path + file_path.to_s.include? '.swift' + } + end + + return unique_modified_swift_files + end + + def self.get_creation_date(file_path) + git_command = 'git log --follow --format=%cD --reverse -- ' + file_path + ' | head -1' + return make_command(git_command) + end + + private + + def self.get_files_from(command) + files_as_string = make_command(command) + return files_as_string.split(',') + end +end \ No newline at end of file diff --git a/xcode/build_phases/multiple_swiftlint/setting_option.rb b/xcode/build_phases/multiple_swiftlint/setting_option.rb new file mode 100644 index 0000000..44b8d7e --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/setting_option.rb @@ -0,0 +1,65 @@ +require 'optparse' +require 'ostruct' + +require_relative 'array_extension.rb' + +class SettingOption + def initialize + @options = OpenStruct.new + OptionParser.new do |opt| + opt.on('-p', '--project_root_path STRING', 'The path of project directory and contains *.xcodeproj file always. ' + + 'Example: project_root_path=~/Projects/MyProject/Source/..') { |option| @options.project_root_path = option } + opt.on('-r', '--source_root_path STRING', 'The path of source directory and may not contains *.xcodeproj file in some cases. ' + + 'Example: source_root_path=~/Projects/MyProject/') { |option| @options.source_root_path = option } + opt.on('-s', '--swiftlint_executable_path STRING', 'The executable path of swiftlint') { |option| @options.swiftlint_executable_path = option } + opt.on('-c', '--check_mode MODE', 'The mode of check is "fully" or "simplified"') { |option| @options.check_mode = option } + opt.on('-u', '--use_multiple BOOL', 'The flag indicates the use of multiple yaml swiftlint configurations') { |option| @options.use_multiple = option } + opt.on('-d', '--source_date DATE', 'The date of grouping files according touchin and old swiftlint rules') { |option| @options.source_date = option } + opt.on('-y', '--touchin_swiftlint_yaml_path STRING', 'The path to the touchin swiftlint yaml relative to the source directory') { |option| @options.touchin_swiftlint_yaml_path = option } + end.parse! + + if @options.check_mode.to_s.nilOrEmpty? + @options.check_mode = 'fully' + end + + if @options.use_multiple.to_s.nilOrEmpty? + @options.use_multiple = 'false' + end + + if @options.source_root_path.to_s.nilOrEmpty? + @options.source_root_path = @options.project_root_path + end + + if @options.touchin_swiftlint_yaml_path.to_s.nilOrEmpty? + @options.touchin_swiftlint_yaml_path = '/build-scripts/xcode/.swiftlint.yml' + end + end + + def project_root_path + @options.project_root_path + end + + def source_date + @options.source_date + end + + def swiftlint_executable_path + @options.swiftlint_executable_path + end + + def check_mode + @options.check_mode + end + + def use_multiple + @options.use_multiple + end + + def source_root_path + @options.source_root_path + end + + def touchin_swiftlint_yaml_path + @options.touchin_swiftlint_yaml_path + end +end diff --git a/xcode/build_phases/multiple_swiftlint/strategy_maker.rb b/xcode/build_phases/multiple_swiftlint/strategy_maker.rb new file mode 100644 index 0000000..a677892 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/strategy_maker.rb @@ -0,0 +1,160 @@ +require 'fileutils' + +require_relative 'array_extension.rb' +require_relative 'git_caretaker.rb' +require_relative 'string_extension.rb' +require_relative 'swift_file_manager.rb' +require_relative 'yaml_manager.rb' + +class StrategyMaker + def initialize(project_root_path, swiftlint_executable_path, touchin_swiftlint_yaml_path) + @project_root_path = project_root_path + @touchin_swiftlint_yaml_path = project_root_path + touchin_swiftlint_yaml_path + @old_swiftlint_yaml_path = project_root_path + '/.swiftlint.yml' + + @temporary_swiftlint_folder_name = project_root_path + '/temporary_swiftlint' + @touchin_swiftlint_yaml_temporary_path = @temporary_swiftlint_folder_name + '/.touchin_swiftlint.yml' + @old_swiftlint_yaml_temporary_path = @temporary_swiftlint_folder_name + '/.old_swiftlint.yml' + + @swiftlint_autocorrect_command = swiftlint_executable_path + ' autocorrect --path ' + @project_root_path + ' --config ' + @swiftlint_lint_command = swiftlint_executable_path + ' --path ' + @project_root_path + ' --config ' + end + + def run_fully_multiple_strategy(source_date) + create_yaml_managers_and_copy_temporary_files + + exclude_files = unique_exclude_files(@touchin_swiftlint_yaml_manager, @old_swiftlint_yaml_manager) + + swift_files = SwiftFileManager.new(exclude_files, source_date) + swift_files.find_list_file_paths(@project_root_path) + + total_touchin_excluded_files = exclude_files + swift_files.old_files + total_old_excluded_files = exclude_files + swift_files.new_files + + @touchin_swiftlint_yaml_manager.update('excluded', total_touchin_excluded_files) + @old_swiftlint_yaml_manager.update('excluded', total_old_excluded_files) + + run_multiple_strategy(@touchin_swiftlint_yaml_temporary_path, @old_swiftlint_yaml_temporary_path) + end + + def run_simplified_multiple_strategy(source_date, source_root_path) + included_files = GitСaretaker.get_modified_files + + if included_files.nilOrEmpty? + puts 'Git did not found swift files to check' + return + end + + create_yaml_managers_and_copy_temporary_files + + exclude_files = unique_exclude_files(@touchin_swiftlint_yaml_manager, @old_swiftlint_yaml_manager) + included_files = included_files.map { |file_path| source_root_path + file_path } + + swift_file_manager = SwiftFileManager.new(exclude_files, source_date) + swift_file_manager.find_list_file_paths_from(included_files) + + total_touchin_included_files = swift_file_manager.new_files + total_old_included_files = swift_file_manager.old_files + + @touchin_swiftlint_yaml_manager.update('excluded', []) + @old_swiftlint_yaml_manager.update('excluded', []) + + @touchin_swiftlint_yaml_manager.update('included', total_touchin_included_files) + @old_swiftlint_yaml_manager.update('included', total_old_included_files) + + is_exist_total_touchin_included_files = (not total_touchin_included_files.nilOrEmpty?) + is_exist_total_old_included_files = (not total_old_included_files.nilOrEmpty?) + + if is_exist_total_touchin_included_files and is_exist_total_old_included_files + run_multiple_strategy(@touchin_swiftlint_yaml_temporary_path, @old_swiftlint_yaml_temporary_path) + elsif is_exist_total_touchin_included_files and not is_exist_total_old_included_files + run_single_strategy(@touchin_swiftlint_yaml_temporary_path) + elsif not is_exist_total_touchin_included_files and is_exist_total_old_included_files + run_single_strategy(@old_swiftlint_yaml_temporary_path) + else + puts 'Git did not found swift files to check' + end + end + + def run_fully_single_strategy + run_single_strategy(@touchin_swiftlint_yaml_path) + end + + def run_simplified_single_strategy(source_root_path) + included_files = GitСaretaker.get_modified_files + + if included_files.nilOrEmpty? + puts 'Git did not found swift files to check' + return + end + + create_copy_temporary_touchin_files + + touchin_swiftlint_yaml_manager = YamlManager.new(@touchin_swiftlint_yaml_temporary_path) + touchin_excluded_files = touchin_swiftlint_yaml_manager.get_configuration('excluded') + swift_files = SwiftFileManager.new(touchin_excluded_files, '') + + included_files = included_files.select { |file_name| not swift_files.is_excluded_file(file_name) } + included_files = included_files.map { |file_path| source_root_path + file_path } + + touchin_swiftlint_yaml_manager.update('excluded', []) + touchin_swiftlint_yaml_manager.update('included', included_files) + + if not included_files.nilOrEmpty? + run_single_strategy(@touchin_swiftlint_yaml_temporary_path) + else + puts 'Git found the swift files to check, but they are excluded in yaml' + end + end + + private + + def run_single_strategy(swiftlint_yaml_path) + result_swiftlint_command = get_swiftlint_command(swiftlint_yaml_path) + puts result_swiftlint_command + run_bash_command(result_swiftlint_command) + end + + def run_multiple_strategy(touchin_swiftlint_yaml_temporary_path, old_swiftlint_yaml_temporary_path) + touchin_swiftlint_command = get_swiftlint_command(touchin_swiftlint_yaml_temporary_path) + old_swiftlint_command = get_swiftlint_command(old_swiftlint_yaml_temporary_path) + result_swiftlint_command = touchin_swiftlint_command + ' && ' + old_swiftlint_command + puts result_swiftlint_command + run_bash_command(result_swiftlint_command) + end + + def get_swiftlint_command(swiftlint_yaml_path) + autocorrect_command = @swiftlint_autocorrect_command + swiftlint_yaml_path + lint_command = @swiftlint_lint_command + swiftlint_yaml_path + return autocorrect_command + ' && ' + lint_command + end + + def run_bash_command(bash_command) + exit (exec bash_command) + end + + def create_yaml_managers_and_copy_temporary_files + create_copy_temporary_files + + @touchin_swiftlint_yaml_manager = YamlManager.new(@touchin_swiftlint_yaml_temporary_path) + @old_swiftlint_yaml_manager = YamlManager.new(@old_swiftlint_yaml_temporary_path) + end + + def create_copy_temporary_files + create_copy_temporary_touchin_files + FileUtils.cp @old_swiftlint_yaml_path, @old_swiftlint_yaml_temporary_path + end + + def create_copy_temporary_touchin_files + Dir.mkdir(@temporary_swiftlint_folder_name) unless Dir.exist?(@temporary_swiftlint_folder_name) + FileUtils.cp @touchin_swiftlint_yaml_path, @touchin_swiftlint_yaml_temporary_path + end + + def unique_exclude_files(touchin_swiftlint_yaml_manager, old_swiftlint_yaml_manager) + touchin_excluded_files = touchin_swiftlint_yaml_manager.get_configuration('excluded') + old_excluded_files = old_swiftlint_yaml_manager.get_configuration('excluded') + common_exclude_files = touchin_excluded_files + old_excluded_files + unique_exclude_files = common_exclude_files.uniq + return unique_exclude_files + end +end \ No newline at end of file diff --git a/xcode/build_phases/multiple_swiftlint/string_extension.rb b/xcode/build_phases/multiple_swiftlint/string_extension.rb new file mode 100644 index 0000000..33a6357 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/string_extension.rb @@ -0,0 +1,17 @@ +class String + def with_wrapped_whitespace + self.gsub(/\s+/, '\ ') + end + + def filter_allowed_symbol_into_path + self.gsub!(/[^0-9A-Za-z \-+.\/]/, '') + end + + def true? + self.to_s.downcase == "true" + end + + def nilOrEmpty? + self.nil? or self.empty? + end +end diff --git a/xcode/build_phases/multiple_swiftlint/swift_file_manager.rb b/xcode/build_phases/multiple_swiftlint/swift_file_manager.rb new file mode 100644 index 0000000..b8edf6c --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/swift_file_manager.rb @@ -0,0 +1,68 @@ +require 'fileutils' +require 'date' + +require_relative 'git_caretaker.rb' + +class SwiftFileManager + def initialize(excluded_files, source_date) + if not source_date.nilOrEmpty? + @source_date = Date.parse(source_date) + end + @excluded_files = excluded_files + @new_files = [] + @old_files = [] + end + + def old_files + @old_files + end + + def new_files + @new_files + end + + def find_list_file_paths(start_folder) + swift_files = File.join('**', '*.swift') + Dir.glob(swift_files, base: start_folder) { |file_path| + if not is_excluded_file(file_path) + compare_timestamp(file_path) + end + } + end + + def find_list_file_paths_from(files_path) + files_path.each { |file_path| + if not is_excluded_file(file_path) + compare_timestamp(file_path) + end + } + end + + def is_excluded_file(file_path) + @excluded_files.each do |exclude_file_path| + if file_path.include? exclude_file_path + return true + end + end + return false + end + + private + + def compare_timestamp(file_path) + wrapped_whitespace_file_path = file_path.with_wrapped_whitespace + creation_date_string = GitСaretaker.get_creation_date(wrapped_whitespace_file_path) + if creation_date_string.nilOrEmpty? + @old_files.push(file_path) + puts ('Creation date of ' + file_path + ' was not found') + else + creation_date = Date.parse(creation_date_string) + puts ('Creation date of ' + file_path + ' is ' + creation_date.to_s) + if @source_date < creation_date + @new_files.push(file_path) + else + @old_files.push(file_path) + end + end + end +end \ No newline at end of file diff --git a/xcode/build_phases/multiple_swiftlint/swiftlint.rb b/xcode/build_phases/multiple_swiftlint/swiftlint.rb new file mode 100644 index 0000000..17f8383 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/swiftlint.rb @@ -0,0 +1,16 @@ +#https://github.com/TouchInstinct/Styleguide/blob/multiple_swiftlint/IOS/Guides/BuildScripts/Multiple_Swiftlint_Guide.md +require_relative 'setting_option.rb' +require_relative 'strategy_maker.rb' + +setting = SettingOption.new +strategy_maker = StrategyMaker.new(setting.project_root_path, setting.swiftlint_executable_path, setting.touchin_swiftlint_yaml_path) + +if setting.check_mode.eql? 'fully' and setting.use_multiple.true? + strategy_maker.run_fully_multiple_strategy(setting.source_date) +elsif setting.check_mode.eql? 'fully' and not setting.use_multiple.true? + strategy_maker.run_fully_single_strategy +elsif setting.check_mode.eql? 'simplified' and setting.use_multiple.true? + strategy_maker.run_simplified_multiple_strategy(setting.source_date, setting.source_root_path) +elsif setting.check_mode.eql? 'simplified' and not setting.use_multiple.true? + strategy_maker.run_simplified_single_strategy(setting.source_root_path) +end diff --git a/xcode/build_phases/multiple_swiftlint/yaml_manager.rb b/xcode/build_phases/multiple_swiftlint/yaml_manager.rb new file mode 100644 index 0000000..cb3bbd8 --- /dev/null +++ b/xcode/build_phases/multiple_swiftlint/yaml_manager.rb @@ -0,0 +1,24 @@ +require 'yaml' +require 'fileutils' + +class YamlManager + def initialize(swiftlint_yaml_path) + @swiftlint_yaml_path = swiftlint_yaml_path + @configuration ||= YAML.load(File.read(@swiftlint_yaml_path)) + end + + def get_configuration(key) + @configuration[key] + end + + def update(key, new_configuration_values) + @configuration[key] = new_configuration_values + save_settings(@configuration) + end + + private + + def save_settings(settings) + File.write(@swiftlint_yaml_path, settings.to_yaml) + end +end diff --git a/xcode/commonFastfile b/xcode/commonFastfile index d903469..8564ade 100644 --- a/xcode/commonFastfile +++ b/xcode/commonFastfile @@ -91,7 +91,7 @@ private_lane :addShield do |options| .split(/(?=[A-Z])/) # -> ["Enterprise", "Customer", "Dev1", "Without", "S", "S", "L", "Pinning", "Release"] .map { |v| v.gsub(/[[:lower:]]+/, "") }[1..2] # -> ["E", "C", "D1", "W", "S", "S", "L", "P", "R"] -> ["C", "D1"] .join # -> "CD1" - + begin add_badge( shield: "#{buildDescription}-#{buildNumber}-green", @@ -168,7 +168,9 @@ private_lane :buildArchive do |options| configuration = options[:configuration] xcodeproj_path = options[:xcodeproj_path] - set_xcconfig_for_configuration_of_project(xcconfig_name, configuration, xcodeproj_path) + if configuration != "AppStore" # AppStore uses xcconfig choosen in Xcode + set_xcconfig_for_configuration_of_project(xcconfig_name, configuration, xcodeproj_path) + end gym( clean: true, @@ -186,7 +188,10 @@ private_lane :buildArchive do |options| ) end -lane :createPushCertificate do |options| +lane :CreatePushCertificate do |options| + configuration = get_configuration_for_type(options[:type] || "development") + options = configuration.to_options.merge(options) + certificates_path = File.expand_path "../Certificates" Dir.mkdir(certificates_path) unless File.directory?(certificates_path) diff --git a/xcode/config_generator/render_xcconfigs.rb b/xcode/config_generator/render_xcconfigs.rb index 64d6b8e..e08f18a 100755 --- a/xcode/config_generator/render_xcconfigs.rb +++ b/xcode/config_generator/render_xcconfigs.rb @@ -2,15 +2,13 @@ require 'json' require 'mustache' require 'yaml' -# Usage: render_xcconfigs.rb # -# Result: Adds .xcconfig files to $configs_folder_name directory. +# Usage: render_xcconfigs.rb [] +# +# Result: Adds .xcconfig files to ouptut folder. # Files are only being added and changed, not removed! # It is recommended to remove old .xcconfig files before running this script. - - -# Constants -$configs_folder_name = "TargetConfigurations" +# class String def in_current_dir @@ -23,10 +21,11 @@ configurations_file_path = ARGV[0] temp_configs_data_file_path = "configs_data.json".in_current_dir generator_path = "build_options_helper/helper.py".in_current_dir template_path = "target_xcconfig.mustache".in_current_dir -build_parameters_path = ARGV[1] || "build_parameters.yaml".in_current_dir +build_parameters_path = ARGV[1] +configs_folder_name = ARGV[2] || "TargetConfigurations" # Create config directory if needed -Dir.mkdir($configs_folder_name) unless Dir.exist?($configs_folder_name) +Dir.mkdir(configs_folder_name) unless Dir.exist?(configs_folder_name) # Call python script and generate configs to config file system("python #{generator_path} -bp #{build_parameters_path} -o #{__dir__} -r ios_build_settings -p ios") @@ -147,7 +146,7 @@ targets.each do |target_name, target| } # Create file for every setting in loop - File.open($configs_folder_name + "/" + target_name + config["name"] + ".xcconfig", 'w') { |file| + File.open(configs_folder_name + "/" + target_name + config["name"] + ".xcconfig", 'w') { |file| file.puts(Mustache.render(target_xcconfig_tempate, config_data)) } end