user_space 4.1.210122 → 5.0.210123

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -14
  3. data/lib/user_space.rb +22 -38
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: affe703bd77f1caf7cd1196cf3d24ccc66340bda26163a4d333ac446bc6249a7
4
- data.tar.gz: 9e0ab93da71eefc3bbc90630f26788ffc7fa4f65a5f581d7da3799c9da9aac37
3
+ metadata.gz: 1d7d345bdf4350375456747427362e151414f4e2eac1e62e890ef7692db500f4
4
+ data.tar.gz: f0cf7bf35d981425f444630a22e68348a829863dc5a9eba9728368fac959af99
5
5
  SHA512:
6
- metadata.gz: ef0d5faf595eb3980161f5725bf9bc1092d63bfd1ce1b46fdd3cc1b6283ee5046033cdd7b8ab881edbff0a4c6fafac54bc46593b5c342695bd303d629880608a
7
- data.tar.gz: e9a9fe8cd05890af3f8527e24c1314c76be12f830ec33682b25834c561a2d9f79b25f058ebbd7d2502210078c2fe7451f796cb3eb6af4b744070c589d88a9aa3
6
+ metadata.gz: c91c634ae02ab53e00108491be15b8cf077caf62bc796bc275f6e1fc548940a55b0a3f87760b61dd02a15561b1098a026dc88c9097c252583c3746957c323c0a
7
+ data.tar.gz: '095f378b67969f6628f56bce47c12fcdd0d9b8596a434c4effd21239853e0fbd962c3e36ada329f4abd24ec62ec0e59ec11159a3e55df0bedd866db8ec2facc2'
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # UserSpace
2
2
 
