Last active
August 29, 2015 14:26
-
-
Save zhengzhou/caa2ba16255babb0bb2a to your computer and use it in GitHub Desktop.
gradle plugin. link source with aar file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.Plugin | |
import org.gradle.api.Project | |
import org.gradle.api.artifacts.ModuleVersionIdentifier | |
import org.gradle.api.artifacts.ResolvedArtifact | |
import org.gradle.internal.component.external.model.DefaultModuleComponentIdentifier | |
import org.gradle.jvm.JvmLibrary | |
import org.gradle.language.base.artifact.SourcesArtifact | |
import org.gradle.language.java.artifact.JavadocArtifact | |
class AARLinkSourcesPlugin implements Plugin<Project> { | |
public static final String LINK_SOURCES_TASK = 'aarLinkSources' | |
@Override | |
void apply(Project project) { | |
final AARLinkSourcesTask aarLinkSourcesTask; | |
if (project.rootProject.tasks.hasProperty(LINK_SOURCES_TASK)) { | |
aarLinkSourcesTask = project.rootProject.tasks.findByName(LINK_SOURCES_TASK); | |
} else { | |
aarLinkSourcesTask = project.rootProject.tasks.create(LINK_SOURCES_TASK, AARLinkSourcesTask) | |
} | |
project.rootProject.gradle.projectsEvaluated { | |
def artifacts = getProjectDependencies(project) | |
.findAll { it.type.equals("aar") } | |
.unique() | |
def identifiers = artifacts.collect { | |
ModuleVersionIdentifier module = it.moduleVersion.id; | |
DefaultModuleComponentIdentifier.newId(module.group, module.name, module.version) | |
} | |
project.dependencies | |
.createArtifactResolutionQuery() | |
.forComponents(identifiers) | |
.withArtifacts(JvmLibrary, SourcesArtifact, JavadocArtifact) | |
.execute() | |
.getResolvedComponents() | |
.each { | |
it.getArtifacts(SourcesArtifact).each { | |
println it.file.path | |
} | |
it.getArtifacts(SourcesArtifact).each { aarLinkSourcesTask.linkSources it.file } | |
it.getArtifacts(JavadocArtifact).each { aarLinkSourcesTask.linkJavadoc it.file } | |
} | |
aarLinkSourcesTask.executeWithoutThrowingTaskFailure() | |
} | |
} | |
private static Collection<ResolvedArtifact> getProjectDependencies(Project project) { | |
return project | |
.rootProject | |
.allprojects | |
.collectMany { it.configurations } | |
.collectMany { it.resolvedConfiguration.resolvedArtifacts } | |
} | |
} | |
import org.gradle.api.DefaultTask | |
import org.gradle.api.tasks.TaskAction | |
class AARLinkSourcesTask extends DefaultTask { | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
// Interfaces | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
void linkSources(File file) { | |
link file, 'sources' | |
} | |
void linkJavadoc(File file) { | |
link file, 'javadoc' | |
} | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
// Task Actions | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
@TaskAction | |
def linkSources() { | |
if (inputs.getProperties() != null) { | |
outputs.files.each { File xml -> | |
def root = new XmlParser().parse(xml) | |
def path = null; | |
if ((path = inputs.getProperties().get("${xml.name}:sources".toString()))) { | |
appendPath(root.library.first().SOURCES.first(), path) | |
} | |
if ((path = inputs.getProperties().get("${xml.name}:javadoc".toString()))) { | |
appendPath(root.library.first().JAVADOC.first(), path) | |
} | |
new XmlNodePrinter(new PrintWriter(new FileWriter(xml))).print(root) | |
project.logger.debug("Link success: {}", xml.name) | |
println "link success, ${xml.name}" | |
} | |
} | |
} | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
// Tools | |
// ------------------------------------------------------------------------------------------------------------------------------------- | |
private Set<String> processedFiles = new HashSet<String>() | |
private void link(File file, String type) { | |
String name = file.name | |
if (!processedFiles.contains(name)) { | |
project.logger.debug("Link {}: {}", type, name) | |
processedFiles.add(name) | |
int index; | |
if ((index = name.lastIndexOf('.')) != -1) { | |
name = name.substring 0, index | |
} | |
if ((index = name.lastIndexOf('-')) != -1) { | |
name = name.substring 0, index | |
} | |
if (name) { | |
name = name.replace('.', '_').replace('-', '_') | |
File xml = project.rootProject.file("./.idea/libraries/${name}.xml") | |
if (xml.exists() && xml.isFile()) { | |
inputs.property "${xml.name}:${type}".toString(), generatePath(file) | |
outputs.file xml | |
} else { | |
project.logger.warn("No such file: {}", xml.absolutePath) | |
println "No such file ${xml.absolutePath}" | |
} | |
} | |
} | |
} | |
private String generatePath(File file) { | |
String path = file.absolutePath | |
String root = null; | |
if ((root = project.rootDir) && path.startsWith(root)) { | |
path = '$PROJECT_DIR$' + path.substring(root.length()) | |
} else if ((root = System.getProperty("user.home")) && path.startsWith(root)) { | |
path = '$USER_HOME$' + path.substring(root.length()) | |
} | |
path.replaceAll(/\\+/, '/') | |
} | |
static def appendPath(Object node, Object path) { | |
if (node) { | |
node.children().clear() | |
node.appendNode('root', [url: "jar://${path}!/"]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more see https://github.com/xujiaao/AARLinkSources