Class: File

Inherits:
IO show all
Defined in:
opal/opal/corelib/file.rb

Overview

helpers: truthy backtick_javascript: true

Constant Summary collapse

Separator =
SEPARATOR = '/'
ALT_SEPARATOR =
nil
PATH_SEPARATOR =
':'
FNM_SYSCASE =

Assuming case insenstive filesystem

0

Instance Attribute Summary

Attributes inherited from IO

#eof, #read_proc, #sync, #tty, #write_proc

Class Method Summary collapse

Methods inherited from IO

#<<, #check_readable, #check_writable, #close, #close_read, #close_write, #closed?, #closed_read?, #closed_write?, #each, #each_byte, #each_char, #fileno, #flush, #getbyte, #getc, #gets, #initialize, #print, #puts, #read, #readbyte, #readchar, #readline, #readlines, #readpartial, #sysread, #sysread_noraise, #tty?, #write

Constructor Details

This class inherits a constructor from IO

Class Method Details

.absolute_path(path, basedir = nil) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'opal/opal/corelib/file.rb', line 13

def absolute_path(path, basedir = nil)
  sep = SEPARATOR
  sep_chars = `$sep_chars()`
  new_parts = []

  path = path.respond_to?(:to_path) ? path.to_path : path
  path = ::Opal.coerce_to!(`path`, ::String, :to_str)

  basedir ||= ::Dir.pwd
  path_abs    = `path.substr(0, sep.length) === sep || windows_root_rx.test(path)`
  basedir_abs = `basedir.substr(0, sep.length) === sep || windows_root_rx.test(basedir)`

  if path_abs
    parts       = path.split(/[#{sep_chars}]/)
    leading_sep = `windows_root_rx.test(path) ? '' : #{path.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    abs         = true
  else
    parts       = basedir.split(/[#{sep_chars}]/) + path.split(/[#{sep_chars}]/)
    leading_sep = `windows_root_rx.test(basedir) ? '' : #{basedir.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    abs         = basedir_abs
  end

  %x{
    var part;
    for (var i = 0, ii = parts.length; i < ii; i++) {
      part = parts[i];

      if (
        (part === nil) ||
        (part === ''  && ((new_parts.length === 0) || abs)) ||
        (part === '.' && ((new_parts.length === 0) || abs))
      ) {
        continue;
      }
      if (part === '..') {
        new_parts.pop();
      } else {
        new_parts.push(part);
      }
    }

    if (!abs && parts[0] !== '.') {
      #{new_parts.unshift '.'}
    }
  }

  new_path = new_parts.join(sep)
  new_path = leading_sep + new_path if abs
  new_path
end

.basename(name, suffix = nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'opal/opal/corelib/file.rb', line 132

def basename(name, suffix = nil)
  sep_chars = `$sep_chars()`
  name = `$coerce_to_path(name)`
  %x{
    if (name.length == 0) {
      return name;
    }

    if (suffix !== nil) {
      suffix = #{::Opal.coerce_to!(suffix, ::String, :to_str)}
    } else {
      suffix = null;
    }

    name = name.replace(new RegExp(#{"(.)[#{sep_chars}]*$"}), '$1');
    name = name.replace(new RegExp(#{"^(?:.*[#{sep_chars}])?([^#{sep_chars}]+)$"}), '$1');

    if (suffix === ".*") {
      name = name.replace(/\.[^\.]+$/, '');
    } else if(suffix !== null) {
      suffix = Opal.escape_regexp(suffix);
      name = name.replace(new RegExp(#{"#{suffix}$"}), '');
    }

    return name;
  }
end

.directory?(path) ⇒ Boolean

Returns:



173
174
175
176
177
178
179
180
181
182
183
# File 'opal/opal/corelib/file.rb', line 173

def directory?(path)
  files = []
  %x{
    for (var key in Opal.modules) {
      #{files}.push(key)
    }
  }
  path = path.gsub(/(^.#{SEPARATOR}+|#{SEPARATOR}+$)/)
  file = files.find { |f| f =~ /^#{path}/ }
  file
end

.dirname(path, level = 1) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'opal/opal/corelib/file.rb', line 103

def dirname(path, level = 1)
  return path if level == 0
  ::Kernel.raise ::ArgumentError, "level can't be negative" if level < 0

  sep_chars = `$sep_chars()`
  path = `$coerce_to_path(path)`
  %x{
    var absolute = path.match(new RegExp(#{"^[#{sep_chars}]"})), out;

    path = path.replace(new RegExp(#{"[#{sep_chars}]+$"}), ''); // remove trailing separators
    path = path.replace(new RegExp(#{"[^#{sep_chars}]+$"}), ''); // remove trailing basename
    path = path.replace(new RegExp(#{"[#{sep_chars}]+$"}), ''); // remove final trailing separators

    if (path === '') {
      out = absolute ? '/' : '.';
    }
    else {
      out = path;
    }

    if (level == 1) {
      return out;
    }
    else {
      return #{dirname(`out`, level - 1)}
    }
  }
end

.exist?(path) ⇒ Boolean Also known as: exists?

Returns:



169
170
171
# File 'opal/opal/corelib/file.rb', line 169

def exist?(path)
  `Opal.modules[#{path}] != null`
end

.expand_path(path, basedir = nil) ⇒ Object Also known as: realpath



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'opal/opal/corelib/file.rb', line 64

def expand_path(path, basedir = nil)
  sep = SEPARATOR
  sep_chars = `$sep_chars()`
  if `path[0] === '~' || (basedir && basedir[0] === '~')`
    home = Dir.home
    ::Kernel.raise(::ArgumentError, "couldn't find HOME environment -- expanding `~'") unless home
    leading_sep = `windows_root_rx.test(home) ? '' : #{home.sub(/^([#{sep_chars}]+).*$/, '\1')}`
    ::Kernel.raise(::ArgumentError, 'non-absolute home') unless home.start_with?(leading_sep)

    home            += sep
    home_path_regexp = /^\~(?:#{sep}|$)/
    path             = path.sub(home_path_regexp, home)
    basedir          = basedir.sub(home_path_regexp, home) if basedir
  end
  absolute_path(path, basedir)
end

.extname(path) ⇒ Object



160
161
162
163
164
165
166
167
# File 'opal/opal/corelib/file.rb', line 160

def extname(path)
  `path = $coerce_to_path(path)`
  filename = basename(path)
  return '' if filename.empty?
  last_dot_idx = filename[1..-1].rindex('.')
  # extension name must contains at least one character .(something)
  last_dot_idx.nil? || last_dot_idx + 1 == filename.length - 1 ? '' : filename[(last_dot_idx + 1)..-1]
end

.join(*paths) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'opal/opal/corelib/file.rb', line 185

def join(*paths)
  if paths.empty?
    return ''
  end
  result = ''
  paths = paths.flatten.each_with_index.map do |item, index|
    if index == 0 && item.empty?
      SEPARATOR
    elsif paths.length == index + 1 && item.empty?
      SEPARATOR
    else
      item
    end
  end
  paths = paths.reject(&:empty?)
  paths.each_with_index do |item, index|
    next_item = paths[index + 1]
    if next_item.nil?
      result = "#{result}#{item}"
    else
      if item.end_with?(SEPARATOR) && next_item.start_with?(SEPARATOR)
        item = item.sub(/#{SEPARATOR}+$/, '')
      end
      result = if item.end_with?(SEPARATOR) || next_item.start_with?(SEPARATOR)
                 "#{result}#{item}"
               else
                 "#{result}#{item}#{SEPARATOR}"
               end
    end
  end
  result
end

.split(path) ⇒ Object



218
219
220
# File 'opal/opal/corelib/file.rb', line 218

def split(path)
  path.split(SEPARATOR)
end