Class: CommitLive::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-live/lesson/open.rb

Constant Summary collapse

HOME_DIR =
File.expand_path("~")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpen

Returns a new instance of Open.



15
16
17
18
19
20
21
# File 'lib/commit-live/lesson/open.rb', line 15

def initialize()
  if File.exists?("#{HOME_DIR}/.ga-config")
    @rootDir = YAML.load(File.read("#{HOME_DIR}/.ga-config"))[:workspace]
  end
  @lesson = CommitLive::Current.new
  @lesson_status = CommitLive::Status.new
end

Instance Attribute Details

#forkedRepoObject (readonly)

Returns the value of attribute forkedRepo.



11
12
13
# File 'lib/commit-live/lesson/open.rb', line 11

def forkedRepo
  @forkedRepo
end

#lessonObject (readonly)

Returns the value of attribute lesson.



11
12
13
# File 'lib/commit-live/lesson/open.rb', line 11

def lesson
  @lesson
end

#lesson_statusObject (readonly)

Returns the value of attribute lesson_status.



11
12
13
# File 'lib/commit-live/lesson/open.rb', line 11

def lesson_status
  @lesson_status
end

#lessonNameObject (readonly)

Returns the value of attribute lessonName.



11
12
13
# File 'lib/commit-live/lesson/open.rb', line 11

def lessonName
  @lessonName
end

#rootDirObject (readonly)

Returns the value of attribute rootDir.



11
12
13
# File 'lib/commit-live/lesson/open.rb', line 11

def rootDir
  @rootDir
end

Instance Method Details

#cdToLessonObject



97
98
99
100
101
102
# File 'lib/commit-live/lesson/open.rb', line 97

def cdToLesson
  puts "Opening lesson..."
  Dir.chdir("#{rootDir}/#{lessonName}")
  puts "Done."
  exec("#{ENV['SHELL']} -l")
end

#change_grp_ownerObject



88
89
90
91
92
93
94
95
# File 'lib/commit-live/lesson/open.rb', line 88

def change_grp_owner
  results = system("chgrp -R ubuntu #{rootDir}/#{lessonName}")
  if results
    puts "..."
  else
    puts "Couldn't change group ownership"
  end
end

#cloneCurrentLessonObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/commit-live/lesson/open.rb', line 72

def cloneCurrentLesson
  puts "Cloning lesson..."
  begin
    Timeout::timeout(15) do
      Git.clone(forkedRepo.ssh_url, lessonName, path: rootDir)
    end
  rescue Git::GitExecuteError => err
    puts "Error while cloning!"
    puts err
    exit
  rescue Timeout::Error
    puts "Cannot clone this lesson right now. Please try again."
    exit
  end
end

#forkCurrentLessonObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/commit-live/lesson/open.rb', line 49

def forkCurrentLesson
  puts "Forking lesson..."
  github = CommitLive::Github.new()
  begin
    Timeout::timeout(15) do
      lessonData = lesson.getAttr('data')
      if !lessonData['chapters'].nil?
        lessonRepo = lessonData['chapters']['lessons']['repo_url']
      else
        lessonRepo = lessonData['repo_url']
      end
      @forkedRepo = github.client.fork(lessonRepo)
    end
  rescue Octokit::Error => err
    puts "Error while forking!"
    puts err
    exit
  rescue Timeout::Error
    puts "Please check your internet connection."
    exit
  end
end

#openALesson(puzzle_name) ⇒ Object



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
# File 'lib/commit-live/lesson/open.rb', line 23

def openALesson(puzzle_name)
  # get currently active lesson
  puts "Getting current lesson..."
  lesson.getCurrentLesson(puzzle_name)
  lessonData = lesson.getAttr('data')
  if !lessonData['chapters'].nil?
    @lessonName = lessonData['chapters']['lessons']['title']
  else
    @lessonName = lessonData['title']
  end
  if !File.exists?("#{rootDir}/#{lessonName}")
    # fork lesson repo via github api
    forkCurrentLesson
    # clone forked lesson into machine
    cloneCurrentLesson
    # change group owner
    change_grp_owner
    # lesson forked API change
    puts 'Updating lesson status...'
    lesson_status.update('lesson_forked', lessonName)
  end
  # install dependencies
  # cd into it and invoke bash
  cdToLesson
end