Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
07369448
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
07369448
编写于
10月 18, 2015
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revises the implementation of the evented file monitor
上级
087a79ab
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
24 deletion
+65
-24
activesupport/lib/active_support/file_evented_update_checker.rb
...support/lib/active_support/file_evented_update_checker.rb
+65
-24
未找到文件。
activesupport/lib/active_support/file_evented_update_checker.rb
浏览文件 @
07369448
require
'listen'
require
'set'
require
'pathname'
module
ActiveSupport
class
FileEventedUpdateChecker
attr_reader
:listener
def
initialize
(
files
,
directories
=
{},
&
block
)
@files
=
files
.
map
{
|
f
|
File
.
expand_path
(
f
)}.
to_set
@dirs
=
Hash
.
new
directories
.
each
do
|
key
,
value
|
@dirs
[
File
.
expand_path
(
key
)]
=
Array
(
value
)
if
!
Array
(
value
).
empty?
def
initialize
(
files
,
dirs
=
{},
&
block
)
@files
=
files
.
map
{
|
f
|
expand_path
(
f
)}.
to_set
@dirs
=
{}
dirs
.
each
do
|
dir
,
exts
|
@dirs
[
expand_path
(
dir
)]
=
Array
(
exts
).
map
(
&
:to_s
)
end
@block
=
block
@modified
=
false
watch_dirs
=
base_directories
@listener
=
Listen
.
to
(
*
watch_dirs
,
&
method
(
:changed
))
if
!
watch_dirs
.
empty?
@listener
.
start
if
@listener
if
(
watch_dirs
=
base_directories
).
any?
@listener
=
Listen
.
to
(
*
watch_dirs
,
&
method
(
:changed
))
@listener
.
start
end
end
def
updated?
...
...
@@ -30,38 +37,72 @@ def execute_if_updated
if
updated?
execute
true
else
false
end
end
private
def
watching?
(
file
)
return
true
if
@files
.
include?
(
file
)
cfile
=
file
while
!
cfile
.
eql?
"/"
cfile
=
File
.
expand_path
(
"
#{
cfile
}
/.."
)
if
!
@dirs
[
cfile
].
nil?
and
file
.
end_with?
(
*
(
@dirs
[
cfile
].
map
{
|
ext
|
".
#{
ext
.
to_s
}
"
}))
return
true
end
end
false
def
expand_path
(
fname
)
File
.
expand_path
(
fname
)
end
def
changed
(
modified
,
added
,
removed
)
return
if
updated?
if
(
modified
+
added
+
removed
).
any?
{
|
f
|
watching?
f
}
if
(
modified
+
added
+
removed
).
any?
{
|
f
|
watching?
(
f
)}
@modified
=
true
end
end
def
watching?
(
file
)
file
=
expand_path
(
file
)
return
true
if
@files
.
member?
(
file
)
file
=
Pathname
.
new
(
file
)
return
false
if
file
.
directory?
ext
=
file
.
extname
.
sub
(
/\A\./
,
''
)
dir
=
file
.
dirname
loop
do
if
@dirs
.
fetch
(
dir
.
to_path
,
[]).
include?
(
ext
)
break
true
else
if
dir
.
root?
# TODO: find a common parent directory in initialize
break
false
end
dir
=
dir
.
parent
end
end
end
# TODO: Better return a list of non-nested directories.
def
base_directories
(
@files
.
map
{
|
f
|
existing_parent
(
File
.
expand_path
(
"
#{
f
}
/.."
))
}
+
@dirs
.
keys
.
map
{
|
dir
|
existing_parent
(
dir
)}).
uniq
[].
tap
do
|
bd
|
bd
.
concat
@files
.
map
{
|
f
|
existing_parent
(
File
.
dirname
(
f
))}
bd
.
concat
@dirs
.
keys
.
map
{
|
dir
|
existing_parent
(
dir
)}
bd
.
compact!
bd
.
uniq!
end
end
def
existing_parent
(
path
)
File
.
exist?
(
path
)
?
path
:
existing_parent
(
File
.
expand_path
(
"
#{
path
}
/.."
))
def
existing_parent
(
dir
)
dir
=
Pathname
.
new
(
File
.
expand_path
(
dir
))
loop
do
if
dir
.
directory?
break
dir
.
to_path
else
if
dir
.
root?
# Edge case in which not even the root exists. For example, Windows
# paths could have a non-existing drive letter. Since the parent of
# root is root, we need to break to prevent an infinite loop.
break
else
dir
=
dir
.
parent
end
end
end
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录