atk_toolbox 0.0.130 → 0.0.133

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b369f7ae82e59686f07304f57e859e8437c3b369ee59c7b8ee8885a0a1fc3d2d
4
- data.tar.gz: d5e67cd57d72c8e42464158912f566266cee10cd23922ded50610f9b437f12eb
3
+ metadata.gz: 07d2a21daf5799bcdee004695ddb8b6ac30b283e3d3aa180ac9bb52c76dbef9b
4
+ data.tar.gz: 5ae84a0a51a2f55782e87a8814f1c29c90000cebffb8456be3c49c91290c8ee6
5
5
  SHA512:
6
- metadata.gz: 8b7cb087906053a8523a2b185239ded611a0b908a20d85ae5a91d62fec8cb6dcda3e32919e0e7ada286bda483dbe114bdae658c87646d9cd124023c189816389
7
- data.tar.gz: a579e2622583111e1a6abecba07c4002ec7f4f5be99ddc2adfe08f5da64c33e75c15ddaa7c9b4698a53cdc2ff537acbe3aeee8319a02fe442a48abdc7faa7209
6
+ metadata.gz: 4f05d179adc38416ff61f379b6979ee6e782bce2bf2e748b5c3bc7372290ebb56abdfea6d999ecc738fb8445b12faf98dcd01708287c0273e3cbed7faf29535a
7
+ data.tar.gz: 691e2660072bee5dfcb8bd0c7500e357c0c1c9d111875150b0a02b8a7c3761a9654fe901f355e1b34c6b55bfd65ad63b54508f27d9bd1ef125d288f981aab9ad
data/lib/atk/console.rb CHANGED
@@ -179,6 +179,41 @@ Console = Class.new do
179
179
  end
180
180
  end
181
181
  end
