https://github.com/adamsanderson/qwandry
qwandry 能高速定位到我们须要找到 库文件, 项目 的工具。
Ruby中实现高速定位的方法有好多种。我知道的有三个:
- 使用bundle
命令是
cd `bundle show activerecord`
这种方法不方便的地方是 仅仅能在支持bundle的环境下执行,并且仅仅能打开指定的gem文件夹
- 通过tag方法(tag 定位更精确,能够定位到方法级别)
局限: 仅仅能在相应的编辑器里执行
- 或者通过 qwandry
安装
gem install qwandry
使用
qw matrix # opens ruby's matrix class in your editor qw rails # will ask you which version of rails you want to open qw activerec 3.1 # will find the gem activerecord 3.1 and open it You can also use Qwandry with other common languages: qw -r python numpy # opens python's numpy library qw -r perl URI # open perl's URI library qw -r node express # open express if it is installed for node
指定编辑器打开
EDITOR=subl qw activerecord 3.2.14
怎样自己定义?
touch ~/.qwandry/init.rb
然后copy例如以下内容到文件里
register 'projects' do add 'your project path' end default :ruby, :gem, :projects
解释
register 方法是 将指定的文件夹打包
add 将文件夹增加到搜索中
default 是设置默认的搜索范围
实现的基本原理
- 通过配置 config 将非常多文件夹打包成 Package, 然后将 Package 打包成 Repository(仓库)
- 初始化一个Launcher(有Editor等)
- 依据输入的名称找到相应的Repository中的package(实际上是一个文件夹地址)
- 运行系统命令: editor(vim) path
源码分析
qwandry中比較重要的几个类
Repository
是一个基类,职责是存储全部的能够搜索的库文件夹和名称. 继承与它的子类必须实现 scan 方法。
它有两个子类: LibraryRepository 和 FlatRepository
Configuration
是一个用于配置搜索库的文件夹的类,能够动态的加入新的搜索路径。 实现的方法比較track, 用的是万恶得 eval 方法。
Launcher
是用于打开指定文件夹的关键类。它有两个关键方法: find 和 launch
find方法的实现
# Searches all of the loaded repositories for `name` def find(*pattern) # Create a glob pattern from the user's input, for instance # ["rails","2.3"] => "rails*2.3*" pattern = pattern.join('*') pattern << '*' unless pattern =~ /*$/ packages = [] repositories = Qwandry::Configuration.repositories repositories.each do |repo| packages.concat(repo.scan(pattern)) end differentiate packages packages end
就是从仓库中找到合适得 Package
launch 方法的实现
# Launches a Package or path represented by a String. Unless `editor` will # check against the environment by default. def launch(package, editor=nil) editor ||= @editor || ENV['QWANDRY_EDITOR'] || ENV['VISUAL'] || ENV['EDITOR'] if (!editor) || (editor =~ /^s*$/) # if the editor is not set, or is blank, exit with a message: puts "Please set QWANDRY_EDITOR, VISUAL or EDITOR, or pass in an editor to use" exit 1 end paths = package.is_a?(String) ? [package] : package.paths # Editors may have options, 'mate -w' for instance editor_and_options = editor.strip.split(/s+/) Dir.chdir(File.dirname paths.first) do # Launch the editor with its options and any paths that we have been passed system(*(editor_and_options + paths)) end end
主要的思路就是找到相应的editor打开指定的文件夹,打开文件夹的方法非常easy
system(*(editor_and_options + paths))