liri 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -2
  3. data/.simplecov +8 -0
  4. data/Gemfile +4 -1
  5. data/Gemfile.lock +27 -15
  6. data/bash_script_examples.sh +1 -0
  7. data/dummy-app/.gitignore +12 -0
  8. data/dummy-app/.rspec +3 -0
  9. data/dummy-app/.ruby-gemset +1 -0
  10. data/dummy-app/.ruby-version +1 -0
  11. data/dummy-app/CODE_OF_CONDUCT.md +84 -0
  12. data/dummy-app/Gemfile +10 -0
  13. data/dummy-app/Gemfile.lock +35 -0
  14. data/dummy-app/LICENSE.txt +21 -0
  15. data/dummy-app/README.md +43 -0
  16. data/dummy-app/Rakefile +8 -0
  17. data/dummy-app/bin/console +15 -0
  18. data/dummy-app/bin/setup +8 -0
  19. data/dummy-app/dummy-app.gemspec +39 -0
  20. data/dummy-app/lib/dummy/app/version.rb +7 -0
  21. data/dummy-app/lib/dummy/app.rb +10 -0
  22. data/dummy-app/spec/dummy/app_spec.rb +11 -0
  23. data/dummy-app/spec/dummy/dummy_spec.rb +11 -0
  24. data/dummy-app/spec/spec_helper.rb +15 -0
  25. data/exe/liri +9 -4
  26. data/lib/agent/agent.rb +91 -50
  27. data/lib/agent/runner.rb +1 -3
  28. data/lib/all_libraries.rb +4 -2
  29. data/lib/common/benchmarking.rb +5 -10
  30. data/lib/common/log.rb +12 -6
  31. data/lib/common/manager_data.rb +30 -0
  32. data/lib/common/progressbar.rb +29 -0
  33. data/lib/common/setup.rb +103 -0
  34. data/lib/common/source_code.rb +22 -12
  35. data/lib/common/tests_result.rb +115 -0
  36. data/lib/common/unit_test/rspec.rb +14 -35
  37. data/lib/liri.rb +26 -35
  38. data/lib/manager/manager.rb +167 -105
  39. data/lib/task.rb +2 -2
  40. data/liri.gemspec +8 -0
  41. data/spec_credentials.yml.example +4 -0
  42. data/template/liri-config.yml +5 -2
  43. metadata +55 -4
  44. data/lib/manager/setup.rb +0 -70
  45. data/lib/manager/test_result.rb +0 -38
data/lib/agent/agent.rb CHANGED
@@ -11,16 +11,19 @@ module Liri
11
11
  class << self
12
12
  # Inicia la ejecución del Agent
13
13
  # @param stop [Boolean] el valor true es para que no se ejecute infinitamente el método en el test unitario.
14
- def run(stop = false)
15
- Liri.create_folders('agent')
14
+ def run(work_folder_path, stop = false)
15
+ setup_manager = Liri.set_setup(work_folder_path)
16
+ agent_folder_path = setup_manager.agent_folder_path
16
17
 
17
- Liri.set_logger(Liri::AGENT_LOGS_FOLDER_PATH, 'liri-agent.log')
18
+ Liri.set_logger(setup_manager.logs_folder_path, 'liri-agent.log')
18
19
  Liri.logger.info("Proceso Agent iniciado")
19
- puts "Presione Ctrl + c para terminar el proceso Agent manualmente\n\n"
20
+ Liri.logger.info("Presione Ctrl + c para terminar el proceso Agent manualmente\n", true)
20
21
 
