script_summoner 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/summon +155 -0
  2. metadata +55 -0
data/bin/summon ADDED
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yaml'
3
+
4
+ unless ARGV.length == 1
5
+ STDERR.puts "[ERROR] Please provide configuration filename (e.g. 'summon example.config')"
6
+ exit
7
+ end
8
+
9
+ config_file = ARGV[0]
10
+
11
+ CONFIG = YAML::load(File.read(config_file))
12
+
13
+ # Check validity of file:
14
+ config_ok = true
15
+ # - mandatory fields
16
+ unless CONFIG.has_key?('author')
17
+ puts "Missing author in config file"
18
+ config_ok = false
19
+ end
20
+ unless CONFIG.has_key?('name')
21
+ puts "Missing script name in config file"
22
+ config_ok = false
23
+ end
24
+ unless CONFIG.has_key?('description')
25
+ puts "Missing script description in config file"
26
+ config_ok = false
27
+ end
28
+ CONFIG['options'].each do |opt|
29
+ unless opt.has_key?('long')
30
+ puts "Missing long name for option in config file"
31
+ config_ok = false
32
+ end
33
+ if opt.has_key?('type') and ! opt['type'] == 'Boolean'
34
+ unless opt.has_key?('example')
35
+ puts "Missing example for option in config file"
36
+ config_ok = false
37
+ end
38
+ end
39
+ end
40
+ # - Options cannot be required and have a default at the same time (otherwise they wouldn't be required...)
41
+ CONFIG['options'].each do |opt|
42
+ if opt.has_key?('required') and opt['required'] and opt.has_key?('default')
43
+ puts "Options cannot have a default value and be required at the same time"
44
+ config_ok = false
45
+ end
46
+ end
47
+ exit unless config_ok
48
+
49
+ outfile = File.new(CONFIG['name'], 'w')
50
+ outfile.puts '#!/usr/bin/env ruby'
51
+
52
+ # Get script name, author, usage and description
53
+ output_part = <<OUT
54
+ # == NAME
55
+ # #{CONFIG['name']}
56
+ #
57
+ # == AUTHOR
58
+ # #{CONFIG['author']}
59
+ #
60
+ # == USAGE
61
+ # #{CONFIG['name']}
62
+ OUT
63
+ outfile.puts output_part
64
+
65
+ CONFIG['options'].each do |opt|
66
+ outfile.puts "# [ -#{opt['short']} | --#{opt['long']} ] #{opt['example']}\n"
67
+ end
68
+
69
+ output_part = <<OUT
70
+ #
71
+ # == DESCRIPTION
72
+ # #{CONFIG['description']}
73
+ #
74
+ # == OPTIONS
75
+ OUT
76
+ outfile.puts output_part
77
+
78
+ CONFIG['options'].each do |opt|
79
+ outfile.print "# -#{opt['short']}, --#{opt['long']}:\n# #{opt['description']}"
80
+ if opt['default']
81
+ outfile.print ' Default: ' + opt['default'] + '.'
82
+ end
83
+ if opt['required']
84
+ outfile.print ' Required.'
85
+ end
86
+ outfile.puts "\n"
87
+ end
88
+
89
+ outfile.puts "\n"
90
+ outfile.puts "require 'rubygems'\n"
91
+ outfile.puts "require 'optparse'\n"
92
+ outfile.puts "require 'rdoc/usage'\n"
93
+ outfile.puts "require 'yaml'\n"
94
+ outfile.puts
95
+
96
+ # Set options defaults
97
+ outfile.puts '# Set option defaults'
98
+ outfile.puts 'options = {'
99
+ default_array = Array.new
100
+ CONFIG['options'].each do |opt|
101
+ if opt.has_key?('default')
102
+ if opt.has_key?('type') and opt['type'] == 'Boolean'
103
+ default_array.push(' :' + opt['long'] + ' => ' + opt['default'].to_s)
104
+ else
105
+ default_array.push(' :' + opt['long'] + ' => "' + opt['default'].to_s + '"')
106
+ end
107
+ end
108
+ end
109
+ outfile.puts default_array.join(",\n")
110
+ outfile.puts '}'
111
+ outfile.puts
112
+
113
+ outfile.puts '# Set options'
114
+ outfile.puts 'optparse = OptionParser.new do |opts|'
115
+ outfile.puts " opts.on('-h','--help',"
116
+ outfile.puts " 'Display the usage information') {RDoc::usage}"
117
+
118
+ CONFIG['options'].each do |opt|
119
+ unless opt.has_key?('type')
120
+ opt['type'] = 'String'
121
+ end
122
+
123
+ if opt['type'] == 'Boolean'
124
+ outfile.puts " opts.on('-#{opt['short']}','--#{opt['long']}',"
125
+ outfile.puts " '#{opt['description']}') {|argument| options[:#{opt['long']}] = true}"
126
+ else
127
+ if opt.has_key?('required') and opt['required']
128
+ outfile.puts " opts.on('-#{opt['short']}','--#{opt['long']} #{opt['long'].gsub(/ /, '_')}',"
129
+ else
130
+ outfile.puts " opts.on('-#{opt['short']}','--#{opt['long']} [#{opt['long'].gsub(/ /, '_')}]',"
131
+ end
132
+ outfile.puts " #{opt['type']},"
133
+ outfile.puts " '#{opt['description']}') {|argument| options[:#{opt['long']}] = argument}"
134
+ end
135
+
136
+ end
137
+ outfile.puts 'end'
138
+ outfile.puts "optparse.parse! rescue RDoc::usage('usage')"
139
+
140
+ outfile.puts
141
+ outfile.puts '# Check that mandatory arguments are present'
142
+ CONFIG['options'].each do |opt|
143
+ if opt.has_key?('required') and opt['required']
144
+ outfile.puts "if options[:#{opt['long']}].nil?"
145
+ outfile.puts " STDERR.puts '[ERROR] Missing argument: #{opt['long']}'"
146
+ outfile.puts " RDoc::usage"
147
+ outfile.puts " exit"
148
+ outfile.puts "end"
149
+ end
150
+ end
151
+ outfile.puts "puts options.to_yaml"
152
+
153
+ outfile.close
154
+
155
+ system('chmod u+x ' + CONFIG['name'])
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: script_summoner
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Jan Aerts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-28 00:00:00 +00:00
13
+ default_executable: summon
14
+ dependencies: []
15
+
16
+ description: script_summoner creates a template ruby script including command-line parsing and documentation based on a small configuration file.
17
+ email: first_name.last_name at gmail.org
18
+ executables:
19
+ - summon
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files: []
25
+
26
+ has_rdoc: true
27
+ homepage: http://github.com/jandot/script_summoner
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Ruby script template generator
54
+ test_files: []
55
+