Module: Milkode::Util

Defined in:
lib/milkode/common/util.rb

Defined Under Namespace

Classes: ZipfileNotFound

Class Method Summary collapse

Class Method Details

.divide_shortpath(shortpath) ⇒ Object

‘package/to/a.txt’ #=> ‘package’, ‘to/a.txt’ ‘package’ #=> ‘package’, nil



178
179
180
181
182
183
184
185
186
187
# File 'lib/milkode/common/util.rb', line 178

def divide_shortpath(shortpath)
  shortpath = shortpath[1..-1] if shortpath[0..0] == '/' # 先頭の'/'はカット
  a = shortpath.split('/')

  if (a.size >= 2)
    return a[0], a[1..-1].join('/')
  else
    return a[0], nil
  end
end

.downcase?(str) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/milkode/common/util.rb', line 114

def downcase?(str)
  str == str.downcase
end

.exist_command?(command) ⇒ Boolean

指定したコマンドが存在するか?

Returns:

  • (Boolean)


237
238
239
240
241
242
243
# File 'lib/milkode/common/util.rb', line 237

def exist_command?(command)
  begin
    Open3.capture3('type', command)[2].exited?
  rescue Errno::ENOENT
    false
  end
end

.filename_to_utf8(str_from_file) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/milkode/common/util.rb', line 84

def filename_to_utf8(str_from_file)
  if platform_osx?
    if (ruby19?)
      str_from_file.encode('UTF-8', 'UTF8-MAC')
    else
      str_from_file
    end
  elsif platform_win?
    Kconv.kconv(str_from_file, Kconv::UTF8)        
  else
    str_from_file
  end
end

.fuzzy_gotoline_keyword?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/milkode/common/util.rb', line 126

def fuzzy_gotoline_keyword?(keyword)
  keyword =~ /\A.*:\d+\Z/
end

.gem_version_more?(name, version) ⇒ Boolean

gem_version_more?(‘rroonga’, ‘2.1.0’) #=> rroonga >= ‘2.1.0’

Returns:

  • (Boolean)


223
224
225
# File 'lib/milkode/common/util.rb', line 223

def gem_version_more?(name, version)
  Gem.loaded_specs[name].version >= Gem::Version.new(version)
end

.git_url?(src) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/milkode/common/util.rb', line 189

def git_url?(src)
  (src =~ /^(:?git[:@])|(:?ssh:)/) != nil
end

.gotoline_keyword?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/milkode/common/util.rb', line 122

def gotoline_keyword?(keyword)
  keyword =~ /\A\/.*:\d+\Z/
end

.gotoline_multi?(words) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/milkode/common/util.rb', line 168

def gotoline_multi?(words)
  if (words.join(" ") =~ /:\d+/)
    true
  else
    false
  end
end

.groonga_table_sort(table, keys, options = {}) ⇒ Object

互換性保持のため



228
229
230
231
232
233
234
# File 'lib/milkode/common/util.rb', line 228

def groonga_table_sort(table, keys, options = {})
  if gem_version_more?('rroonga', '2.1.0')
    table.sort(keys, options).map{|r| r.value}
  else
    table.sort(keys, options)
  end
end

.highlight_keywords(src, keywords, css_class) ⇒ Object



245
246
247
248
249
250
251
# File 'lib/milkode/common/util.rb', line 245

def highlight_keywords(src, keywords, css_class)
  if keywords.empty?
    src
  else
    highlight_keywords_sub(src, keywords, css_class, 0)
  end
end

.highlight_keywords_sub(src, keywords, css_class, index) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/milkode/common/util.rb', line 253

def highlight_keywords_sub(src, keywords, css_class, index)
  keyword = keywords[index]

  array = src.split(keyword)

  if index + 1 <= keywords.size
    array = array.map do |subsrc|
      highlight_keywords_sub(subsrc, keywords, css_class, index + 1)
    end
  end
  
  array.join("<span class='#{css_class}'>#{keyword}</span>")
end

.ignore_case?(pattens, is_sensitive) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/milkode/common/util.rb', line 118

def ignore_case?(pattens, is_sensitive)
  !is_sensitive && (pattens.all? {|v| Util::downcase? v})
end

.larger_than_oneline(content) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/milkode/common/util.rb', line 98

def larger_than_oneline(content)
  begin
    content && content.count($/) > 1      
  rescue ArgumentError
    true
  end
end

.load_content(out, filename) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/milkode/common/util.rb', line 206

def load_content(out, filename)
  str = File.read(filename)
  begin
    Kconv.kconv(str, Kconv::UTF8)
  rescue ArgumentError
    warning_alert(out, "skip kconv. file size too big (or negative string size) : #{filename}.")
    str
  end
end

