Module: Milkode::Util

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

Defined Under Namespace

Classes: ZipfileNotFound

Class Method Summary collapse

Class Method Details

.downcase?(str) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/milkode/common/util.rb', line 103

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

.filename_to_utf8(str_from_file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/milkode/common/util.rb', line 77

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

.gotoline_multi?(words) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
# File 'lib/milkode/common/util.rb', line 145

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

.larger_than_oneline(content) ⇒ Object



91
92
93
# File 'lib/milkode/common/util.rb', line 91

def larger_than_oneline(content)
  content.count($/) > 1      
end

.normalize_filename(str) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/milkode/common/util.rb', line 95

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]]



110
111
112
113
114
115
116
# File 'lib/milkode/common/util.rb', line 110

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

.parse_gotoline_multi(words) ⇒ Object



138
139
140
141
142
143
# File 'lib/milkode/common/util.rb', line 138

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/milkode/common/util.rb', line 118

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

.platform_osx?Boolean

Returns:

  • (Boolean)


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

def platform_osx?
  RUBY_PLATFORM =~ /darwin/
end

.platform_win?Boolean

Returns:

  • (Boolean)


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

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

.relative_path(path, basedir) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/milkode/common/util.rb', line 47

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/milkode/common/util.rb', line 35

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)


57
58
59
# File 'lib/milkode/common/util.rb', line 57

def ruby19?
  RUBY_VERSION >= '1.9.0'
end

.shell_kcodeObject



69
70
71
72
73
74
75
# File 'lib/milkode/common/util.rb', line 69

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

.zip_extract(filename, dst_dir) ⇒ Object

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

Raises:



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

def zip_extract(filename, dst_dir)
  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