atk_toolbox 0.0.133 → 0.0.134
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.
- checksums.yaml +4 -4
- data/lib/atk/file_system.rb +162 -84
- data/lib/atk_toolbox/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8547c094c0776ccc49a4f81b4db4c3d895b0a545c2fad417a4a635179ac8a44
|
4
|
+
data.tar.gz: e32165b8ee0dcce88a235dc3b656af1af742aca39c6c7daab1f3714668bf2ce4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d95da0117bc36d4030650c39ee6bed3c88532c50fda2c632cf19cf247a65fa5e6d1efbc39059a36a8557381db51eee0b9f2d33b5aec6373aa7de2572d35a19a
|
7
|
+
data.tar.gz: e706e87c839abbd2095d565ce0558104afd19bbc64d63a7a9c47abba18d40088f3b26618aacfdb0fb8ed6a5b84787dfbca65f9b0ca477f1c2b42087c713e1285
|
data/lib/atk/file_system.rb
CHANGED
@@ -25,8 +25,7 @@ class String
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
|
29
|
-
FS = FileSystem = Class.new do
|
28
|
+
module FileSystem
|
30
29
|
# This is a combination of the FileUtils, File, Pathname, IO, Etc, and Dir classes,
|
31
30
|
# along with some other helpful methods
|
32
31
|
# It is by-default forceful (dangerous/overwriting)
|
@@ -41,21 +40,21 @@ FS = FileSystem = Class.new do
|
|
41
40
|
# zip
|
42
41
|
# unzip
|
43
42
|
|
44
|
-
def write(data, to:nil)
|
43
|
+
def self.write(data, to:nil)
|
45
44
|
# make sure the containing folder exists
|
46
45
|
FileSystem.makedirs(File.dirname(to))
|
47
46
|
# actually download the file
|
48
47
|
IO.write(to, data)
|
49
48
|
end
|
50
49
|
|
51
|
-
def append(data, to:nil)
|
50
|
+
def self.append(data, to:nil)
|
52
51
|
FileSystem.makedirs(File.dirname(to))
|
53
52
|
return open(to, 'a') do |file|
|
54
53
|
file << data
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
def save(value, to:nil, as:nil)
|
57
|
+
def self.save(value, to:nil, as:nil)
|
59
58
|
# assume string if as was not given
|
60
59
|
if as == nil
|
61
60
|
as = :s
|
@@ -102,7 +101,7 @@ FS = FileSystem = Class.new do
|
|
102
101
|
end
|
103
102
|
end
|
104
103
|
|
105
|
-
def read(filepath)
|
104
|
+
def self.read(filepath)
|
106
105
|
begin
|
107
106
|
return IO.read(filepath)
|
108
107
|
rescue Errno::ENOENT => exception
|
@@ -110,7 +109,7 @@ FS = FileSystem = Class.new do
|
|
110
109
|
end
|
111
110
|
end
|
112
111
|
|
113
|
-
def delete(path)
|
112
|
+
def self.delete(path)
|
114
113
|
if File.file?(path)
|
115
114
|
File.delete(path)
|
116
115
|
elsif File.directory?(path)
|
@@ -118,7 +117,7 @@ FS = FileSystem = Class.new do
|
|
118
117
|
end
|
119
118
|
end
|
120
119
|
|
121
|
-
def username
|
120
|
+
def self.username
|
122
121
|
if OS.is?(:windows)
|
123
122
|
return File.basename(ENV["userprofile"])
|
124
123
|
else
|
@@ -126,11 +125,11 @@ FS = FileSystem = Class.new do
|
|
126
125
|
end
|
127
126
|
end
|
128
127
|
|
129
|
-
def makedirs(path)
|
128
|
+
def self.makedirs(path)
|
130
129
|
FileUtils.makedirs(path)
|
131
130
|
end
|
132
131
|
|
133
|
-
def in_dir(path_to_somewhere)
|
132
|
+
def self.in_dir(path_to_somewhere)
|
134
133
|
# save the current working dir
|
135
134
|
current_dir = Dir.pwd
|
136
135
|
# switch dirs
|
@@ -142,7 +141,7 @@ FS = FileSystem = Class.new do
|
|
142
141
|
return output
|
143
142
|
end
|
144
143
|
|
145
|
-
def copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
|
144
|
+
def self.copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
|
146
145
|
if new_name == ""
|
147
146
|
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)"
|
148
147
|
elsif new_name == nil
|
@@ -154,7 +153,7 @@ FS = FileSystem = Class.new do
|
|
154
153
|
FileUtils.copy_entry(from, to/new_name, preserve, dereference_root, force)
|
155
154
|
end
|
156
155
|
|
157
|
-
def move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
|
156
|
+
def self.move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
|
158
157
|
if new_name == ""
|
159
158
|
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)"
|
160
159
|
elsif new_name == nil
|
@@ -166,7 +165,7 @@ FS = FileSystem = Class.new do
|
|
166
165
|
FileUtils.move(from, to/new_name, force: force, noop: noop, verbose: verbose, secure: secure)
|
167
166
|
end
|
168
167
|
|
169
|
-
def rename(path, new_name:nil, force: true)
|
168
|
+
def self.rename(path, new_name:nil, force: true)
|
170
169
|
if File.dirname(new_name) != "."
|
171
170
|
raise <<-HEREDOC.remove_indent
|
172
171
|
|
@@ -187,38 +186,38 @@ FS = FileSystem = Class.new do
|
|
187
186
|
File.rename(path, to)
|
188
187
|
end
|
189
188
|
|
190
|
-
def touch(path)
|
189
|
+
def self.touch(path)
|
191
190
|
FileSystem.makedirs(File.dirname(path))
|
192
191
|
if not FileSystem.file?(path)
|
193
192
|
return IO.write(path, "")
|
194
193
|
end
|
195
194
|
end
|
196
|
-
|
197
|
-
|
195
|
+
singleton_class.send(:alias_method, :touch_file, :touch)
|
196
|
+
singleton_class.send(:alias_method, :new_file, :touch)
|
198
197
|
|
199
|
-
def touch_dir(path)
|
198
|
+
def self.touch_dir(path)
|
200
199
|
if not FileSystem.directory?(path)
|
201
200
|
FileUtils.makedirs(path)
|
202
201
|
end
|
203
202
|
end
|
204
|
-
|
203
|
+
singleton_class.send(:alias_method, :new_folder, :touch_dir)
|
205
204
|
|
206
205
|
# Pathname aliases
|
207
|
-
def absolute_path?(path)
|
206
|
+
def self.absolute_path?(path)
|
208
207
|
Pathname.new(path).absolute?
|
209
208
|
end
|
210
|
-
|
211
|
-
|
212
|
-
|
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?)
|
213
212
|
|
214
|
-
def relative_path?(path)
|
213
|
+
def self.relative_path?(path)
|
215
214
|
Pathname.new(path).relative?
|
216
215
|
end
|
217
|
-
|
218
|
-
|
219
|
-
|
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?)
|
220
219
|
|
221
|
-
def path_pieces(path)
|
220
|
+
def self.path_pieces(path)
|
222
221
|
# use this function like this:
|
223
222
|
# *path, filename, extension = FS.path_pieces('/Users/jeffhykin/Desktop/place1/file1.pdf')
|
224
223
|
pieces = Pathname(path).each_filename.to_a
|
@@ -237,45 +236,45 @@ FS = FileSystem = Class.new do
|
|
237
236
|
end
|
238
237
|
|
239
238
|
# dir aliases
|
240
|
-
def home
|
239
|
+
def self.home
|
241
240
|
HOME
|
242
241
|
end
|
243
|
-
def glob(path)
|
242
|
+
def self.glob(path)
|
244
243
|
Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..]
|
245
244
|
end
|
246
|
-
def list_files(path=".")
|
245
|
+
def self.list_files(path=".")
|
247
246
|
Dir.children(path).map{|each| path/each }.select {|each| FileSystem.file?(each)}
|
248
247
|
end
|
249
|
-
def list_folders(path=".")
|
248
|
+
def self.list_folders(path=".")
|
250
249
|
Dir.children(path).map{|each| path/each }.select {|each| FileSystem.directory?(each)}
|
251
250
|
end
|
252
|
-
def ls(path=".")
|
251
|
+
def self.ls(path=".")
|
253
252
|
Dir.children(path)
|
254
253
|
end
|
255
|
-
def pwd
|
254
|
+
def self.pwd
|
256
255
|
Dir.pwd
|
257
256
|
end
|
258
|
-
def cd(*args, verbose: false)
|
257
|
+
def self.cd(*args, verbose: false)
|
259
258
|
if args.size == 0
|
260
259
|
args[0] = FS.home
|
261
260
|
end
|
262
261
|
FileUtils.cd(args[0], verbose: verbose)
|
263
262
|
end
|
264
|
-
def chdir(*args)
|
263
|
+
def self.chdir(*args)
|
265
264
|
FS.cd(*args)
|
266
265
|
end
|
267
266
|
|
268
267
|
# File aliases
|
269
|
-
def time_access(*args)
|
268
|
+
def self.time_access(*args)
|
270
269
|
File.atime(*args)
|
271
270
|
end
|
272
|
-
def time_created(*args)
|
271
|
+
def self.time_created(*args)
|
273
272
|
File.birthtime(*args)
|
274
273
|
end
|
275
|
-
def time_modified(*args)
|
274
|
+
def self.time_modified(*args)
|
276
275
|
end
|
277
276
|
|
278
|
-
def join(*args)
|
277
|
+
def self.join(*args)
|
279
278
|
if OS.is?("windows")
|
280
279
|
folders_without_leading_or_trailing_slashes = args.map do |each|
|
281
280
|
# replace all forward slashes with backslashes
|
@@ -291,69 +290,69 @@ FS = FileSystem = Class.new do
|
|
291
290
|
end
|
292
291
|
|
293
292
|
# inherit from File
|
294
|
-
def absolute_path(*args)
|
293
|
+
def self.absolute_path(*args)
|
295
294
|
File.absolute_path(*args)
|
296
295
|
end
|
297
|
-
def dirname(*args)
|
296
|
+
def self.dirname(*args)
|
298
297
|
File.dirname(*args)
|
299
298
|
end
|
300
|
-
def basename(*args)
|
299
|
+
def self.basename(*args)
|
301
300
|
File.basename(*args)
|
302
301
|
end
|
303
|
-
def extname(*args)
|
302
|
+
def self.extname(*args)
|
304
303
|
File.extname(*args)
|
305
304
|
end
|
306
|
-
def folder?(*args)
|
305
|
+
def self.folder?(*args)
|
307
306
|
File.directory?(*args)
|
308
307
|
end
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
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?)
|
314
313
|
|
315
|
-
def exists?(*args)
|
314
|
+
def self.exists?(*args)
|
316
315
|
File.exist?(*args)
|
317
316
|
end
|
318
|
-
|
319
|
-
|
317
|
+
singleton_class.send(:alias_method, :does_exist, :exists?)
|
318
|
+
singleton_class.send(:alias_method, :exist?, :exists?)
|
320
319
|
|
321
|
-
def file?(*args)
|
320
|
+
def self.file?(*args)
|
322
321
|
File.file?(*args)
|
323
322
|
end
|
324
|
-
|
323
|
+
singleton_class.send(:alias_method, :is_file, :file?)
|
325
324
|
|
326
|
-
def empty?(*args)
|
325
|
+
def self.empty?(*args)
|
327
326
|
File.empty?(*args)
|
328
327
|
end
|
329
|
-
|
328
|
+
singleton_class.send(:alias_method, :is_empty, :empty?)
|
330
329
|
|
331
|
-
def executable?(*args)
|
330
|
+
def self.executable?(*args)
|
332
331
|
File.executable?(*args)
|
333
332
|
end
|
334
|
-
|
333
|
+
singleton_class.send(:alias_method, :is_executable, :executable?)
|
335
334
|
|
336
|
-
def symlink?(*args)
|
335
|
+
def self.symlink?(*args)
|
337
336
|
File.symlink?(*args)
|
338
337
|
end
|
339
|
-
|
338
|
+
singleton_class.send(:alias_method, :is_symlink, :symlink?)
|
340
339
|
|
341
|
-
def owned?(*args)
|
340
|
+
def self.owned?(*args)
|
342
341
|
File.owned?(*args)
|
343
342
|
end
|
344
|
-
|
343
|
+
singleton_class.send(:alias_method, :is_owned, :owned?)
|
345
344
|
|
346
|
-
def pipe?(*args)
|
345
|
+
def self.pipe?(*args)
|
347
346
|
File.pipe?(*args)
|
348
347
|
end
|
349
|
-
|
348
|
+
singleton_class.send(:alias_method, :is_pipe, :pipe?)
|
350
349
|
|
351
|
-
def readable?(*args)
|
350
|
+
def self.readable?(*args)
|
352
351
|
File.readable?(*args)
|
353
352
|
end
|
354
|
-
|
353
|
+
singleton_class.send(:alias_method, :is_readable, :readable?)
|
355
354
|
|
356
|
-
def size?(*args)
|
355
|
+
def self.size?(*args)
|
357
356
|
if File.directory?(args[0])
|
358
357
|
# recursively get the size of the folder
|
359
358
|
return Dir.glob(File.join(args[0], '**', '*')).map{ |f| File.size(f) }.inject(:+)
|
@@ -361,49 +360,49 @@ FS = FileSystem = Class.new do
|
|
361
360
|
File.size?(*args)
|
362
361
|
end
|
363
362
|
end
|
364
|
-
|
363
|
+
singleton_class.send(:alias_method, :size_of, :size?)
|
365
364
|
|
366
|
-
def socket?(*args)
|
365
|
+
def self.socket?(*args)
|
367
366
|
File.socket?(*args)
|
368
367
|
end
|
369
|
-
|
368
|
+
singleton_class.send(:alias_method, :is_socket, :socket?)
|
370
369
|
|
371
|
-
def world_readable?(*args)
|
370
|
+
def self.world_readable?(*args)
|
372
371
|
File.world_readable?(*args)
|
373
372
|
end
|
374
|
-
|
373
|
+
singleton_class.send(:alias_method, :is_world_readable, :world_readable?)
|
375
374
|
|
376
|
-
def world_writable?(*args)
|
375
|
+
def self.world_writable?(*args)
|
377
376
|
File.world_writable?(*args)
|
378
377
|
end
|
379
|
-
|
378
|
+
singleton_class.send(:alias_method, :is_world_writable, :world_writable?)
|
380
379
|
|
381
|
-
def writable?(*args)
|
380
|
+
def self.writable?(*args)
|
382
381
|
File.writable?(*args)
|
383
382
|
end
|
384
|
-
|
383
|
+
singleton_class.send(:alias_method, :is_writable, :writable?)
|
385
384
|
|
386
|
-
def writable_real?(*args)
|
385
|
+
def self.writable_real?(*args)
|
387
386
|
File.writable_real?(*args)
|
388
387
|
end
|
389
|
-
|
388
|
+
singleton_class.send(:alias_method, :is_writable_real, :writable_real?)
|
390
389
|
|
391
|
-
def expand_path(*args)
|
390
|
+
def self.expand_path(*args)
|
392
391
|
File.expand_path(*args)
|
393
392
|
end
|
394
|
-
def mkfifo(*args)
|
393
|
+
def self.mkfifo(*args)
|
395
394
|
File.mkfifo(*args)
|
396
395
|
end
|
397
|
-
def stat(*args)
|
396
|
+
def self.stat(*args)
|
398
397
|
File.stat(*args)
|
399
398
|
end
|
400
399
|
|
401
|
-
def download(the_url, to:nil)
|
400
|
+
def self.download(the_url, to:nil)
|
402
401
|
require 'open-uri'
|
403
402
|
FileSystem.write(open(URI.encode(the_url)).read, to: to)
|
404
403
|
end
|
405
404
|
|
406
|
-
def online?
|
405
|
+
def self.online?
|
407
406
|
require 'open-uri'
|
408
407
|
begin
|
409
408
|
true if open("http://www.google.com/")
|
@@ -411,7 +410,86 @@ FS = FileSystem = Class.new do
|
|
411
410
|
false
|
412
411
|
end
|
413
412
|
end
|
414
|
-
|
413
|
+
|
414
|
+
class ProfileHelper
|
415
|
+
def initialize(unqiue_id)
|
416
|
+
function_def = "ProfileHelper.new(unqiue_id)"
|
417
|
+
if unqiue_id =~ /\n/
|
418
|
+
raise <<-HEREDOC.remove_indent
|
419
|
+
|
420
|
+
|
421
|
+
Inside the #{function_def.color_as :code}
|
422
|
+
the unqiue_id contains a newline (\\n)
|
423
|
+
|
424
|
+
unqiue_id: #{"#{unqiue_id}".inspect}
|
425
|
+
|
426
|
+
Sadly newlines are not allowed in the unqiue_id due to how they are searched for.
|
427
|
+
Please provide a unqiue_id that doesn't have newlines.
|
428
|
+
HEREDOC
|
429
|
+
end
|
430
|
+
if "#{unqiue_id}".size < 5
|
431
|
+
raise <<-HEREDOC.remove_indent
|
432
|
+
|
433
|
+
|
434
|
+
Inside the #{function_def.color_as :code}
|
435
|
+
the unqiue_id is: #{"#{unqiue_id}".inspect}
|
436
|
+
|
437
|
+
That's not even 5 characters. Come on man, there's going to be problems if the unqiue_id isn't unqiue
|
438
|
+
generate a random number (once), then put the name of the service at the front of that random number
|
439
|
+
HEREDOC
|
440
|
+
end
|
441
|
+
@unqiue_id = unqiue_id
|
442
|
+
end
|
443
|
+
|
444
|
+
@bash_comment_out = comment_out_line = ->(code) do
|
445
|
+
"### #{code}"
|
446
|
+
end
|
447
|
+
|
448
|
+
def add_to_bash_profile(code)
|
449
|
+
uniquely_append(code, HOME/".bash_profile", @bash_comment_out)
|
450
|
+
end
|
451
|
+
|
452
|
+
def add_to_zsh_profile(code)
|
453
|
+
uniquely_append(code, HOME/".zprofile", @bash_comment_out)
|
454
|
+
end
|
455
|
+
|
456
|
+
def add_to_bash_rc(code)
|
457
|
+
uniquely_append(code, HOME/".bashrc", @bash_comment_out)
|
458
|
+
end
|
459
|
+
|
460
|
+
def add_to_zsh_rc(code)
|
461
|
+
uniquely_append(code, HOME/".zshrc", @bash_comment_out)
|
462
|
+
end
|
463
|
+
|
464
|
+
def uniquely_append(string_to_add, location_of_file, comment_out_line)
|
465
|
+
_UNQIUE_HELPER = 'fj03498hglkasjdgoghu2904' # dont change this, its a 1-time randomly generated string
|
466
|
+
final_string = "\n"
|
467
|
+
final_string += comment_out_line["start of ID: #{@unqiue_id} #{_UNQIUE_HELPER}"] + "\n"
|
468
|
+
final_string += comment_out_line["NOTE! if you remove this, remove the whole thing (don't leave a dangling start/end comment)"] + "\n"
|
469
|
+
final_string += string_to_add + "\n"
|
470
|
+
final_string += comment_out_line["end of ID: #{@unqiue_id} #{_UNQIUE_HELPER}"]
|
471
|
+
|
472
|
+
# open the existing file if there is one
|
473
|
+
file = FS.read(location_of_file) || ""
|
474
|
+
# remove any previous versions
|
475
|
+
file.gsub!(/### start of ID: (.+) #{_UNQIUE_HELPER}[\s\S]*### end of ID: \1 #{_UNQIUE_HELPER}/) do |match|
|
476
|
+
if $1 == @unqiue_id
|
477
|
+
""
|
478
|
+
else
|
479
|
+
match
|
480
|
+
end
|
481
|
+
end
|
482
|
+
# append the the new code at the bottom (highest priority)
|
483
|
+
file += final_string
|
484
|
+
# overwrite the file
|
485
|
+
FS.write(file, to: location_of_file)
|
486
|
+
end
|
487
|
+
end
|
488
|
+
end
|
489
|
+
# create an FS singleton_class.send(:alias_method, :FS = :FileSystem)
|
490
|
+
FS = FileSystem
|
491
|
+
|
492
|
+
|
415
493
|
|
416
494
|
|
417
495
|
# TODO: add zip/unzip functionality
|
data/lib/atk_toolbox/version.rb
CHANGED