Skip to content

Commit

Permalink
[feature] upgrade gradle version && chang dsl to extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshichang committed Jul 5, 2021
1 parent 8c249f5 commit 15c0fda
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 96 deletions.
File renamed without changes.
16 changes: 16 additions & 0 deletions plugin/PeaPod/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'groovy'

dependencies {
compile gradleApi()
compile localGroovy()
compile fileTree(dir: 'libs', include: ['*.jar'])
}

buildscript {
repositories {
jcenter()
}
dependencies {

}
}
File renamed without changes.
118 changes: 118 additions & 0 deletions plugin/PeaPod/src/main/groovy/pea/pod/Log.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package pea.pod;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Log {

private static LogImp debugLog = new LogImp() {

@Override
public void v(final String tag, final String msg, final Object... obj) {
String log = obj == null ? msg : String.format(msg, obj);
System.out.println(String.format("[VERBOSE][%s]%s", tag, log));
}

@Override
public void i(final String tag, final String msg, final Object... obj) {
String log = obj == null ? msg : String.format(msg, obj);
System.out.println(String.format("[INFO][%s]%s", tag, log));
}

@Override
public void d(final String tag, final String msg, final Object... obj) {
String log = obj == null ? msg : String.format(msg, obj);
System.out.println(String.format("[DEBUG][%s]%s", tag, log));
}

@Override
public void w(final String tag, final String msg, final Object... obj) {
String log = obj == null ? msg : String.format(msg, obj);
System.out.println(String.format("[WARN][%s]%s", tag, log));
}

@Override
public void e(final String tag, final String msg, final Object... obj) {
String log = obj == null ? msg : String.format(msg, obj);
System.out.println(String.format("[ERROR][%s]%s", tag, log));
}

@Override
public void printErrStackTrace(String tag, Throwable tr, String format, Object... obj) {
String log = obj == null ? format : String.format(format, obj);
if (log == null) {
log = "";
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
log += " " + sw.toString();
System.out.println(String.format("[ERROR][%s]%s", tag, log));
}
};

private static LogImp logImp = debugLog;

private Log() {
}

public static void setLogImp(LogImp imp) {
logImp = imp;
}

public static LogImp getImpl() {
return logImp;
}

public static void v(final String tag, final String msg, final Object... obj) {
if (logImp != null) {
logImp.v(tag, msg, obj);
}
}

public static void e(final String tag, final String msg, final Object... obj) {
if (logImp != null) {
logImp.e(tag, msg, obj);
}
}

public static void w(final String tag, final String msg, final Object... obj) {
if (logImp != null) {
logImp.w(tag, msg, obj);
}
}

public static void i(final String tag, final String msg, final Object... obj) {
if (logImp != null) {
logImp.i(tag, msg, obj);
}
}

public static void d(final String tag, final String msg, final Object... obj) {
if (logImp != null) {
logImp.d(tag, msg, obj);
}
}

public static void printErrStackTrace(String tag, Throwable tr, final String format, final Object... obj) {
if (logImp != null) {
logImp.printErrStackTrace(tag, tr, format, obj);
}
}

public interface LogImp {

void v(final String tag, final String msg, final Object... obj);

void i(final String tag, final String msg, final Object... obj);

void w(final String tag, final String msg, final Object... obj);

void d(final String tag, final String msg, final Object... obj);

void e(final String tag, final String msg, final Object... obj);

void printErrStackTrace(String tag, Throwable tr, final String format, final Object... obj);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package greenbean.pods
package pea.pod

/**
* DSL 基础脚本
Expand All @@ -7,7 +7,7 @@ package greenbean.pods
* @description Basic Script
* @since 2018.11.7
*/
abstract class GreenBean extends Script {
abstract class Pea extends Script {

public static ArrayList<Pod> pods = new ArrayList<>()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package greenbean.pods
package pea.pod

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.initialization.Settings
import org.gradle.api.plugins.PluginAware
import pea.pod.extension.PeaPodExt

/**
* Green Bean Pods
* PeaPodPlugin
*
* @since 2018.11.7
* @author jacky
* @author JackyWang since 2018.11.7
*/
class GreenBeanPods implements Plugin<PluginAware> {
class PeaPodPlugin implements Plugin<PluginAware> {

private String dslPath = "greenbean.pods/PodsSpec.groovy"
def TAG = "PeaPodPlugin"
def dslPath = "gradle/pea_pod.gradle"
PeaPodExt peaPodExtension

@Override
void apply(PluginAware pluginAware) {
def isInclude = pluginAware instanceof Settings
def isReplace = pluginAware instanceof Project
if (isInclude) {
includePods(pluginAware)
} else if (isReplace) {
replacePods(pluginAware)
peaPodExtension = project.getExtensions().create("PeaPod", PeaPodExt)
if (pluginAware instanceof Settings) {
includeStage(pluginAware)
} else if (pluginAware instanceof Project) {
projectStage(pluginAware)
}
}

/**
* 加载依赖描述
*/
private void evaluateDSL(String path) {
def shell = new GroovyShell(this.getClass().getClassLoader())
// 加载内部DSL
Expand All @@ -47,11 +45,11 @@ class GreenBeanPods implements Plugin<PluginAware> {
* @param pluginAware
* @return
*/
void includePods(PluginAware pluginAware) {
def includeStage(PluginAware pluginAware) {
def settings = (Settings) pluginAware
def rootDir = settings.rootDir
evaluateDSL(rootDir.path + File.separator + dslPath)
GreenBean.pods.each { pod ->
// evaluateDSL(rootDir.path + File.separator + dslPath)
peaPodExtension.peaPods.each { pod ->
if (pod.on_off) {
if (pod.path == null && pod.absPath == null) {
return
Expand All @@ -67,44 +65,40 @@ class GreenBeanPods implements Plugin<PluginAware> {
def proc = pod.cmd.execute()
def outputStream = new StringBuffer()
proc.waitForProcessOutput(outputStream, System.err)
println "执行Hook命令:" + pod.cmd + " 结果:" + outputStream.toString()
Log.i(TAG, "exec hook cmd:" + pod.cmd + " result:" + outputStream.toString())
}
// 切换分支
// checkout branch.
if (pod.branch != null && !pod.branch.isEmpty()) {
String cmd = "git -C " + path + " checkout " + pod.branch
def proc = cmd.execute()
def outputStream = new StringBuffer()
proc.waitForProcessOutput(outputStream, System.err)
println "模块:" + pod.name + " 检出分支:" + pod.branch + " 结果:" + outputStream.toString()
Log.i(TAG, "module:" + pod.name + " checkout:" + pod.branch + " result:" + outputStream.toString())
}
def projectName = ":" + pod.name
settings.include(projectName)
settings.project(projectName).projectDir = new File(path)
println("被包含的模块 名字:" + pod.name + " 路径:" + path)
Log.i(TAG, "included module name:" + pod.name + " path:" + path)
}
}
}

/**
* 动态替换依赖
* @param pluginAware
* @return
*/
void replacePods(PluginAware pluginAware) {
def projectStage(PluginAware pluginAware) {
def project = (Project) pluginAware
evaluateDSL(project.rootDir.path + File.separator + dslPath)
// evaluateDSL(project.rootDir.path + File.separator + dslPath)
def peaPods = peaPodExtension.peaPods
project.afterEvaluate {
//寻找当前节点
def currentItem = GreenBean.pods.find {
def currentNode = peaPods.find {
project.name == it.name
}
if (currentItem == null) return
if (currentNode == null) return
// 寻找子节点
List<String> seeds = currentItem.seeds
List<String> seeds = currentNode.seeds
if (seeds == null) return
seeds.each { seedName ->
// 寻找子节点
def seed = GreenBean.pods.find {
def seed = peaPods.find {
it.name == seedName
}
if (seed == null) return
Expand All @@ -120,18 +114,23 @@ class GreenBeanPods implements Plugin<PluginAware> {
excludeModule = seed.name
map.put("module", excludeModule)
map.put("group", excludeGroup)
project.configurations.compile.exclude(map)
// project.configurations.compile.exclude(map)
project.configurations.each {
it.exclude(map)
}
} else {
seed.excludes.each {
Map<String, String> map = new HashMap<>()
excludeModule = it
map.put("module", excludeModule)
map.put("group", excludeGroup)
project.configurations.compile.exclude(map)
project.configurations.each {
it.exclude(map)
}
}
}
println("Project:" + currentItem.name + " exclude online dependence,group:" + excludeGroup + " module:" + excludeModule)
// 添加本地依赖
Log.i(TAG, "Project:" + currentNode.name + " exclude online dependence, group:" + excludeGroup + " module:" + excludeModule)
// add local module.
StringBuilder buildTypes = new StringBuilder()
if (seed.buildTypes == null || seed.buildTypes.size() == 0) {
buildTypes.append("api")
Expand All @@ -142,11 +141,11 @@ class GreenBeanPods implements Plugin<PluginAware> {
if (project.configurations.findByName(buildType) != null) {
project.dependencies.add(buildType, project.dependencies.project([path: ":" + seed.name]))
} else {
System.err.println "Replace dependency error , build type " + buildType + " not exists."
Log.e(TAG, "Replace dependency error , build type " + buildType + " not exists.")
}
}
}
println("Project:" + currentItem.name + " add local dependence,config name:" + buildTypes.toString() + " path:" + seed.name)
Log.i(TAG, "Project:" + currentNode.name + " add local dependence, build type:" + buildTypes.toString() + " path:" + seed.name)
}
}
}
Expand Down
Loading

0 comments on commit 15c0fda

Please sign in to comment.