21
- source_code = Liri::Common::SourceCode.new(Liri::AGENT_FOLDER_PATH, Liri.compression_class, Liri.unit_test_class)
22
- runner = Liri::Agent::Runner.new(Liri.unit_test_class, source_code.decompressed_file_folder_path)
23
- agent = Agent.new(Liri.udp_port, Liri.tcp_port, source_code, runner)
22
+ decompressed_source_code_path = File.join(agent_folder_path, '/', Common::SourceCode::DECOMPRESSED_FOLDER_NAME)
23
+ source_code = Common::SourceCode.new(decompressed_source_code_path, agent_folder_path, Liri.compression_class, Liri.unit_test_class)
24
+ runner = Agent::Runner.new(Liri.unit_test_class, source_code.decompressed_file_folder_path)
25
+ tests_result = Common::TestsResult.new(agent_folder_path)
26
+ agent = Agent.new(Liri.udp_port, Liri.tcp_port, source_code, runner, tests_result, agent_folder_path)
24
27
  threads = []
25
28
  threads << agent.start_server_socket_to_process_manager_connection_request # Esperar y procesar la petición de conexión del Manager
26
29
 
@@ -32,15 +35,20 @@ module Liri
32
35
  end
33
36
  end
34
37
 
35
- def initialize(udp_port, tcp_port, source_code, runner)
38
+ def initialize(udp_port, tcp_port, source_code, runner, tests_result, agent_folder_path)
36
39
  @udp_port = udp_port
37
40
  @udp_socket = UDPSocket.new
38
41
  @tcp_port = tcp_port
39
42
 
40
43
  @source_code = source_code
41
44
  @runner = runner
45
+ @tests_result = tests_result
46
+
47
+ @all_tests = {}
42
48
 
43
49
  @managers = {}
50
+
51
+ @agent_folder_path = agent_folder_path
44
52
  end
45
53
 
46
54
  # Inicia un servidor udp que se mantiene en espera de la primera petición de conexión del Manager
@@ -62,48 +70,52 @@ module Liri
62
70
  loop do
63
71
  @manager_request = @udp_socket.recvfrom(1024)
64
72
  manager_ip_address = @manager_request.last.last
65
- user, pass, dir = @manager_request.first.split(";")
66
- process_manager_connection_request(manager_ip_address, user, pass, dir)
73
+ manager_data = get_manager_data(JSON.parse(@manager_request.first))
74
+ process_manager_connection_request(manager_ip_address, manager_data)
67
75
  end
68
76
  end
69
77
  end
70
78
 
71
79
  # Inicia un cliente tcp para responder a la petición broadcast del Manager para que éste sepa donde enviar las pruebas
72
- def start_client_socket_to_process_tests(manager_ip_address)
80
+ def start_client_socket_to_process_tests(manager_ip_address, manager_data)
73
81
  tcp_socket = TCPSocket.open(manager_ip_address, @tcp_port)
74
82
 
