Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
Booster
提交
82919409
B
Booster
项目概览
DiDi
/
Booster
通知
31
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
Booster
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
82919409
编写于
5月 08, 2019
作者:
J
johnsonlee
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor `booster-task-compression`
上级
7c09699f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
59 addition
and
35 deletion
+59
-35
booster-task-all/build.gradle
booster-task-all/build.gradle
+1
-0
booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt
...l/booster/task/compression/CompressionVariantProcessor.kt
+57
-34
settings.gradle
settings.gradle
+1
-1
未找到文件。
booster-task-all/build.gradle
浏览文件 @
82919409
dependencies
{
compile
project
(
':booster-task-artifact'
)
compile
project
(
':booster-task-compression'
)
compile
project
(
':booster-task-dependency'
)
compile
project
(
':booster-task-permission'
)
}
booster-task-compression/src/main/kotlin/com/didiglobal/booster/task/compression/CompressionVariantProcessor.kt
浏览文件 @
82919409
...
...
@@ -7,24 +7,18 @@ import com.didiglobal.booster.gradle.processedRes
import
com.didiglobal.booster.gradle.project
import
com.didiglobal.booster.gradle.scope
import
com.didiglobal.booster.kotlinx.execute
import
com.didiglobal.booster.kotlinx.file
import
com.didiglobal.booster.kotlinx.touch
import
com.didiglobal.booster.task.spi.VariantProcessor
import
com.didiglobal.booster.util.FileFinder
import
com.google.auto.service.AutoService
import
java.io.File
import
java.io.PrintWriter
import
java.text.DecimalFormat
import
java.util.zip.ZipEntry
import
java.util.zip.ZipFile
import
java.util.zip.ZipInputStream
import
java.util.zip.ZipOutputStream
val
NO_COMPRESS
=
setOf
(
"jpg"
,
"jpeg"
,
"png"
,
"gif"
,
"wav"
,
"mp2"
,
"mp3"
,
"ogg"
,
"aac"
,
"mpg"
,
"mpeg"
,
"mid"
,
"midi"
,
"smf"
,
"jet"
,
"rtttl"
,
"imy"
,
"xmf"
,
"mp4"
,
"m4a"
,
"m4v"
,
"3gp"
,
"3gpp"
,
"3g2"
,
"3gpp2"
,
"amr"
,
"awb"
,
"wma"
,
"wmv"
,
"webm"
,
"mkv"
)
/**
* Represents a variant processor for processed resources compression
*
...
...
@@ -33,50 +27,79 @@ val NO_COMPRESS = setOf(
@AutoService
(
VariantProcessor
::
class
)
class
CompressionVariantProcessor
:
VariantProcessor
{
private
lateinit
var
logger
:
PrintWriter
override
fun
process
(
variant
:
BaseVariant
)
{
variant
.
preBuild
.
doFirst
{
logger
=
variant
.
project
.
buildDir
.
file
(
"reports"
,
Build
.
ARTIFACT
,
variant
.
name
,
"report.txt"
).
touch
().
printWriter
()
}
variant
.
project
.
tasks
.
withType
(
ProcessAndroidResources
::
class
.
java
).
findByName
(
"process${variant.name.capitalize()}Resources"
)
?.
doLast
{
compressProcessedRes
(
variant
)
}
variant
.
assemble
.
doLast
{
logger
.
close
()
}
}
private
fun
compressProcessedRes
(
variant
:
BaseVariant
)
{
FileFinder
(
variant
.
scope
.
processedRes
)
{
val
files
=
FileFinder
(
variant
.
scope
.
processedRes
)
{
it
.
name
.
startsWith
(
SdkConstants
.
FN_RES_BASE
)
&&
it
.
extension
==
SdkConstants
.
EXT_RES
}.
execute
().
forEach
{
ap_
->
}.
execute
()
val
maxWidth
=
files
.
map
{
it
.
name
.
length
}.
max
()
?:
0
val
base
=
variant
.
project
.
projectDir
.
toURI
()
files
.
parallelStream
().
forEach
{
ap_
->
val
s0
=
ap_
.
length
().
toDouble
()
ap_
.
repack
{
!
NO_COMPRESS
.
contains
(
it
.
name
.
substringAfterLast
(
'.'
))
}
val
s1
=
ap_
.
length
().
toDouble
()
logger
.
println
(
"${base.relativize(ap_.toURI()).path} ${"
".repeat(maxWidth - ap_.name.length)}: ${decimal(s0 - s1)} bytes (${percentage((s0 - s1) * 100 / s0)})"
)
}
}
}
private
fun
File
.
repack
(
shouldCompress
:
(
ZipEntry
)
->
Boolean
)
{
val
dest
=
File
.
createTempFile
(
SdkConstants
.
FN_RES_BASE
+
SdkConstants
.
RES_QUALIFIER_SEP
,
SdkConstants
.
DOT_RES
)
internal
fun
File
.
repack
(
shouldCompress
:
(
ZipEntry
)
->
Boolean
)
{
val
dest
=
File
.
createTempFile
(
SdkConstants
.
FN_RES_BASE
+
SdkConstants
.
RES_QUALIFIER_SEP
,
SdkConstants
.
DOT_RES
)
ZipOutputStream
(
dest
.
outputStream
()).
use
{
output
->
ZipFile
(
this
).
use
{
zip
->
zip
.
entries
().
asSequence
().
forEach
{
origin
->
val
target
=
ZipEntry
(
origin
.
name
).
apply
{
size
=
origin
.
size
crc
=
origin
.
crc
comment
=
origin
.
comment
extra
=
origin
.
extra
method
=
if
(
shouldCompress
(
origin
))
ZipEntry
.
DEFLATED
else
origin
.
method
}
ZipOutputStream
(
dest
.
outputStream
()).
use
{
output
->
ZipFile
(
this
).
use
{
zip
->
zip
.
entries
().
asSequence
().
forEach
{
origin
->
val
target
=
ZipEntry
(
origin
.
name
).
apply
{
size
=
origin
.
size
crc
=
origin
.
crc
comment
=
origin
.
comment
extra
=
origin
.
extra
method
=
if
(
shouldCompress
(
origin
))
ZipEntry
.
DEFLATED
else
origin
.
method
output
.
putNextEntry
(
target
)
zip
.
getInputStream
(
origin
).
use
{
it
.
copyTo
(
output
)
}
output
.
closeEntry
()
}
output
.
putNextEntry
(
target
)
zip
.
getInputStream
(
origin
).
use
{
it
.
copyTo
(
output
)
}
output
.
closeEntry
()
}
}
}
if
(
this
.
delete
())
{
if
(!
dest
.
renameTo
(
this
))
{
dest
.
copyTo
(
this
,
true
)
if
(
this
.
delete
())
{
if
(!
dest
.
renameTo
(
this
))
{
dest
.
copyTo
(
this
,
true
)
}
}
}
}
private
val
NO_COMPRESS
=
setOf
(
"jpg"
,
"jpeg"
,
"png"
,
"gif"
,
"wav"
,
"mp2"
,
"mp3"
,
"ogg"
,
"aac"
,
"mpg"
,
"mpeg"
,
"mid"
,
"midi"
,
"smf"
,
"jet"
,
"rtttl"
,
"imy"
,
"xmf"
,
"mp4"
,
"m4a"
,
"m4v"
,
"3gp"
,
"3gpp"
,
"3g2"
,
"3gpp2"
,
"amr"
,
"awb"
,
"wma"
,
"wmv"
,
"webm"
,
"mkv"
)
private
val
percentage
:
(
Number
)
->
String
=
DecimalFormat
(
"#,##0.00 '%'"
)
::
format
private
val
decimal
:
(
Number
)
->
String
=
DecimalFormat
(
"#,##0"
)
::
format
settings.gradle
浏览文件 @
82919409
...
...
@@ -11,9 +11,9 @@ include ':booster-android-gradle-v3_3'
include
':booster-gradle-plugin'
include
':booster-task-all'
include
':booster-task-artifact'
include
':booster-task-compression'
include
':booster-task-dependency'
include
':booster-task-permission'
include
':booster-task-compression'
include
':booster-task-spi'
include
':booster-transform-all'
include
':booster-transform-asm'
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录