Class: Belajar::Terminal::Courses

Inherits:
Thor
  • Object
show all
Includes:
Output
Defined in:
lib/belajar/terminal/courses.rb

Constant Summary collapse

GITHUB =
/github\.com/
MASTER_ZIP_URL =
%r{github.com\/(.*)\/archive\/master.zip}
URL =
/\A#{URI.regexp(%w(http https))}\z/
ZIP_FILE =
/\.zip/

Instance Method Summary collapse

Instance Method Details

#delete(course_name = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/belajar/terminal/courses.rb', line 85

def delete(course_name = nil)
  if options[:all]
    get_confirm('Are you shure you want to delete all courses?') do
      course_dirs = Dir[File.join(Belajar.config.courses_path, '*')]

      course_dirs.each do |dir|
        FileUtils.remove_dir(dir)
        QuickStore.store.delete(Storeable.key(File.basename(dir), prefix: 'courses'))
      end

      say_info "All belajar courses were successfully deleted."
    end
  elsif course_name
    path = File.join(Belajar.config.courses_path, course_name)

    unless Dir.exist?(path)
      print_course_not_available(course_name)
      return
    end

    get_confirm("Are you sure you want to delete the course \"#{course_name}\"?") do
      FileUtils.remove_dir(path)
      QuickStore.store.delete(Storeable.key(course_name, prefix: 'courses'))
      say_info "The course \"#{course_name}\" was successfully deleted."
    end
  else
    system 'belajar courses help delete'
  end
end

#download(url = nil, action = 'downloaded') ⇒ Object



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
# File 'lib/belajar/terminal/courses.rb', line 24

def download(url = nil, action = 'downloaded')
  use_initial_course = url.nil? && options[:github].nil?
  url = GithubClient.master_zip_url(Belajar.config.initial_course) if use_initial_course
  url = GithubClient.master_zip_url(options[:github]) if options[:github]

  url_given = (url =~ URL)
  github = use_initial_course || options[:github] || url.match(GITHUB)

  raise Download::NoUrlError unless url_given
  raise Download::NoZipFileUrlError unless File.basename(url) =~ ZIP_FILE

  courses_path = Belajar.config.courses_path
  FileUtils.makedirs(courses_path) unless Dir.exist?(courses_path)

  file_name = File.join(courses_path, url.split('/').last)

  File.open(file_name, 'w') { |file| file << URI.open(url).read }
  course = Course.unzip(file_name, github_repo: github)

  if github
    user_and_repo = url.match(MASTER_ZIP_URL).captures.first
    store_repo_data(options[:github] || user_and_repo)
  end

  QuickStore.store.set(course.key(:url), url)
  QuickStore.store.set(course.key(:updated_at), Time.now.to_s)
  scaffold_solutions

  say_info "Successfully #{action} the course \"#{course.title}\"!"
rescue Download::NoUrlError
  print_download_warning(url, "\"#{url}\" is not a valid URL!")
rescue Download::NoZipFileUrlError
  print_download_warning(url, "\"#{url}\" is not a URL of a *.zip file!")
rescue StandardError => e
  print_download_warning(url, e.message)
ensure
  FileUtils.rm(file_name) if File.exist?(file_name.to_s)
end

#listObject



17
18
19
20
# File 'lib/belajar/terminal/courses.rb', line 17

def list
  courses = Loading::Courses.load(Belajar.config.courses_path)
  say_info courses_list_text(courses)
end

#update(course_name = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/belajar/terminal/courses.rb', line 65

def update(course_name = nil)
  if options[:all]
    courses = Loading::Courses.load(Belajar.config.courses_path)
    courses.each { |course| update_course(course) }
  elsif course_name
    path = File.join(Belajar.config.courses_path, course_name)

    unless Dir.exist?(path)
      print_course_not_available(course_name)
      return
    end

    update_course(Course.new(course_name))
  else
    system 'belajar course help update'
  end
end