182
+
183
+ def set_command(name, code)
184
+ require_relative './file_system'
185
+ require_relative './os'
186
+ require_relative './atk_info'
187
+ if OS.is?("unix")
188
+ exec_path = "#{Atk.paths[:commands]}/#{name}"
189
+ local_place = Atk.temp_path(name)
190
+ # add the hash bang
191
+ hash_bang = "#!#{Atk.paths[:ruby]}\n"
192
+ # create the file
193
+ FS.write(hash_bang+code, to: local_place)
194
+ # copy to command folder
195
+ system("sudo", "cp", local_place, exec_path)
196
+ system("sudo", "chmod", "ugo+x", exec_path)
197
+ elsif OS.is?("windows")
198
+ # check for invalid file paths, see https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
199
+ if name =~ /[><:"\/\\|?*]/
200
+ raise <<-HEREDOC.remove_indent
201
+
202
+
203
+ When using the ATK Console.set_command(name)
204
+ The name: #{name}
205
+ is not a valid file path on windows
206
+ which means it cannot be a command
207
+ HEREDOC
208
+ end
209
+ exec_path = "#{Atk.paths[:commands]}\\#{name}"
210
+
211
+ # create the code
212
+ IO.write(exec_path+".rb", code)
213
+ # create an executable to call the code
214
+ IO.write(exec_path+".bat", "@echo off\nruby \"#{exec_path}.rb\" %*")
215
+ end
216
+ end
182
217
  end.new
183
218
 
184
219
  def log(*args)
@@ -25,7 +25,8 @@ class String
25
25
  end
26
26
  end
27
27
 
28
- module FileSystem
28
+
29
+ FS = FileSystem = Class.new do
29
30
  # This is a combination of the FileUtils, File, Pathname, IO, Etc, and Dir classes,
30
31
  # along with some other helpful methods
31
32
  # It is by-default forceful (dangerous/overwriting)
@@ -40,21 +41,21 @@ module FileSystem
40
41
  # zip
41
42
  # unzip
42
43
 
43
- def self.write(data, to:nil)
44
+ def write(data, to:nil)
44
45
  # make sure the containing folder exists
45
46
  FileSystem.makedirs(File.dirname(to))
46
47
  # actually download the file
47
48
  IO.write(to, data)
48
49
  end
49
50
 
50
- def self.append(data, to:nil)
51
+ def append(data, to:nil)
51
52
  FileSystem.makedirs(File.dirname(to))
52
53
  return open(to, 'a') do |file|
53
54
  file << data
54
55
  end
55
56
  end
56
57
 
57
- def self.save(value, to:nil, as:nil)
58
+ def save(value, to:nil, as:nil)
58
59
  # assume string if as was not given
59
60
  if as == nil
60
61
  as = :s
@@ -101,7 +102,7 @@ module FileSystem
101
102
  end
102
103
  end
103
104
 
104
- def self.read(filepath)
105
+ def read(filepath)
105
106
  begin
106
107
  return IO.read(filepath)
107
108
  rescue Errno::ENOENT => exception
@@ -109,7 +110,7 @@ module FileSystem
109
110
  end
110
111
  end
111
112
 
112
- def self.delete(path)
113
+ def delete(path)
113
114
  if File.file?(path)
114
115
  File.delete(path)
115
116
  elsif File.directory?(path)
@@ -117,7 +118,7 @@ module FileSystem
117
118
  end
118
119
  end
119
120
 
120
- def self.username
121
+ def username
121
122
  if OS.is?(:windows)
122
123
  return File.basename(ENV["userprofile"])
123
124
  else
@@ -125,11 +126,11 @@ module FileSystem
125
126
  end
126
127
  end
127
128
 
128
- def self.makedirs(path)
129
+ def makedirs(path)
129
130
  FileUtils.makedirs(path)
130
131
  end
131
132
 
132
- def self.in_dir(path_to_somewhere)
133
+ def in_dir(path_to_somewhere)
133
134
  # save the current working dir
134
135
  current_dir = Dir.pwd
135
136
  # switch dirs
@@ -141,7 +142,7 @@ module FileSystem
141
142
  return output
142
143
  end
143
144
 
144
- def self.copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
145
+ def copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
145
146
  if new_name == ""
146
147
  raise "\n\nFileSystem.copy() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSystem.copy(from:'place/thing', to:'place', new_name:nil)"
147
148
  elsif new_name == nil
@@ -153,7 +154,7 @@ module FileSystem
153
154
  FileUtils.copy_entry(from, to/new_name, preserve, dereference_root, force)
154
155
  end
155
156
 
156
- def self.move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
157
+ def move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
157
158
  if new_name == ""
158
159
  raise "\n\nFileSystem.move() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSystem.move(from:'place/thing', to:'place', new_name:nil)"
159
160
  elsif new_name == nil
@@ -165,7 +166,7 @@ module FileSystem
165
166
  FileUtils.move(from, to/new_name, force: force, noop: noop, verbose: verbose, secure: secure)
166
167
  end
167
168
 
168
- def self.rename(path, new_name:nil, force: true)
169
+ def rename(path, new_name:nil, force: true)
169
170
  if File.dirname(new_name) != "."
170
171
  raise <<-HEREDOC.remove_indent
171
172
 
@@ -186,38 +187,38 @@ module FileSystem
186
187
  File.rename(path, to)
187
188
  end
188
189
 
189
- def self.touch(path)
190
+ def touch(path)
190
191
  FileSystem.makedirs(File.dirname(path))
191
192
  if not FileSystem.file?(path)
192
193
  return IO.write(path, "")
193
194
  end
194
195
  end
195
- singleton_class.send(:alias_method, :touch_file, :touch)
196
- singleton_class.send(:alias_method, :new_file, :touch)
196
+ alias :touch_file :touch
197
+ alias :new_file :touch
197
198
 
198
- def self.touch_dir(path)
199
+ def touch_dir(path)
199
200
  if not FileSystem.directory?(path)
200
201
  FileUtils.makedirs(path)
201
202
  end
202
203
  end
203
- singleton_class.send(:alias_method, :new_folder, :touch_dir)
204
+ alias :new_folder :touch_dir
204
205
 
205
206
  # Pathname aliases
206
- def self.absolute_path?(path)
207
+ def absolute_path?(path)
207
208
  Pathname.new(path).absolute?
208
209
  end
209
- singleton_class.send(:alias_method, :is_absolute_path, :absolute_path?)
210
- singleton_class.send(:alias_method, :abs?, :absolute_path?)
211
- singleton_class.send(:alias_method, :is_abs, :abs?)
210
+ alias :is_absolute_path :absolute_path?
211
+ alias :abs? :absolute_path?
212
+ alias :is_abs :abs?
212
213
 
213
- def self.relative_path?(path)
214
+ def relative_path?(path)
214
215
  Pathname.new(path).relative?
215
216
  end
216
- singleton_class.send(:alias_method, :is_relative_path, :relative_path?)
217
- singleton_class.send(:alias_method, :rel?, :relative_path?)
218
- singleton_class.send(:alias_method, :is_rel, :rel?)
217
+ alias :is_relative_path :relative_path?
218
+ alias :rel? :relative_path?
219
+ alias :is_rel :rel?
219
220
 
220
- def self.path_pieces(path)
221
+ def path_pieces(path)
221
222
  # use this function like this:
222
223
  # *path, filename, extension = FS.path_pieces('/Users/jeffhykin/Desktop/place1/file1.pdf')
223
224
  pieces = Pathname(path).each_filename.to_a
@@ -236,45 +237,45 @@ module FileSystem
236
237
  end
237
238
 
238
239
  # dir aliases
239
- def self.home
240
+ def home
240
241
  HOME
241
242
  end
242
- def self.glob(path)
243
+ def glob(path)
243
244
  Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..]