75
- Liri.logger.info("Se inicia una conexión con el Manager: #{manager_ip_address} en el puerto TCP: #{@tcp_port}
76
- (Se establece una conexión para procesar la ejecución de las pruebas)
77
- ")
83
+ agent_ip_address = tcp_socket.addr[2]
84
+
85
+ tcp_socket.puts({ msg: 'Listo' }) # Se envía un mensaje inicial al Manager
86
+
87
+ Liri.logger.info("Se inicia una conexión para procesar pruebas con el Manager: #{manager_ip_address} en el puerto TCP: #{@tcp_port}")
88
+ Liri.logger.info("\nConexión iniciada con el Manager: #{manager_ip_address}", true)
89
+
90
+ # En la siguiente línea se espera un puts desde el Manager, pero el Manager no lo hace
91
+ response = JSON.parse(tcp_socket.gets)
92
+ return unless response['exist_tests']
78
93
 
79
- tcp_socket.print("Listo para ejecutar pruebas") # Se envía un mensaje inicial al Manager
80
- puts "\nConexión iniciada con el Manager: #{manager_ip_address}"
94
+ get_source_code(manager_ip_address, manager_data)
81
95
 
82
- # Se procesan las pruebas enviadds por el Manager
96
+ # Se procesan las pruebas enviadas por el Manager
83
97
  while line = tcp_socket.gets
84
98
  response = line.chop
85
99
  break if response == 'exit'
86
100
 
87
- tests = JSON.parse(response)
88
- Liri.logger.debug("Pruebas recibidas del Manager #{manager_ip_address}:")
89
- Liri.logger.debug(tests)
101
+ tests_batch = JSON.parse(response)
102
+ tests = get_tests(tests_batch, manager_ip_address)
90
103
 
91
- tests_result = @runner.run_tests(tests)
104
+ raw_tests_result = @runner.run_tests(tests)
92
105
 
93
- json_tests_result = tests_result.to_json
94
- Liri.logger.debug("Resultados de la ejecución de las pruebas recibidas del Manager #{manager_ip_address}:")
95
- Liri.logger.debug(json_tests_result)
106
+ tests_batch_number = tests_batch['tests_batch_number']
107
+ tests_result_file_name = @tests_result.build_file_name(agent_ip_address, tests_batch_number)
108
+ tests_result_file_path = @tests_result.save(tests_result_file_name, raw_tests_result)
96
109
 
97
- Liri.logger.info("
98
- #{tests.size} pruebas recibidas, #{tests_result[:example_quantity]} pruebas ejecutadas
99
- ")
100
- tcp_socket.print(json_tests_result) # Este no logra enviar toda la información, porque? en cambio el cliente recibe un json grande sin problemas
110
+ send_tests_results_file(manager_ip_address, manager_data, tests_result_file_path)
111
+ result = { tests_batch_number: tests_batch_number, tests_result_file_name: tests_result_file_name, tests_batch_keys_size: tests_batch['tests_batch_keys'].size}
112
+ tcp_socket.puts(result.to_json) # Envía el número de lote y el nombre del archivo de resultados.
101
113
  end
102
114
 
103
115
  tcp_socket.close
104
- Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address}")
116
+ Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address} en el puerto TCP: #{@tcp_port}")
105
117
 
106
- Liri.clean_folder(Liri::AGENT_FOLDER_PATH)
118
+ Liri.clean_folder_content(@agent_folder_path)
107
119
 
108
120
  start_client_to_close_manager_server(manager_ip_address, 'Conexión Terminada')
109
121
  unregister_manager(manager_ip_address)
@@ -112,11 +124,11 @@ module Liri
112
124
  rescue Errno::ECONNRESET => e
113
125
  tcp_socket.close
114
126
  Liri.logger.error("Exception(#{e}) Conexión cerrada en el puerto TCP #{@tcp_port}")
115
- Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address}")
127
+ Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address} en el puerto TCP: #{@tcp_port}")
116
128
  unregister_manager(manager_ip_address)
117
129
  rescue Errno::ECONNREFUSED => e
118
130
  Liri.logger.error("Exception(#{e}) Conexión rechazada en el puerto TCP #{@tcp_port}")
119
- Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address}")
131
+ Liri.logger.info("Se termina la conexión con el Manager #{manager_ip_address} en el puerto TCP: #{@tcp_port}")
120
132
  unregister_manager(manager_ip_address)
121
133
  end
122
134
 
@@ -125,15 +137,15 @@ module Liri
125
137
  # Inserta el ip recibido dentro del hash si es que ya no existe en el hash
126
138
  # Nota: Se requieren imprimir datos para saber el estado de la aplicación, sería muy útil usar algo para logear
127
139
  # estas cosas en los diferentes niveles, debug, info, etc.
128
- def process_manager_connection_request(manager_ip_address, user, pass, dir)
140
+ def process_manager_connection_request(manager_ip_address, manager_data)
129
141
  unless registered_manager?(manager_ip_address)
130
142
  register_manager(manager_ip_address)
131
143
  Liri.logger.info("Petición broadcast UDP recibida del Manager: #{manager_ip_address} en el puerto UDP: #{@udp_port}")
132
- if process_manager_connection_scp(manager_ip_address, user, pass, dir)
133
- start_client_socket_to_process_tests(manager_ip_address)
134
- else
135
- unregister_manager(manager_ip_address)
136
- end
144
+ #if get_source_code(manager_ip_address, manager_data)
145
+ start_client_socket_to_process_tests(manager_ip_address, manager_data)
146
+ #else
147
+ #unregister_manager(manager_ip_address)
148
+ #end
137
149
  end
