Class: QiitaGetTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_org/get_template.rb

Instance Method Summary collapse

Constructor Details

#initialize(os) ⇒ QiitaGetTemplate

Returns a new instance of QiitaGetTemplate.



8
9
10
11
12
13
# File 'lib/qiita_org/get_template.rb', line 8

def initialize(os)
  @os = os
  search = SearchConfPath.new(Dir.pwd, Dir.home)
  @conf_dir = search.search_conf_path()
  # check_write_header()
end

Instance Method Details

#check_write_contentsObject



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/qiita_org/get_template.rb', line 76

def check_write_contents()
  if @os == "mac"
    ["MacOS", "ruby"].each do |src|
      print "Write #{src} version?(y/n) "
      ans = STDIN.gets.chomp
      next if ans == "n"
      if ans == "y"
        send("get_#{src.downcase}_version")
      end
    end
  elsif @os == "windows"
    ["Ubuntu", "ruby"].each do |src|
      print "Write #{src} version?(y/n) "
      ans = STDIN.gets.chomp
      next if ans == "n"
      if ans == "y"
        send("get_#{src.downcase}_version")
      end
    end
  else
    ["ruby"].each do |src|
      print "Write #{src} version?(y/n) "
      ans = STDIN.gets.chomp
      next if ans == "n"
      if ans == "y"
        send("get_#{src.downcase}_version")
      end
    end
  end
end

#check_write_headerObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/qiita_org/get_template.rb', line 107

def check_write_header()
  ["name", "email"].each do |src|
    print "Write your #{src}?(y/n) "
    ans = STDIN.gets.chomp
    next if ans == "n"
    if ans == "y"
      send("get_#{src}")
    end
  end
end

#cp_templateObject

cp template.org



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/qiita_org/get_template.rb', line 64

def cp_template()
  lib = File.expand_path("../../../lib", __FILE__)
  cp_file = File.join(lib, "qiita_org", "template.org")

  if File.exists?("./template.org")
    puts "template.org exists.".red
    exit
  else
    FileUtils.cp(cp_file, ".", verbose: true)
  end
end

#get_emailObject



126
127
128
129
130
131
132
# File 'lib/qiita_org/get_template.rb', line 126

def get_email()
  conts = File.readlines("template.org")
  p "Type your email"
  email = STDIN.gets
  conts[4] = "#+EMAIL:     (concat \"#{email.chomp}\")\n"
  File.write("template.org", conts.join)
end

#get_macos_versionObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/qiita_org/get_template.rb', line 15

def get_macos_version()
  system 'sw_vers > hoge.txt'
  version = File.read("hoge.txt")
  m = []
  m = version.match(/ProductName:\t(.+)\nProductVersion:\t(.+)\nBuildVersion:\t(.+)\n/)
  system 'rm hoge.txt'
  conts = File.read("template.org")
  conts << "![#{m[1]}-#{m[2]}](https://img.shields.io/badge/#{m[1].gsub(" ", "")}-#{m[2]}-brightgreen) "
  File.write("template.org", conts) # + "# {m[1]}: # {m[2]}\n")
end

#get_nameObject



118
119
120
121
122
123
124
# File 'lib/qiita_org/get_template.rb', line 118

def get_name()
  conts = File.readlines("template.org")
  p "Type your name"
  name = STDIN.gets
  conts[3] = "#+AUTHOR: #{name}"
  File.write("template.org", conts.join)
end

#get_ruby_versionObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/qiita_org/get_template.rb', line 52

def get_ruby_version()
  system 'ruby --version > hoge.txt'
  version = File.read("hoge.txt")
  m = []
  m = version.match(/ruby (.+) \((.+)/)
  system 'rm hoge.txt'
  conts = File.read("template.org")
  conts << "![ruby-#{m[1]}](https://img.shields.io/badge/ruby-#{m[1].gsub(" ", "")}-brightgreen) "
  File.write("template.org", conts) # + "ruby: # {m[1]}\n")
end

#get_ubuntu_versionObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/qiita_org/get_template.rb', line 41

def get_ubuntu_version()
  system 'cat /etc/issue > hoge.txt'
  version = File.read("hoge.txt")
  m = []
  m = version.match(/(.+) (.+) LTS /)
  system 'rm hoge.txt'
  conts = File.read("template.org")
  conts << "![#{m[1]}-#{m[2]}](https://img.shields.io/badge/#{m[1]}-#{m[2]}-brightgreen) "
  File.write("template.org", conts)
end

#get_windowsos_versionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/qiita_org/get_template.rb', line 26

def get_windowsos_version()
  system 'wmic.exe os get caption > hoge1.txt'
  system 'wmic.exe os get osarchitecture > hoge2.txt'
  version1 = Kconv.tosjis(File.read("hoge1.txt"))
  version2 = Kconv.tosjis(File.read("hoge2.txt"))
  m1, m2 = [], []
  m1 = version1.match(/Caption\nMicrosoft (.+) (.+)/)
  m2 = version2.match(/OSArchitecture\n(.+)-bit/)
  system 'rm hoge1.txt'
  system 'rm hoge2.txt'
  conts = File.read("template.org")
  conts << "![#{m1[1]}-#{m1[2]}](https://img.shields.io/badge/#{m1[1].gsub(" ", "")}#{m1[2]}-#{m2[1]}bit-brightgreen) "
  File.write("template.org", conts) # + "# {m[1]}: # {m[2]}\n")
end

#runObject



145
146
147
148
149
150
# File 'lib/qiita_org/get_template.rb', line 145

def run()
  ErrorMessage.new().config_set_error(@conf_dir)
  cp_template()
  set_name_and_email()
  check_write_contents()
end

#set_name_and_emailObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/qiita_org/get_template.rb', line 134

def set_name_and_email()
  conf_path = File.join(@conf_dir, ".qiita.conf")
  conf = JSON.load(File.read(conf_path))
  name = conf["name"]
  email = conf["email"]
  conts = File.readlines("template.org")
  conts[3] = "#+AUTHOR: #{name}\n"
  conts[4] = "#+EMAIL:     (concat \"#{email}\")\n"
  File.write("template.org", conts.join)
end