Class: Chouette::Command

Inherits:
Object
  • Object
show all
Includes:
CommandLineSupport
Defined in:
app/models/chouette/command.rb

Overview

end

Defined Under Namespace

Classes: Option

Constant Summary collapse

@@command =
"chouette"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLineSupport

#available_loggers, #execute!, #logger, #max_output_length

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command.



22
23
24
25
26
# File 'app/models/chouette/command.rb', line 22

def initialize(options = {})
  database_options_from_active_record.merge(options).each do |k,v|
    send "#{k}=", v
  end
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def database
  @database
end

#hostObject

Returns the value of attribute host.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def host
  @host
end

#passwordObject

Returns the value of attribute password.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def password
  @password
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def port
  @port
end

#schemaObject

Returns the value of attribute schema.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def schema
  @schema
end

#userObject

Returns the value of attribute user.



20
21
22
# File 'app/models/chouette/command.rb', line 20

def user
  @user
end

Instance Method Details

#command_options(options) ⇒ Object



86
87
88
89
90
# File 'app/models/chouette/command.rb', line 86

def command_options(options)
  options.collect do |key, value|
    Option.new(key, value)
  end.sort_by(&:key).join(' ')
end

#database_options_from_active_recordObject



28
29
30
31
32
33
34
35
36
37
# File 'app/models/chouette/command.rb', line 28

def database_options_from_active_record
  config = Chouette::ActiveRecord.connection_pool.spec.config
  { 
    :database => config[:database], 
    :user => config[:username],
    :password => config[:password],
    :port => config[:port],
    :host => (config[:host] or "localhost")
  }
end

#run!(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/chouette/command.rb', line 40

def run!(options = {})
  Dir.mktmpdir do |config_dir|
    chouette_properties = File.join(config_dir, "chouette.properties")
    open(chouette_properties, "w") do |f|
      f.puts "database.name = #{database}"
      f.puts "database.schema = #{schema}"
      #f.puts "database.showsql = true"
      f.puts "hibernate.username = #{user}"
      f.puts "hibernate.password = #{password}"
      f.puts "jdbc.url=jdbc:postgresql://#{host}:#{port}/#{database}"
      f.puts "jdbc.username = #{user}"
      f.puts "jdbc.password = #{password}"
      #f.puts "database.hbm2ddl.auto=update"
    end

    logger.debug "Chouette properties: #{File.readlines(chouette_properties).collect(&:strip).join(', ')}"

    command_line = "#{command} -classpath #{config_dir} #{command_options(options)}"
    logger.debug "Execute '#{command_line}'"

    execute! command_line
  end
end