lib/produce/config.rb in produce-0.1.0 vs lib/produce/config.rb in produce-0.1.1

- old
+ new

@@ -1,41 +1,74 @@ module Produce class Config - attr_accessor :config + ASK_MESSAGES = { + bundle_identifier: "App Identifier (Bundle ID, e.g. com.krausefx.app): ", + app_name: "App Name: ", + version: "Initial version number (e.g. '1.0'): ", + sku: "SKU Number (e.g. '1234'): ", + primary_language: "Primary Language (e.g. 'English', 'German'): " + } + attr_reader :config + def self.shared_config @@shared ||= self.new end - def initialize - @config = { - :bundle_identifier => ENV['PRODUCE_APP_IDENTIFIER'], - :app_name => ENV['PRODUCE_APP_NAME'], - :primary_language => ENV['PRODUCE_LANGUAGE'], - :version => ENV['PRODUCE_VERSION'], - :sku => ENV['PRODUCE_SKU'] - } + def self.shared_config= config + @@shared = config + end - @config[:bundle_identifier] ||= CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier) - @config[:bundle_identifier] ||= ask("App Identifier (Bundle ID, e.g. com.krausefx.app): ") - @config[:app_name] ||= ask("App Name: ") - - while @config[:primary_language].to_s.length == 0 - input = ask("Primary Language (e.g. 'English', 'German'): ") - input = input.split.map(&:capitalize).join(' ') - if not AvailableDefaultLanguages.all_langauges.include?(input) - Helper.log.error "Could not find langauge #{input} - available languages: #{AvailableDefaultLanguages.all_langauges}" + def self.env_options + hash = { + bundle_identifier: ENV['PRODUCE_APP_IDENTIFIER'], + app_name: ENV['PRODUCE_APP_NAME'], + version: ENV['PRODUCE_VERSION'], + sku: ENV['PRODUCE_SKU'], + skip_itc: skip_itc?(ENV['PRODUCE_SKIP_ITC']) + } + if ENV['PRODUCE_LANGUAGE'] + language = ENV['PRODUCE_LANGUAGE'] + if is_valid_language?(language) + hash[:primary_language] = language else - @config[:primary_language] = input + Helper.log.error "PRODUCE_LANGUAGE is set to #{language} but it's not one of available languages. You'll be asked to set language again if needed." end end + hash[:bundle_identifier] ||= CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier) + hash.delete_if { |key, value| value.nil? } + hash + end - @config[:version] ||= ask("Initial version number (e.g. '1.0'): ") - @config[:sku] ||= ask("SKU Number (e.g. '1234'): ") + def initialize(options = {}) + @config = Config.env_options.merge(options) end def self.val(key) - raise "Please only pass symbols, no Strings to this method".red unless key.kind_of?Symbol - self.shared_config.config[key] + raise "Please only pass symbols, no Strings to this method".red unless key.kind_of? Symbol + + unless self.shared_config.config.has_key? key + self.shared_config.config[key] = ask(ASK_MESSAGES[key]) do |q| + case key + when :primary_language + q.validate = lambda { |val| is_valid_language?(val) } + q.responses[:not_valid] = "Please enter one of available languages: #{AvailableDefaultLanguages.all_langauges}" + else + q.validate = lambda { |val| !val.empty? } + q.responses[:not_valid] = "#{key.to_s.gsub('_', ' ').capitalize} can't be blank" + end + end + end + + return self.shared_config.config[key] + end + + def self.is_valid_language? language + language = language.split.map(&:capitalize).join(' ') + AvailableDefaultLanguages.all_langauges.include? language + end + + def self.skip_itc? value + %w( true t 1 yes y ).include? value.to_s.downcase end end end \ No newline at end of file