在build.gradle文件内部,获取所有包列表的最简洁方法是SourceSet什么?
我可以通过解析每个源文件来获得它,如下所示 -
def sourceSet = project.sourceSets.findByName('test')
def pattern = Pattern.compile('package (\\S+);')
def packages = []
sourceSet.allSource.each {
for (def scan = new Scanner(it); scan.hasNext();) {
def matcher = scan.nextLine() =~ pattern
if (matcher.matches()) {
packages << matcher[0][1]
break
}
}
}
packages.unique(true)
// allows, e.g.
packages.each { println it }
Run Code Online (Sandbox Code Playgroud)
- 但似乎应该有更好的方法.
编辑添加:我需要这些信息,以便在编译时将其他参数传递给JVM,因此我需要在编译之前从源代码中获取它.
所以我发现自己需要<br />在我正在处理的项目中从字符串的开头和结尾删除标签.我做了一个快速的小方法来完成我需要做的事情,但我不相信这是做这类事情的最好方法.我怀疑可能有一个方便的正则表达式,我可以用它来做几行.这是我得到的:
def remove_breaks(text)
if text != nil and text != ""
text.strip!
index = text.rindex("<br />")
while index != nil and index == text.length - 6
text = text[0, text.length - 6]
text.strip!
index = text.rindex("<br />")
end
text.strip!
index = text.index("<br />")
while index != nil and index == 0
text = test[6, text.length]
text.strip!
index = text.index("<br />")
end
end
return text
end
Run Code Online (Sandbox Code Playgroud)
现在"<br />"可能真的是任何东西,并且制作一个通用的函数可能更有用,该函数将需要从开头和结尾剥离的字符串作为参数.
我对如何使这个更清洁的任何建议持开放态度,因为这似乎可以改进.
我上传的zip文件是19.5MB,但在App Store上,Apple报告它为24.5MB - 对于无线下载来说太大了.Apple是否添加了5MB的包装?减压和再压缩效率较低?使用815K"兆字节"?
简单地说,我有两个列表,我需要提取添加到其中一个的新元素.我有以下内容
val x = List(1,2,3)
val y = List(1,2,4)
val existing :List[Int]= x.map(xInstance => {
if (!y.exists(yInstance =>
yInstance == xInstance))
xInstance
})
Result :existing: List[AnyVal] = List((), (), 3)
Run Code Online (Sandbox Code Playgroud)
我需要以最低成本删除除数字之外的所有其他元素.
我正在尝试将一个链接显示为由rails action mailer呈现的视图中的动作.
mailer.rb
class Mailer < ActionMailer::Base
default from: "foo@bar.com"
def catalog_download_request(email, catalog)
@catalog = catalog
mail({
to: email
})
end
end
Run Code Online (Sandbox Code Playgroud)
的routes.rb
Rails.application.routes.draw do
scope "(:locale)" do
resources :catalogs, :path => I18n.t("routes.catalogs"), only: [:index, :show]
end
end
Run Code Online (Sandbox Code Playgroud)
development.rb:
config.action_mailer.default_url_options = { host: "http://localhost:3000" }
config.action_mailer.asset_host = "http://localhost:3000"
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "localhost",
port: 1025
}
config.action_mailer.raise_delivery_errors = false
Run Code Online (Sandbox Code Playgroud)
我打电话给邮件的模特:
class CatalogDownloadRequest < ActiveRecord::Base
belongs_to :catalog
after_create :send_mail
private
def send_mail
Mailer.catalog_download_request(email, catalog).deliver
end
end
Run Code Online (Sandbox Code Playgroud)
这就是我在我看来尝试的内容: …
在共享项目上工作时,我经常发现我Gemfile.lock与存储库不同步,产生如下错误消息:
$ git pull
Updating 1911275..8c5d26f
error: Your local changes to the following files would be overwritten by merge:
Gemfile.lock
Please commit your changes or stash them before you merge.
Aborting
Run Code Online (Sandbox Code Playgroud)
当我尝试git stash更改时,它不起作用:
$ git stash
Saved working directory and index state WIP on development: 1911275 Merge branch 'development' of https://github.com/mygroup/myrepo into development
HEAD is now at 1911275 Merge branch 'development' of https://github.com/mygroup/myrepo into development
$ git status
On branch development
Your branch is behind 'origin/development' …Run Code Online (Sandbox Code Playgroud) 是否有一种简洁,惯用的方式来创建来自Javaslang/Vavr的订单保留地图List?List.toMap()返回一个普通的HashMap,所以不这样做.我现在拥有的是这样的 -
listOfKeys.foldLeft(
LinkedHashMap.<Key, Value>.empty(),
(m, k) -> m.put(k, valueFor(k))
);
Run Code Online (Sandbox Code Playgroud)
- 但这似乎比必要的更冗长,也可能效率更低.
现在我们有一个硬编码的字符集,我们检查 - :*?"<>|/\- 基本上是Windows抱怨的.但是,在Linux上运行时,限制性太强了.
我知道在Java 7 NIO中,Path该类应该足够智能,以依赖于操作系统的方式检查它,InvalidPathException如果指定了无效的文件名,则抛出一个.但是我们没有运行Java 7.在Java 6中有可靠的方法吗?
(请注意,new File("foo:bar")在Windows中不出现工作.但是你实际上如果,例如,尝试用FileWriter写信给你的新File,被称为空文件foo.exists()在File将在此时返回true,但它或多或少说谎.)
我想实现一个方法,可以从已检出SVN存储库的路径获取svn修订号.方法声明看起来像这样:
long getRevisionNumber(String localPath) { ... }
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用SVNKit,但它似乎需要一个SVN URL开头.有没有办法从本地路径开始?
这是交易:我需要用一些方法扩展class Box的特定实例.我需要包含实时内部模块的方法,我希望Box实例能够动态地包含模块.现在我使用带有eval的钩子:
class Box
def after_initialize
if self.injected_module.present?
eval("class << self; include #{self.injected_module}; end")
end
end
end
Run Code Online (Sandbox Code Playgroud)
它工作得很好但是当我使用eval时我真的感觉很脏.我正在寻找类似的东西:
module_to_inject = self.injected_module
self.eigenclass.class_eval do
include module_to_inject
end
Run Code Online (Sandbox Code Playgroud)
但是我无法让eigenclass在没有monkeypatching类的情况下运行class_eval:
class Box; def eigenclass; class << self; self; end end end
Run Code Online (Sandbox Code Playgroud)
有一种美丽(可靠)的方式让我这样做吗?