Class: Furoshiki::Shoes::Configuration
- Inherits:
-
Object
- Object
- Furoshiki::Shoes::Configuration
- 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.
Class Method Summary collapse
-
.load(path = 'app.yaml') ⇒ Object
Convenience method for loading config from a file.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #error_message_list ⇒ Object
- #errors ⇒ Object
-
#initialize(config = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #shortname ⇒ Object
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Configuration
Returns a new instance of Configuration.
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) = class << self; self; end @config.keys.reject {|k| k == :shortname}.each do |k| .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.
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. = { run: pathname.basename.to_s, name: pathname.basename(pathname.extname).to_s.gsub(/\W/, '-') }.to_yaml = pathname.exist? ? : pathname file = dummy_file.new() 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_list ⇒ Object
147 148 149 |
# File 'lib/furoshiki/shoes/configuration.rb', line 147 def @errors.map {|m| " - #{m}"}.join("\n") end |
#errors ⇒ Object
143 144 145 |
# File 'lib/furoshiki/shoes/configuration.rb', line 143 def errors @errors.dup end |
#shortname ⇒ Object
120 121 122 |
# File 'lib/furoshiki/shoes/configuration.rb', line 120 def shortname @config[:shortname] || @config[:name].downcase.gsub(/\W+/, '') end |
#to_hash ⇒ Object
124 125 126 |
# File 'lib/furoshiki/shoes/configuration.rb', line 124 def to_hash @config end |
#valid? ⇒ Boolean
138 139 140 141 |
# File 'lib/furoshiki/shoes/configuration.rb', line 138 def valid? validate return errors.empty? end |
#validate ⇒ Object
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 |