Class: EditorLearner::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/editor_learner.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



13
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
# File 'lib/editor_learner.rb', line 13

def initialize(*args)
  super
  @prac_dir="#{ENV['HOME']}/editor_learner/workshop"
  @lib_location = Open3.capture3("gem environment gemdir")
  @versions = Open3.capture3("gem list editor_learner")
  p @latest_version = @versions[0].chomp.gsub(' (', '-').gsub(')','')
  @inject = File.join(@lib_location[0].chomp, "/gems/#{@latest_version}/lib")
  if File.exist?(@prac_dir) != true then
    FileUtils.mkdir_p(@prac_dir)
    FileUtils.touch("#{@prac_dir}/question.rb")
    FileUtils.touch("#{@prac_dir}/answer.rb")
    FileUtils.touch("#{@prac_dir}/random_h.rb")
    if File.exist?("#{@inject}/random_h.rb") == true then
      FileUtils.cp("#{@inject}/random_h.rb", "#{@prac_dir}/random_h.rb")
    elsif  
      FileUtils.cp("#{ENV['HOME']}/editor_learner/lib/random_h.rb", "#{@prac_dir}/random_h.rb")
    end
  end
  range = 1..6
  range_ruby = 1..3
  range.each do|num|
    if File.exist?("#{@prac_dir}/ruby_#{num}") != true then
      FileUtils.mkdir("#{@prac_dir}/ruby_#{num}")
      FileUtils.touch("#{@prac_dir}/ruby_#{num}/q.rb")
      FileUtils.touch("#{@prac_dir}/ruby_#{num}/sequential_h.rb")
      if File.exist?("#{@inject}/sequential_h.rb") == true then
        FileUtils.cp("#{@inject}/sequential_h.rb", "#{@prac_dir}/ruby_#{num}/sequential_h.rb")
      else
        FileUtils.cp("#{ENV['HOME']}/editor_learner/lib/sequential_h.rb", "#{@prac_dir}/ruby_#{num}/sequential_h.rb")
      end
      range_ruby.each do|n|
        FileUtils.touch("#{@prac_dir}/ruby_#{num}/#{n}.rb")
      end
    end
  end
end

Instance Method Details

#delete(n, m) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/editor_learner.rb', line 52

def delete(n, m)
  range = n..m
  range.each{|num|
  if File.exist?("#{@prac_dir}/ruby_#{num}") == true then
    system "rm -rf #{@prac_dir}/ruby_#{num}"
  end
  }
end

#random_check(*argv) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/editor_learner.rb', line 108

def random_check(*argv)
  random = rand(1..15)
  p random
  s = "#{random}.rb"
  puts "check starting ..."
  puts "type following commands on the terminal"
  puts "> emacs question.rb answer.rb"

  src_dir = File.expand_path('../..', __FILE__) # "Users/souki/editor_learner"
  if File.exist?("#{@inject}/random_check_question/#{s}") == true then
    FileUtils.cp("#{@inject}/random_check_question/#{s}", "#{@prac_dir}/question.rb")
  elsif
    FileUtils.cp(File.join(src_dir, "lib/random_check_question/#{s}"),  "#{@prac_dir}/question.rb")
  end
  open_terminal
  
  start_time = Time.now
  loop do
    a = STDIN.gets.chomp
    if a == "check" && FileUtils.compare_file("#{@prac_dir}/question.rb", "#{@prac_dir}/answer.rb") == true then
      puts "It have been finished!"
      break
    elsif FileUtils.compare_file("#{@prac_dir}/question.rb", "#{@prac_dir}/answer.rb") != true then
      @inputdata = File.open("#{@prac_dir}/answer.rb").readlines
      @checkdata = File.open("#{@prac_dir}/question.rb").readlines
      diffs = Diff::LCS.diff("#{@inputdata}", "#{@checkdata}")
      diffs.each do |diff|
        p diff
      end
    end
  end
  end_time = Time.now
  time = end_time - start_time - 1
  
  puts "#{time} sec"
end

#sequential_check(*argv, n, m) ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/editor_learner.rb', line 62

def sequential_check(*argv, n, m)
  l = m.to_i - 1
 
  @seq_dir = "lib/sequential_check_question"
  q_rb = "ruby_#{n}/#{m}.rb"
  @seqnm_dir = File.join(@seq_dir,q_rb)
  @pracnm_dir = "#{ENV['HOME']}/editor_learner/workshop/ruby_#{n}/#{m}.rb"
  @seqnq_dir = "lib/sequential_check_question/ruby_#{n}/q.rb"
  @pracnq_dir = "#{ENV['HOME']}/editor_learner/workshop/ruby_#{n}/q.rb"      
  @seqnl_dir = "lib/sequential_check_question/ruby_#{n}/#{l}.rb"
  @pracnl_dir = "#{ENV['HOME']}/editor_learner/workshop/ruby_#{n}/#{l}.rb"      
  puts "check starting ..."
  puts "type following commands on the terminal"
  src_dir = File.expand_path('../..', __FILE__)
  if File.exist?("#{@inject}/sequential_check_question/ruby_#{n}/#{m}.rb") == true then
    FileUtils.cp("#{@inject}/sequential_check_question/ruby_#{n}/#{m}.rb", "#{@pracnq_dir}")
  elsif
    FileUtils.cp(File.join(src_dir, "#{@seqnm_dir}"),  "#{@pracnq_dir}")
  end
  if l != 0 && FileUtils.compare_file("#{@pracnm_dir}", "#{@pracnq_dir}") != true
    FileUtils.compare_file("#{@pracnl_dir}", (File.join(src_dir, "#{@seqnl_dir}"))) == true
    FileUtils.cp("#{@pracnl_dir}", "#{@pracnm_dir}")
  end
  
  if FileUtils.compare_file(@pracnm_dir, @pracnq_dir) != true then
    system "osascript -e 'tell application \"Terminal\" to do script \"cd #{@prac_dir}/ruby_#{n} \" '"
    loop do
      a = STDIN.gets.chomp
      if a == "check" && FileUtils.compare_file("#{@pracnm_dir}", "#{@pracnq_dir}") == true then
        puts "ruby_#{n}/#{m}.rb is done!"
        break
      elsif FileUtils.compare_file("#{@pracnm_dir}", "#{@pracnq_dir}") != true then
        @inputdata = File.open("#{@pracnm_dir}").readlines
        @checkdata = File.open("#{@pracnq_dir}").readlines
        diffs = Diff::LCS.diff("#{@inputdata}", "#{@checkdata}")
        diffs.each do |diff|
          p diff
        end
      end
    end
   else
    p "ruby_#{n}/#{m}.rb is finished!"
  end
end