Class: Elscripto::App

Inherits:
Object
  • Object
show all
Defined in:
lib/elscripto/app.rb

Overview

This is the application class used by the elscripto binary Initialize it by passing a config file to it. Default file path is <current_dir>/.elscripto

Constant Summary collapse

CONFIG_FILE =
File.join('.','.elscripto')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ App

Returns a new instance of App.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elscripto/app.rb', line 46

def initialize opts
  @opts = opts
  @commands = []
  @command = @opts.command
  @generated_script = ""
  @platform = self.class.get_platform(RbConfig::CONFIG['host_os'])
  first_run?
  @enviroment = @opts.enviroment
  
  if @command == 'start'
    expand_commands!
    add_adhoc_commands!
    raise NoDefinitionsError if @commands.size == 0
  end
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def command
  @command
end

#commandsObject

Returns the value of attribute commands.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def commands
  @commands
end

#enviromentObject

Returns the value of attribute enviroment.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def enviroment
  @enviroment
end

#generated_scriptObject

Returns the value of attribute generated_script.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def generated_script
  @generated_script
end

#optsObject

Returns the value of attribute opts.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def opts
  @opts
end

#platformObject

Returns the value of attribute platform.



43
44
45
# File 'lib/elscripto/app.rb', line 43

def platform
  @platform
end

Class Method Details

.get_platform(host_os) ⇒ Object

Determine the platform we’re running on



234
235
236
237
238
239
# File 'lib/elscripto/app.rb', line 234

def get_platform(host_os)
  return :osx if host_os =~ /darwin/
  return :linux if host_os =~ /linux/
  return :windows if host_os =~ /mingw32|mswin32/
  return :unknown
end

.global_conf_pathObject



229
230
231
# File 'lib/elscripto/app.rb', line 229

def global_conf_path
  Elscripto::GLOBAL_CONF_PATHS[self.get_platform(RbConfig::CONFIG['host_os'])]
end

.init!Object



211
212
213
214
215
216
217
218
219
# File 'lib/elscripto/app.rb', line 211

def init!
  if File.exists?(CONFIG_FILE)
    raise Elscripto::AlreadyInitializedError.new
  else
    File.open(CONFIG_FILE,'w') do |f|
      f.write File.read(File.join(File.dirname(__FILE__),'..','..','config','elscripto.init.yml')).gsub('{{GLOBAL_CONF_PATH}}',self.global_conf_path)
    end
  end
end

.is_gnome?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/elscripto/app.rb', line 221

def is_gnome?
  system('which gnome-terminal > /dev/null') 
end

.is_kde?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/elscripto/app.rb', line 225

def is_kde?
  system('which konsole > /dev/null') 
end

Instance Method Details

#add_adhoc_commands!Object



81
82
83
84
85
86
87
88
# File 'lib/elscripto/app.rb', line 81

def add_adhoc_commands!
  # add in incoming adhoc commands
  i = 1
  @opts.commands.each do |c|
    @commands << Command.new("cmd#{i}",:command => c)
    i+=1
  end
end

#exec!Object



90
91
92
# File 'lib/elscripto/app.rb', line 90

def exec!
  send(@command.to_sym)
end

#expand_commands!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/elscripto/app.rb', line 62

def expand_commands!
  # load configuration from file
  if File.exists?(@opts.config_file)
    file_opts = YAML.load_file(@opts.config_file)
    @commands << file_opts['commands'] if file_opts['commands'].class == Array
  end
  # add in definitions
  @commands << @opts.definitions unless @opts.definitions.size == 0
  
  @commands = @commands.flatten.map do |cmd|
    case cmd.class.to_s
    when 'Hash'
      Command.new(cmd['name'], :command => cmd['command'])
    when 'String'
      Command.new(cmd)
    end
  end
end

#first_run?Boolean

Returns:

  • (Boolean)


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/elscripto/app.rb', line 190

def first_run?
  global_conf_file = File.join(GLOBAL_CONF_PATHS[platform],'_default.conf')
  
  case platform
  when :osx,:linux
    unless File.exists?(global_conf_file)
      require 'fileutils'
      FileUtils.mkdir_p(GLOBAL_CONF_PATHS[platform])
      File.open(global_conf_file,'w') do |f|
        f.write(File.read(File.join(File.dirname(__FILE__),'..','..','config','elscripto.conf.yml')))
      end
      puts_unless_test "Wrote global configuration to #{global_conf_file}".yellow
    end
  end
end

#initObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/elscripto/app.rb', line 94