138
150
  end
139
151
 
@@ -141,27 +153,28 @@ module Liri
141
153
  # Esta conexión permitirá al Manager cerrar sus hilos pendientes con servidores TCP en espera y terminar el proceso
142
154
  def start_client_to_close_manager_server(manager_ip_address, msg)
143
155
  tcp_socket = TCPSocket.open(manager_ip_address, @tcp_port)
144
- Liri.logger.info("Se termina cualquier proceso pendiente con el Manager #{manager_ip_address}")
145
- tcp_socket.print({msg: msg}.to_json)
156
+ Liri.logger.info("Se termina cualquier proceso pendiente con el Manager #{manager_ip_address} en el puerto TCP: #{@tcp_port}")
157
+ tcp_socket.print({ msg: msg }.to_json)
146
158
  tcp_socket.close
147
159
  end
148
160
 
149
- def process_manager_connection_scp(manager_ip_address, user, password, dir)
150
- # puts "User: #{user} Password: #{password}"
161
+ def get_source_code(manager_ip_address, manager_data)
162
+ #puts "#{manager_data.to_h}"
151
163
  puts ''
152
- Liri::Common::Benchmarking.start(start_msg: "Obteniendo código fuente. Espere... ") do
164
+ Liri::Common::Benchmarking.start(start_msg: "Obteniendo código fuente. Espere... ", stdout: true) do
153
165
  puts ''
154
- Net::SCP.start(manager_ip_address, user, :password => password) do |scp|
155
- scp.download!(dir, @source_code.compressed_file_folder_path)
166
+ Net::SCP.start(manager_ip_address, manager_data.user, password: manager_data.password) do |scp|
167
+ scp.download!(manager_data.compressed_file_path, @source_code.compressed_file_folder_path)
156
168
  end
157
169
  end
158
170
  puts ''
159
171
 
160
- downloaded_file_name = dir.split('/').last
172
+ downloaded_file_name = manager_data.compressed_file_path.split('/').last
161
173
  downloaded_file_path = File.join(@source_code.compressed_file_folder_path, '/', downloaded_file_name)
162
174
 
163
- Liri::Common::Benchmarking.start(start_msg: "Descomprimiendo código fuente. Espere... ") do
175
+ Liri::Common::Benchmarking.start(start_msg: "Descomprimiendo código fuente. Espere... ", stdout: true) do
164
176
  @source_code.decompress_file(downloaded_file_path)
177
+ @all_tests = @source_code.all_tests
165
178
  end
166
179
  puts ''
167
180
 
@@ -180,19 +193,19 @@ module Liri
180
193
  # Se setea la versión de ruby y el gemset para el código fuente descomprimido
181
194
  # Se especifica el Gemfile del cual se van a instalar los requerimientos
182
195
  # Esto se hace porque por defecto se usa la versión de Ruby de Liri y su Gemset y por ello hay que cambiarlos explicitamente aquí
183
- Liri::Common::Benchmarking.start(start_msg: "Ejecutando bundle install. Espere... ", end_msg: "Ejecución de bundle install. Duración: ") do
196
+ Liri::Common::Benchmarking.start(start_msg: "Ejecutando bundle install. Espere... ", end_msg: "Ejecución de bundle install. Duración: ", stdout: true) do
184
197
  puts ''
185
198
  system("bash -lc 'rvm use #{Liri.current_folder_ruby_and_gemset}; BUNDLE_GEMFILE=Gemfile bundle install'")
186
199
  end
187
200
  puts ''
188
201
 
189
- Liri::Common::Benchmarking.start(start_msg: "Ejecutando rake db:migrate RAILS_ENV=test. Espere... ", end_msg: "Ejecución de rake db:migrate RAILS_ENV=test. Duración: ") do
202
+ Liri::Common::Benchmarking.start(start_msg: "Ejecutando rake db:migrate RAILS_ENV=test. Espere... ", end_msg: "Ejecución de rake db:migrate RAILS_ENV=test. Duración: ", stdout: true) do
190
203
  puts ''
