Module: DotOpts

Defined in:
lib/dotopts/api.rb,
lib/dotopts/parser.rb,
lib/dotopts/command.rb

Defined Under Namespace

Classes: Command, Parser

Constant Summary collapse

OPTIONS_FILES =
Note:

Sorry, I could not decide between these two.

Configuration file names.

['.opts', '.option']

Class Method Summary collapse

Class Method Details

.apply(argv, env = {}) ⇒ void

This method returns an undefined value.

Apply arguments and environment options.



67
68
69
70
71
# File 'lib/dotopts/api.rb', line 67

def self.apply(argv, env={})
  env.each{ |k,v| ENV[k.to_s] = v.to_s }
  ARGV.concat(argv)
  #ARGV.replace(argv + ARGV)
end

.commandObject

TODO:

Is ENV okay? Maybe [‘dotopts_command’] would be better?

Note:

This is take from basename of ‘$0`. In the future, we may need to find a way to tweak this to somehow include parrent directories.

Get the current command.



10
11
12
# File 'lib/dotopts/command.rb', line 10

def self.command
  ENV['cmd'] || File.basename($0)
end

.configure!(io = nil) ⇒ void

This method returns an undefined value.

Configure

Parameters:

  • file (String)

    The configuration file to load. (optional)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dotopts/api.rb', line 15

def self.configure!(io=nil)
  text, file = read_config(io)

  if text
    cmds = Parser.parse(text)

    applicable = cmds.select do |c|
      c.current?
    end

    applicable.each do |c|
      argv = c.arguments
      env  = c.environment

      debug(file, argv, env)
      apply(argv, env)
    end
  end
end

.debug(file, argv, env) ⇒ void

This method returns an undefined value.

Print message to stderr if dopts_debug flag it set.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dotopts/api.rb', line 76

def self.debug(file, argv, env)
  return unless ENV['dotopts_debug'] 

  $stderr.puts "dotopts file: #{file}"

  unless argv.empty?
    msg = argv.join(' ')
    $stderr.puts "dotopts argv: " + msg
  end

  unless env.empty?
    msg = env.map{ |k,v| "#{k}=#{v.inspect}" }.join(' ')
    $stderr.puts "dotopts env: " + msg
  end
end

.options_fileString

Returns the options file of the current project.

Returns:

  • (String)

    The options file of the project.



38
39
40
41
42
43
44
45
# File 'lib/dotopts/api.rb', line 38

def self.options_file
  if project_root
    OPTIONS_FILES.each do |optfile|
      file = File.join(project_root, optfile)
      return file if File.exist?(file)
    end
  end
end

.project_root(start_dir = Dir.pwd) ⇒ String?

Find the root directory of the current project.

Returns:

  • (String, nil)

    The root directory of the project.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dotopts/api.rb', line 50

def self.project_root(start_dir=Dir.pwd)
  dir  = start_dir
  home = File.expand_path('~')
  until dir == home || dir == '/'
    OPTIONS_FILES.each do |optfile|
      if File.exist?(File.join(dir, optfile))
        return dir
      end
    end
    dir = File.dirname(dir)
  end
  nil
end

.read_config(io) ⇒ Array<String>

Take an IO object and read it in. If it is a File object also return the file name. Strings are passed through untouched.

Returns:

  • (Array<String>)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dotopts/api.rb', line 97

def self.read_config(io)
  text, file = nil, '(io)'

  case io
  when String
    text = io
  when File
    text = io.read
    file = io.path
  when nil
    if file = options_file
      text = File.read(file)
    end
  else
    text = io.read
  end

  return text, file
end