Class: ShellWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/shellutils/shell_wrapper.rb

Constant Summary collapse

MAX_STDOUT_LENGTH =
100000

Instance Method Summary collapse

Instance Method Details

#cp(source, destination) ⇒ Object



9
10
11
# File 'lib/shellutils/shell_wrapper.rb', line 9

def cp source, destination
  FileUtils.cp source, destination
end

#cp_r(source, destination) ⇒ Object



13
14
15
# File 'lib/shellutils/shell_wrapper.rb', line 13

def cp_r source, destination
  FileUtils.cp_r source, destination
end

#creation_time(filename) ⇒ Object



63
64
65
# File 'lib/shellutils/shell_wrapper.rb', line 63

def creation_time filename
  File.ctime(filename)
end

#current_fileObject



35
36
37
# File 'lib/shellutils/shell_wrapper.rb', line 35

def current_file
	__FILE__
end

#dir_separatorObject



146
147
148
# File 'lib/shellutils/shell_wrapper.rb', line 146

def dir_separator
	File::ALT_SEPARATOR ? File::ALT_SEPARATOR : File::SEPARATOR
end

#execute(command) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/shellutils/shell_wrapper.rb', line 39

def execute command
process = ShellProcess.new 
 redirect_stderr_to_stdout = "2>&1"
  spec_pipe = IO.popen("#{command} #{redirect_stderr_to_stdout}", "r")
  process.output = spec_pipe.read
  spec_pipe.close
process.return_code = $?.exitstatus
  puts process.output unless process.output.nil?
  process
end

#expand_run_command(command) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/shellutils/shell_wrapper.rb', line 130

def expand_run_command command
	if command.end_with?(".sh") then
		"bash #{command}"
	else
		command
	end
end

#file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/shellutils/shell_wrapper.rb', line 100

def file? filename
	File.file? filename
end

#file_matches_regexp?(dir_entry, regexp_pattern) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/shellutils/shell_wrapper.rb', line 87

def file_matches_regexp? dir_entry, regexp_pattern
	File.file?(dir_entry) and (not regexp_pattern or dir_entry =~ Regexp.new(regexp_pattern))
end

#files_in_dir(dir, regexp_pattern = nil) ⇒ Object



75
76
77
78
79
# File 'lib/shellutils/shell_wrapper.rb', line 75

def files_in_dir dir, regexp_pattern=nil
	Dir.new(dir).entries.find_all{|entry|
		file_matches_regexp? entry, regexp_pattern
	}
end

#files_in_dir_tree(dir, regexp_pattern = nil) ⇒ Object



81
82
83
84
85
# File 'lib/shellutils/shell_wrapper.rb', line 81

def files_in_dir_tree dir, regexp_pattern=nil
	Dir.glob("#{dir}/**/*").find_all{|entry|
		file_matches_regexp? entry, regexp_pattern
	}.sort
end

#mac?Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/shellutils/shell_wrapper.rb', line 155

def mac?
	platform = RUBY_PLATFORM.downcase
	platform.include?("darwin")
end

#mkdir(dir) ⇒ Object



23
24
25
# File 'lib/shellutils/shell_wrapper.rb', line 23

def mkdir dir
  FileUtils.mkdir dir
end

#mkdir_p(dirs) ⇒ Object



27
28
29
# File 'lib/shellutils/shell_wrapper.rb', line 27

def mkdir_p dirs
  FileUtils.mkdir_p dirs
end

#modification_time(filename) ⇒ Object



67
68
69
# File 'lib/shellutils/shell_wrapper.rb', line 67

def modification_time filename
  File.mtime(filename)
end

#newest_dir_entry(dir, files = nil) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/shellutils/shell_wrapper.rb', line 92

def newest_dir_entry dir, files=nil
	unless files then files = real_dir_entries(dir) end
	files.sort_by do |entry| 
		complete_path = File.join dir, entry
		File.mtime(complete_path)
	end.last
end

#open_command_nameObject



120
121
122
123
124
125
126
127
128
# File 'lib/shellutils/shell_wrapper.rb', line 120

def open_command_name
	if windows? then
		'start'
	elsif mac? then
		'open'
	else
		'xdg-open'
	end
end

#open_with_default_app(filename) ⇒ Object



104
105
106
# File 'lib/shellutils/shell_wrapper.rb', line 104

def open_with_default_app filename
	execute "#{open_command_name} #{filename}"
end

#path_separatorObject



142
143
144
# File 'lib/shellutils/shell_wrapper.rb', line 142

def path_separator
	windows? ? ';' : ':'
end

#read_file(filename) ⇒ Object



56
57
58
59
60
61
# File 'lib/shellutils/shell_wrapper.rb', line 56

def read_file filename
  file = File.new filename
  content = file.read
  file.close
  content
end

#read_properties(filename) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/shellutils/shell_wrapper.rb', line 108

def read_properties filename
	begin
		YAML.load_file filename
	rescue
		raise PropertyFileMissingException.new filename
	end
end

#real_dir_entries(dir) ⇒ Object



71
72
73
# File 'lib/shellutils/shell_wrapper.rb', line 71

def real_dir_entries dir
	Dir.new(dir).entries - ["..", "."]
end

#remove_command_nameObject



116
117
118
# File 'lib/shellutils/shell_wrapper.rb', line 116

def remove_command_name
	windows? ? 'del' : 'rm'
end

#rename(old_filename, new_filename) ⇒ Object



31
32
33
# File 'lib/shellutils/shell_wrapper.rb', line 31

def rename old_filename, new_filename
	File.rename old_filename, new_filename
end

#rm_r(file_regex) ⇒ Object



17
18
19
20
21
# File 'lib/shellutils/shell_wrapper.rb', line 17

def rm_r file_regex
	files_in_dir_tree('.', file_regex).each do |file|
		FileUtils.rm file
	end
end

#shell_extensionObject



138
139
140
# File 'lib/shellutils/shell_wrapper.rb', line 138

def shell_extension
	windows? ? 'cmd' : 'sh'
end

#windows?Boolean

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/shellutils/shell_wrapper.rb', line 150

def windows?
	platform = RUBY_PLATFORM.downcase
	platform.include?("windows") or platform.include?("mingw32")
end

#write_file(filename, content) ⇒ Object



50
51
52
53
54
# File 'lib/shellutils/shell_wrapper.rb', line 50

def write_file filename, content
  file = File.new filename, "w"
  file << content
  file.close
end