Module: QED::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/qed/utils.rb

Instance Method Summary collapse

Instance Method Details

#find_root(path = nil) ⇒ Object

Locate project's root directory. This is done by searching upward in the file heirarchy for the existence of one of the following:

.ruby
.git/
.hg/
_darcs/
.qed
.qed.rb
qed.rb

Failing to find any of these locations, resort to the fallback:

lib/

If that isn't found, then returns a temporary system location. and sets `rootless` to true.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/qed/utils.rb', line 71

def find_root(path=nil)
  return ($ROOT = system_tmpdir) if @rootless

  path = File.expand_path(path || Dir.pwd)
  path = File.dirname(path) unless File.directory?(path)

  root = lookup(ROOT_PATTERN, path) || lookup(CONFIG_PATTERN, path)
  return root if root

  #root = lookup(path, '{qed,demo,spec}/')
  #return root if root

  root = lookup('lib/', path)

  if !root
    warn "QED is running rootless."
    system_tmpdir
    @rootless = true
  else
    root
  end

  $ROOT = root

  #abort "QED failed to resolve project's root location.\n" +
  #      "QED looks for following entries to identify the root:\n" +
  #      ROOT_PATTERN +
  #      "Please add one of them to your project to proceed."
end

#load_configObject


17
18
19
20
# File 'lib/qed/utils.rb', line 17

def load_config
  load_etc unless ENV['noetc']
  load_rc  unless ENV['norc']
end

#load_etcObject


38
39
40
41
42
43
# File 'lib/qed/utils.rb', line 38

def load_etc
  file = Dir.glob(File.join(root, CONFIG_PATTERN)).first
  if file
    load file
  end
end

#load_rcObject


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/qed/utils.rb', line 23

def load_rc
  rc_file = File.join(root, '.rubyrc')
  if File.exist?(rc_file)
    begin
      require 'rc/api'
      RC.profile_switch 'qed', '-p', '--profile'
      RC.configure 'qed' do |config|
        QED.configure(config.profile, &config)
      end
    rescue LoadError
    end
  end
end

#lookup(glob, path = Dir.pwd) ⇒ Object

Lookup path glob, searching each higher directory in turn until just before the users home directory is reached or just before the system's root directory.


106
107
108
109
110
111
112
# File 'lib/qed/utils.rb', line 106

def lookup(glob, path=Dir.pwd)
  until path == HOME or path == '/' # until home or root
    mark = Dir.glob(File.join(path,glob), File::FNM_CASEFOLD).first
    return path if mark
    path = File.dirname(path)
  end
end

#rootObject


46
47
48
# File 'lib/qed/utils.rb', line 46

def root
  @root ||= find_root
end

#system_tmpdirObject

System-wide temporary directory for QED executation.


117
118
119
120
121
# File 'lib/qed/utils.rb', line 117

def system_tmpdir
  @system_tmpdir ||= (
    File.join(Dir.tmpdir, 'qed', File.basename(Dir.pwd), Time.new.strftime("%Y%m%d%H%M%S"))
  )
end