Class: Appship::Config

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

Constant Summary collapse

TEMPLATE =
<<~YAML
  # appship configuration. Secrets should be supplied through environment variables.
  # Run `appship doctor` to validate the local toolchain.
  workspace: MyApp.xcworkspace
  # project: MyApp.xcodeproj
  scheme: MyApp
  configuration: Debug
  sdk: iphoneos
  output: build/MyApp-Debug.ipa

  # Optional icon badge. Remove or set to null to disable it.
  # badge: DEBUG
  # assets: MyApp/Assets.xcassets
  # app_icon: AppIcon

  upload:
    provider: pgyer
    api_key_env: PGYER_API_KEY
    # fir_api_token_env: FIR_API_TOKEN
    # fir_password_env: FIR_PASSWORD
    install_type: 1
    # password_env: PGYER_INSTALL_PASSWORD
    # channel: internal
YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/appship/config.rb', line 32

def initialize(path = nil)
  @path = path && File.expand_path(path)
  @data = if @path && File.file?(@path)
            YAML.safe_load(File.read(@path), permitted_classes: [], aliases: false) || {}
          else
            {}
          end
  raise ConfigurationError, "配置文件必须是 YAML 对象: #{@path}" unless @data.is_a?(Hash)
rescue Psych::SyntaxError => e
  raise ConfigurationError, "配置文件解析失败: #{e.message}"
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



30
31
32
# File 'lib/appship/config.rb', line 30

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/appship/config.rb', line 30

def path
  @path
end

Class Method Details

.find(path = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/appship/config.rb', line 51

def self.find(path = nil)
  return new(path) if path

  candidates = [
    File.join(Dir.pwd, ".appship.yml"),
    File.join(Dir.pwd, ".appship.yaml"),
    # Backward compatibility with the first provider-specific prototype.
    File.join(Dir.pwd, ".pgyer.yml"),
    File.join(Dir.pwd, ".pgyer.yaml")
  ]
  new(candidates.find { |candidate| File.file?(candidate) })
end

.write_template(path, force: false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/appship/config.rb', line 64

def self.write_template(path, force: false)
  expanded = File.expand_path(path)
  if File.exist?(expanded) && !force
    raise ConfigurationError, "文件已存在: #{expanded}(使用 --force 覆盖)"
  end

  FileUtils.mkdir_p(File.dirname(expanded))
  File.write(expanded, TEMPLATE)
  expanded
end

Instance Method Details

#get(*keys, default: nil) ⇒ Object



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

def get(*keys, default: nil)
  value = keys.reduce(@data) do |current, key|
    current.is_a?(Hash) ? current[key.to_s] : nil
  end
  value.nil? ? default : value
end