191
204
  system("bash -lc 'rvm use #{Liri.current_folder_ruby_and_gemset}; rake db:migrate RAILS_ENV=test'")
192
205
  end
193
206
  puts ''
194
207
 
195
- #Liri::Common::Benchmarking.start(start_msg: "Ejecutando rake db:migrate:reset RAILS_ENV=test. Espere... ", end_msg: "Ejecución de rake db:migrate:reset RAILS_ENV=test. Duración: ") do
208
+ #Liri::Common::Benchmarking.start(start_msg: "Ejecutando rake db:migrate:reset RAILS_ENV=test. Espere... ", end_msg: "Ejecución de rake db:migrate:reset RAILS_ENV=test. Duración: ", stdout: true) do
196
209
  # puts ''
197
210
  # system("bash -lc 'rvm use #{Liri.current_folder_ruby_and_gemset}; rake db:migrate:reset RAILS_ENV=test'")
198
211
  #end
@@ -220,6 +233,34 @@ module Liri
220
233
  false
221
234
  end
222
235
 
236
+ def get_manager_data(manager_data_hash)
237
+ Common::ManagerData.new(
238
+ folder_path: manager_data_hash['folder_path'],
239
+ compressed_file_path: manager_data_hash['compressed_file_path'],
240
+ user: manager_data_hash['user'],
241
+ password: manager_data_hash['password']
242
+ )
243
+ end
244
+
245
+ def send_tests_results_file(manager_ip_address, manager_data, tests_result_file_path)
246
+ puts ''
247
+ Liri::Common::Benchmarking.start(start_msg: "Enviando archivo de resultados. Espere... ", stdout: true) do
248
+ Net::SCP.start(manager_ip_address, manager_data.user, password: manager_data.password) do |scp|
249
+ scp.upload!(tests_result_file_path, manager_data.folder_path)
250
+ end
251
+ end
252
+ puts ''
253
+ end
254
+
255
+ def get_tests(tests_batch, manager_ip_address)
256
+ # Se convierte "[5, 9, 13, 1]" a un arreglo [5, 9, 13, 1]
257
+ tests_keys = tests_batch['tests_batch_keys']
258
+ Liri.logger.debug("Claves de pruebas recibidas del Manager #{manager_ip_address}: #{tests_keys}")
259
+ # Se buscan obtienen los tests que coincidan con las claves recibidas de @all_tests = {1=>"spec/hash_spec.rb:2", 2=>"spec/hash_spec.rb:13", 3=>"spec/hash_spec.rb:24", ..., 29=>"spec/liri_spec.rb:62"}
260
+ # Se retorna un arreglo con los tests a ejecutar ["spec/liri_spec.rb:4", "spec/hash_spec.rb:5", "spec/hash_spec.rb:59", ..., "spec/hash_spec.rb:37"]
261
+ tests_keys.map { |test_key| @all_tests[test_key] }
262
+ end
263
+
223
264
  def registered_manager?(manager_ip_address)
224
265
  @managers[manager_ip_address]
225
266
  end
data/lib/agent/runner.rb CHANGED
@@ -10,9 +10,7 @@ module Liri
10
10
  end
11
11
 
12
12
  def run_tests(tests)
13
- result_hash = @unit_test.run_tests(tests.values)
14
- result_hash[:test_keys] = tests.keys
15
- result_hash
13
+ @unit_test.run_tests(tests)
16
14
  end
17
15
  end
18
16
  end
data/lib/all_libraries.rb CHANGED
@@ -20,11 +20,13 @@ require 'agent/runner'
20
20
 
21
21
  require 'common/benchmarking'
22
22
  require 'common/log'
23
+ require 'common/manager_data'
24
+ require 'common/progressbar'
25
+ require 'common/setup'
23
26
  require 'common/source_code'
24
27
  require 'common/compressor/zip'
25
28
  require 'common/unit_test/rspec'
29
+ require 'common/tests_result'
26
30
 
27
31
  require 'manager/manager'
28
- require 'manager/setup'
29
32
  require 'manager/credential'
30
- require 'manager/test_result'
@@ -6,7 +6,7 @@
6
6
  # Este módulo se encarga de medir el tiempo de ejecución de algunos bloques de código
7
7
 
8
8
  require 'benchmark'
9
- require 'i18n' # requirimiento de la gema to_duration
9
+ require 'i18n' # requerimiento de la gema to_duration
10
10
  require 'to_duration'
11
11
 
12
12
  # Se configura la ubicación del archivo de internacionalización de la gema to_duration
@@ -17,18 +17,13 @@ module Liri
17
17
  module Common
18
18
  module Benchmarking
19
19
  class << self
20
- def start(start_msg: nil, end_msg: 'Duración: ')
21
- print start_msg
20
+ def start(start_msg: nil, end_msg: 'Duración: ', stdout: true)
21
+ Liri.logger.info(start_msg, stdout)
22
22
  seconds = Benchmark.realtime do
23
23
  yield
24
24
  end
25
- print_result(end_msg, seconds)
26
- end
27
-
28
- private
29
-
30
- def print_result(msg, seconds)
31
- print "#{msg}#{seconds.to_duration}"
25
+ Liri.logger.info("#{end_msg}#{seconds.to_duration}", stdout)
26
+ seconds
32
27
  end
33
28
  end
34
29
  end
data/lib/common/log.rb CHANGED
@@ -26,32 +26,38 @@ module Liri
26
26
  create_file_logger
27
27
  end
28
28
 
29
- def debug(text)
29
+ def debug(text, stdout = false)
30
+ puts(text) if stdout
30
31
  @stdout_logger.debug(text) if @stdout
31
32
  @file_logger.debug(text)
32
33
  end
33
34
 
34
- def info(text)
35
+ def info(text, stdout = false)
36
+ puts(text) if stdout
35
37
  @stdout_logger.info(text) if @stdout
36
38
  @file_logger.info(text)
37
39
  end
38
40
 
39
- def warn(text)
41
+ def warn(text, stdout = false)
42
+ puts(text) if stdout
40
43
  @stdout_logger.warn(text) if @stdout
41
44
  @file_logger.warn(text)
42
45
  end
43
46
 
44
- def error(text)
47
+ def error(text, stdout = false)
48
+ puts(text) if stdout
45
49
  @stdout_logger.error(text) if @stdout
46
50
  @file_logger.error(text)
47
51
  end
48
52
 
49
- def fatal(text)
53
+ def fatal(text, stdout = false)
54
+ puts(text) if stdout
50
55
  @stdout_logger.fatal(text) if @stdout
51
56
  @file_logger.fatal(text)
52
57
  end
53
58
 
54
- def unknown(text)
59
+ def unknown(text, stdout = false)
60
+ puts(text) if stdout
55
61
  @stdout_logger.unknown(text) if @stdout
56
62
  @file_logger.unknown(text)
57
63
  end
