Module: Buzztools::File
- Defined in:
- lib/buzztools/file.rb
Class Method Summary collapse
- .append_slash(aPath, aSep = nil) ⇒ Object
-
.canonize_path(aPath, aRootPath = nil) ⇒ Object
takes a path and combines it with a root path (which defaults to Dir.pwd) unless it is absolute the final result is then expanded.
- .change_ext(aFile, aExt, aExtend = false) ⇒ Object
-
.expand_magic_path(aPath, aBasePath = nil) ⇒ Object
allows special symbols in path currently only …
- .extension(aFile, aExtended = true) ⇒ Object
- .find_upwards(aStartPath, aPath) ⇒ Object
- .is_root_path?(aPath) ⇒ Boolean
- .no_extension(aFile, aExtended = true) ⇒ Object
- .path_combine(aBasePath, aPath) ⇒ Object
-
.path_debase(aPath, aBase) ⇒ Object
Remove base dir from given path.
- .path_parent(aPath) ⇒ Object
- .path_parts(aPath) ⇒ Object
- .path_rebase(aPath, aOldBase, aNewBase) ⇒ Object
- .path_relative?(aPath) ⇒ Boolean
-
.real_path(aPath) ⇒ Object
make path real according to file system.
- .remove_slash(aPath) ⇒ Object
- .simple_dir_name(aPath) ⇒ Object
- .simple_file_name(aPath) ⇒ Object
- .sniff_seperator(aPath) ⇒ Object
Class Method Details
.append_slash(aPath, aSep = nil) ⇒ Object
17 18 19 20 |
# File 'lib/buzztools/file.rb', line 17 def append_slash(aPath,aSep=nil) aSep = sniff_seperator(aPath) unless aSep aPath.ensure_suffix(aSep) end |
.canonize_path(aPath, aRootPath = nil) ⇒ Object
takes a path and combines it with a root path (which defaults to Dir.pwd) unless it is absolute the final result is then expanded
70 71 72 73 74 |
# File 'lib/buzztools/file.rb', line 70 def canonize_path(aPath,aRootPath=nil) path = path_combine(aRootPath,aPath) path = real_path(path) if path path end |
.change_ext(aFile, aExt, aExtend = false) ⇒ Object
135 136 137 |
# File 'lib/buzztools/file.rb', line 135 def change_ext(aFile,aExt,aExtend=false) no_extension(aFile,false)+(aExtend ? '.'+aExt+'.'+extension(aFile,false) : '.'+aExt) end |
.expand_magic_path(aPath, aBasePath = nil) ⇒ Object
allows special symbols in path currently only … supported, which looks upward in the filesystem for the following relative path from the basepath
86 87 88 89 90 91 92 93 94 |
# File 'lib/buzztools/file.rb', line 86 def (aPath,aBasePath=nil) aBasePath ||= Dir.pwd path = aPath if path.begins_with?('...') rel_part = path.split3(/\.\.\.[\/\\]/)[2] return find_upwards(aBasePath,rel_part) end path_combine(aBasePath,aPath) end |
.extension(aFile, aExtended = true) ⇒ Object
124 125 126 127 128 |
# File 'lib/buzztools/file.rb', line 124 def extension(aFile,aExtended=true) f = ::File.basename(aFile) dot = aExtended ? f.index('.') : f.rindex('.') return dot ? f[dot+1..-1] : f end |
.find_upwards(aStartPath, aPath) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/buzztools/file.rb', line 76 def find_upwards(aStartPath,aPath) curr_path = ::File.(aStartPath) while curr_path && !(test_path_exists = ::File.exists?(test_path = ::File.join(curr_path,aPath))) do curr_path = path_parent(curr_path) end curr_path && test_path_exists ? test_path : nil end |
.is_root_path?(aPath) ⇒ Boolean
96 97 98 99 100 101 102 |
# File 'lib/buzztools/file.rb', line 96 def is_root_path?(aPath) # if is_windows? # (aPath =~ /^[a-zA-Z]\:[\\\/]$/)==0 # else aPath == '/' # end end |
.no_extension(aFile, aExtended = true) ⇒ Object
130 131 132 133 |
# File 'lib/buzztools/file.rb', line 130 def no_extension(aFile,aExtended=true) ext = extension(aFile,aExtended) return aFile.chomp('.'+ext) end |
.path_combine(aBasePath, aPath) ⇒ Object
57 58 59 60 61 |
# File 'lib/buzztools/file.rb', line 57 def path_combine(aBasePath,aPath) return aBasePath if !aPath return aPath if !aBasePath return path_relative?(aPath) ? ::File.join(aBasePath,aPath) : aPath end |
.path_debase(aPath, aBase) ⇒ Object
Remove base dir from given path. Result will be relative to base dir and not have a leading or trailing slash ‘/a/b/c’,‘/a’ = ‘b/c’ ‘/a/b/c’,‘/’ = ‘a/b/c’ ‘/’,‘/’ = ”
40 41 42 43 44 |
# File 'lib/buzztools/file.rb', line 40 def path_debase(aPath,aBase) aBase = append_slash(aBase) aPath = remove_slash(aPath) unless aPath=='/' aPath[0,aBase.length]==aBase ? aPath[aBase.length,aPath.length-aBase.length] : aPath end |
.path_parent(aPath) ⇒ Object
104 105 106 107 |
# File 'lib/buzztools/file.rb', line 104 def path_parent(aPath) return nil if is_root_path?(aPath) append_slash(::File.dirname(remove_slash(::File.(aPath)))) end |
.path_parts(aPath) ⇒ Object
119 120 121 122 |
# File 'lib/buzztools/file.rb', line 119 def path_parts(aPath) sep = sniff_seperator(aPath) aPath.split(sep) end |
.path_rebase(aPath, aOldBase, aNewBase) ⇒ Object
46 47 48 49 |
# File 'lib/buzztools/file.rb', line 46 def path_rebase(aPath,aOldBase,aNewBase) rel_path = path_debase(aPath,aOldBase) append_slash(aNewBase)+rel_path end |
.path_relative?(aPath) ⇒ Boolean
51 52 53 54 55 |
# File 'lib/buzztools/file.rb', line 51 def path_relative?(aPath) return false if aPath[0,1]=='/' return false if aPath =~ /^[a-zA-Z]:/ return true end |
.real_path(aPath) ⇒ Object
make path real according to file system
64 65 66 |
# File 'lib/buzztools/file.rb', line 64 def real_path(aPath) (path = Pathname.new(::File.(aPath))) && path.realpath.to_s end |
.remove_slash(aPath) ⇒ Object
22 23 24 25 26 |
# File 'lib/buzztools/file.rb', line 22 def remove_slash(aPath) last_char = aPath[-1,1] aPath = aPath[0..-2] if last_char=='\\' || last_char=='/' return aPath end |
.simple_dir_name(aPath) ⇒ Object
109 110 111 |
# File 'lib/buzztools/file.rb', line 109 def simple_dir_name(aPath) ::File.basename(remove_slash(aPath)) end |
.simple_file_name(aPath) ⇒ Object
113 114 115 116 117 |
# File 'lib/buzztools/file.rb', line 113 def simple_file_name(aPath) f = ::File.basename(aPath) dot = f.index('.') return dot ? f[0,dot] : f end |
.sniff_seperator(aPath) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/buzztools/file.rb', line 8 def sniff_seperator(aPath) result = 0.upto(aPath.length-1) do |i| char = aPath[i,1] break char if char=='\\' || char=='/' end result = ::File::SEPARATOR if result==0 return result end |