Class: Greyatom::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/greyatom/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.



14
15
16
17
18
19
# File 'lib/greyatom/lesson/open.rb', line 14

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

Instance Attribute Details

#lessonObject (readonly)

Returns the value of attribute lesson.



10
11
12
# File 'lib/greyatom/lesson/open.rb', line 10

def lesson
  @lesson
end

#lessonNameObject (readonly)

Returns the value of attribute lessonName.



10
11
12
# File 'lib/greyatom/lesson/open.rb', line 10

def lessonName
  @lessonName
end

#rootDirObject (readonly)

Returns the value of attribute rootDir.



10
11
12
# File 'lib/greyatom/lesson/open.rb', line 10

def rootDir
  @rootDir
end

Instance Method Details

#cdToLessonObject



72
73
74
75
76
77
# File 'lib/greyatom/lesson/open.rb', line 72

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

#cloneCurrentLessonObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/greyatom/lesson/open.rb', line 55

def cloneCurrentLesson
	puts "Cloning lesson..."
	begin
         	Timeout::timeout(15) do
         		cloneUrl = lesson.getAttr('forked_repo')
           	Git.clone("[email protected]:#{cloneUrl}.git", 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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/greyatom/lesson/open.rb', line 37

def forkCurrentLesson
	puts "Forking lesson..."
	github = Greyatom::Github.new()
	begin
		Timeout::timeout(15) do
			lessonRepo = lesson.getAttr('github_repo')
			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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/greyatom/lesson/open.rb', line 21

def openALesson(*puzzle_name)
	# get currently active lesson
	puts "Getting current lesson..."
	lesson.getCurrentLesson(puzzle_name)
	@lessonName = lesson.getAttr('lesson_name')
	if !File.exists?("#{rootDir}/#{lessonName}")
		# fork lesson repo via github api
		forkCurrentLesson
		# clone forked lesson into machine
		cloneCurrentLesson
	end
	# install dependencies
	# cd into it and invoke bash
	cdToLesson
end