Class: Ucc::Runner
- Inherits:
-
Object
- Object
- Ucc::Runner
- Defined in:
- lib/ucc.rb
Instance Attribute Summary collapse
-
#source_files ⇒ Object
readonly
Returns the value of attribute source_files.
Instance Method Summary collapse
-
#app_filename ⇒ Object
Filename to use when executing compiled app.
-
#enquote(param) ⇒ Object
Enquotes param if needed, also pre-escapes quotes that already are inside the param.
-
#initialize(compiler) ⇒ Runner
constructor
Here we pass the compiler (to be able to choose between gcc or g++).
-
#options ⇒ Object
Options array accessor.
-
#optparse ⇒ Object
Defines options for ucc.
-
#parse_options ⇒ Object
Does option parsing using optparse.
-
#trace(text) ⇒ Object
Prints text if verbose mode is on.
-
#work ⇒ Object
Everything special goes here.
Constructor Details
#initialize(compiler) ⇒ Runner
Here we pass the compiler (to be able to choose between gcc or g++)
15 16 17 18 19 20 |
# File 'lib/ucc.rb', line 15 def initialize(compiler) @compiler = compiler raise "Compiler must be specified" unless @compiler work end |
Instance Attribute Details
#source_files ⇒ Object (readonly)
Returns the value of attribute source_files.
12 13 14 |
# File 'lib/ucc.rb', line 12 def source_files @source_files end |
Instance Method Details
#app_filename ⇒ Object
Filename to use when executing compiled app
68 69 70 71 72 73 |
# File 'lib/ucc.rb', line 68 def app_filename return @app_filename if @app_filename @app_filename = source_files[0].sub(/\.\w+$/, '') @app_filename += ".exe" if WINDOWS @app_filename end |
#enquote(param) ⇒ Object
Enquotes param if needed, also pre-escapes quotes that already are inside the param
117 118 119 120 121 |
# File 'lib/ucc.rb', line 117 def enquote(param) param = param.gsub(/(["'])/, '\\\\\\1') param = %Q["#{param}"] if param =~ /\s/ param end |
#options ⇒ Object
Options array accessor
63 64 65 |
# File 'lib/ucc.rb', line 63 def @options ||= {} end |
#optparse ⇒ Object
Defines options for ucc
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ucc.rb', line 26 def optparse @optparse ||= OptionParser.new do |opts| opts. = "Usage: #{CURRENT_EXECUTABLE} [options] file..." [:runopts] = nil opts.on( '-r', '--runopts "STRING"', 'Pass STRING as the command line argument to the compiled app' ) do |s| [:runopts] = s end [:compileopts] = nil opts.on( '-c', '--compileopts "STRING"', 'Pass STRING as the command line argument to the compiler' ) do |s| [:compileopts] = s end [:memcheck] = false opts.on( '-V', '--valgrind', 'Run the app in valgrind' ) do [:memcheck] = true end [:verbose] = false opts.on( '--verbose', 'Enable debug messages' ) do [:verbose] = true end opts.on_tail( '-v', '--version', 'Show app version' ) do puts "#{CURRENT_EXECUTABLE} #{VERSION}" exit end opts.on_tail( '-h', '--help', 'Display this screen' ) do puts opts exit end end end |
#parse_options ⇒ Object
Does option parsing using optparse
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ucc.rb', line 79 def begin optparse.parse! rescue OptionParser::InvalidOption=> e puts "#{CURRENT_EXECUTABLE}: #{e}" exit(1) end # Here we have already parsed ARGV @source_files = ARGV if @source_files.empty? puts "#{CURRENT_EXECUTABLE}: no input files" exit(1) end end |
#trace(text) ⇒ Object
Prints text if verbose mode is on
112 113 114 |
# File 'lib/ucc.rb', line 112 def trace(text) puts "[ucc] Debug: #{text}" if [:verbose] end |
#work ⇒ Object
Everything special goes here
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ucc.rb', line 96 def work compilation_params = %Q[#{@compiler} -Wall -o "#{app_filename}" #{source_files.map{ |f| enquote(f) }.join(" ")}] compilation_params = "#{compilation_params} #{[:compileopts]}" if [:compileopts] trace compilation_params exit unless system compilation_params exec_params = enquote(app_filename) exec_params = "./#{exec_params}" unless WINDOWS exec_params = "#{exec_params} #{[:runopts]}" if [:runopts] exec_params = "valgrind #{exec_params}" if [:memcheck] trace exec_params puts "=== Compiled successfully, executing... === " exec exec_params end |