atk_toolbox 0.0.45 → 0.0.46

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99c34226e0e1d77b1616de7e5e16cce6a8496eb47fda2020fa76ee2597c62498
4
- data.tar.gz: b0725578fae82ad3df7139e6a7fc546a664ab423aff7ec561d34e4dcb0628c04
3
+ metadata.gz: b3b9bd3949490ee39a6afe827c568f40b9e9c152f530269ffe17e80e797bc8e4
4
+ data.tar.gz: 6a7ca257444314078b134db311eb33e1b397eb75538e9b0c098dabe0a0e02651
5
5
  SHA512:
6
- metadata.gz: 1b2096e55bdc352c77064a619ea7ae813bda4d2afd49b022e32c2ebe1cfdcc71e6695ba28504f9f7b7517798f27401b050c2580a6fc8e44c82504cae79d4cb15
7
- data.tar.gz: bf3a9017e56c9ff1b06698c2254162666a9b12533834e37fce980797eabb63b5906dcbbd7f06e87c076cccfbc01b8a49b2d24b8930370a0498db363ffff2da04
6
+ metadata.gz: 590cd15964c6322f6501fc4032fd914cddee9ca1eb45b75d3d44eaa9929b8435c9a2f2a5dce495175bc4c59d0be49fa8bed1f7a7da930345655820896e891203
7
+ data.tar.gz: 034a4883a6274a8f0d1dd1273d0d595601310bc5762688dafacde4f5958a42d9471ad932fc92fcc426b6bf15a8a3c3d238e8b47d1de8058d6393aec83b93fecc
data/lib/atk/atk_info.rb CHANGED
@@ -36,9 +36,8 @@ module ATK
36
36
  def self.temp_path(filepath)
37
37
  new_path = ATK.paths[:temp]/filepath
38
38
  # make sure the path is empty
39
- begin
40
- File.delete(new_path)
41
- end
39
+ FS.write("", to: new_path)
40
+ FS.delete(new_path)
42
41
  return new_path
43
42
  end
44
43
 
@@ -46,8 +45,8 @@ module ATK
46
45
  settings_path = ATK.paths[:info]
47
46
  atk_settings_key = "atk_settings"
48
47
  # if it doesn't exist then create it
49
- if not File.exist?(settings_path)
50
- IO.write(settings_path, "#{atk_settings_key}: {}")
48
+ if not FS.exist?(settings_path)
49
+ FS.write("#{atk_settings_key}: {}", to: settings_path)
51
50
  return {}
52
51
  else
53
52
  data = YAML.load_file(settings_path)
@@ -1,6 +1,7 @@
1
1
  require 'etc'
2
2
  require 'fileutils'
3
- require 'set'
3
+ require 'pathname'
4
+ require 'open-uri'
4
5
  require_relative './os'
5
6
 
6
7
  if OS.is?("unix")
@@ -10,6 +11,8 @@ else # windows
10
11
  end
11
12
 
12
13
  class String
14
+ # this is for easy creation of cross-platform filepaths
15
+ # ex: "foldername"/"filename"
13
16
  def /(next_string)
14
17
  if OS.is?("windows")
15
18
  self + "\\" + next_string
@@ -17,9 +20,67 @@ class String
17
20
  File.join(self, next_string)
18
21
  end
19
22
  end
