Class: Jstyler::Beautify::BeautifyRunner

Inherits:
Object
  • Object
show all
Includes:
Jstyler
Defined in:
lib/jstyler/beautify.rb

Constant Summary

Constants included from Jstyler

FORMATTER_LIB, GEM_DIRECTORY, JAVA_LIBS, VERSION

Instance Method Summary collapse

Constructor Details

#initializeBeautifyRunner

Returns a new instance of BeautifyRunner.



16
17
18
19
# File 'lib/jstyler/beautify.rb', line 16

def initialize
  @conventions = ['java', 'eclipse']
  @allowed_options = ['config', 'convention', 'verbose']
end

Instance Method Details

#flatten_options(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/jstyler/beautify.rb', line 57

def flatten_options(options = {})
  options_string = ''
  options.each {|key,val|
    val = File.expand_path val if key.to_s.include? 'config' 
    options_string += " -#{key}  #{val}"
  }
  
  options_string
end

#flatten_sources(srcs = []) ⇒ Object



67
68
69
70
71
72
# File 'lib/jstyler/beautify.rb', line 67

def flatten_sources(srcs = [])
  src_string = ''
  srcs.uniq.flatten.each {|src| src_string += " " + File.expand_path(src)}
  
  src_string
end

#run(config_options, *srcs) ⇒ Object

:call-seq:

run = config_file, src/ src2
run = hash, src (hash - {:config=>config_file, :verbose=>true })


24
25
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
# File 'lib/jstyler/beautify.rb', line 24

def run(config_options, *srcs)
  @options = { :config=>config_options } unless Hash === config_options
  @options = config_options if @options == nil
  current_dir = Dir.pwd
  
  #validation
  result = validate_config_path @options
  result = validate_source_directory srcs if result
  result = validate_env if result
  
  #prepare call
  execution_string = ''
  if result
    execution_string = flatten_sources srcs
    execution_string = flatten_options(@options) + execution_string
    
    Dir.chdir(JAVA_LIBS)
    java = File.expand_path ENV['JAVA_HOME']+'/bin/java'
    cmd = "#{java} -jar #{FORMATTER_LIB} #{execution_string} "
    shell = Session::Shell.new
    stdout, stderr = shell.execute cmd
    status = shell.status 
    puts stdout
    puts stderr if !status
    
    #change directory to previous one
    Dir.chdir(current_dir)
    result = execution_string
  end
  
  result
end