Skip to content

Commit

Permalink
Allow importing protos from dependencies.
Browse files Browse the repository at this point in the history
- If a project compiles proto files, they are packaged as resources in
  the compiled jar along with the class files. protobuf-java artifacts
  have already been including proto definitions for built-in types such
  as Any. This is only done for Java. There is a TODO for Android.
- proto files from dependencies are extracted into a temporary
  directory, which is added to the --proto_path argument of the protoc
  command line, so that they can be imported in the proto files of this
  project, but won't be compiled again.

Resolves #15
  • Loading branch information
zhangkun83 committed Jul 24, 2015
1 parent 47d1430 commit f50d441
Show file tree
Hide file tree
Showing 24 changed files with 334 additions and 173 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ sourceSets {
// In addition to the default 'src/main/proto'
srcDir 'src/main/protobuf'
srcDir 'src/main/protocolbuffers'
// In addition to the default '**/*.proto'
// In addition to the default '**/*.proto' (use with caution).
// Using an extension other than 'proto' is NOT recommended,
// because when proto files are published along with class files, we can
// only tell the type of a file from its extension.
include '**/*.protodevel'
}
java {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if ('install' in gradle.startParameter.taskNames) {
project(':testProject').projectDir = "$rootDir/testProject" as File
include ':testProjectCustomProtoDir'
project(':testProjectCustomProtoDir').projectDir = "$rootDir/testProjectCustomProtoDir" as File
include ':testProjectDependent'
project(':testProjectDependent').projectDir = "$rootDir/testProjectDependent" as File
include ':testProjectAndroid'
project(':testProjectAndroid').projectDir = "$rootDir/testProjectAndroid" as File
}
76 changes: 41 additions & 35 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2015, Alex Antonov. All rights reserved.
* Original work copyright (c) 2015, Alex Antonov. All rights reserved.
* Modified work copyright (c) 2015, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -34,45 +35,50 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

/**
* Created by aantonov on 5/19/14.
* Extracts proto files from a dependency configuration.
*/
class ProtobufExtract extends DefaultTask {

File destDir
/**
* The directory for the extracted files.
*/
File destDir

String configName
/**
* The name of the configuration that contains proto files.
*/
String configName

@TaskAction
def extract() {
project.configurations[configName].files.each { file ->
if (file.path.endsWith('.proto')) {
ant.copy(
file: file.path,
toDir: destDir
)
//generateJavaTask.getSource().create(project.files(file))
} else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) {
ant.unzip(src: file.path, dest: destDir)
} else {
def compression

if (file.path.endsWith('.tar')) {
compression = 'none'
} else
if (file.path.endsWith('.tar.gz')) {
compression = 'gzip'
} else if (file.path.endsWith('.tar.bz2')) {
compression = 'bzip2'
} else {
throw new GradleException(
"Unsupported file type (${file.path}); handles only jar, tar, tar.gz & tar.bz2")
}

ant.untar(
src: file.path,
dest: destDir,
compression: compression)
}
@TaskAction
def extract() {
logger.debug "Extracting protos from configuration ${configName} to ${destDir}"
project.configurations[configName].files.each { file ->
logger.debug "Extracting protos from ${file} to ${destDir}"
if (file.path.endsWith('.proto')) {
project.copy {
includeEmptyDirs(false)
from(file.path)
into(destDir)
}
} else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) {
project.copy {
includeEmptyDirs(false)
from(project.zipTree(file.path)) {
include '**/*.proto'
}
into(destDir)
}
} else if (file.path.endsWith('.tar') || file.path.endsWith('.tar.gz') || file.path.endsWith('.tar.bz2')) {
project.copy {
includeEmptyDirs(false)
from(project.tarTree(file.path)) {
include '**/*.proto'
}
into(destDir)
}
} else {
logger.debug "Skipping unsupported file type (${file.path}); handles only jar, tar, tar.gz & tar.bz2"
}
}
}
}
Loading

0 comments on commit f50d441

Please sign in to comment.