源碼高速定位工具-qwandry
阿新 • • 發佈:2017-06-12
-m rec mat efault pass config [] 名稱 com
指定編輯器打開
然後copy例如以下內容到文件裏
解釋
register 方法是 將指定的文件夾打包
add 將文件夾增加到搜索中
default 是設置默認的搜索範圍
就是從倉庫中找到合適得 Package
主要的思路就是找到相應的editor打開指定的文件夾,打開文件夾的方法非常easy
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))
源碼高速定位工具-qwandry