.normalize_filename(str) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/milkode/common/util.rb', line 106

def normalize_filename(str)
  if platform_win?
    str.gsub(/\A([a-z]):/) { "#{$1.upcase}:" }
  else
    str
  end
end

.parse_gotoline(words) ⇒ Object

parse_gotoline([‘a’, ‘123’, ‘b’]) #=> [[‘a’, ‘b’], 123]] parse_gotoline([‘a’, ‘123’, ‘b’, 55]) #=> [[‘a’, ‘b’, ‘123’], 55]] parse_gotoline() #=> [[‘a’], 55]]



133
134
135
136
137
138
139
# File 'lib/milkode/common/util.rb', line 133

def parse_gotoline(words)
  if gotoline_multi?(words)
    parse_gotoline_multi(words)
  else
    [parse_gotoline_single(words)]
  end
end

.parse_gotoline_multi(words) ⇒ Object



161
162
163
164
165
166
# File 'lib/milkode/common/util.rb', line 161

def parse_gotoline_multi(words)
  words.map do |v|
    a = v.split(':')
    [[a[0..-2].join(':')], a[-1].to_i]
  end
end

.parse_gotoline_single(words) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/milkode/common/util.rb', line 141

def parse_gotoline_single(words)
  lineno = -1
  index = -1

  words.each_with_index do |v, idx|
    n = v.to_i
    if (n != 0)
      lineno = n
      index = idx
    end
  end

  if (lineno == -1)
    [words, 1]              # 行番号らしきものは見つからなかった
  else
    words.delete_at(index)
    [words, lineno]        
  end
end

.pipe?(io) ⇒ Boolean

StringIO patch

Returns:

  • (Boolean)


198
199
200
# File 'lib/milkode/common/util.rb', line 198

def pipe?(io)
  !io.instance_of?(IO) || !File.pipe?(io) 
end

.platform_osx?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/milkode/common/util.rb', line 72

def platform_osx?
  RUBY_PLATFORM =~ /darwin/
end

.platform_win?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/milkode/common/util.rb', line 68

def platform_win?
  RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/
end

.relative_path(path, basedir) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/milkode/common/util.rb', line 50

def relative_path(path, basedir)
  path = Pathname.new(normalize_filename path)
  basedir = Pathname.new(normalize_filename basedir)
  begin
    path.relative_path_from(basedir)
  rescue ArgumentError
    path
  end
end

.root_entrylist(filename) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/milkode/common/util.rb', line 38

def root_entrylist(filename)
  list = []
  
  Archive::Zip.open(filename) do |archive|
    archive.each do |entry|
      list << entry.zip_path if entry.zip_path.split('/').size == 1
    end
  end

  list
end

.ruby19?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/milkode/common/util.rb', line 64

def ruby19?
  RUBY_VERSION >= '1.9.0'
end

.ruby20?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/milkode/common/util.rb', line 60

def ruby20?
  RUBY_VERSION >= '2.0.0'
end

.shell_kcodeObject



76
77
78
79
80
81
82
# File 'lib/milkode/common/util.rb', line 76

def shell_kcode
  if platform_win?
    Kconv::SJIS             # win7? cygwin utf8?
  else
    Kconv::UTF8
  end
end

.svn_url?(src) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/milkode/common/util.rb', line 193

def svn_url?(src)
  (src =~ /^(:?svn|svn\+ssh):\/\//) != nil
end

.truncate_nsec(t) ⇒ Object

Timeからnsecを切り捨てる

rroongaのTimeカラムはマイクロ秒までしか格納出来ない


218
219
220
# File 'lib/milkode/common/util.rb', line 218

def truncate_nsec(t)
  Time.at(t.to_i, t.usec) 
end

.warning_alert(out, msg) ⇒ Object



202
203
204
# File 'lib/milkode/common/util.rb', line 202

def warning_alert(out, msg)
  out.puts "[warning] #{msg}"
end

.zip_extract(filename, dst_dir) ⇒ Object

zipファイルを展開し、展開フォルダ名を返す ファイルが見つからなかった時はnilを返す

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/milkode/common/util.rb', line 18

def zip_extract(filename, dst_dir)
  require 'archive/zip'

  raise ZipfileNotFound unless File.exist?(filename)
  
  root_list = root_entrylist(filename)
  
  if (root_list.size == 1)
    # そのまま展開
    Archive::Zip.extract filename, dst_dir
    return root_list[0].gsub("/", "")
  else
    # ディレクトリを作ってその先で展開
    dir = File.basename(filename).sub(/#{File.extname(filename)}$/, "")
    FileUtils.mkdir_p File.join(dst_dir, dir)
    Archive::Zip.extract filename, File.join(dst_dir, dir)
    return dir
  end
end