Class: Reflex::Packager::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/reflex/packager/config.rb

Overview

Application project configuration loaded from ‘reflex.yml’.

Constant Summary collapse

EXCLUDES =
%w[build dist]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, dir, hash = nil, stderr: $stderr) ⇒ Config

Returns a new instance of Config.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/reflex/packager/config.rb', line 51

def initialize(profile, dir, hash = nil, stderr: $stderr)
  raise Error, "no such directory: '#{dir}'" unless File.directory? dir

  @profile = profile
  @dir     = File.expand_path dir
  hash = symbolize_keys hash || {}
  hash = validate_input hash, Config.defaults(profile, @dir, hash), stderr: stderr

  @name      = hash[:name]     .to_s
  @bundle_id = hash[:bundle_id].to_s
  @version   = hash[:version]  .to_s
  @main      = hash[:main]     .to_s
  @icon      = hash[:icon]    &.to_s
  @files     = hash[:files]&.then {Array(_1).map(&:to_s)}
  @pods      = hash[:pods].transform_values &:compact
  @macos     = MacOSConfig.new hash[:macos]
  validate
end

Instance Attribute Details

#bundle_idObject (readonly)

Returns the value of attribute bundle_id.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def bundle_id
  @bundle_id
end

#dirObject (readonly)

Returns the value of attribute dir.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def dir
  @dir
end

#filesObject (readonly)

Returns the value of attribute files.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def files
  @files
end

#iconObject (readonly)

Returns the value of attribute icon.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def icon
  @icon
end

#macosObject (readonly)

Returns the value of attribute macos.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def macos
  @macos
end

#mainObject (readonly)

Returns the value of attribute main.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def name
  @name
end

#podsObject (readonly)

Returns the value of attribute pods.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def pods
  @pods
end

#profileObject (readonly)

Returns the value of attribute profile.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def profile
  @profile
end

#versionObject (readonly)

Returns the value of attribute version.



70
71
72
# File 'lib/reflex/packager/config.rb', line 70

def version
  @version
end

Class Method Details

.default_bundle_id(profile, name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/reflex/packager/config.rb', line 91

def self.default_bundle_id(profile, name)
  id = name.downcase.gsub(/[^a-z0-9\-]+/, '')
  if id.empty?
    raise Error, "cannot derive a bundle_id from name '#{name}', " +
      "set 'bundle_id' in #{profile.config_files.first}"
  end
  "#{profile.bundle_id_prefix}.#{id}"
end

.defaults(profile, dir, hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reflex/packager/config.rb', line 18

def self.defaults(profile, dir, hash)
  name = hash[:name]&.to_s || File.basename(dir)
  {
    name:      name,
    bundle_id: hash[:bundle_id] || default_bundle_id(profile, name),
    version:   '0.1.0',
    main:      'main.rb',
    icon:      nil,
    files:     nil,
    pods: {
      :cruby          => {tag: nil, branch: nil, git: nil, path: nil},
      profile.pod_key => {tag: nil, branch: nil, git: nil, path: nil}
    },
    macos: MacOSConfig.defaults
  }
end

.load(profile, dir, path = nil) ⇒ Config

Load the config file in the project directory.

Parameters:

  • profile (Profile)

    runtime profile to package for

  • dir (String)

    project directory

  • path (String) (defaults to: nil)

    config file path to use instead of the default

Returns:



43
44
45
46
47
48
49
# File 'lib/reflex/packager/config.rb', line 43

def self.load(profile, dir, path = nil)
  raise Error, "config file not found: '#{path}'" if path && !File.file?(path)
  path ||= profile.config_files.map {File.join dir, _1}.find {File.file? _1}
  new profile, dir, (path ? YAML.safe_load(File.read(path), aliases: true) : nil)
rescue Psych::SyntaxError => e
  raise Error, "failed to parse '#{path}': #{e.message}"
end

Instance Method Details

#app_filesArray<String>

Returns paths to be bundled into the application, relative to the project directory.

Returns:

  • (Array<String>)

    relative paths



78
79
80
81
82
83
84
85
# File 'lib/reflex/packager/config.rb', line 78

def app_files()
  excludes = EXCLUDES + @profile.config_files
  [@main, *@files]
    .flat_map {Dir.glob _1, base: @dir}
    .reject {_1.start_with?('.') || excludes.include?(_1)}
    .uniq
    .sort
end