dohroot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Makani Mason, Kem Mason
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/doh/findup.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Doh
2
+ def self.findup(start_directory, filename, max_tries = 20)
3
+ curr_directory = start_directory
4
+ max_tries.times do
5
+ path = File.expand_path(File.join(curr_directory, filename))
6
+ return path if File.exist?(path)
7
+ return nil if (path == '/')
8
+ curr_directory = File.join(curr_directory, '..')
9
+ end
10
+ nil
11
+ end
12
+ end
@@ -0,0 +1,108 @@
1
+ require 'ostruct'
2
+ require 'optparse'
3
+
4
+ module Doh
5
+
6
+ class Options
7
+ # this class is for command line parsing
8
+ # the hash is of the form: {'variable_name' => option_list}
9
+ # you can also pass an array of hashes [{'var1' => optlist1}, {'var2' => optlist2}]
10
+ # if you use an array of hashes, the help will be shown in the order specified (otherwise it's random)
11
+
12
+ # option_list is an array, the first element of which is the default value of the option
13
+ # the others are of the form with examples shown on
14
+ # http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
15
+ # returns any non-options parameters via method varargs
16
+ # set allow_unknown_options to true to allow for filename specification, etc..
17
+ # if the default value is :required for a field, then if that field isn't
18
+ # specified, an exception will be raised
19
+ def initialize(param_definition_hash_or_array, allow_unknown_options = false, explanation_text = '', unknown_options_name = 'file', allow_no_prefix = false)
20
+ @vals = OpenStruct.new
21
+
22
+ @opts = OptionParser.new
23
+ @opts.banner = "Usage: #{$0} [options]"
24
+ if explanation_text != ''
25
+ @opts.separator ""
26
+ @opts.separator explanation_text
27
+ end
28
+ @opts.separator ""
29
+ @opts.separator "Specific options:"
30
+ @opts.separator ""
31
+
32
+ if param_definition_hash_or_array.class == Array
33
+ param_definition_array = param_definition_hash_or_array
34
+ param_definition_hash = param_definition_hash_or_array.inject({}) {|sum, elem| sum.merge(elem)}
35
+ else
36
+ param_definition_array = param_definition_hash_or_array.collect {|elem1, elem2| {elem1 => elem2}}
37
+ param_definition_hash = param_definition_hash_or_array
38
+ end
39
+
40
+ param_definition_array.each do |elem|
41
+ keyname = elem.to_a[0][1][2]
42
+ if keyname =~ /^--no-/
43
+ raise "key name: #{keyname} will not behave as desired if meant to be a boolean, rename it, or pass in allow_no_prefix flag as 5th param"
44
+ end
45
+ end
46
+
47
+ param_definition_array.each do |elem|
48
+ key = elem.to_a[0][0]
49
+ val = elem.to_a[0][1]
50
+ default = val.shift
51
+ @vals.send("#{key}=", default)
52
+ val.last.insert(0, '(required) - ') if val.last.is_a?(String) && default == :required
53
+ @opts.on(*val) do |optval|
54
+ @vals.send("#{key}=", optval)
55
+ end
56
+ end
57
+
58
+ @opts.separator ""
59
+ @opts.separator "Common options:"
60
+ @opts.separator ""
61
+
62
+ @opts.on("-h", "-?", "--help", "Show this message") do
63
+ puts @opts
64
+ exit
65
+ end
66
+
67
+ varargs = []
68
+ @opts.order(ARGV) do |arg|
69
+ varargs.push(arg)
70
+ end
71
+ @vals.varargs = varargs
72
+
73
+ unset_vars = []
74
+ param_definition_hash.each do |key,val|
75
+ if @vals.send(key) == :required
76
+ unset_vars.push(key)
77
+ end
78
+ end
79
+
80
+ exception = ''
81
+ exception << "Required options not specified: #{unset_vars.inspect}\n" unless unset_vars.size == 0
82
+ exception << "Unknown options specified: #{varargs.inspect}\n" unless allow_unknown_options || (varargs.size == 0)
83
+ exception << "You must specify #{allow_unknown_options} #{unknown_options_name}#{allow_unknown_options > 1 ? 's' : ''}" if allow_unknown_options.class == Fixnum && varargs.size != allow_unknown_options
84
+ if allow_unknown_options.class == Range
85
+ if (allow_unknown_options.min == allow_unknown_options.max)
86
+ if (varargs.size < allow_unknown_options.min)
87
+ exception << "You must specify at least #{allow_unknown_options.min} #{unknown_options_name}#{allow_unknown_options.min > 1 ? 's' : ''}"
88
+ end
89
+ elsif !allow_unknown_options.include?(varargs.size)
90
+ exception << "You must specify between #{allow_unknown_options.min} and #{allow_unknown_options.max} #{unknown_options_name}s"
91
+ end
92
+ end
93
+ if exception != ''
94
+ puts @opts
95
+ raise exception
96
+ end
97
+ end
98
+
99
+ def method_missing(sym, *args)
100
+ @vals.send(sym, *args)
101
+ end
102
+
103
+ def to_s
104
+ @opts.to_s
105
+ end
106
+ end
107
+
108
+ end
data/lib/doh/root.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'doh/findup'
2
+
3
+ module Doh
4
+ def self.root
5
+ @root
6
+ end
7
+
8
+ def self.root=(directory)
9
+ @root = directory
10
+ libdir = File.join(@root, 'lib')
11
+ $LOAD_PATH.push(libdir) if libdir
12
+ end
13
+
14
+ def self.find_root(start_directory, filename = 'dohroot', max_tries = 20)
15
+ rootfile = Doh::findup(start_directory, filename, max_tries)
16
+ if rootfile
17
+ Doh::root = File.dirname(rootfile)
18
+ end
19
+ end
20
+
21
+ def self.find_root_from_file(filepath = nil)
22
+ Doh::find_root(File.dirname(filepath || caller[0]))
23
+ end
24
+
25
+ def self.find_root_from_path(path)
26
+ if File.directory?(path)
27
+ Doh::find_root(path)
28
+ else
29
+ Doh::find_root(File.dirname(path))
30
+ end
31
+ end
32
+
33
+ def self.find_root_from_prog
34
+ Doh::find_root(File.dirname($PROGRAM_NAME))
35
+ end
36
+
37
+ def self.find_root_from_pwd
38
+ Doh::find_root(Dir.pwd)
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dohroot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Makani Mason
9
+ - Kem Mason
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-01 00:00:00.000000000Z
14
+ dependencies: []
15
+ description: Implements the notion of dohroot that enables most other doh stuff.
16
+ email:
17
+ - devinfo@atpsoft.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - MIT-LICENSE
22
+ files:
23
+ - lib/doh/findup.rb
24
+ - lib/doh/options.rb
25
+ - lib/doh/root.rb
26
+ - MIT-LICENSE
27
+ homepage: https://github.com/atpsoft/dohroot
28
+ licenses:
29
+ - MIT
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 1.9.2
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.15
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: tiniest root of doh stuff
52
+ test_files: []
53
+ has_rdoc: