Class: Contracto::SystemAction

Inherits:
Object
  • Object
show all
Extended by:
Constants
Defined in:
lib/contracto/system_action.rb

Constant Summary

Constants included from Constants

Constants::CONTRACTO_DIR, Constants::CONTRACTO_TMP_DIR, Constants::CONTRACT_FILENAME, Constants::CONTRACT_PID_FILEPATH, Constants::GEM_DIR, Constants::PORT, Constants::RUBY_SERVER_DIR

Class Method Summary collapse

Methods included from Constants

contract_filename, contract_pid_filepath, contracto_dir, contracto_tmp_dir, gem_dir, port, ruby_server_dir

Class Method Details

.clone_repo_to_tmp_contracto_dirObject



57
58
59
60
# File 'lib/contracto/system_action.rb', line 57

def clone_repo_to_tmp_contracto_dir
  success = system "git clone -q  --depth 1 --single-branch --branch master #{Contracto::Config.repo_url} #{contracto_tmp_dir}"
  raise(Contracto::CouldNotDownloadContractError.new(Contracto::Config.repo_url)) unless success
end

.copy_server_filesObject



15
16
17
18
# File 'lib/contracto/system_action.rb', line 15

def copy_server_files
  FileUtils.cp_r ruby_server_dir, contracto_tmp_dir
  FileUtils.mv contracto_tmp_dir, contracto_dir
end

.create_sample_contractObject



25
26
27
28
29
30
31
32
33
# File 'lib/contracto/system_action.rb', line 25

def create_sample_contract
  if contract_already_exists?
    puts 'contract already exists, creating sample contract skipped'
    remove_sample_contract
  else
    FileUtils.mv sample_contract_path, FileUtils.pwd
    puts "created: #{contract_filename}"
  end
end

.move_repo_files_to_root_dirObject



66
67
68
# File 'lib/contracto/system_action.rb', line 66

def move_repo_files_to_root_dir
  system "mv #{contracto_tmp_dir}/* #{contracto_tmp_dir}/.[^.]* . 2> /dev/null"  # Could not use FileUtils for some reason
end

.remove_contracto_dirObject



7
8
9
# File 'lib/contracto/system_action.rb', line 7

def remove_contracto_dir
  FileUtils.rm_rf contracto_dir
end

.remove_tmp_contracto_dirObject



11
12
13
# File 'lib/contracto/system_action.rb', line 11

def remove_tmp_contracto_dir
  FileUtils.rm_rf contracto_tmp_dir
end

.revert_clone_repo_to_tmp_contracto_dirObject



62
63
64
# File 'lib/contracto/system_action.rb', line 62

def revert_clone_repo_to_tmp_contracto_dir
  remove_tmp_contracto_dir
end

.revert_copy_server_filesObject



20
21
22
23
# File 'lib/contracto/system_action.rb', line 20

def revert_copy_server_files
  remove_contracto_dir
  remove_tmp_contracto_dir
end

.revert_start_serverObject



52
53
54
55
# File 'lib/contracto/system_action.rb', line 52

def revert_start_server
  stop_server
rescue StandardError
end

.start_serverObject



35
36
37
38
39
40
41
42
# File 'lib/contracto/system_action.rb', line 35

def start_server
  raise Contracto::ServerAlreadyRunningError if server_already_running?

  system "rackup #{contracto_dir}/config.ru -p #{port} -D -P #{contract_pid_filepath}"
  # TODO: loop below should terminate after n tries
  system "while ! echo exit | nc localhost #{port} > /dev/null && echo \"waiting for contracto server...\"; do sleep 1; done"
  test_request
end

.stop_serverObject



44
45
46
47
48
49
50
# File 'lib/contracto/system_action.rb', line 44

def stop_server
  puts 'killing server...'
  Process.kill(15, File.read(contract_pid_filepath).to_i)
  puts '...server killed'
rescue Errno::ENOENT
  puts 'could not kill server (pidfile not found)'
end