@@ -0,0 +1,30 @@
1
+ # = manager_data.rb
2
+ #
3
+ # @author Rodrigo Fernández
4
+ #
5
+ # == Clase ManagerData
6
+
7
+ module Liri
8
+ module Common
9
+ # Esta clase guarda los datos del Manager
10
+ class ManagerData
11
+ attr_accessor :folder_path, :compressed_file_path, :user, :password
12
+
13
+ def initialize(folder_path:, compressed_file_path:, user:, password:)
14
+ @folder_path = folder_path
15
+ @compressed_file_path = compressed_file_path
16
+ @user = user
17
+ @password = password
18
+ end
19
+
20
+ def to_h
21
+ {
22
+ folder_path: @folder_path,
23
+ compressed_file_path: @compressed_file_path,
24
+ user: @user,
25
+ password: @password
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # = progressbar.rb
2
+ #
3
+ # @author Rodrigo Fernández
4
+ #
5
+ # == Módulo Progressbar
6
+ # Este módulo se encarga de mostrar una barra de progreso
7
+
8
+ require 'ruby-progressbar'
9
+
10
+ module Liri
11
+ module Common
12
+ module Progressbar
13
+ class << self
14
+ def start(params = {})
15
+ @compressing = true
16
+ progressbar = ProgressBar.create(params)
17
+ Thread.new do
18
+ while @compressing
19
+ progressbar.increment
20
+ sleep(0.1)
21
+ end
22
+ end
23
+ yield
24
+ @compressing = false
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,103 @@
1
+ =begin
2
+ Este modulo se encarga de manejar la configuración
3
+ =end
4
+ require 'yaml'
5
+ require 'json'
6
+
7
+ module Liri
8
+ module Common
9
+ class Setup
10
+ SETUP_FOLDER_NAME = 'liri'
11
+ SETUP_FILE_NAME = 'liri-config.yml'
12
+ TEMPLATE_PATH = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), 'template/liri-config.yml')
13
+ LOGS_FOLDER_NAME = 'logs'
14
+ MANAGER_FOLDER_NAME = 'manager'
15
+ AGENT_FOLDER_NAME = 'agent'
16
+
17
+ attr_reader :setup_folder_path, :setup_file_path, :logs_folder_path, :manager_folder_path, :agent_folder_path
18
+
19
+ def initialize(destination_folder_path)
20
+ @setup_folder_path = File.join(destination_folder_path, '/', SETUP_FOLDER_NAME)
21
+ @setup_file_path = File.join(@setup_folder_path, '/', SETUP_FILE_NAME)
22
+ @logs_folder_path = File.join(@setup_folder_path, '/', LOGS_FOLDER_NAME)
23
+ @manager_folder_path = File.join(@setup_folder_path, '/', MANAGER_FOLDER_NAME)
24
+ @agent_folder_path = File.join(@setup_folder_path, '/', AGENT_FOLDER_NAME)
25
+ end
26
+
27
+ def init
28
+ create_folder(@setup_folder_path)
29
+ create_folder(@logs_folder_path)
30
+ create_folder(@manager_folder_path)
31
+ create_folder(@agent_folder_path)
32
+ create_setup_file
33
+ end
34
+
35
+ # Retorna los datos del archivo de configuración
36
+ def load
37
+ if File.exist?(@setup_file_path)
38
+ data = YAML.load(File.read(@setup_file_path))
39
+ JSON.parse(data.to_json, object_class: OpenStruct)
40
+ else
41
+ raise Liri::FileNotFoundError.new(@setup_file_path)
42
+ end
43
+ end
44
+
45
+ def set(value, *keys)
46
+ data = YAML.load(File.read(@setup_file_path))
47
+ keys = keys.first
48
+ aux = data
49
+ keys.each_with_index do |key, index|
50
+ if (keys[index + 1])
51
+ aux = data[key]
52
+ else
53
+ aux[key] = value
54
+ end
55
+ end
56
+ File.open(@setup_file_path, 'w') { |f| f.write data.to_yaml }
57
+ end
58
+
59
+ def create_folder(folder_path)
60
+ if Dir.exist?(folder_path)
61
+ false
62
+ else
63
+ Dir.mkdir(folder_path)
64
+ Dir.exist?(folder_path) ? true : false
65
+ end
66
+ end
67
+
68
+ def delete_setup_folder
69
+ if Dir.exist?(@setup_folder_path)
70
+ FileUtils.rm_rf(@setup_folder_path)
71
+ Dir.exist?(@setup_folder_path) ? false : true
72
+ else
73
+ false
74
+ end
75
+ end
76
+
77
+ # Crea un archivo de configuración en la raiz del proyecto desde un template
78
+ def create_setup_file
79
+ if File.exist?(@setup_file_path)
80
+ false
81
+ else
82
+ File.open(@setup_file_path, 'w') do |output_file|
83
+ File.foreach(TEMPLATE_PATH) do |input_line|
84
+ output_file.write(input_line)
85
+ end
86
+ end
87
+
88
+ File.exist?(@setup_file_path) ? true : false
89
+ end
90
+ end
91
+
92
+ # Borra el archivo de configuración
93
+ def delete_setup_file
94
+ if File.exist?(@setup_file_path)
95
+ File.delete(@setup_file_path)
96
+ File.exist?(@setup_file_path) ? false : true
97
+ else
98
+ false
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -5,23 +5,23 @@
5
5
  module Liri
