Class: Furoshiki::Shoes::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/furoshiki/shoes/configuration.rb

Overview

Configuration for Shoes packagers.

If your configuration uses hashes, the keys will always be symbols, even if you have created it with string keys. It’s just easier that way.

This is a value object. If you need to modify your configuration after initialization, dump it with #to_hash, make your changes, and instantiate a new object.

Examples:

config_file = '/path/to/app.yaml'
config = Shoes::Package::Configuration.load(config_file)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • config (Hash) (defaults to: {})

    user options

  • working_dir (String)

    directory in which do packaging work



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/furoshiki/shoes/configuration.rb', line 82

def initialize(config = {})
  defaults = {
    name: 'Shoes App',
    version: '0.0.0',
    release: 'Rookie',
    run: nil,
    ignore: 'pkg',
    icons: {
      #osx: 'path/to/default/App.icns',
      #gtk: 'path/to/default/app.png',
      #win32: 'path/to/default/App.ico',
    },
    dmg: {
      ds_store: 'path/to/default/.DS_Store',
      background: 'path/to/default/background.png'
    },
    working_dir: Dir.pwd
  }

  # Overwrite defaults with supplied config
  @config = config.inject(defaults) { |c, (k, v)| set_symbol_key c, k, v }

  # Ensure that we always have what we need
  [:ignore, :gems].each { |k| @config[k] = Array(@config[k]) }
  @config[:gems] << 'shoes'
  @config[:working_dir] = Pathname.new(@config[:working_dir])

  # Define reader for each key (#shortname defined below)
  metaclass = class << self; self; end
  @config.keys.reject {|k| k == :shortname}.each do |k|
    metaclass.send(:define_method, k) do
      @config[k]
    end
  end

  @errors = []
end

Class Method Details

.load(path) ⇒ Object .load(path) ⇒ Object .load(path) ⇒ Object

Convenience method for loading config from a file. Note that you can pass four kinds of paths to the loader. Given the following file structure:

├── a
│   ├── app.yaml
│   └── shoes-app-a.rb
└── b
└── shoes-app-b.rb

To package an app that has an ‘app.yaml`, like `shoes-app-a.rb`, you can call the loader with any of:

  • a/app.yaml

  • a

  • a/shoes-app-a.rb

These will all find and use your configuration in ‘a/app.yaml`. To package an app that does not have an `app.yaml`, like `b/shoes-app-b.rb`, you must call the loader with the path of the script itself. Note that without an `app.yaml`, you will only bundle a single file, and your app will simply use the Shoes app icon.

Overloads:

  • .load(path) ⇒ Object

    Parameters:

    • path (String)

      location of the app’s ‘app.yaml’

  • .load(path) ⇒ Object

    Parameters:

    • path (String)

      location of the directory that contains the app’s ‘app.yaml’

  • .load(path) ⇒ Object

    Parameters:

    • path (String)

      location of the app



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/furoshiki/shoes/configuration.rb', line 51

def self.load(path = 'app.yaml')
  pathname = Pathname.new(path)
  app_yaml = Pathname.new('app.yaml')

  dummy_file = Struct.new(:read)

  if pathname.basename == app_yaml
    file, dir = pathname, pathname.dirname
  elsif pathname.directory?
    file, dir = pathname.join(app_yaml), pathname
  elsif pathname.file? && pathname.parent.children.include?(pathname.parent.join app_yaml)
    file, dir = pathname.parent.join(app_yaml), pathname.parent
  else
    # Can't find any 'app.yaml', so assume we just want to wrap
    # this file. If it exists, load default options. If not, let
    # the filesystem raise an error.
    default_options = {
      run: pathname.basename.to_s,
      name: pathname.basename(pathname.extname).to_s.gsub(/\W/, '-')
    }.to_yaml
    options = pathname.exist? ? default_options : pathname
    file = dummy_file.new(options)
    dir = pathname.parent
  end
  config = YAML.load(file.read)
  config[:working_dir] = dir
  new(config)
end

Instance Method Details

#==(other) ⇒ Object



151
152
153
154
155
156
# File 'lib/furoshiki/shoes/configuration.rb', line 151

def ==(other)
  super unless other.class == self.class && other.respond_to?(:to_hash)
  @config == other.to_hash &&
    working_dir == other.working_dir &&
    errors == other.errors
end

#error_message_listObject



147
148
149
# File 'lib/furoshiki/shoes/configuration.rb', line 147

def error_message_list
  @errors.map {|m| "  - #{m}"}.join("\n")
end

#errorsObject



143
144
145
# File 'lib/furoshiki/shoes/configuration.rb', line 143

def errors
  @errors.dup
end

#shortnameObject



120
121
122
# File 'lib/furoshiki/shoes/configuration.rb', line 120

def shortname
  @config[:shortname] || @config[:name].downcase.gsub(/\W+/, '')
end

#to_hashObject



124
125
126
# File 'lib/furoshiki/shoes/configuration.rb', line 124

def to_hash
  @config
end

#valid?Boolean

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/furoshiki/shoes/configuration.rb', line 138

def valid?
  validate
  return errors.empty?
end

#validateObject



128
129
130
131
132
133
134
135
136
# File 'lib/furoshiki/shoes/configuration.rb', line 128

def validate
  unless @config[:run] && working_dir.join(@config[:run]).exist?
    add_missing_file_error(@config[:run], "Run file")
  end

  if @config[:icons][:osx] && !working_dir.join(@config[:icons][:osx]).exist?
    add_missing_file_error(@config[:icons][:osx], "OS X icon file")
  end
end