244
245
  end
245
- def self.list_files(path=".")
246
+ def list_files(path=".")
246
247
  Dir.children(path).map{|each| path/each }.select {|each| FileSystem.file?(each)}
247
248
  end
248
- def self.list_folders(path=".")
249
+ def list_folders(path=".")
249
250
  Dir.children(path).map{|each| path/each }.select {|each| FileSystem.directory?(each)}
250
251
  end
251
- def self.ls(path=".")
252
+ def ls(path=".")
252
253
  Dir.children(path)
253
254
  end
254
- def self.pwd
255
+ def pwd
255
256
  Dir.pwd
256
257
  end
257
- def self.cd(*args, verbose: false)
258
+ def cd(*args, verbose: false)
258
259
  if args.size == 0
259
260
  args[0] = FS.home
260
261
  end
261
262
  FileUtils.cd(args[0], verbose: verbose)
262
263
  end
263
- def self.chdir(*args)
264
+ def chdir(*args)
264
265
  FS.cd(*args)
265
266
  end
266
267
 
267
268
  # File aliases
268
- def self.time_access(*args)
269
+ def time_access(*args)
269
270
  File.atime(*args)
270
271
  end
271
- def self.time_created(*args)
272
+ def time_created(*args)
272
273
  File.birthtime(*args)
273
274
  end
274
- def self.time_modified(*args)
275
+ def time_modified(*args)
275
276
  end
276
277
 
277
- def self.join(*args)
278
+ def join(*args)
278
279
  if OS.is?("windows")
279
280
  folders_without_leading_or_trailing_slashes = args.map do |each|
280
281
  # replace all forward slashes with backslashes
@@ -290,69 +291,69 @@ module FileSystem
290
291
  end
291
292
 
292
293
  # inherit from File
293
- def self.absolute_path(*args)
294
+ def absolute_path(*args)
294
295
  File.absolute_path(*args)
295
296
  end
296
- def self.dirname(*args)
297
+ def dirname(*args)
297
298
  File.dirname(*args)
298
299
  end
299
- def self.basename(*args)
300
+ def basename(*args)
300
301
  File.basename(*args)
301
302
  end
302
- def self.extname(*args)
303
+ def extname(*args)
303
304
  File.extname(*args)
304
305
  end
305
- def self.folder?(*args)
306
+ def folder?(*args)
306
307
  File.directory?(*args)
