Class: FileWrapper

Inherits:
Object show all
Defined in:
lib/ceedling/file_wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_include_guard(name) ⇒ Object



18
19
20
21
22
23
# File 'lib/ceedling/file_wrapper.rb', line 18

def self.generate_include_guard(name)
  # abc-XYZ.h --> _ABC_XYZ_H_
  base = File.basename(name, '.*') # Remove any extension
  guard = '__' + CEEDLING_GENERATED + '_' + base.gsub(/\W/, '_').upcase + '_H__'
  return guard
end

Instance Method Details

#basename(path, extension = nil) ⇒ Object



29
30
31
32
# File 'lib/ceedling/file_wrapper.rb', line 29

def basename(path, extension=nil)
  return File.basename(path, extension) if extension
  return File.basename(path)
end

#compare(from, to) ⇒ Object



86
87
88
# File 'lib/ceedling/file_wrapper.rb', line 86

def compare(from, to)
  return FileUtils.compare_file(from, to)
end

#cp(source, destination, options = {}) ⇒ Object



74
75
76
# File 'lib/ceedling/file_wrapper.rb', line 74

def cp(source, destination, options={})
  FileUtils.cp(source, destination, **options)
end

#cp_r(source, destination, options = {}) ⇒ Object



78
79
80
# File 'lib/ceedling/file_wrapper.rb', line 78

def cp_r(source, destination, options={})
  FileUtils.cp_r(source, destination, **options)
end

#directory?(path) ⇒ Boolean

Is path a directory and does it exist?

Returns:

  • (Boolean)


44
45
46
# File 'lib/ceedling/file_wrapper.rb', line 44

def directory?(path)
  return File.directory?(path)
end

#directory_listing(glob) ⇒ Object



56
57
58
59
60
# File 'lib/ceedling/file_wrapper.rb', line 56

def directory_listing(glob)
  # Note: `sort()` to ensure platform-independent directory listings (Github Issue #860)
  # FNM_PATHNAME => Case insensitive globs
  return Dir.glob(glob, File::FNM_PATHNAME).sort()
end

#dirname(path) ⇒ Object



52
53
54
# File 'lib/ceedling/file_wrapper.rb', line 52

def dirname(path)
  return File.dirname(path)
end

#exist?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/ceedling/file_wrapper.rb', line 34

def exist?(filepath)
  return true if (filepath == NULL_FILE_PATH)
  return File.exist?(filepath)
end

#extname(filepath) ⇒ Object



39
40
41
# File 'lib/ceedling/file_wrapper.rb', line 39

def extname(filepath)
  return File.extname(filepath)
end

#get_expanded_path(path) ⇒ Object



25
26
27
# File 'lib/ceedling/file_wrapper.rb', line 25

def get_expanded_path(path)
  return File.expand_path(path)
end

#instantiate_file_list(files = []) ⇒ Object



128
129
130
# File 'lib/ceedling/file_wrapper.rb', line 128

def instantiate_file_list(files=[])
  return FileList.new(files)
end

#mkdir(folder) ⇒ Object



132
133
134
# File 'lib/ceedling/file_wrapper.rb', line 132

def mkdir(folder)
  return FileUtils.mkdir_p(folder)
end

#mkdir_tmp(prefix, parent) ⇒ Object

Creates a uniquely-named, empty directory nested inside parent and returns its path. parent must already exist. Collision-free even across concurrent callers targeting the same parent -- Dir.mktmpdir retries internally on name clash.



139
140
141
# File 'lib/ceedling/file_wrapper.rb', line 139

def mkdir_tmp(prefix, parent)
  return Dir.mktmpdir(prefix, parent)
end

#mv(source, destination, options = {}) ⇒ Object



82
83
84
# File 'lib/ceedling/file_wrapper.rb', line 82

def mv(source, destination, options={})
  FileUtils.mv(source, destination, **options)
end

#newer?(filepathA, filepathB) ⇒ Boolean

Is filepath A newer than B?

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/ceedling/file_wrapper.rb', line 91

def newer?(filepathA, filepathB)
  return false unless File.exist?(filepathA)
  return false unless File.exist?(filepathB)

  return (File.mtime(filepathA) > File.mtime(filepathB))
end

#open(filepath, flags) ⇒ Object



98
99
100
101
102
# File 'lib/ceedling/file_wrapper.rb', line 98

def open(filepath, flags)
  File.open(filepath, flags) do |file|
    yield(file)
  end
end

#read(filepath, length = nil) ⇒ Object



104
105
106
# File 'lib/ceedling/file_wrapper.rb', line 104

def read(filepath, length=nil)
  return File.read(filepath, length)
end

#readlines(filepath) ⇒ Object



124
125
126
# File 'lib/ceedling/file_wrapper.rb', line 124

def readlines(filepath)
  return File.readlines(filepath)
end

#relative?(path) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ceedling/file_wrapper.rb', line 48

def relative?(path)
  return Pathname.new( path).relative?
end

#rm_f(filepath, options = {}) ⇒ Object



62
63
64
# File 'lib/ceedling/file_wrapper.rb', line 62

def rm_f(filepath, options={})
  FileUtils.rm_f(filepath, **options)
end

#rm_r(filepath, options = {}) ⇒ Object



66
67
68
# File 'lib/ceedling/file_wrapper.rb', line 66

def rm_r(filepath, options={})
  FileUtils.rm_r(filepath, **options={})
end

#rm_rf(path, options = {}) ⇒ Object



70
71
72
# File 'lib/ceedling/file_wrapper.rb', line 70

def rm_rf(path, options={})
  FileUtils.rm_rf(path, **options={})
end

#touch(filepath, options = {}) ⇒ Object



108
109
110
# File 'lib/ceedling/file_wrapper.rb', line 108

def touch(filepath, options={})
  FileUtils.touch(filepath, **options)
end

#write(filepath, contents, flags = 'w') ⇒ Object



118
119
120
121
122
# File 'lib/ceedling/file_wrapper.rb', line 118

def write(filepath, contents, flags='w')
  File.open(filepath, flags) do |file|
    file.write(contents)
  end
end

#write_blank_file(filepath) ⇒ Object



112
113
114
115
116
# File 'lib/ceedling/file_wrapper.rb', line 112

def write_blank_file(filepath)
  File.open(filepath, 'w') do |file|
    file.write("// Ceedling intentionally blank file\n\n")
  end
end