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.



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

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

Instance Method Details

#check_write_contentsObject



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
106
107
# File 'lib/qiita_org/get_template.rb', line 78

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#set_name_and_emailObject



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

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