shell-utils 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ class DateTimeFormatter
2
+
3
+ def format time=Time.new
4
+ year = format_to_length time.year, 4
5
+ month = format_to_length time.month, 2
6
+ day = format_to_length time.day, 2
7
+ hour = format_to_length time.hour, 2
8
+ minute = format_to_length time.min, 2
9
+ second = format_to_length time.sec, 2
10
+ "#{year}-#{month}-#{day}_#{hour}-#{minute}-#{second}"
11
+ end
12
+
13
+ private
14
+
15
+ def format_to_length value, len
16
+ value.to_s.rjust len,"0"
17
+ end
18
+
19
+ end
@@ -0,0 +1,61 @@
1
+ class Filename
2
+
3
+ DIR_SEPARATOR = '/'
4
+ WINDOWS_DIR_SEPARATOR = '\\'
5
+
6
+ attr_accessor :path_items
7
+
8
+ def initialize filename
9
+ filename = filename.gsub(WINDOWS_DIR_SEPARATOR, DIR_SEPARATOR)
10
+ @path_items = filename.split(DIR_SEPARATOR).delete_if{|item| item.empty?}
11
+ @absolute_path = filename.start_with? DIR_SEPARATOR
12
+ end
13
+
14
+ def absolute_path?
15
+ @absolute_path
16
+ end
17
+
18
+ def relative_path?
19
+ not absolute_path?
20
+ end
21
+
22
+ def without_extension
23
+ if @path_items.size > 0 and @path_items.last.include? '.' then
24
+ Filename.new(to_s.split('.')[0..-2].join('.'))
25
+ else
26
+ clone
27
+ end
28
+ end
29
+
30
+ def extract_last_path_item
31
+ Filename.new(nil_to_empty_string @path_items.last)
32
+ end
33
+
34
+ def remove_last_path_item
35
+ Filename.new(make_string(@path_items[0..-2]))
36
+ end
37
+
38
+ def to_unix_s
39
+ make_string @path_items
40
+ end
41
+
42
+ def to_windows_s
43
+ make_string @path_items, WINDOWS_DIR_SEPARATOR
44
+ end
45
+
46
+ def to_s
47
+ to_unix_s
48
+ end
49
+
50
+ private
51
+
52
+ def make_string the_path_items, seperator=DIR_SEPARATOR
53
+ prefix = absolute_path? ? seperator : ''
54
+ prefix + the_path_items.join(seperator)
55
+ end
56
+
57
+ def nil_to_empty_string object
58
+ object.nil? ? "" : object
59
+ end
60
+
61
+ end
@@ -0,0 +1,12 @@
1
+ class Progress
2
+
3
+ def self.next
4
+ STDOUT.print "."
5
+ STDOUT.flush
6
+ end
7
+
8
+ def self.end
9
+ STDOUT.puts
10
+ end
11
+ end
12
+
@@ -0,0 +1,19 @@
1
+ require 'zip/zip'
2
+ require 'zip/zipfilesystem'
3
+
4
+ class SessionZipper
5
+
6
+ def compress(path)
7
+ path.sub!(%r[/$],'')
8
+ archive = File.join(path,File.basename(path))+'.zip'
9
+ FileUtils.rm archive, :force=>true
10
+
11
+ Zip::ZipFile.open(archive, 'w') do |zipfile|
12
+ Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file|
13
+ zipfile.add(file.sub(path+'/',''),file)
14
+ end
15
+ end
16
+ archive
17
+ end
18
+
19
+ end
@@ -0,0 +1,9 @@
1
+ class ShellArgumentException < Exception
2
+
3
+ attr_accessor :command
4
+
5
+ def initialize command
6
+ @command = command
7
+ end
8
+
9
+ end
@@ -0,0 +1,5 @@
1
+ class ShellProcess
2
+
3
+ attr_accessor :output, :return_code
4
+
5
+ end
@@ -0,0 +1,161 @@
1
+ require 'tempfile'
2
+ require 'shell_process'
3
+ require 'yaml'
4
+
5
+ class ShellWrapper
6
+
7
+ MAX_STDOUT_LENGTH = 100000
8
+
9
+ def cp source, destination
10
+ FileUtils.cp source, destination
11
+ end
12
+
13
+ def cp_r source, destination
14
+ FileUtils.cp_r source, destination
15
+ end
16
+
17
+ def rm_r file_regex
18
+ files_in_dir_tree('.', file_regex).each do |file|
19
+ FileUtils.rm file
20
+ end
21
+ end
22
+
23
+ def mkdir dir
24
+ FileUtils.mkdir dir
25
+ end
26
+
27
+ def mkdir_p dirs
28
+ FileUtils.mkdir_p dirs
29
+ end
30
+
31
+ def rename old_filename, new_filename
32
+ File.rename old_filename, new_filename
33
+ end
34
+
35
+ def current_file
36
+ __FILE__
37
+ end
38
+
39
+ def execute command
40
+ process = ShellProcess.new
41
+ redirect_stderr_to_stdout = "2>&1"
42
+ spec_pipe = IO.popen("#{command} #{redirect_stderr_to_stdout}", "r")
43
+ process.output = spec_pipe.read MAX_STDOUT_LENGTH
44
+ spec_pipe.close
45
+ process.return_code = $?.exitstatus
46
+ puts process.output unless process.output.nil?
47
+ process
48
+ end
49
+
50
+ def write_file filename, content
51
+ file = File.new filename, "w"
52
+ file << content
53
+ file.close
54
+ end
55
+
56
+ def read_file filename
57
+ file = File.new filename
58
+ content = file.read
59
+ file.close
60
+ content
61
+ end
62
+
63
+ def creation_time filename
64
+ File.ctime(filename)
65
+ end
66
+
67
+ def modification_time filename
68
+ File.mtime(filename)
69
+ end
70
+
71
+ def real_dir_entries dir
72
+ Dir.new(dir).entries - ["..", "."]
73
+ end
74
+
75
+ def files_in_dir dir, regexp_pattern=nil
76
+ Dir.new(dir).entries.find_all{|entry|
77
+ file_matches_regexp? entry, regexp_pattern
78
+ }
79
+ end
80
+
81
+ def files_in_dir_tree dir, regexp_pattern=nil
82
+ Dir.glob("#{dir}/**/*").find_all{|entry|
83
+ file_matches_regexp? entry, regexp_pattern
84
+ }.sort
85
+ end
86
+
87
+ def file_matches_regexp? dir_entry, regexp_pattern
88
+ File.file?(dir_entry) and (not regexp_pattern or dir_entry =~ Regexp.new(regexp_pattern))
89
+ end
90
+
91
+
92
+ def newest_dir_entry dir, files=nil
93
+ unless files then files = real_dir_entries(dir) end
94
+ files.sort_by do |entry|
95
+ complete_path = File.join dir, entry
96
+ File.mtime(complete_path)
97
+ end.last
98
+ end
99
+
100
+ def file? filename
101
+ File.file? filename
102
+ end
103
+
104
+ def open_with_default_app filename
105
+ execute "#{open_command_name} #{filename}"
106
+ end
107
+
108
+ def read_properties filename
109
+ begin
110
+ YAML.load_file filename
111
+ rescue
112
+ raise PropertyFileMissingException.new filename
113
+ end
114
+ end
115
+
116
+ def remove_command_name
117
+ windows? ? 'del' : 'rm'
118
+ end
119
+
120
+ def open_command_name
121
+ if windows? then
122
+ 'start'
123
+ elsif mac? then
124
+ 'open'
125
+ else
126
+ 'xdg-open'
127
+ end
128
+ end
129
+
130
+ def expand_run_command command
131
+ if command.end_with?(".sh") then
132
+ "bash #{command}"
133
+ else
134
+ command
135
+ end
136
+ end
137
+
138
+ def shell_extension
139
+ windows? ? 'cmd' : 'sh'
140
+ end
141
+
142
+ def path_separator
143
+ windows? ? ';' : ':'
144
+ end
145
+
146
+ def dir_separator
147
+ File::ALT_SEPARATOR ? File::ALT_SEPARATOR : File::SEPARATOR
148
+ end
149
+
150
+ def windows?
151
+ platform = RUBY_PLATFORM.downcase
152
+ platform.include?("windows") or platform.include?("mingw32")
153
+ end
154
+
155
+ def mac?
156
+ platform = RUBY_PLATFORM.downcase
157
+ platform.include?("universal-darwin")
158
+ end
159
+
160
+ end
161
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - CodersDojo-Team
@@ -39,8 +39,14 @@ extensions: []
39
39
 
40
40
  extra_rdoc_files: []
41
41
 
42
- files: []
43
-
42
+ files:
43
+ - lib/shellutils/date_time_formatter.rb
44
+ - lib/shellutils/filename.rb
45
+ - lib/shellutils/progress.rb
46
+ - lib/shellutils/session_zipper.rb
47
+ - lib/shellutils/shell_argument_exception.rb
48
+ - lib/shellutils/shell_process.rb
49
+ - lib/shellutils/shell_wrapper.rb
44
50
  has_rdoc: true
45
51
  homepage:
46
52
  licenses: []