307
308
  end
308
- singleton_class.send(:alias_method, :is_folder, :folder?)
309
- singleton_class.send(:alias_method, :dir?, :folder?)
310
- singleton_class.send(:alias_method, :is_dir, :dir?)
311
- singleton_class.send(:alias_method, :directory?, :folder?)
312
- singleton_class.send(:alias_method, :is_directory, :directory?)
309
+ alias :is_folder :folder?
310
+ alias :dir? :folder?
311
+ alias :is_dir :dir?
312
+ alias :directory? :folder?
313
+ alias :is_directory :directory?
313
314
 
314
- def self.exists?(*args)
315
+ def exists?(*args)
315
316
  File.exist?(*args)
316
317
  end
317
- singleton_class.send(:alias_method, :does_exist, :exists?)
318
- singleton_class.send(:alias_method, :exist?, :exists?)
318
+ alias :does_exist :exists?
319
+ alias :exist? :exists?
319
320
 
320
- def self.file?(*args)
321
+ def file?(*args)
321
322
  File.file?(*args)
322
323
  end
323
- singleton_class.send(:alias_method, :is_file, :file?)
324
+ alias :is_file :file?
324
325
 
325
- def self.empty?(*args)
326
+ def empty?(*args)
326
327
  File.empty?(*args)
327
328
  end
328
- singleton_class.send(:alias_method, :is_empty, :empty?)
329
+ alias :is_empty :empty?
329
330
 
330
- def self.executable?(*args)
331
+ def executable?(*args)
331
332
  File.executable?(*args)
332
333
  end
333
- singleton_class.send(:alias_method, :is_executable, :executable?)
334
+ alias :is_executable :executable?
334
335
 
335
- def self.symlink?(*args)
336
+ def symlink?(*args)
336
337
  File.symlink?(*args)
337
338
  end
338
- singleton_class.send(:alias_method, :is_symlink, :symlink?)
339
+ alias :is_symlink :symlink?
339
340
 
340
- def self.owned?(*args)
341
+ def owned?(*args)
341
342
  File.owned?(*args)
342
343
  end
343
- singleton_class.send(:alias_method, :is_owned, :owned?)
344
+ alias :is_owned :owned?
344
345
 
345
- def self.pipe?(*args)
346
+ def pipe?(*args)
346
347
  File.pipe?(*args)
347
348
  end
348
- singleton_class.send(:alias_method, :is_pipe, :pipe?)
349
+ alias :is_pipe :pipe?
349
350
 
350
- def self.readable?(*args)
351
+ def readable?(*args)
351
352
  File.readable?(*args)
352
353
  end
353
- singleton_class.send(:alias_method, :is_readable, :readable?)
354
+ alias :is_readable :readable?
354
355
 
355
- def self.size?(*args)
356
+ def size?(*args)
356
357
  if File.directory?(args[0])
357
358
  # recursively get the size of the folder
358
359
  return Dir.glob(File.join(args[0], '**', '*')).map{ |f| File.size(f) }.inject(:+)
@@ -360,49 +361,49 @@ module FileSystem
360
361
  File.size?(*args)
361
362
  end
362
363
  end
363
- singleton_class.send(:alias_method, :size_of, :size?)
364
+ alias :size_of :size?
364
365
 
365
- def self.socket?(*args)
366
+ def socket?(*args)
366
367
  File.socket?(*args)
367
368
  end
368
- singleton_class.send(:alias_method, :is_socket, :socket?)
369
+ alias :is_socket :socket?
369
370
 
370
- def self.world_readable?(*args)
371
+ def world_readable?(*args)
371
372
  File.world_readable?(*args)
372
373
  end
373
- singleton_class.send(:alias_method, :is_world_readable, :world_readable?)
374
+ alias :is_world_readable :world_readable?
374
375
 
375
- def self.world_writable?(*args)
376
+ def world_writable?(*args)
376
377
  File.world_writable?(*args)
377
378
  end
378
- singleton_class.send(:alias_method, :is_world_writable, :world_writable?)
379
+ alias :is_world_writable :world_writable?
379
380
 
