base_app 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/base_app.rb +78 -7
- metadata +2 -2
data/lib/base_app.rb
CHANGED
@@ -11,16 +11,17 @@ class BaseApp
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def command_line_arguments
|
14
|
-
[['v','verbose','Be more verbose']
|
14
|
+
[['v','verbose','Be more verbose.'],
|
15
|
+
['h', 'help', 'Request help.']]
|
15
16
|
end
|
16
17
|
|
17
18
|
def construct_opts
|
18
19
|
opts = OptionParser.new do |opts|
|
19
20
|
command_line_arguments.each do |argspec|
|
20
|
-
short, long, description, required = argspec
|
21
|
+
short, long, description, required, default_value = argspec
|
21
22
|
param_name = long.gsub '=', ''
|
22
23
|
@required_opts << param_name if required
|
23
|
-
|
24
|
+
create_getter_setter(long)
|
24
25
|
opts.on("-#{short}", "--#{long}", description) do |val|
|
25
26
|
@options[param_name] = val
|
26
27
|
setter = param_name.gsub("-", "_") + "="
|
@@ -30,8 +31,8 @@ class BaseApp
|
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
def
|
34
|
-
name = name.gsub("-", "_").gsub(
|
34
|
+
def create_getter_setter(name)
|
35
|
+
name = name.gsub("-", "_").gsub(/=.?/, "")
|
35
36
|
self.class.send(:attr_accessor, name) unless self.respond_to?(name)
|
36
37
|
end
|
37
38
|
|
@@ -45,19 +46,89 @@ class BaseApp
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
49
|
+
# file util helpers
|
50
|
+
def exists? file
|
51
|
+
File.exists? file
|
52
|
+
end
|
53
|
+
|
54
|
+
def mkdir dir
|
55
|
+
FileUtils.mkdir(dir) unless exists?(dir)
|
56
|
+
end
|
57
|
+
|
58
|
+
def help_text_firstline
|
59
|
+
"#$0: \n"
|
60
|
+
end
|
61
|
+
|
62
|
+
def help_text
|
63
|
+
text = help_text_firstline
|
64
|
+
command_line_arguments.each do |argspec|
|
65
|
+
short, long, description, required, default_value = argspec
|
66
|
+
short_text = (long =~ /=/) ? "#{short}=s" : short
|
67
|
+
text << sprintf(" -%-3s | --%-20s %s%s\n", short_text, long, description, (required ? "(required)" : ""))
|
68
|
+
end
|
69
|
+
text
|
70
|
+
end
|
71
|
+
|
72
|
+
def process_template_to_file(name,target,props={})
|
73
|
+
write_file(target, process_template(name,target,props))
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_template(template_name)
|
77
|
+
unless @templates
|
78
|
+
@templates = {}
|
79
|
+
template_data = DATA.read
|
80
|
+
template_data.scan(/(__.+?__.+?__.+?__)/m).each do |template|
|
81
|
+
template = template[0]
|
82
|
+
template =~ /^(__.+?__)$/m
|
83
|
+
name = $1
|
84
|
+
template.gsub! /^__.+?__$/m, ''
|
85
|
+
@templates[name] = template
|
86
|
+
end
|
87
|
+
end
|
88
|
+
@templates["__#{template_name.to_s.upcase}__"] or
|
89
|
+
raise "Error: no template named: #{template_name} (#{@templates.keys.join(", ")})"
|
90
|
+
end
|
91
|
+
|
92
|
+
def process_template (template_name, additional_properties={} )
|
93
|
+
template = get_template template_name
|
94
|
+
|
95
|
+
@properties.merge(additional_properties).each do |prop,val|
|
96
|
+
term = "{{#{prop}}}"
|
97
|
+
printf "substituting: %-40s => %s\n", term, val
|
98
|
+
template.gsub! term, val
|
99
|
+
end
|
100
|
+
|
101
|
+
template
|
102
|
+
end
|
103
|
+
|
104
|
+
def write_file(target,*content)
|
105
|
+
File.open(target,'w') do |f|
|
106
|
+
content.each do |c|
|
107
|
+
f.puts c.to_s
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
48
112
|
def self.main
|
49
113
|
app = self.new
|
50
114
|
app.parse_command_line_options
|
115
|
+
|
116
|
+
if app.help
|
117
|
+
$stderr.print app.help_text
|
118
|
+
exit app.status
|
119
|
+
end
|
120
|
+
|
51
121
|
max_width = app.command_line_arguments.map {|x| x[1].length}.max
|
52
122
|
if app.verbose
|
53
123
|
$stderr.puts "#$0 Parameters:"
|
54
124
|
app.command_line_arguments.each do |opt_spec|
|
55
125
|
short, arg, descr = opt_spec
|
56
|
-
option = arg.gsub(
|
57
|
-
$stderr.printf( " %*s : %s\n", max_width, arg.gsub('
|
126
|
+
option = arg.gsub(/=.?$/,'').gsub('-','_')
|
127
|
+
$stderr.printf( " %*s : %s\n", max_width, arg.gsub(/=.?$/,''), app.send(option.to_sym))
|
58
128
|
end
|
59
129
|
$stderr.puts ""
|
60
130
|
end
|
131
|
+
|
61
132
|
app.run
|
62
133
|
exit app.status
|
63
134
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle Burton
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2010-01-
|
13
|
+
date: 2010-01-25 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|