Ruby on rails跨平台的win / linux脚本

时间:2015-06-10 23:59:03

标签: ruby linux windows

我想使用Ruby on Rails应用程序列出子目录中的文件/目录,但我不知道脚本是在Linux还是Windows下运行。

在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上使用它的正确方法是什么? 有没有快速的方法来检查我是否在Windows上? on_windows? dir_command : find_command

编辑:有没有一种快速的方法来生成这样的目录内容的树视图?

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

1 个答案:

答案 0 :(得分:1)

我根本不会使用子命令。内置Dir.glob应该适用于这种情况。

类似的东西:

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

在任何情况下,您的find都是递归的,但您的dir不会。

如果确实想知道是否在Windows上运行,那么有一些以Ruby为中心的方法,但多年来我一直在检查环境中的WINDIR变量,例如< / p>

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