Module: UnixTree

Defined in:
lib/shopifydev/pry/save_json.rb

Class Method Summary collapse

Class Method Details

.get_path(path, opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/shopifydev/pry/save_json.rb', line 31

def get_path(path, opts={})
  path = Pathname.new(path) unless path.nil?
  while path.nil? || !viable_path(path, opts) do
    missing = nil
    path.expand_path.descend{|p| unless p.exist?; missing = p; break; end} if path      
    if missing
      what = (missing == path) ? opts[:require].to_s : 'directory'
      puts TColor.red{ "couldn't find #{what} #{missing.to_s}" } 
      missing = missing.dirname
      print_tree missing

    end 
    print TColor.yellow{ "enter path: "}
    in_path = $stdin.gets.chomp
    return nil if in_path == 'q'
    unless in_path.blank?
      in_path = Pathname(in_path)
      path = missing if missing
      path ||= Pathname.getwd
      path = in_path.relative? ? path.join(in_path) : in_path
    end
  end   
  path.expand_path
end


73
74
75
76
77
78
79
# File 'lib/shopifydev/pry/save_json.rb', line 73

def print_tree(path=nil)
  path ||= Pathname.getwd
  path = Pathname.new(path) unless path.is_a?(Pathname)
  path = path.dirname if path.file?
  puts TColor.blue{ "listing #{path}..."}
  puts `cd #{path.expand_path.to_s}; tree`.gsub(%r{(^[^\w]+)}, TColor.black{'\1'})
end

.viable_path(path, opts = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shopifydev/pry/save_json.rb', line 56

def viable_path(path, opts={})
  path = Pathname(path) unless path.is_a?(Pathname)
  if opts[:new]
    return false unless path.dirname.expand_path.exist?
  else
    return false unless path.expand_path.exist?
  end
  case opts[:require]
  when :file
    opts[:new] ? !(path.exist? && path.directory?) : path.expand_path.file?
  when :directory
    opts[:new] ? !path.exist? : path.expand_path.directory?
  else
    true
  end
end