ruby gem关系可视化

参考 https://ruby-china.org/topics/26502

新建测试项目

rails new app

查看Gemfile.lock

截取部分,文件根据空格缩进来表示依赖层级。

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.3)
      actionpack (= 4.2.3)
      actionview (= 4.2.3)
      activejob (= 4.2.3)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.3)
      actionview (= 4.2.3)
      activesupport (= 4.2.3)
      rack (~> 1.6)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)

脚本

vim gem_graph.rb

File.open('gems.gv','w') do |w|
    w.puts "digraph a{"
    File.open('Gemfile.lock').each do |l|
        l =~ /^( {4,6})([a-zA-Z0-9_-]+)/
        # $1匹配空格 , $2匹配gem名称
        if $1 and $1.length == 4
            @k = $2
        elsif $1 and $1.length == 6
            @v = $2
            #根据样式推断,此时一定存在相对的@k
            w.puts "  \"#{@k}\" -> \"#{@v}\" "
        end
    end
    w.puts "}"
end

ruby gem_graph.rb

生成了gems.gv文件。

安装graphviz

可以打开gems.gv看看效果了
生成png图片。

dot -Tpng -o ./gems.png ./gems.gv

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值