Class: ET::Runner

Inherits:
Object
  • Object
show all
Includes:
GLI::App
Defined in:
lib/et/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cwd = Dir.pwd) ⇒ Runner

Returns a new instance of Runner.



10
11
12
# File 'lib/et/runner.rb', line 10

def initialize(cwd = Dir.pwd)
  @cwd = cwd
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



8
9
10
# File 'lib/et/runner.rb', line 8

def cwd
  @cwd
end

Instance Method Details

#apiObject



96
97
98
# File 'lib/et/runner.rb', line 96

def api
  @api ||= API.new(host: host, username: username, token: token)
end

#configObject



116
117
118
# File 'lib/et/runner.rb', line 116

def config
  @config ||= Config.new(cwd)
end

#go(args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/et/runner.rb', line 14

def go(args)
  version VERSION

  pre { |_, _, _, _| check_config! }

  desc "Initialize current directory as a work area."
  skips_pre
  command :init do |c|
    c.flag [:u, :user], desc: "Username"
    c.flag [:t, :token], desc: "Login token"
    c.flag [:h, :host], desc: "Server hosting the lessons"

    c.action do |_global_options, options, _cmdargs|
      settings = {
        "username" => options[:user],
        "token" => options[:token],
        "host" => options[:host]
      }

      settings = prompt_for_missing(settings)
      config.save!(settings)

      puts "Saved configuration to #{config.path}"
    end
  end

  desc "List available lessons."
  command :list do |c|
    c.action do |_global_options, _options, _cmdargs|
      Formatter.print_table(api.list_lessons, 'slug', 'title', 'type')
    end
  end

  desc "Download lesson to your working area."
  command :get do |c|
    c.action do |_global_options, _options, cmdargs|
      cmdargs.each do |slug|
        lesson = api.get_lesson(slug)
        archive = api.download_file(lesson['archive_url'])
        archive_manager = ET::ArchiveManager.new(archive, cwd)
        archive_manager.unpack

        if !archive_manager.unpacked_files.empty?
          archive_manager.delete_archive
          puts "'#{slug}' extracted to '#{archive_manager.destination}'"
        else
          raise StandardError.new("Failed to extract the archive.")
        end
      end
    end
  end

  desc "Submit the lesson in this directory."
  command :submit do |c|
    c.action do |_global_options, _options, _cmdargs|
      lesson = Lesson.new(cwd)

      if lesson.exists?
        api.submit_lesson(lesson)
        puts "Lesson submitted"
      else
        raise StandardError.new("Not in a lesson directory.")
      end
    end
  end

  desc "Run an exercise test suite."
  command :test do |c|
    c.action do |_global_options, _options, _cmdargs|
      exercise = Exercise.new(cwd)

      if exercise.exists?
        exercise.run_tests
      else
        raise StandardError.new("Not in an exercise directory.")
      end
    end
  end

  run(args)
end

#hostObject



100
101
102
# File 'lib/et/runner.rb', line 100

def host
  config.host
end

#setting(key) ⇒ Object



112
113
114
# File 'lib/et/runner.rb', line 112

def setting(key)
  config.exists? && config[key]
end

#tokenObject



108
109
110
# File 'lib/et/runner.rb', line 108

def token
  config.token
end

#usernameObject



104
105
106
# File 'lib/et/runner.rb', line 104

def username
  config.username
end