3
- * [VERSION 4.1.210122](https://github.com/carlosjhr64/user_space/releases)
3
+ * [VERSION 5.0.210123](https://github.com/carlosjhr64/user_space/releases)
4
4
  * [github](https://www.github.com/carlosjhr64/user_space)
5
5
  * [rubygems](https://rubygems.org/gems/user_space)
6
6
 
@@ -12,7 +12,7 @@ with the gem-apps cache, config, and data files.
12
12
  ## SYNOPSIS:
13
13
 
14
14
  ```ruby
15
- require 'json' # Using JSON parser for the config file.
15
+ require 'rbon' # Using RBON(is not JSON!) parser for the config file.
16
16
  require 'user_space'
17
17
 
18
18
  module App
@@ -21,32 +21,25 @@ module App
21
21
  VERSION = '1.2.3'
22
22
  end
23
23
 
24
- USERSPACE = UserSpace.new(parser:JSON, appname:'myapp') #~> ^#<UserSpace:
24
+ USERSPACE = UserSpace.new(parser:RBON, appname:'myapp') #~> ^#<UserSpace:
25
25
  # Will maintain these directories:
26
26
  ['~/.cache/myapp',
27
27
  '~/.config/myapp',
28
28
  '~/.local/share/myapp'
29
29
  ].all?{File.directory? File.expand_path _1} #=> true
30
30
 
31
- # Unless this version has been installed,
32
- # we copy over our data and cache directories.
33
- USERSPACE.install unless USERSPACE.version == App::VERSION
34
- # We have a version file:
35
- File.exist? File.expand_path '~/.local/share/myapp/VERSION' #=> true
36
-
37
31
  if USERSPACE.config?
38
- # Because JSON hashes by String, converting to Symbol.
39
- # We pad up App::CONFIG with user's preferences:
40
- USERSPACE.config.each{|opt, value| App::CONFIG[opt.to_sym] = value}
32
+ # Update APP::CONFIG with user's preferences.
33
+ App::CONFIG.merge! USERSPACE.config
41
34
  else
42
- # We initialize user preferences with our initial App::CONFIG
35
+ # Write initial configuration with App::CONFIG
43
36
  STDERR.puts "Writting '#{USERSPACE.config_file_name}'"
44
37
  USERSPACE.config = App::CONFIG
45
38
  end
46
39
  # To do the same thing, you can also say:
47
40
  # USERSPACE.configures(App::CONFIG)
48
41
  # We have a config file:
49
- File.exist? File.expand_path '~/.config/myapp/config.json' #=> true
42
+ File.exist? File.expand_path '~/.config/myapp/config.rbon' #=> true
50
43
  ```
51
44
 
52
45
  ## INSTALL:
@@ -3,15 +3,15 @@ require 'fileutils'
3
3
  #`ruby`
4
4
 
5
5
  class UserSpace
6
- VERSION = '4.1.210122'
6
+ VERSION = '5.0.210123'
7
7
  XDG = {
8
8
  'cache' => ENV['XDG_CACHE_HOME'] || File.expand_path('~/.cache'),
9
9
  'config' => ENV['XDG_CONFIG_HOME'] || File.expand_path('~/.config'),
10
10
  'data' => ENV['XDG_DATA_HOME'] || File.expand_path('~/.local/share'),
11
11
  }
12
12
 
13
- def self.appdir
14
- File.dirname File.dirname File.expand_path caller(1..2)[1].split(':',2)[0]
13
+ def self.appdir(lib='/lib')
14
+ (_ = caller(1..2)[-1]&.split(':',2)&.fetch(0)) and File.dirname(File.dirname(File.expand_path(_)))&.chomp(lib)
15
15
  end
16
16
 
17
17
  attr_reader :parser,:ext,:appname,:xdgbases,:appdir,:config
@@ -22,7 +22,7 @@ class UserSpace
22
22
  xdgbases: ['cache', 'config', 'data'],
23
23
  config: 'config')
24
24
  @parser,@ext,@appname,@xdgbases,@appdir,@config = parser,ext,appname,xdgbases,appdir,config
25
- install(false) # install with no overwrite
25
+ install
26
26
  end
27
27
 
28
28
  def xdg_pairs
@@ -32,30 +32,31 @@ class UserSpace
32
32
  end
33
33
  end
34
34
 
35
- # Note that initialize will not overwrite anything.
36
- # This overwrites the user's data directory with a fresh install.
37
- # App should consider being nice about this,
38
- # like warn the user or something.
39
- def install(overwrite=true)
35
+ # Will not overwrite anything.
36
+ # Only copies over missing directories and files.
37
+ # Verifies directory expectations.
38
+ def install
40
39
  xdg_pairs do |basedir, userdir|
41
40
  if File.exist?(userdir)
42
41
  # Sanity check
43
- raise "Not a directory: #{userdir}" unless File.directory?(userdir)
44
- # Pre-existing directory.
45
- # Return unless user wants to overwrite.
46
- next unless overwrite
42
+ assert_directory(userdir)
47
43
  else
48
44
  Dir.mkdir(userdir, 0700)
49
45
  end
50
46
  if File.directory? basedir
51
47
  Dir.glob("#{basedir}/**/*").each do |src|
52
48
  dest = src.sub(basedir, userdir)
53
- if File.directory? src
54
- Dir.mkdir dest unless File.exist? dest
49
+ if File.exist? dest
50
+ # Sanity checks
51
+ assert_directory(dest) if File.directory? src
55
52
  else
56
- FileUtils.cp src, dest
53
+ if File.directory? src
54
+ Dir.mkdir dest
55
+ else
56
+ FileUtils.cp src, dest
57
+ end
58
+ FileUtils.chmod('u+rwX,go-rwx', dest)
57
59
  end
58
- FileUtils.chmod('u+rwX,go-rwx', dest)
59
60
  end
60
61
  end
61
62
  end
@@ -73,16 +74,10 @@ class UserSpace
73
74
  File.join XDG['data'], @appname
74
75
  end
75
76
 
76
- # Not really for public use.
77
77
  def config_file_name
78
78
  File.join XDG['config'], @appname, "#{@config}.#{@ext}"
79
79
  end
80
80
 
81
- # Not really for public use.
82
- def version_file_name
83
- File.join XDG['data'], @appname, 'VERSION'
84
- end
85
-
86
81
  def config?
87
82
  File.exist?(config_file_name)
88
83
  end
@@ -104,27 +99,16 @@ class UserSpace
104
99
 
105
100
  def configures(hash)
106
101
  if config? # file exists
107
- config.each{|opt, value| hash[opt.to_sym] = value}
102
+ hash.merge! config
108
103
  else
109
104
  $stderr.puts config_file_name if $VERBOSE
110
105
  self.config = hash
111
106
  end
112
107
  end
113
108
 
114
- def version?
115
- File.exist?(version_file_name)
116
- end
117
-
118
- # This reads the data directory's version file
119
- def version
120
- File.read(version_file_name).strip
121
- rescue
122
- $stderr.puts $!.message if $VERBOSE
123
- $stderr.puts $!.backtrace if $DEBUG
124
- nil
125
- end
109
+ private
126
110
 
127
- def version=(v)
128
- File.open(version_file_name, 'w', 0600){|fh| fh.puts v}
111
+ def assert_directory(dir)
112
+ raise "Not a directory: #{dir}" unless File.directory?(dir)
129
113
  end
130
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: user_space
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.210122
4
+ version: 5.0.210123
5
5
  platform: ruby
6
6
  authors:
7
7
  - carlosjhr64
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-01-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Maintains the user's cache, config, and data directories