6
6
  module Common
7
7
  class SourceCode
8
- FOLDER_PATH = Dir.pwd
9
- FOLDER_NAME = FOLDER_PATH.split('/').last
10
8
  DECOMPRESSED_FOLDER_NAME = 'decompressed'
11
- attr_reader :compressed_file_folder_path, :compressed_file_path, :decompressed_file_folder_path
9
+ attr_reader :folder_path, :compressed_file_folder_path, :compressed_file_path, :decompressed_file_folder_path
12
10
 
13
- def initialize(compressed_file_folder_path, compression_class, unit_test_class)
11
+ def initialize(folder_path, compressed_file_folder_path, compression_class, unit_test_class)
12
+ @folder_path = folder_path
13
+ @folder_name = @folder_path.split('/').last
14
14
  @compressed_file_folder_path = compressed_file_folder_path
15
15
  @decompressed_file_folder_path = File.join(@compressed_file_folder_path, '/', DECOMPRESSED_FOLDER_NAME)
16
- @compressed_file_path = File.join(@compressed_file_folder_path, '/', "#{FOLDER_NAME}.zip")
16
+ @compressed_file_path = File.join(@compressed_file_folder_path, '/', "#{@folder_name}.zip")
17
17
  # Inicializa un compresor acorde a compression_class, la siguiente línea en realidad hace lo siguiente:
18
18
  # @compressor = Liri::Common::Compressor::Zip.new(input_dir, output_file)
19
19
  # compression_class en este caso es Zip pero podría ser otro si existiera la implementación, por ejemplo Rar
20
- @compressor = Object.const_get(compression_class).new(FOLDER_PATH, @compressed_file_path)
20
+ @compressor = Object.const_get(compression_class).new(@folder_path, @compressed_file_path)
21
21
  # Inicializa un ejecutor de pruebas acorde a unit_test_class, la siguiente línea en realidad hace lo siguiente:
22
22
  # @unit_test = Liri::Common::UnitTest::Rspec.new(source_code_folder_path)
23
23
  # unit_test_class en este caso es Rspec pero podría ser otro si existiera la implementación, por ejemplo UnitTest
24
- @unit_test = Object.const_get(unit_test_class).new(FOLDER_PATH)
24
+ @unit_test = Object.const_get(unit_test_class).new(@folder_path)
25
25
  end
26
26
 
27
27
  def compress_folder
@@ -29,7 +29,12 @@ module Liri
29
29
  end
30
30
 
31
31
  def decompress_file(compressed_file_path = @compressed_file_path)
32
- @compressor.decompress(compressed_file_path, @decompressed_file_folder_path)
32
+ if File.exist?(compressed_file_path)
33
+ @compressor.decompress(compressed_file_path, @decompressed_file_folder_path)
34
+ Dir.exist?(@decompressed_file_folder_path) ? true : false
35
+ else
36
+ raise FileNotFoundError, compressed_file_path
37
+ end
33
38
  end
34
39
 
35
40
  def delete_compressed_file
@@ -41,12 +46,17 @@ module Liri
41
46
  end
42
47
  end
43
48
 
44
- def all_tests
45
- @unit_test.all_tests
49
+ def delete_decompressed_file_folder_path
50
+ if Dir.exist?(@decompressed_file_folder_path)
51
+ FileUtils.rm_rf(@decompressed_file_folder_path)
52
+ Dir.exist?(@decompressed_file_folder_path) ? false : true
53
+ else
54
+ false
55
+ end
46
56
  end
47
57
 
48
- def folder_path
49
- FOLDER_PATH
58
+ def all_tests
59
+ @unit_test.all_tests
50
60
  end
51
61
  end
52
62
  end