From edf04d890af708f90678a95d4cd3068b06c33876 Mon Sep 17 00:00:00 2001 From: LinYYY Date: Wed, 1 Mar 2023 15:30:22 +0800 Subject: [PATCH] #0 v2.0.0 --- build.gradle | 6 +- .../com/plugin/component/Constants.groovy | 29 ++++ .../com/plugin/component/Runtimes.groovy | 8 +- .../plugin/component/plugin/SdkPlugin.groovy | 144 +++++++++++++++++- .../plugin/component/utils/FileUtil.groovy | 19 ++- .../com/plugin/component/utils/JarUtil.groovy | 36 +---- .../component/utils/PublicationUtil.groovy | 3 +- hooks/pre-push | 20 +++ settings.gradle | 6 +- 9 files changed, 214 insertions(+), 57 deletions(-) create mode 100755 hooks/pre-push diff --git a/build.gradle b/build.gradle index 6454468..c12d777 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ apply from: "./gradleScript/config.gradle" buildscript { ext.kotlin_version = '1.6.21' - ext.component_version = '1.0.13' + ext.component_version = '2.0.0' repositories { google() @@ -11,7 +11,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:4.1.0' - classpath "com.github.DSAppTeam:ComponentCornerstone:$component_version" +// classpath "com.github.DSAppTeam:ComponentCornerstone:$component_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" //调试需要 // classpath "com.effective.plugins:component-plugin" @@ -47,4 +47,4 @@ allprojects { // delete rootProject.buildDir //} // -apply from: "./gradleScript/component.gradle" +//apply from: "./gradleScript/component.gradle" diff --git a/component-plugin/src/main/groovy/com/plugin/component/Constants.groovy b/component-plugin/src/main/groovy/com/plugin/component/Constants.groovy index d2e8d92..fe5336d 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/Constants.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/Constants.groovy @@ -66,6 +66,35 @@ branch=\$(git symbolic-ref --short -q HEAD) strB="BUILD SUCCESSFUL" echo "current branch:\$branch" + +#尝试读取指定的JAVA_HOME配置 +config="./local.properties" +echo "config path: \${config}" +# 定义一个函数从properties文件读取key +function prop { +\t[ -f "\$config" ] && grep -E "^\\s*[^#]?\${1}=.*\$" \$config | cut -d'=' -f2 +} + +hookJavaHome=\$(prop "hook.gradle.jdk.home") +echo "\$hookJavaHome" +if [ -n "\$hookJavaHome" ]; then + export JAVA_HOME="\$hookJavaHome" +fi +if [ -z "\$hookJavaHome" ]; then + echo "hookJavaHome is empty" + config="./gradle.properties" + echo "config path: \${config}" + + spJavaHome=\$(prop "org.gradle.java.home") + echo "\$spJavaHome" + if [ -n "\$spJavaHome" ]; then + export JAVA_HOME="\$spJavaHome" + fi + if [ -z "\$spJavaHome" ]; then + echo "spJavaHome is empty" + fi +fi + tempLog=/tmp/\$(date +%s).log && touch \$tempLog if [[ "\$branch" =~ (^develop\$)|(^release/*) ]]; then ./gradlew ComponentPublishDebug | tee \$tempLog diff --git a/component-plugin/src/main/groovy/com/plugin/component/Runtimes.groovy b/component-plugin/src/main/groovy/com/plugin/component/Runtimes.groovy index 55b65a3..80cb452 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/Runtimes.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/Runtimes.groovy @@ -126,7 +126,11 @@ class Runtimes { osw.flush() osw.close() } - FileUtil.shell("git config core.hooksPath ${hooksDir.absolutePath} && chmod 700 ${hooksDir.absolutePath}/*") + String hooksDirPath = hooksDir.absolutePath + String cmd = "git config core.hooksPath ${hooksDirPath} && chmod 700 ${hooksDirPath}${File.separator}*" + Logger.buildOutput("hooks Path:${hooksDirPath}") + Logger.buildOutput("hooks cmd:${cmd}") + FileUtil.shell(cmd) } } @@ -205,7 +209,7 @@ class Runtimes { } } PublicationUtil.filterPublicationDependencies(publication) - if (!publication.forceLocal && publication.sdkVersion != null) { + if (/*!publication.forceLocal && */publication.sdkVersion != null) { mutLineLog.build4("* " + JarUtil.handleMavenJar(childProject, publication)) } else { mutLineLog.build4("* " + JarUtil.handleLocalJar(childProject, publication)) diff --git a/component-plugin/src/main/groovy/com/plugin/component/plugin/SdkPlugin.groovy b/component-plugin/src/main/groovy/com/plugin/component/plugin/SdkPlugin.groovy index 73e29e0..9e8aebf 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/plugin/SdkPlugin.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/plugin/SdkPlugin.groovy @@ -73,6 +73,8 @@ class SdkPlugin extends BasePlugin { // handleImpAar(root) List topSort = PublicationManager.getInstance().dependencyGraph.topSort() Collections.reverse(topSort) + boolean isAssemble = false + boolean isPublish = false topSort.each { PublicationOption publication = PublicationManager.getInstance().publicationDependencies.get(it) if (publication == null) { @@ -81,12 +83,14 @@ class SdkPlugin extends BasePlugin { Project childProject = root.findProject(publication.project) ProjectInfo projectInfo = Runtimes.getProjectInfo(childProject.name) if (projectInfo.isAssemble) { + isAssemble = true if (Runtimes.sSdkOption.isPublishMode) { Runtimes.checkPublishEnable(childProject) } PublicationUtil.handleSdkPublish(childProject, ComponentPlugin.androidProject, publication, null) handleImpAar(childProject, publication) } else if (projectInfo.isPublish) { + isPublish = true publication.impNeedPublish = true publication.sdkNeedPublish = true def publishTask = null @@ -100,7 +104,131 @@ class SdkPlugin extends BasePlugin { PublicationManager.getInstance().addImpPublication(publication) } } - dealAssemble(root) + if (isAssemble) { + dealAssemble(root) + } else if (isPublish) { + dealPublish(root) + } else { + dealAssemble(root) + } + } + + private dealPublish(Project root) { + MutLineLog mutLineLog = new MutLineLog() + mutLineLog.build4("开始处理Publish场景") + //收集所有模块的引用配置 + ArrayList allConfigList = new ArrayList<>() + root.allprojects.each { project -> + if (project == root) return + if (!Runtimes.shouldApplyComponentPlugin(project)) return + project.configurations.each { config -> + if (config.name == "api" || config.name == "implementation" + || config.name == "compileOnly" + || config.name == "debugImplementation" + || config.name == "debugApi" + || config.name == "debugCompileOnly") { + allConfigList.add(config) + } + } + } + //收集project之间的引用依赖关系 收集被依赖的project,需要参与编译 + HashMap> needCompileProjectConfigMap = new HashMap<>() + HashMap> aarList = new HashMap<>() + root.allprojects.each { project -> + if (project == root) return + allConfigList.each { config -> + for (Dependency dependency : config.dependencies) { + if (dependency instanceof DefaultProjectDependency && dependency.dependencyProject.name == project.name) { + ArrayList configs = needCompileProjectConfigMap.get(project) + if (configs == null) { + configs = new ArrayList<>() + needCompileProjectConfigMap.put(project, configs) + } + configs.add(config) + //一个config只需要添加一次就好了 + return + } + } + } + PublicationOption publication = Runtimes.getImplPublication(project.name) + if (publication != null && !publication.impNeedPublish) { + ProjectInfo projectInfo = Runtimes.getProjectInfo(project.name) + Map map = new LinkedHashMap<>() + map.put("path", PublicationUtil.getImpMavenGAV(publication, !projectInfo.isDebug)) + aarList.put(project.name, map) + } + } + + if (!aarList.isEmpty()) { + root.allprojects.each { project -> + if (project == root) return + // 替换成aar文件 + aarList.each { map -> + //剔除所有配置中的不需要编译模块的依赖 + project.configurations.each { config -> + config.dependencies.removeAll { dependency -> + dependency instanceof DefaultProjectDependency && dependency.dependencyProject.name == map.key + } + } + project.dependencies { + api map.value.get("path") + } + } + } + aarList.each { map -> + allConfigList.each { config -> + config.dependencies.removeAll { dependency -> + dependency instanceof DefaultProjectDependency && dependency.dependencyProject.name == map.key + } + } + } + List> needAndDependency = new ArrayList<>() + //分离所有dependency 进行预处理 + allConfigList.each { config -> + config.dependencies.each { dependency -> + if (dependency instanceof DefaultProjectDependency) { + def dependencyClone = dependency.copy() + dependencyClone.targetConfiguration = null + Map map = new HashMap<>() + map.put(config.name, dependencyClone) + needAndDependency.add(map) + } else { + if (dependency instanceof DefaultSelfResolvingDependency && (dependency.files instanceof DefaultConfigurableFileCollection || dependency.files instanceof DefaultConfigurableFileTree)) { + // 这里的依赖是以下两种: 无需添加在 编译project ,因为 jar 包直接进入 自身的 aar 中的libs 文件夹 + // implementation rootProject.files("libs/*.jar") + // implementation fileTree(dir: "libs", include: ["*.jar"]) + + } else { + Map map = new HashMap<>() + map.put(config.name, dependency) + needAndDependency.add(map) + } + } + + } + } + //遍历所有需要编译的project 将引用传递至需要编译的project中,module引用只需要传递api方式引用的 + needCompileProjectConfigMap.each { entry -> + if (entry.key == root) return + needAndDependency.each { map -> + Map.Entry config = map.entrySet().first() + Dependency dependency = config.value + if (config.key == "api") { + if (dependency instanceof DefaultProjectDependency && dependency.dependencyProject.name == entry.key.name) { + return + } + entry.key.dependencies.add(config.key, config.value) + } else { + //不传递implement project + if (dependency instanceof DefaultProjectDependency) { + return + } + entry.key.dependencies.add(config.key, config.value) + } + } + } + } + Logger.buildBlockLog("处理Publish场景", mutLineLog) } private static void handleImpAar(Project project, PublicationOption publication) { @@ -209,10 +337,10 @@ class SdkPlugin extends BasePlugin { } private void handleCompileModuleDependency(ProjectInfo compileProject, Project root, HashSet hasResolve, MutLineLog mutLineLog) { - StringBuilder stringBuilder = new StringBuilder() + //收集所有模块的引用配置 ArrayList allConfigList = new ArrayList<>() - compileProject.project.rootProject.allprojects.each { project -> + root.allprojects.each { project -> if (project == root) return if (!Runtimes.shouldApplyComponentPlugin(project)) return project.configurations.each { config -> @@ -227,7 +355,7 @@ class SdkPlugin extends BasePlugin { } //收集project之间的引用依赖关系 收集被依赖的project,需要参与编译 HashMap> needCompileProjectConfigMap = new HashMap<>() - compileProject.project.rootProject.allprojects.each { project -> + root.allprojects.each { project -> if (project == root) return allConfigList.each { config -> for (Dependency dependency : config.dependencies) { @@ -246,7 +374,7 @@ class SdkPlugin extends BasePlugin { } //剔除被收集的编译工程的引用 allConfigList.clear() - compileProject.project.rootProject.allprojects.each { project -> + root.allprojects.each { project -> if (project == root) return if (!Runtimes.shouldApplyComponentPlugin(project)) return project.configurations.each { config -> @@ -261,6 +389,7 @@ class SdkPlugin extends BasePlugin { } } HashMap> aarList = new HashMap<>() + StringBuilder stringBuilder = new StringBuilder() for (String realDependency : hasResolve) { PublicationOption publication = Runtimes.getImplPublication(realDependency) Logger.buildOutput("resolve publication ${publication}") @@ -296,8 +425,9 @@ class SdkPlugin extends BasePlugin { needCompileProjectConfigMap.put(Runtimes.getProjectInfo(realDependency).project, new ArrayList()) } } + mutLineLog.build4("application[" + compileProject.name + "] component 合并依赖 = " + stringBuilder.toString()) if (compileProject.isDebug && !aarList.isEmpty()) { - compileProject.project.rootProject.allprojects.each { project -> + root.allprojects.each { project -> if (project == root) return Logger.buildOutput("replace aar:project: $project.name") // 替换成aar文件 @@ -372,6 +502,6 @@ class SdkPlugin extends BasePlugin { } } } - mutLineLog.build4("application[" + compileProject.name + "] component 合并依赖 = " + stringBuilder.toString()) + } } diff --git a/component-plugin/src/main/groovy/com/plugin/component/utils/FileUtil.groovy b/component-plugin/src/main/groovy/com/plugin/component/utils/FileUtil.groovy index 24abce9..d71c08d 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/utils/FileUtil.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/utils/FileUtil.groovy @@ -2,6 +2,7 @@ package com.plugin.component.utils import com.plugin.component.ComponentPlugin import com.plugin.component.Constants +import com.plugin.component.log.Logger import org.gradle.api.Project class FileUtil { @@ -82,12 +83,18 @@ class FileUtil { } static String shell(String cmd) { - def out = new ByteArrayOutputStream() - ComponentPlugin.rootProject.exec { - executable 'bash' - args '-c', cmd - standardOutput = out + try { + def out = new ByteArrayOutputStream() + ComponentPlugin.rootProject.exec { + executable 'bash' + args '-c', cmd + standardOutput = out + } + return out.toString().trim() + } catch (Exception e) { + Logger.buildOutput("bash error: ${e.getMessage()}") + return cmd.execute() } - return out.toString().trim() + } } diff --git a/component-plugin/src/main/groovy/com/plugin/component/utils/JarUtil.groovy b/component-plugin/src/main/groovy/com/plugin/component/utils/JarUtil.groovy index 04474b4..519e9e7 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/utils/JarUtil.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/utils/JarUtil.groovy @@ -323,45 +323,11 @@ class JarUtil { String fileName = publication.artifactId + "-" + publication.sdkVersion + ".jar" // http://172.16.xxx.xxx:8081/nexus/content/groups/public/com/xxx/cif/xxx-cif-api/0.0.1-SNAPSHOT/xxx-cif-api-0.0.1-20170515.040917-89.jar String url = Runtimes.sSdkOption.getMavenUrl() + if (TextUtils.isEmpty(url)) return false String line = HttpUrlConnectHelper.sendRequest("$url/${publication.groupId.replace('.', '/')}/${publication.artifactId}/${publication.sdkVersion}/$fileName", "HEAD") if (!TextUtils.isEmpty(line)) { filePath = "$url/${publication.groupId.replace('.', '/')}/${publication.artifactId}/${publication.sdkVersion}/$fileName" } -// def name = "component[${publication.groupId}-${publication.artifactId}]Classpath" -// Configuration configuration = project.configurations.create(name) -// if (publication.dependencies != null) { -// if (publication.dependencies.implementation != null) { -// publication.dependencies.implementation.each { dependency -> -//// Logger.buildOutput("packJar: publication:${PublicationUtil.getPublicationId(publication)} move dependencies:${it}") -// if (dependency instanceof PublicationDependencyModuleOption) { -// project.dependencies.add(name, dependency.path) -// } else { -// project.dependencies.add(name, dependency) -// } -// } -// } -// if (publication.dependencies.compileOnly != null) { -// publication.dependencies.compileOnly.each { dependency -> -// if (dependency instanceof PublicationDependencyModuleOption) { -// project.dependencies.add(name, dependency.path) -// } else { -// project.dependencies.add(name, dependency) -// } -// } -// } -// } -// project.dependencies.add(name, PublicationUtil.getMavenGAV(publication)) -// try { -// configuration.copy().files.each { -// if (it.name.endsWith(fileName)) { -// filePath = it.absolutePath -// } -// } -// } catch (Exception e) { -//// e.printStackTrace() -// Logger.buildOutput(e.getMessage()) -// } -// project.configurations.remove(configuration) return filePath != null } diff --git a/component-plugin/src/main/groovy/com/plugin/component/utils/PublicationUtil.groovy b/component-plugin/src/main/groovy/com/plugin/component/utils/PublicationUtil.groovy index 15a2f00..96dd046 100644 --- a/component-plugin/src/main/groovy/com/plugin/component/utils/PublicationUtil.groovy +++ b/component-plugin/src/main/groovy/com/plugin/component/utils/PublicationUtil.groovy @@ -506,12 +506,13 @@ class PublicationUtil { } static void handleImplPublish(Project project, PublicationOption publication, boolean isDebug, Task dependTask) { - Logger.buildOutput("Handle ${publication.name} ImplPublish,isDebug:${isDebug}") boolean isAarExists = AarUtil.isArrExits(project, publication, !isDebug) if (isAarExists) { publication.impNeedPublish = false + Logger.buildOutput("Handle ${publication.name} ImplPublish,isDebug:${isDebug} impNeedPublish:${publication.impNeedPublish}") return } + Logger.buildOutput("Handle ${publication.name} ImplPublish,isDebug:${isDebug} impNeedPublish:${publication.impNeedPublish}") project.plugins.apply(Constants.PLUGIN_MAVEN_PUBLISH) if (isDebug) { //注册打包arr task diff --git a/hooks/pre-push b/hooks/pre-push new file mode 100755 index 0000000..75f0a36 --- /dev/null +++ b/hooks/pre-push @@ -0,0 +1,20 @@ +#!/bin/sh + +remote="$1" +url="$2" +branch=$(git symbolic-ref --short -q HEAD) + +strB="BUILD SUCCESSFUL" +echo "current branch:$branch" +tempLog=/tmp/$(date +%s).log && touch $tempLog +if [[ "$branch" =~ (^develop$)|(^release/*) ]]; then + ./gradlew ComponentPublishDebug | tee $tempLog + result=$(cat $tempLog) + checkStr=$(echo "$result" | grep "${strB}") + if [[ "$checkStr" != "" ]]; then + exit 0; + else + exit 1; + fi +fi +exit 0; diff --git a/settings.gradle b/settings.gradle index 528ff72..0f76c8a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,11 +2,11 @@ //include ':component-core' //发布插件 -//include ':component-plugin' +include ':component-plugin' //调试插件 //includeBuild './component-plugin' -include ':app'/*, ':pins'*/ +//include ':app'/*, ':pins'*/ ////include ':debugModule' -include ':library', ':libraryKotlin', ':libraryWithoutPlugin' +//include ':library', ':libraryKotlin', ':libraryWithoutPlugin'