Skip to content

Commit

Permalink
merge internal:refactor refer-checker,add ByteXBuildListener
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzhiqian committed Aug 31, 2020
1 parent 4fa5445 commit b367178
Show file tree
Hide file tree
Showing 38 changed files with 1,765 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void requestNotIncremental() {
if (this.state.compareTo(State.RUNNING) >= 0) {
throw new RuntimeException("You Should request for not incremental before traversing.");
}
if (!this.isIncremental()) {
return;
}
this.isPluginIncremental = false;
this.transformInputs.requestNotIncremental();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ protected void init(TransformContext transformContext) {
super.init(transformContext);
}

@Override
public boolean shouldSaveCache() {
return context.extension.isShouldSaveCache() && super.shouldSaveCache();
}

@Override
protected void afterTransform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
super.afterTransform(transformInvocation);
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ dependencies {
compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
apply from: rootProject.file('gradle/publish.gradle')
38 changes: 23 additions & 15 deletions common/src/main/java/com/ss/android/ugc/bytex/common/AbsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.android.build.api.transform.Transform;
import com.android.build.gradle.AppExtension;
import com.google.common.reflect.TypeToken;
import com.ss.android.ugc.bytex.common.builder.internal.GlobalByteXBuildListener;
import com.ss.android.ugc.bytex.common.configuration.BooleanProperty;
import com.ss.android.ugc.bytex.common.configuration.ProjectOptions;
import com.ss.android.ugc.bytex.common.exception.GlobalWhiteListManager;
Expand All @@ -28,6 +29,16 @@ protected Transform getTransform() {
return new SimpleTransform<>(new BaseContext<>(project, android, extension), this);
}

@Override
public String name() {
return extension == null ? getClass().getSimpleName() : extension.getName();
}

@Override
public BaseExtension getExtension() {
return extension;
}

@Override
public boolean enable(TransformContext transformContext) {
return extension.isEnable() && (extension.isEnableInDebug() || transformContext.isReleaseBuild());
Expand All @@ -40,6 +51,9 @@ public boolean alone() {
if (aloneProperty != null) {
alone = Boolean.parseBoolean(aloneProperty.toString());
}
if (!alone && !transformConfiguration().isIncremental() && BooleanProperty.ENABLE_SEPARATE_PROCESSING_NOTINCREMENTAL.value()) {
alone = true;
}
return alone;
}

Expand All @@ -48,16 +62,9 @@ public boolean isRunningAlone() {
return isRunningAlone;
}

@Override
public boolean shouldSaveCache() {
return extension.isShouldSaveCache() && transformConfiguration().isIncremental();
}

@Override
public final void apply(@NotNull Project project) {
if (!transformConfiguration().isIncremental()) {
System.err.println("[ByteX Warning]:" + this.getClass().getName() + " does not yet support incremental build");
}
GlobalByteXBuildListener.INSTANCE.onByteXPluginApply(project, this);
this.project = project;
this.android = project.getExtensions().getByType(AppExtension.class);
ProjectOptions.INSTANCE.init(project);
Expand All @@ -69,13 +76,6 @@ public final void apply(@NotNull Project project) {
project.getExtensions().add(extension.getName(), extension);
}
onApply(project);
if (BooleanProperty.CHECK_INCREMENTAL_INDEBUG.value()) {
project.afterEvaluate(p -> {
if (!transformConfiguration().isIncremental() && extension.isEnableInDebug()) {
throw new IllegalStateException("ByteX plugin " + extension.getName() + " does not support incremental");
}
});
}
String hookTransformName = hookTransformName();
if (hookTransformName != null) {
TransformHook.inject(project, android, this);
Expand All @@ -94,6 +94,7 @@ public final void apply(@NotNull Project project) {
isRunningAlone = true;
}
}
GlobalByteXBuildListener.INSTANCE.onByteXPluginApplied(this);
}


Expand All @@ -115,7 +116,14 @@ protected E createExtension(Instantiator instantiator, Class<E> clazz) {
protected void onApply(@Nonnull Project project) {
}


@Override
public void startExecute(TransformContext transformContext) {
GlobalByteXBuildListener.INSTANCE.onByteXPluginStart(this);
}

@Override
public void afterExecute() throws Throwable {
GlobalByteXBuildListener.INSTANCE.onByteXPluginFinished(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public TransformContext getTransformContext() {
return transformContext;
}

void setTransformContext(TransformContext transformContext) {
public void setTransformContext(TransformContext transformContext) {
if (transformContext == null) {
this.transformContext = null;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public abstract class CommonPlugin<E extends BaseExtension, X extends BaseContex

@Override
public void startExecute(TransformContext transformContext) {
super.startExecute(transformContext);
context.setTransformContext(transformContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.ss.android.ugc.bytex.common.builder.internal.GlobalByteXBuildListener;
import com.ss.android.ugc.bytex.common.configuration.BooleanProperty;
import com.ss.android.ugc.bytex.common.internal.ITransformPipeline;
import com.ss.android.ugc.bytex.common.internal.TransformFlowerManager;
Expand Down Expand Up @@ -155,29 +156,36 @@ public Map<String, Object> getParameterInputs() {

@Override
public boolean isIncremental() {
boolean result = true;
for (TransformConfiguration config : getConfigurations()) {
if (!config.isIncremental()) {
result = false;
break;
}
}
return result;
return true;
}

public boolean shouldSaveCache() {
return getPlugins().stream().allMatch(IPlugin::shouldSaveCache);
return context.extension.isShouldSaveCache() && getPlugins().stream().allMatch(IPlugin::shouldSaveCache);
}

@Override
public final synchronized void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
try {
GlobalByteXBuildListener.INSTANCE.onByteXPluginTransformStart(this, transformInvocation);
transformInternal(transformInvocation);
GlobalByteXBuildListener.INSTANCE.onByteXPluginTransformFinished(this, null);
} catch (Exception e) {
GlobalByteXBuildListener.INSTANCE.onByteXPluginTransformFinished(this, e);
throw e;
}
}

private void transformInternal(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
super.transform(transformInvocation);
if (!transformInvocation.isIncremental() && transformInvocation.getOutputProvider() != null) {
transformInvocation.getOutputProvider().deleteAll();
}
TransformContext transformContext = getTransformContext(transformInvocation);
init(transformContext);
List<IPlugin> plugins = getPlugins().stream().filter(p -> p.enable(transformContext)).collect(Collectors.toList());
if (plugins.stream().anyMatch(iPlugin -> !iPlugin.transformConfiguration().isIncremental())) {
transformContext.requestNotIncremental();
}

Timer timer = new Timer();
final ITransformPipeline manager = new TransformFlowerManager(transformContext);
Expand All @@ -204,6 +212,7 @@ public final synchronized void transform(TransformInvocation transformInvocation
}
}
transformContext.release();
this.configurations = null;
timer.record("Total cost time = [%s ms]");
if (BooleanProperty.ENABLE_HTML_LOG.value()) {
HtmlReporter.getInstance().createHtmlReporter(getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
import com.ss.android.ugc.bytex.transformer.TransformContext;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface IPlugin {

default String name() {
return getClass().getSimpleName();
}

// plugin是否可用的开关
default BaseExtension getExtension() {
return null;
}

/**
* Determine whether your plugin is enable.
Expand Down Expand Up @@ -54,6 +62,7 @@ default TransformConfiguration transformConfiguration() {
/**
* @return get the TransformFlow that the current plugin runs on
*/
@Nullable
TransformFlow getTransformFlow();


Expand All @@ -62,7 +71,7 @@ default String hookTransformName() {
}

default boolean shouldSaveCache() {
return transformConfiguration().isIncremental();
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ss.android.ugc.bytex.common.builder;

import com.android.build.api.transform.Transform;
import com.android.build.api.transform.TransformInvocation;
import com.ss.android.ugc.bytex.common.IPlugin;
import com.ss.android.ugc.bytex.common.flow.TransformFlowListener;

import org.gradle.api.Project;

import javax.annotation.Nullable;

/**
* Created by yangzhiqian on 2020/8/26<br/>
* Desc:
*/
public interface ByteXBuildListener extends TransformFlowListener {
void onByteXPluginApply(Project project, IPlugin plugin);

void onByteXPluginApplied(IPlugin plugin);

void onProjectBuildStart(Project project);

void onByteXPluginTransformStart(Transform transform, TransformInvocation transformInvocation);

void onByteXPluginStart(IPlugin plugin);

void onByteXPluginFinished(IPlugin plugin);

void onByteXPluginTransformFinished(Transform transform, @Nullable Exception exception);

void onProjectBuildFinished(Project project, @Nullable Throwable throwable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.ss.android.ugc.bytex.common.builder

import com.android.build.gradle.internal.utils.toImmutableList
import com.ss.android.ugc.bytex.common.builder.internal.DefaultByteXBuildListener
import com.ss.android.ugc.bytex.common.flow.main.MainProcessHandlerListener
import com.ss.android.ugc.bytex.common.flow.main.MainProcessHandlerListenerManager
import java.util.*

/**
* Created by yangzhiqian on 2020/8/26<br/>
*/
object ByteXBuildListenerManager {
private val listeners: MutableList<ByteXBuildListener> = LinkedList()

init {
registerByteXBuildListener(DefaultByteXBuildListener)
registerMainProcessHandlerListener(DefaultByteXBuildListener)
}

@Synchronized
fun registerByteXBuildListener(listener: ByteXBuildListener) {
if (listener !in listeners) {
listeners.add(listener)
}
}

@Synchronized
fun unRegisterByteXBuildListener(listener: ByteXBuildListener) {
listeners.remove(listener)
}

fun registerMainProcessHandlerListener(listener: MainProcessHandlerListener) {
MainProcessHandlerListenerManager.registerMainProcessHandlerListener(listener)
}

fun unregisterMainProcessHandlerListener(listener: MainProcessHandlerListener) {
MainProcessHandlerListenerManager.unregisterMainProcessHandlerListener(listener)
}

@Synchronized
internal fun getByteXBuildListeners(): List<ByteXBuildListener> {
return listeners.toImmutableList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ss.android.ugc.bytex.common.builder.internal

import com.google.common.reflect.TypeToken
import com.google.gson.GsonBuilder
import com.ss.android.ugc.bytex.common.IPlugin
import org.gradle.api.Project
import org.gradle.util.GFileUtils
import java.io.File
import java.io.FileWriter
import java.util.*

/**
* Created by yangzhiqian on 2020/8/27<br/>
*/
internal class BuildRecorder {
private val gson = GsonBuilder().setPrettyPrinting().create()
private lateinit var outputDir: File
private val transformInfoRecords = LinkedList<TransformInfoRecord>()
fun start(project: Project) {
outputDir = File(project.buildDir, "ByteX/build")
// GFileUtils.deleteDirectory(outputDir)
outputDir.mkdirs()
}

fun recordTransform(transformInfo: DefaultByteXBuildListener.TransformInfo, pluginStatuses: Map<IPlugin, PluginStatus>) {
transformInfoRecords.add(TransformInfoRecord(transformInfo, pluginStatuses))
}

fun recordPluginStatuses(pluginStatuses: Collection<PluginStatus>) {
val file = File(outputDir, "pluginStatuses.json")
if (!file.exists()) {
file.parentFile.mkdirs()
}
FileWriter(file).use {
gson.toJson(pluginStatuses.sorted(), object : TypeToken<List<PluginStatus>>() {}.type, it)
}
}

fun stop() {
val file = File(outputDir, "transformInfo.json")
if (!file.exists()) {
file.parentFile.mkdirs()
}
FileWriter(file).use {
gson.toJson(transformInfoRecords, object : TypeToken<List<TransformInfoRecord>>() {}.type, it)
}
transformInfoRecords.clear()
}


private class TransformInfoRecord(transformInfo: DefaultByteXBuildListener.TransformInfo, pluginStatuses: Map<IPlugin, PluginStatus>) {
val name = transformInfo.transform.name
val className = transformInfo.transform::class.java.name
val taskName = transformInfo.getTaskName()
var startTime = transformInfo.startTime
var endTime = transformInfo.endTime
var exception = transformInfo.exception?.message
var flowInfos = transformInfo.flowInfos.values.map { TransformFlowInfoRecord(it, pluginStatuses) }.sortedBy {
it.startRunningTime
}

class TransformFlowInfoRecord(flowInfo: DefaultByteXBuildListener.TransformFlowInfo, pluginStatuses: Map<IPlugin, PluginStatus>) {
val handlers = flowInfo.handlers.map { it.value }
val plugins = flowInfo.plugins.map { pluginStatuses[it]!! }
var startPrepareTime = flowInfo.startPrepareTime
var finishPrepareTime = flowInfo.finishPrepareTime
var startRunningTime = flowInfo.startRunningTime
var finishRunningTime = flowInfo.finishRunningTime
}
}
}
Loading

0 comments on commit b367178

Please sign in to comment.