380
- def self.writable?(*args)
381
+ def writable?(*args)
381
382
  File.writable?(*args)
382
383
  end
383
- singleton_class.send(:alias_method, :is_writable, :writable?)
384
+ alias :is_writable :writable?
384
385
 
385
- def self.writable_real?(*args)
386
+ def writable_real?(*args)
386
387
  File.writable_real?(*args)
387
388
  end
388
- singleton_class.send(:alias_method, :is_writable_real, :writable_real?)
389
+ alias :is_writable_real :writable_real?
389
390
 
390
- def self.expand_path(*args)
391
+ def expand_path(*args)
391
392
  File.expand_path(*args)
392
393
  end
393
- def self.mkfifo(*args)
394
+ def mkfifo(*args)
394
395
  File.mkfifo(*args)
395
396
  end
396
- def self.stat(*args)
397
+ def stat(*args)
397
398
  File.stat(*args)
398
399
  end
399
400
 
400
- def self.download(the_url, to:nil)
401
+ def download(the_url, to:nil)
401
402
  require 'open-uri'
402
403
  FileSystem.write(open(URI.encode(the_url)).read, to: to)
403
404
  end
404
405
 
405
- def self.online?
406
+ def online?
406
407
  require 'open-uri'
407
408
  begin
408
409
  true if open("http://www.google.com/")
@@ -410,11 +411,7 @@ module FileSystem
410
411
  false
411
412
  end
412
413
  end
413
- end
414
- # create an FS singleton_class.send(:alias_method, :FS = :FileSystem)
415
- FS = FileSystem
416
-
417
-
414
+ end.new
418
415
 
419
416
 
420
417
  # TODO: add zip/unzip functionality
@@ -1,3 +1,3 @@
1
1
  module AtkToolbox
2
- VERSION = '0.0.130'
2
+ VERSION = '0.0.133'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atk_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.130
4
+ version: 0.0.133
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Hykin
@@ -47,7 +47,6 @@ files:
47
47
  - lib/atk/info.rb
48
48
  - lib/atk/os.rb
49
49
  - lib/atk/remove_indent.rb
50
- - lib/atk/set_command.rb
51
50
  - lib/atk/version.rb
52
51
  - lib/atk_toolbox.rb
53
52
  - lib/atk_toolbox/version.rb
@@ -1,43 +0,0 @@
1
- require_relative './file_system'
2
- require_relative './os'
3
- require_relative './atk_info'
4
-
5
- # the reason this isn't inside of the console.rb
6
- # is because atk_info requires console
7
- # and this requires atk_info
8
- # which would cause a circular dependency
9
-
10
- # add set_command to the Console
11
- class TTY::Prompt
12
- def set_command(name, code)
13
- if OS.is?("unix")
14
- exec_path = "#{Atk.paths[:commands]}/#{name}"
15
- local_place = Atk.temp_path(name)
16
- # add the hash bang
17
- hash_bang = "#!#{Atk.paths[:ruby]}\n"
18
- # create the file
19
- FS.write(hash_bang+code, to: local_place)
20
- # copy to command folder
21
- system("sudo", "cp", local_place, exec_path)
22
- system("sudo", "chmod", "ugo+x", exec_path)
23
- elsif OS.is?("windows")
24
- # check for invalid file paths, see https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
25
- if name =~ /[><:"\/\\|?*]/
26
- raise <<-HEREDOC.remove_indent
27
-
28
-
29
- When using the ATK Console.set_command(name)
30
- The name: #{name}
31
- is not a valid file path on windows
32
- which means it cannot be a command
33
- HEREDOC
34
- end
35
- exec_path = "#{Atk.paths[:commands]}\\#{name}"
36
-
37
- # create the code
38
- IO.write(exec_path+".rb", code)
39
- # create an executable to call the code
40
- IO.write(exec_path+".bat", "@echo off\nruby \"#{exec_path}.rb\" %*")
41
- end
42
- end
43
- end