23
+
24
+ # this is for having docstrings that get their indent removed
25
+ # this is used frequently for multi-line strings and error messages
26
+ # example usage
27
+ # puts <<-HEREDOC.remove_indent
28
+ # This command does such and such.
29
+ # this part is extra indented
30
+ # HEREDOC
31
+ def remove_indent
32
+ gsub(/^[ \t]{#{self.match(/^[ \t]*/)[0].length}}/, '')
33
+ end
20
34
  end
21
35
 
22
36
  class FileSys
37
+ # This is a combination of the FileUtils, File, Pathname, IO, Etc, and Dir classes,
38
+ # along with some other helpful methods
39
+ # It is by-default forceful (dangerous/overwriting)
40
+ # it is made to get things done in a no-nonsense error-free way and to have every pratical tool in one place
41
+
42
+ # TODO
43
+ # change_owner
44
+ # set_permissions
45
+ # relative_path_between
46
+ # relative_path_to
47
+ # add a force: true option to most of the commands
48
+
49
+ def self.write(data, to:nil)
50
+ # make sure the containing folder exists
51
+ FileSys.makedirs(File.dirname(to))
52
+ # actually download the file
53
+ IO.write(to, data)
54
+ end
55
+
56
+ def self.read(filepath)
57
+ begin
58
+ return IO.read(filepath)
59
+ rescue Errno::ENOENT => exception
60
+ return nil
61
+ end
62
+ end
63
+
64
+ def self.delete(path)
65
+ if File.file?(path)
66
+ File.delete(path)
67
+ elsif File.directory?(path)
68
+ FileUtils.rm_rf(path)
69
+ end
70
+ end
71
+
72
+ def username
73
+ if OS.is?(:windows)
74
+ return File.basename(ENV["userprofile"])
75
+ else
76
+ return Etc.getlogin
77
+ end
78
+ end
79
+
80
+ def self.makedirs(path)
81
+ FileUtils.makedirs(path)
82
+ end
83
+
23
84
  def self.in_dir(path_to_somewhere)
24
85
  # save the current working dir
25
86
  current_dir = Dir.pwd
@@ -32,27 +93,206 @@ class FileSys
32
93
  return output
33
94
  end
34
95
 
35
- def self.write(data, to:nil)
36
- # make sure the containing folder exists
37
- FileUtils.makedirs(File.dirname(to))
38
- # actually download the file
39
- IO.write(to, data)
96
+ def self.copy(from:nil, to:nil, new_name:"", force: true, preserve: false, dereference_root: false)
97
+ if new_name == ""
98
+ raise "\n\nFileSys.copy() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSys.copy(from:'place/thing', to:'place', new_name:nil)"
99
+ end
100
+ # make sure the "to" path exists
101
+ FileSys.touch_dir(to)
102
+ # perform the copy
103
+ FileUtils.copy_entry(from, to/new_name, preserve, dereference_root, force)
104
+ end
105
+
106
+ def self.move(from:nil, to:nil, new_name:"", force: true, noop: nil, verbose: nil, secure: nil)
107
+ if new_name == ""
108
+ raise "\n\nFileSys.move() needs a new_name: argument\nset new_name:nil if you wish the file/folder to keep the same name\ne.g. FileSys.move(from:'place/thing', to:'place', new_name:nil)"
109
+ end
110
+ # make sure the "to" path exists
111
+ FileSys.touch_dir(to)
112
+ # perform the move
113
+ FileUtils.move(from, to/new_name, force: force, noop: noop, verbose: verbose, secure: secure)
40
114
  end
41
115
 
42
- def self.read(filepath)
43
- begin
44
- return IO.read(filepath)
45
- rescue => exception
46
- return nil
116
+ def self.rename(from:nil, to:nil, force: true)
117
+ # if the directories are different, then throw an error
118
+ if not File.identical?(File.dirname(from), File.dirname(to))
119
+ raise "\n\nFileSys.rename() requires that the the file stay in the same place and only change names.\nIf you want to move a file, use FileSys.move()"
47
120
  end
121
+ # make sure the path is clear
122
+ if force
123
+ FileSys.delete(to)
124
+ end
125
+ # perform the copy
126
+ File.rename(from, to)
48
127
  end
49
128
 
50
- def self.delete(filepath)
51
- begin
52
- return File.delete(filepath)
53
- # if file doesnt exist, thats fine
54
- rescue Errno::ENOENT => exception
55
- return nil
129
+ def self.touch(*args)
130
+ return FileUtils.touch(*args)
131
+ end
132
+
133
+ def self.touch_dir(path)
134
+ if not FileSys.directory?(path)
135
+ FileUtils.makedirs(path)
56
136
  end
57
137
  end
58
- end
138
+
139
+ # Pathname aliases
140
+ def self.absolute_path?(path)
141
+ Pathname.new(path).absolute?
142
+ end
143
+ def self.abs?(path)
144
+ Pathname.new(path).absolute?
145
+ end
146
+ def self.relative_path?(path)
147
+ Pathname.new(path).relative?
148
+ end
149
+ def self.rel?(path)
150
+ Pathname.new(path).relative?
151
+ end
152
+
153
+ # dir aliases
154
+ def self.home
155
+ HOME
156
+ end
157
+ def self.glob(path)
158
+ Dir.glob(path, File::FNM_DOTMATCH) - %w[. ..]
159
+ end
160
+ def list_files(path=".")
161
+ Dir.children(path).select {|each| FileSys.file?(each)}
162
+ end
163
+ def list_folders(path=".")
164
+ Dir.children(path).select {|each| FileSys.directory?(each)}
165
+ end
166
+ def ls(path)
167
+ Dir.children(path)
168
+ end
169
+ def pwd
170
+ Dir.pwd
171
+ end
172
+ def cd(path, verbose: false)
173
+ FileUtils.cd(path, verbose: verbose)
174
+ end
175
+ def chdir(path, verbose: false)
176
+ FileUtils.cd(path, verbose: verbose)
177
+ end
178
+
179
+ # File aliases
180
+ def self.time_access(*args, **kwargs)
181
+ File.atime(*args, **kwargs)
182
+ end
183
+ def self.time_created(*args, **kwargs)
184
+ File.birthtime(*args, **kwargs)
185
+ end
186
+ def self.time_modified(*args, **kwargs)
187
+ end
188
+ def self.dir?(*args, **kwargs)
189
+ File.directory?(*args, **kwargs)
190
+ end
191
+ def self.exists?(*args, **kwargs)
192
+ File.exist?(*args,**kwargs)
193
+ end
194
+
195
+ # inherit from File
196
+ def self.absolute_path(*args, **kwargs)
197
+ File.absolute_path(*args,**kwargs)
198
+ end
199
+ def self.dirname(*args, **kwargs)
200
+ File.dirname(*args,**kwargs)
201
+ end
202
+ def self.basename(*args, **kwargs)
203
+ File.basename(*args,**kwargs)
204
+ end
205
+ def self.extname(*args, **kwargs)
206
+ File.extname(*args,**kwargs)
207
+ end
208
+ def self.directory?(*args, **kwargs)
209
+ File.directory?(*args,**kwargs)
210
+ end
211
+ def self.file?(*args, **kwargs)
212
+ File.file?(*args,**kwargs)
213
+ end
214
+ def self.empty?(*args, **kwargs)
215
+ File.empty?(*args,**kwargs)
216
+ end
217
+ def self.exist?(*args, **kwargs)
218
+ File.exist?(*args,**kwargs)
219
+ end
220
+ def self.executable?(*args, **kwargs)
221
+ File.executable?(*args,**kwargs)
222
+ end
223
+ def self.symlink?(*args, **kwargs)
224
+ File.symlink?(*args,**kwargs)
225
+ end
226
+ def self.owned?(*args, **kwargs)
227
+ File.owned?(*args,**kwargs)
228
+ end
229
+ def self.pipe?(*args, **kwargs)
230
+ File.pipe?(*args,**kwargs)
231
+ end
232
+ def self.readable?(*args, **kwargs)
233
+ File.readable?(*args,**kwargs)
234
+ end
235
+ def self.size?(*args, **kwargs)
236
+ File.size?(*args,**kwargs)
237
+ end
238
+ def self.socket?(*args, **kwargs)
239
+ File.socket?(*args,**kwargs)
240
+ end
241
+ def self.world_readable?(*args, **kwargs)
242
+ File.world_readable?(*args,**kwargs)
243
+ end
244
+ def self.world_writable?(*args, **kwargs)
245
+ File.world_writable?(*args,**kwargs)
246
+ end
247
+ def self.writable?(*args, **kwargs)
248
+ File.writable?(*args,**kwargs)
249
+ end
250
+ def self.writable_real?(*args, **kwargs)
251
+ File.writable_real?(*args,**kwargs)
252
+ end
253
+ def self.expand_path(*args, **kwargs)
254
+ File.expand_path(*args,**kwargs)
255
+ end
256
+ def self.mkfifo(*args, **kwargs)
257
+ File.mkfifo(*args,**kwargs)
258
+ end
259
+ def self.stat(*args, **kwargs)
260
+ File.stat(*args,**kwargs)
261
+ end
262
+
263
+ def download(input=nil, from:nil, url:nil, to:nil)
264
+ # if only one argument, either input or url
265
+ if ((input!=nil) != (url!=nil)) && (from==nil) && (to==nil)
266
+ # this covers:
267
+ # download 'site.com/file'
268
+ the_url = url || input
269
+ file_name = the_url.match /(?<=\/)[^\/]+\z/
270
+ file_name = file_name[0]
271
+ elsif (to != nil) && ((input!=nil)!=(url!=nil))
272
+ # this covers:
273
+ # download 'site.com/file' to:'file'
274
+ # download url:'site.com/file' to:'file'
275
+ the_url = url || input
276
+ file_name = to
277
+ elsif ((from!=nil) != (url!=nil)) && input!=nil
278
+ # this covers:
279
+ # download 'file' from:'site.com/file'
280
+ # download 'file' url:'site.com/file'
281
+ the_url = from || url
282
+ file_name = input
283
+ else
284
+ raise <<-HEREDOC.remove_indent
285
+ I'm not sure how you're using the download function.
286
+ Please use one of the following methods:
287
+ download 'site.com/file'
288
+ download 'site.com/file', to:'file'
289
+ download url:'site.com/file', to:'file'
290
+ download 'file', from:'site.com/file'
291
+ download 'file', url:'site.com/file'
292
+ HEREDOC
293
+ end
294
+ FileSys.write(open(URI.encode(the_url)).read, to: file_name)
295
+ end
296
+ end
297
+ # create an FS alias
298
+ FS = FileSys
@@ -6,12 +6,10 @@ def set_command(name, code)
6
6
  if OS.is?("unix")
7
7
  exec_path = "/usr/local/bin/#{name}"
8
8
  local_place = ATK.temp_path(name)
9
- # create temp if it doesn't exist
10
- FileUtils.makedirs(File.dirname(local_place))
11
9
  # add the hash bang
12
10
  hash_bang = "#!#{ATK.paths[:ruby]}\n"
13
11
  # create the file
14
- IO.write(local_place, hash_bang+code)
12
+ FS.write(hash_bang+code, to: local_place)
15
13
  # copy to command folder
16
14
  system("sudo", "cp", local_place, exec_path)
17
15
  system("sudo", "chmod", "ugo+x", exec_path)
@@ -25,8 +23,8 @@ def set_command(name, code)
25
23
  exec_path = "C:\\Users\\#{username}\\AppData\\local\\Microsoft\\WindowsApps\\#{name}"
26
24
 
27
25
  # create the code
28
- IO.write(exec_path+".rb", code)
26
+ FS.write(code, to: exec_path+".rb")
29
27
  # create an executable to call the code
30
- IO.write(exec_path+".bat", "@echo off\nruby \"#{exec_path}.rb\" %*")
28
+ FS.write("@echo off\nruby \"#{exec_path}.rb\" %*", to: exec_path+".bat")
31
29
  end
32
30
  end
@@ -1,3 +1,3 @@
1
1
  module AtkToolbox
2
- VERSION = '0.0.45'
2
+ VERSION = '0.0.46'
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.45
4
+ version: 0.0.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Hykin
@@ -59,7 +59,6 @@ files:
59
59
  - lib/atk/atk_info.rb
60
60
  - lib/atk/cmd.rb
61
61
  - lib/atk/default_info.yaml
62
- - lib/atk/download.rb
63
62
  - lib/atk/extra_file_utils.rb
64
63
  - lib/atk/extra_yaml.rb
65
64
  - lib/atk/git.rb
data/lib/atk/download.rb DELETED
@@ -1,38 +0,0 @@
1
- require 'open-uri'
2
- require 'fileutils'
3
- require_relative './extra_file_utils'
4
-
5
- def download(input_1=nil, from:nil, url:nil, as:nil)
6
- # argument checking
7
- # if only one argument, either input_1 or url
8
- if ((input_1!=nil) != (url!=nil)) && (from==nil) && (as==nil)
9
- # this covers:
10
- # download 'site.com/file'
11
- the_url = url || input_1
12
- file_name = the_url.match /(?<=\/)[^\/]+\z/
13
- file_name = file_name[0]
14
- elsif (as != nil) && ((input_1!=nil)!=(url!=nil))
15
- # this covers:
16
- # download 'site.com/file' as:'file'
17
- # download url:'site.com/file' as:'file'
18
- the_url = url || input_1
19
- file_name = as
20
- elsif ((from!=nil) != (url!=nil)) && input_1!=nil
21
- # this covers:
22
- # download 'file' from:'site.com/file'
23
- # download 'file' url:'site.com/file'
24
- the_url = from || url
25
- file_name = input_1
26
- else
27
- message_ = "I'm not sure how you're using the download function.\n"
28
- message_ << "Please use one of the following methods:\n"
29
- message_ << " download 'site.com/file'\n"
30
- message_ << " download 'site.com/file', as:'file'\n"
31
- message_ << " download url:'site.com/file', as:'file'\n"
32
- message_ << " download 'file', from:'site.com/file'\n"
33
- message_ << " download 'file', url:'site.com/file'\n"
34
- raise message_
35
- end#if
36
- #end argument checking
37
- FileSys.write(open(URI.encode(the_url)).read, to: file_name)
38
- end