ext { //存储增量的Java文件,给pmd使用 commitJavaFiles = new ArrayList() //存储增量的kotlin文件,给detekt使用 commitKotlinFiles = new ArrayList() commitAuthor = "" appVersionName = "" } /** * 增量开关, 给 lint 插件做增量扫描使用 */ def isIncrement = false /** * 通过cmd命令获取结果值 * @return string 结果值 */ def getResultFromCmd(String cmd) { logger.lifecycle("${project.name} cmd:$cmd") def out = new ByteArrayOutputStream() if (System.properties['os.name'].toLowerCase().contains('windows')) { project.exec { ExecSpec execSpec -> executable 'cmd' args '/c', "$cmd" standardOutput = out }.assertNormalExitValue() } else { // jenkins 打包出异常,需要区分linux和macOS // project.exec { // ExecSpec execSpec -> // executable 'bash' // args '-c', "$cmd" // standardOutput = out // }.assertNormalExitValue() } return out.toString().trim() } /** * 获取提交作者 * @return 提交作者 */ def getCommitAuthor() { def commitAuthor = "" // if (isCiServer()) { // commitAuthor = "${System.env.CI_COMMIT_AUTHOR}" // commitAuthor = commitAuthor.substring(0, commitAuthor.indexOf('<')).trim() // } else { commitAuthor = getResultFromCmd("git config --get user.name") // } return commitAuthor } /** * 获取提交 sha,用于 git diff 出增量文件,其它项目仓库,这个方法需要更改实现 * @return 提交 sha */ def getCommitTargetSha() { //目前这个仓库只能根据上一个merge节点,进行代码增量检测 def commitTargetSha = getResultFromCmd("git log --max-count=1 --pretty=%H --merges") return commitTargetSha } /** * 获取提交文件,只包含.java和.kt的文件 * @return 文件相对路径数组 */ def getCommitFiles() { def commitTargetSha = getCommitTargetSha() def commitSha = getResultFromCmd("git log --max-count=1 --pretty=%H") def commitAuthor = getCommitAuthor() def cmd = "git log --name-only --author=$commitAuthor --pretty=\"format:\" $commitTargetSha..$commitSha" def result = getResultFromCmd(cmd) def projectStartPath = getProjectStartPath() logger.lifecycle("projectStartPath=$projectStartPath") if (result != null && result.trim().length() > 0) { def files = result.readLines() .findAll { it.startsWith("${projectStartPath}") && isFilePathValid(it) }.toArray() return files.toUnique() } return null } /** * 判断文件路径是否合理 * @param path 文件路径 * @return true合理,否则不合理 */ static def isFilePathValid(String path) { return (path.endsWith('.kt') || path.endsWith('.java') || path.endsWith('.xml') || path.endsWith('.png')) && !path.contains('/test/') && !path.contains('/androidTest/') && !path.contains('/generated/') } /** * 获取src目录下的所有文件,只包含.java和.kt的文件 * @return 文件相对路径数组 */ def getAllFiles() { def fileCollection = project.files("src") if (fileCollection == null) { return null } def files = fileCollection.asFileTree.getFiles() def iterator = files.iterator() def allFiles = new ArrayList(files.size()) while (iterator.hasNext()) { File file = iterator.next() def path = getProjectStartPath() + File.separator + project.relativePath(file.path) if (path != null && isFilePathValid(path)) { allFiles.add(path) } } return allFiles.toArray(new Object[allFiles.size()]) } /** * 获取项目名称,如gradleengine,framework/asmbase * @return 项目名称 */ def getProjectStartPath() { def projectRelativePath = project.relativePath(project.path).replaceAll(':', '/') return projectRelativePath.substring(1) } /** * 存储提交文件 * @param commitFiles 提交文件 */ def saveCommitFiles(Object[] commitFiles) { //commitFiles 是对象 def lintBuildDir = new File(project.buildDir, "code_inspect") if (!lintBuildDir.exists()) { lintBuildDir.mkdirs() } def changedFile = new File(lintBuildDir, "commit_files.txt") def fileOutputStream = new FileOutputStream(changedFile) def rootProjectPath = project.rootProject.rootDir.path + File.separator //存储绝对路径 for (int i = 0; i < commitFiles.length; i++) { def commitFile = commitFiles[i].toString() fileOutputStream.write((rootProjectPath + commitFile).getBytes("utf-8")) if (i != commitFiles.length - 1) fileOutputStream.write("\n".getBytes("utf-8")) } fileOutputStream.flush() fileOutputStream.close() logger.lifecycle("Save Commit Files In: ${changedFile.path} \n$commitFiles") } task runCodeInspectIncrement { // group = "codeInspect" // description = "存储增量文件" // def commitJavaFiles = new ArrayList() // def commitKotlinFiles = new ArrayList() // // def commitFiles = new Object[0] // if (isIncrement) { // commitFiles = getCommitFiles() // } else { // commitFiles = getAllFiles() // } // saveCommitFiles(commitFiles) // if (commitFiles != null && commitFiles.length > 0) { // def projectStartPath = getProjectStartPath() // for (String s : commitFiles) { // def commitFile = s.toString().trim() // if (commitFile.endsWith('.kt')) { // commitKotlinFiles.add(commitFile.substring(projectStartPath.length() + 1)) // } else if (commitFile.endsWith('.java')) { // commitJavaFiles.add(commitFile.substring(projectStartPath.length() + 1)) // } // } // } // if (!commitKotlinFiles.isEmpty()) { // logger.lifecycle("${project.name} commitKotlinFiles:$commitKotlinFiles") // } // if (!commitJavaFiles.isEmpty()) { // logger.lifecycle("${project.name} commitJavaFiles:$commitJavaFiles") // } // project.setProperty("commitKotlinFiles", commitKotlinFiles) // project.setProperty("commitJavaFiles", commitJavaFiles) // project.setProperty("commitAuthor", getCommitAuthor()) }