Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions build-gradle/src/main/groovy/com.dd.buildgradle/ComBuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class ComBuild implements Plugin<Project> {
System.out.println("taskNames is " + taskNames);
String module = project.path.replace(":", "")
System.out.println("current module is " + module);
AssembleTask assembleTask = getTaskInfo(project.gradle.startParameter.taskNames)
AssembleTask assembleTask = getTaskInfo(taskNames)

if (assembleTask.isAssemble) {
fetchMainmodulename(project, assembleTask);
compilemodule = fetchMainmodulename(project, assembleTask);
System.out.println("compilemodule is " + compilemodule);
}

Expand All @@ -46,6 +46,11 @@ public class ComBuild implements Plugin<Project> {
//根据配置添加各种组件依赖,并且自动化生成组件加载代码
if (isRunAlone) {
project.apply plugin: 'com.android.application'
System.out.println("apply plugin is " + 'com.android.application');
if (assembleTask.isAssemble && module.equals(compilemodule)) {
compileComponents(assembleTask, project)
project.android.registerTransform(new ComCodeTransform(project))
}
if (!module.equals(mainmodulename)) {
project.android.sourceSets {
main {
Expand All @@ -55,11 +60,6 @@ public class ComBuild implements Plugin<Project> {
}
}
}
System.out.println("apply plugin is " + 'com.android.application');
if (assembleTask.isAssemble && module.equals(compilemodule)) {
compileComponents(assembleTask, project)
project.android.registerTransform(new ComCodeTransform(project))
}
} else {
project.apply plugin: 'com.android.library'
System.out.println("apply plugin is " + 'com.android.library');
Expand All @@ -85,29 +85,6 @@ public class ComBuild implements Plugin<Project> {

}

/**
* 根据当前的task,获取要运行的组件,规则如下:
* assembleRelease ---app
* app:assembleRelease :app:assembleRelease ---app
* sharecomponent:assembleRelease :sharecomponent:assembleRelease ---sharecomponent
* @param assembleTask
*/
private void fetchMainmodulename(Project project, AssembleTask assembleTask) {
if (!project.rootProject.hasProperty("mainmodulename")) {
throw new RuntimeException("you should set compilemodule in rootproject's gradle.properties")
}
if (assembleTask.modules.size() > 0 && assembleTask.modules.get(0) != null
&& assembleTask.modules.get(0).trim().length() > 0
&& !assembleTask.modules.get(0).equals("all")) {
compilemodule = assembleTask.modules.get(0);
} else {
compilemodule = project.rootProject.property("mainmodulename")
}
if (compilemodule == null || compilemodule.trim().length() <= 0) {
compilemodule = "app"
}
}

private AssembleTask getTaskInfo(List<String> taskNames) {
AssembleTask assembleTask = new AssembleTask();
for (String task : taskNames) {
Expand All @@ -126,6 +103,31 @@ public class ComBuild implements Plugin<Project> {
return assembleTask
}

/**
* 根据当前的task,获取要运行的组件,规则如下:
* assembleRelease ---app
* app:assembleRelease :app:assembleRelease ---app
* sharecomponent:assembleRelease :sharecomponent:assembleRelease ---sharecomponent
* @param assembleTask
*/
private String fetchMainmodulename(Project project, AssembleTask assembleTask) {
String mainModule;
if (!project.rootProject.hasProperty("mainmodulename")) {
throw new RuntimeException("you should set mainmodulename in rootproject's gradle.properties")
}
if (assembleTask.modules.size() > 0 && assembleTask.modules.get(0) != null
&& assembleTask.modules.get(0).trim().length() > 0
&& !assembleTask.modules.get(0).equals("all")) {
mainModule = assembleTask.modules.get(0);
} else {
mainModule = project.rootProject.property("mainmodulename")
}
if (mainModule == null || mainModule.trim().length() <= 0) {
mainModule = "app"
}
return mainModule;
}

/**
* 自动添加依赖,只在运行assemble任务的才会添加依赖,因此在开发期间组件之间是完全感知不到的,这是做到完全隔离的关键
* 支持两种语法:module或者modulePackage:module,前者之间引用module工程,后者使用componentrelease中已经发布的aar
Expand Down