Class: UserSpace

Inherits:
Object
  • Object
show all
Defined in:
lib/user_space.rb

Overview

‘ruby`

Constant Summary collapse

VERSION =
'5.2.230101'
XDG =
{
  'cache'  => ENV['XDG_CACHE_HOME']  || File.expand_path('~/.cache'),
  'config' => ENV['XDG_CONFIG_HOME'] || File.expand_path('~/.config'),
  'data'   => ENV['XDG_DATA_HOME']   || File.expand_path('~/.local/share'),
}
APPDIR =
lambda{
File.dirname(File.expand_path(_1)).sub(%r([/\\]lib$),'')}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser:, appdir: UserSpace.appdir, ext: parser.to_s.downcase, appname: File.basename($0), xdgbases: ['cache', 'config', 'data'], config: 'config') ⇒ UserSpace

Returns a new instance of UserSpace.



21
22
23
24
25
26
27
28
29
30
# File 'lib/user_space.rb', line 21

def initialize( parser:,
                appdir:   UserSpace.appdir,
                ext:      parser.to_s.downcase,
                appname:  File.basename($0),
                xdgbases: ['cache', 'config', 'data'],
                config:   'config')
  @parser,@ext,@appname,@xdgbases,@appdir,@cfg =
    parser,ext,appname,xdgbases,appdir,config
  install
end

Instance Attribute Details

#appdirObject (readonly)

Returns the value of attribute appdir.



20
21
22
# File 'lib/user_space.rb', line 20

def appdir
  @appdir
end

#appnameObject (readonly)

Returns the value of attribute appname.



20
21
22
# File 'lib/user_space.rb', line 20

def appname
  @appname
end

#cfgObject (readonly)

Returns the value of attribute cfg.



20
21
22
# File 'lib/user_space.rb', line 20

def cfg
  @cfg
end

#extObject (readonly)

Returns the value of attribute ext.



20
21
22
# File 'lib/user_space.rb', line 20

def ext
  @ext
end

#parserObject (readonly)

Returns the value of attribute parser.



20
21
22
# File 'lib/user_space.rb', line 20

def parser
  @parser
end

#xdgbasesObject (readonly)

Returns the value of attribute xdgbases.



20
21
22
# File 'lib/user_space.rb', line 20

def xdgbases
  @xdgbases
end

Class Method Details

.appdir(dir = File.dirname(caller(1..2)[-1].split(':',2).fetch(0))) ⇒ Object



16
17
18
# File 'lib/user_space.rb', line 16

def UserSpace.appdir(dir=File.dirname(caller(1..2)[-1].split(':',2).fetch(0)))
  APPDIR[dir]
end

Instance Method Details

#cachedirObject



69
70
71
# File 'lib/user_space.rb', line 69

def cachedir
  File.join XDG['cache'], @appname
end

#configObject

Reads config



90
91
92
93
94
95
96
# File 'lib/user_space.rb', line 90

def config
  @parser.load File.read(config_file_name)
rescue
  $stderr.puts $!.message   if $VERBOSE
  $stderr.puts $!.backtrace if $DEBUG
  nil
end

#config=(obj) ⇒ Object

Saves config



99
100
101
102
103
# File 'lib/user_space.rb', line 99

def config=(obj)
  dump = (@parser.respond_to?(:pretty_generate))?
    @parser.pretty_generate(obj) : @parser.dump(obj)
  File.open(config_file_name, 'w', 0600){|fh|fh.puts dump}
end

#config?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/user_space.rb', line 85

def config?
  File.exist?(config_file_name)
end

#config_file_nameObject



81
82
83
# File 'lib/user_space.rb', line 81

def config_file_name
  File.join XDG['config'], @appname, "#{@cfg}.#{@ext}"
end

#configdirObject



73
74
75
# File 'lib/user_space.rb', line 73

def configdir
  File.join XDG['config'], @appname
end

#configures(hash) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/user_space.rb', line 105

def configures(hash)
  if config? # file exists
    hash.merge! config
  else
    $stderr.puts config_file_name if $VERBOSE
    self.config = hash
  end
end

#datadirObject



77
78
79
# File 'lib/user_space.rb', line 77

def datadir
  File.join XDG['data'], @appname
end

#installObject

Will not overwrite anything. Only copies over missing directories and files. Verifies directory expectations.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/user_space.rb', line 42

def install
  xdg_pairs do |basedir, userdir|
    if File.exist?(userdir)
      # Sanity check
      assert_directory(userdir)
    else
      Dir.mkdir(userdir, 0700)
    end
    if File.directory? basedir
      Dir.glob("#{basedir}/**/*").each do |src|
        dest = src.sub(basedir, userdir)
        if File.exist? dest
          # Sanity checks
          assert_directory(dest) if File.directory? src
        else
          if File.directory? src
            Dir.mkdir dest
          else
            FileUtils.cp src, dest
          end
          FileUtils.chmod('u+rwX,go-rwx', dest)
        end
      end
    end
  end
end

#xdg_pairsObject



32
33
34
35
36
37
# File 'lib/user_space.rb', line 32

def xdg_pairs
  @xdgbases.each do |base|
    # yield basedir, userdir
    yield File.join(@appdir, base), File.join(XDG[base], @appname)
  end
end