2015-06-10 109 views
2

我想使用Ruby on Rails應用程序列出子目錄中的文件/目錄,但我不知道該腳本是否可以在Linux或Windows下運行。Ruby on rails跨平臺win/linux腳本

在Linux上,這是很容易的,我可以做一個

`find my_path`.split("\n").each{|line| do_sthing_with_line} 

在Windows中,相當於將使用dir命令。但是,即使在這種閱讀許多帖子後,我無法想出一個辦法,使其正常工作

`dir my_path` will output a string that is recognized as ruby as utf-8, but which in reality isn't. 

什麼讓它在Windows上工作的正確方法是什麼? 有沒有快速的方法來檢查我是否在窗戶上? on_windows? dir_command : find_command

編輯:和有沒有一種快速的方式來生成這樣的目錄的內容樹視圖?

-- rails_app 
-- -- app/ 
-- -- -- models/ 
-- -- -- controllers/ 
-- -- -- views/ 
-- -- bin/ 
-- -- config/ 
etc. 
+0

這是傳統Ruby認爲使用系統工具和功能而不是語言功能的情況下,當您嘗試成爲平臺包容性時,這種情況會持平。這只是一種務實或徹底的折衷。 – Azolo

回答

1

我根本不會使用子命令。內置的Dir.glob應該在這種情況下工作。

喜歡的東西:

Dir.glob("#{path}/**/**").each { |fileOrDir| do_something } 

在任何情況下,你的find是遞歸的,但你的dir不會。

如果你真的想知道,如果在Windows上運行,還有一些紅寶石爲中心的方式,但多年來我一直在環境被檢查的WINDIR變量,例如

if ENV["WINDIR"] 
    puts "On Windows" 
else 
    puts "Not Windows *probably*" 
end 

編輯:這是我幾年前寫的一個工具,生成一個節點樹,然後顯示它們以各種方式排序。

def usage 
    puts <<END 
Recursive listing of files or dirs, sortable by date or size or count. 
    rl [-cdfnrsuv] [-p pathname] [pathname ...] 
Where: 
    pathname = Dir or file to process; default is ".". 
    -c = Sort by file count; default is sort by date. 
    -d = List dirs only, with their contents sizes and counts. 
    -f = List files only, no dirs or links. 
    -n = Sort by name; default is sort by date. 
    -p = Add pathname even if it starts with "-". 
    -r = Reverse sort order; default order is desc, except by name is asc. 
    -s = Sort by size; default is sort by date. 
    -u = Unsorted; default is sort by date. 
    -v = Verbose, including type, perms, and owner. 
END 
    exit(1) 
end # usage 

class Node 
    attr_reader :path, :stat 
    def load(path) 
     @path, @stat, @children = path, File.lstat(path), [] 
     @stat.directory? and Dir.glob("#{path}/*", File::FNM_DOTMATCH).each { |sub_path| 
      sub_path[-2,2] != "/." && sub_path[-3,3] != "/.." and @children << Node.new.load(sub_path) 
     } 
     self 
    end 
    def size 
     @size or @size = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.size }) : @stat.size 
    end 
    def count 
     @count or @count = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.count }) : 1 
    end 
    def to_a 
     @children.map { |child| child.to_a }.flatten + [self] 
    end 
end # Node 

only_dirs = only_files = by_count = by_name = by_sz = verbose = false; sort = 1; paths = [] 
while (arg = ARGV.shift) 
    arg =~ /^-[^-]*[h?]/ and usage 
    arg =~ /^-[^-]*c/ and by_count = true 
    arg =~ /^-[^-]*d/ and only_dirs = true 
    arg =~ /^-[^-]*f/ and only_files = true 
    arg =~ /^-[^-]*n/ and by_name = true 
    arg =~ /^-[^-]*r/ and sort *= -1 
    arg =~ /^-[^-]*s/ and by_sz = true 
    arg =~ /^-[^-]*u/ and sort = 0 
    arg =~ /^-[^-]*v/ and verbose = true 
    arg =~ /^-[^-]*p/ and paths << ARGV.shift 
    arg !~ /^-/  and paths << arg 
end 
nodes = (paths.empty? ? ["."] : paths).map { |path| Node.new.load(path).to_a }.flatten 
if sort != 0 
    if by_sz then nodes.sort! { |a, b| sort * (2 * (b.size <=> a.size) + (a.path <=> b.path)) } 
    elsif by_count then nodes.sort! { |a, b| sort * (2 * (b.count <=> a.count) + (a.path <=> b.path)) } 
    elsif by_name then nodes.sort! { |a, b| sort * (a.path <=> b.path) } 
    else nodes.sort! { |a, b| sort * (2 * (b.stat.mtime <=> a.stat.mtime) + (a.path <=> b.path)) } 
    end 
end 
for node in nodes 
    next if only_dirs && ! node.stat.directory? 
    next if only_files && ! node.stat.file? 
    puts "%s %11s %6s %s%s" % [ 
     node.stat.mtime.strftime("%Y-%m-%d %H:%M:%S"), 
     node.size.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse, 
     node.count.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse, 
     verbose ? "%-9s %6o %4d %4d " % [:ftype, :mode, :uid, :gid].map { |v| node.stat.send(v) } : "", 
     node.path] 
end 
+0

只是另一個小珍聞,你的'find'也會給出以'。'開頭的名字,但是'Dir.glob'不會被重寫爲:'Dir.glob(「#{path}/**/** 「,File :: FNM_DOTMATCH)',你需要忽略以'」/。「'或'」/ ..「'結尾的路徑。 – lilole

+0

謝謝,它像一個魅力。有沒有一種快速的方法來產生一個樹狀的視圖? (請參閱我的編輯) –

+1

我添加了一些代碼,它應該具有可用於您自己代碼中的目錄樹或文件節點樹的部分。 – lilole