def init
  print "\nInitializing elscripto...".yellow
  begin
    self.class.init!
    puts_unless_test " done."
    puts_unless_test "Before continuing, update ./.elscripto with the desired script definitions\n\n".yellow
  rescue Elscripto::AlreadyInitializedError
    puts_unless_test " nah, it's already there!\n".green
  end
end

#puts_unless_test(msg) ⇒ Object



206
207
208
# File 'lib/elscripto/app.rb', line 206

def puts_unless_test msg
  puts msg unless enviroment == :test
end

#startObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/elscripto/app.rb', line 105

def start
  raise Elscripto::NoDefinitionsError.new if self.commands.size == 0
  
  puts_unless_test 'Starting ElScripto Spctacularrr!'.green
  case self.platform
  # tell application "Terminal"
  # 	activate
  # 	delay 1
  # 	do script "clear && echo \"--STARTING SPORK SERVER--\" && cd Sites/tabbo/ && spork" in front window
  # 	tell application "System Events" to keystroke "t" using command down
  # 	do script "clear && echo \"--STARTING RAILS SERVER--\" && cd Sites/tabbo/ && rails c" in front window
  # 	tell application "System Events" to keystroke "t" using command down
  # 	do script "clear && echo \"--STARTING AUTOTEST--\" && cd Sites/tabbo/ && autotest" in front window
  # end tell
  when :osx
    @generated_script = %{tell application "Terminal"\n}
    @generated_script<< %{activate\n}
    @generated_script<< commands.map { |cmd| %{tell application "System Events" to keystroke "t" using command down\ndelay 0.5\ndo script "clear && echo \\"-- Running #{cmd.name} Spectacularrr --\\" && #{cmd.system_call}" in front window} }.join("\n")
    @generated_script<< "\nend tell"
    if self.enviroment == :production
      begin
        tempfile = File.join(ENV['TMPDIR'],'elscripto.tmp')
        File.open(tempfile,'w') { |f| f.write(@generated_script) }
        raise Elscripto::LaunchFailedError.new unless system("osascript #{tempfile}")
      ensure
        File.delete(tempfile)
      end
    else
      @generated_script
    end
  when :linux
	# Gnome desktops
	# Example script: gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
    if self.class.is_gnome?
      @generated_script = %{gnome-terminal }
      @generated_script<< commands.map { |cmd| %{--tab --title '#{cmd.name}' -e '#{cmd.system_call}'} }.join(" ")
      if self.enviroment == :production
        raise Elscripto::LaunchFailedError.new unless system(@generated_script)
      else
        @generated_script
      end
	# KDE Desktops, using qdbus
	# CDCMD='cd ~/elscripto'
	# elCommands=('htop' 'tail -f LICENSE.txt' 'tail -f README.md');
	# for i in "${elCommands[@]}"
	# do
	#   session=$(qdbus org.kde.konsole /Konsole  newSession)
	#   qdbus org.kde.konsole /Sessions/${session} sendText "${CDCMD}"
	#   qdbus org.kde.konsole /Sessions/${session} sendText $'\n'
	#   qdbus org.kde.konsole /Sessions/${session} sendText "${i}"
	#   qdbus org.kde.konsole /Sessions/${session} sendText $'\n'
	#   qdbus org.kde.konsole /Sessions/${session} setMonitorActivity true
	# done
	elsif self.class.is_kde?
	  @generated_script = %{CDCMD='cd #{Dir.pwd}'\n}
	  @generated_script<< "elCommands=("
	  @generated_script<< commands.map { |cmd| %{'#{cmd.system_call}'} }.join(" ")
	  @generated_script<< ")\n"
	  @generated_script<< %{for i in "${elCommands[@]}"
do
  session=$(qdbus org.kde.konsole /Konsole  newSession)
  qdbus org.kde.konsole /Sessions/${session} sendText "${CDCMD}"
  qdbus org.kde.konsole /Sessions/${session} sendText $'\n'
  qdbus org.kde.konsole /Sessions/${session} sendText "${i}"
  qdbus org.kde.konsole /Sessions/${session} sendText $'\n'
  qdbus org.kde.konsole /Sessions/${session} setMonitorActivity true
done}
      if self.enviroment == :production
      begin
        tempfile = File.join('/','tmp','elscripto.tmp')
        File.open(tempfile,'w') { |f| f.write(@generated_script) }
        raise Elscripto::LaunchFailedError.new unless system("/bin/bash #{tempfile}")
      ensure
        File.delete(tempfile)
      end
        @generated_script
      end
	else
      Elscripto::UnsupportedOSError.new('your flavour of linux')
    end
  else
    raise Elscripto::UnsupportedOSError.new(self.platform)
  end
end