SBT插件:如何列出通过增量重新编译输出的文件 [英] SBT plugin: How to list files output by incremental recompilation

查看:117
本文介绍了SBT插件:如何列出通过增量重新编译输出的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为SBT编写一个插件,该插件需要由Scala编译器的最后一次运行生成的类文件的列表.

I am writing a plugin for SBT that requires a list of the class files generated by the last run of the Scala compiler.

然后,此类文件列表被传递到执行某些字节码转换的程序中.由于此转换过程可能很慢,因此我只希望由Scala编译器的最后一次运行编写的类文件(即经过修改的类文件),而不是输出目录中的所有类文件.

This list of class files is then passed into a program that performs some bytecode transformations. Since this transformation process can be slow, I only want the class files written by the last run of the Scala compiler (i.e. those that there modified), not all class files in the output directory.

如何获取compile任务最后生成的文件的列表?

How can I obtain a list of the files last generated by the compile task?

推荐答案

我认为您不能直接从compile任务返回的Analysis对象中获取此信息.

I think you cannot get this information directly from the Analysis object returned by the compile task.

但是,您可以做的是 检查analysis.relations.allProducts的更改.如果修改了任何文件,则可以执行您的任务,该任务将执行字节码转换.

However, what you could do is to check analysis.relations.allProducts for changes. If any of the files is modified you can execute yours task, which performs bytecode transformations.

您可以使用 FileFunction.cached ,以检查更改.

You could use a modified version of a FileFunction.cached, to check for changes.

def cached(cacheBaseDirectory: File, inStyle: FilesInfo.Style)(action: Set[File] => Unit): Set[File] => Unit = {
  import Path._
  lazy val inCache = Difference.inputs(cacheBaseDirectory / "in-cache", inStyle)
  inputs => {
    inCache(inputs) { inReport =>
      if(!inReport.modified.isEmpty) action(inReport.modified)
    }   
  }     
}

该函数具有以下参数:

  • cacheBaseDirectory-缓存的位置
  • inStyle-有关如何发现更改的描述(请参见 sbt.FilesInfo (可能的选项)
  • action-修改文件后运行的函数.该函数将已修改文件的列表作为参数.
  • cacheBaseDirectory - location of the cache
  • inStyle - description of how the changes should be discovered (see sbt.FilesInfo for possible options)
  • action - a function run, when the files has been modified. The function takes a list of modified files as an argument.

该函数返回另一个函数,该函数仅在修改作为参数传递给它的文件集时才运行.

The function returns another function which is run only if the set of files passed to it as an argument is modified.

val transformBytecode = taskKey[Unit]("Transforms bytecode of modified files")

def cached(cacheBaseDirectory: File, inStyle: FilesInfo.Style)(action: Set[File] => Unit): Set[File] => Unit = {
  import Path._
  lazy val inCache = Difference.inputs(cacheBaseDirectory / "in-cache", inStyle)
  inputs => {
    inCache(inputs) { inReport =>
      if(!inReport.modified.isEmpty) action(inReport.modified)
    }   
  }     
}

transformBytecode <<= Def.task {
  val analysis = (compile in Compile).value
  val cachedFunction = cached(streams.value.cacheDirectory / "transform-bytecode", FilesInfo.lastModified) { modified =>
    // here you want to run the bytecode transformations on `modified` files
    println(s"Modified files $modified")
  }
  cachedFunction(analysis.relations.allProducts.toSet)
}.triggeredBy(compile in Compile)

这篇关于SBT插件:如何列出通过增量重新编译输出的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