Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
4dec3822
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,发现更多精彩内容 >>
提交
4dec3822
编写于
7月 12, 2019
作者:
X
Xavier Noria
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improves zeitwerk:check
上级
7cf570c2
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
25 addition
and
73 deletion
+25
-73
railties/lib/rails/tasks/zeitwerk.rake
railties/lib/rails/tasks/zeitwerk.rake
+25
-73
未找到文件。
railties/lib/rails/tasks/zeitwerk.rake
浏览文件 @
4dec3822
# frozen_string_literal: true
indent
=
" "
*
2
ensure_classic_mode
=
->
()
do
if
Rails
.
autoloaders
.
zeitwerk_enabled?
abort
<<~
EOS
Please, enable temporarily :classic mode:
# config/application.rb
config.autoloader = :classic
and try again. When all is good, you can delete that line.
EOS
ensure_zeitwerk_mode
=
->
()
do
unless
Rails
.
autoloaders
.
zeitwerk_enabled?
abort
"Please, enable :zeitwerk mode in config/application.rb and try again."
end
end
eager_load
=
->
()
do
Rails
.
configuration
.
eager_load_namespaces
.
each
(
&
:eager_load!
)
end
check_directory
=
->
(
directory
,
parent
,
mismatches
)
do
Dir
.
foreach
(
directory
)
do
|
entry
|
next
if
entry
.
start_with?
(
"."
)
next
if
parent
==
Object
&&
entry
==
"concerns"
abspath
=
File
.
join
(
directory
,
entry
)
if
File
.
directory?
(
abspath
)
||
abspath
.
end_with?
(
".rb"
)
print
"."
cname
=
File
.
basename
(
abspath
,
".rb"
).
camelize
.
to_sym
if
parent
.
const_defined?
(
cname
,
false
)
if
File
.
directory?
(
abspath
)
check_directory
[
abspath
,
parent
.
const_get
(
cname
),
mismatches
]
end
else
mismatches
<<
[
abspath
,
parent
,
cname
]
end
end
end
end
report_mismatches
=
->
(
mismatches
)
do
puts
rails_root_prefix_re
=
%r{
\A
#{
Regexp
.
escape
(
Rails
.
root
.
to_path
)
}
/}
mismatches
.
each
do
|
abspath
,
parent
,
cname
|
relpath
=
abspath
.
sub
(
rails_root_prefix_re
,
""
)
cpath
=
parent
==
Object
?
cname
:
"
#{
parent
.
name
}
::
#{
cname
}
"
puts
indent
+
"Mismatch: Expected
#{
relpath
}
to define
#{
cpath
}
"
end
puts
puts
<<~
EOS
Please revise the reported mismatches. You can normally fix them by adding
acronyms to config/initializers/inflections.rb or renaming the constants.
EOS
puts
"Hold on, I am eager loading the application."
Zeitwerk
::
Loader
.
eager_load_all
end
report_not_checked
=
->
(
not_checked
)
do
...
...
@@ -64,51 +19,48 @@ report_not_checked = ->(not_checked) do
EOS
puts
not_checked
.
each
{
|
dir
|
puts
indent
+
dir
}
not_checked
.
each
{
|
dir
|
puts
"
#{
dir
}
"
}
puts
puts
<<~
EOS
You may verify them manually, or add them to config.eager_load_paths
in config/application.rb and run zeitwerk:check again.
EOS
puts
end
report
=
->
(
mismatches
,
not_checked
)
do
puts
if
mismatches
.
empty?
&&
not_checked
.
empty?
puts
"All is good!"
puts
"Please, remember to delete `config.autoloader = :classic` from config/application.rb."
report
=
->
(
not_checked
)
do
if
not_checked
.
any?
report_not_checked
[
not_checked
]
puts
"Otherwise, all is good!"
else
report_mismatches
[
mismatches
]
if
mismatches
.
any?
report_not_checked
[
not_checked
]
if
not_checked
.
any?
puts
"All is good!"
end
end
namespace
:zeitwerk
do
desc
"Checks project structure for Zeitwerk compatibility"
task
check: :environment
do
ensure_classic_mode
[]
eager_load
[]
ensure_zeitwerk_mode
[]
eager_load_paths
=
Rails
.
configuration
.
eager_load_namespaces
.
map
do
|
eln
|
if
eln
.
respond_to?
(
:config
)
eln
.
config
.
eager_load_paths
.
select
do
|
elp
|
Dir
.
exist?
(
elp
)
begin
eager_load
[]
rescue
NameError
=>
e
if
e
.
message
=~
/expected file .*? to define constant \S+/
abort
$&
.
sub
(
/
#{
Regexp
.
escape
(
Rails
.
root
.
to_s
)
}
./
,
""
)
else
raise
end
end
end
.
compact
.
flatten
mismatches
=
[]
$stdout
.
sync
=
true
eager_load_paths
.
each
do
|
eager_load_path
|
check_directory
[
eager_load_path
,
Object
,
mismatches
]
end
eager_load_paths
=
Rails
.
configuration
.
eager_load_namespaces
.
map
do
|
eln
|
eln
.
config
.
eager_load_paths
if
eln
.
respond_to?
(
:config
)
end
.
compact
.
flatten
not_checked
=
ActiveSupport
::
Dependencies
.
autoload_paths
-
eager_load_paths
not_checked
.
select!
{
|
dir
|
Dir
.
exist?
(
dir
)
}
not_checked
.
reject!
{
|
dir
|
Dir
.
empty?
(
dir
)
}
report
[
mismatches
,
not_checked
]
report
[
not_checked
]
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录