Class: Appship::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner: Runner.new) ⇒ Builder

Returns a new instance of Builder.



7
8
9
# File 'lib/appship/builder.rb', line 7

def initialize(runner: Runner.new)
  @runner = runner
end

Instance Attribute Details

#runnerObject (readonly)

Returns the value of attribute runner.



5
6
7
# File 'lib/appship/builder.rb', line 5

def runner
  @runner
end

Instance Method Details

#build!(options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/appship/builder.rb', line 11

def build!(options)
  target = target_options(options)
  scheme = required!(options[:scheme], "--scheme 或配置 scheme")
  configuration = options[:configuration] || "Debug"
  sdk = options[:sdk] || "iphoneos"
  output = File.expand_path(options[:output] || File.join("build", "#{scheme}-#{configuration}.ipa"))
  temp_root = nil
  managed_paths = []

  if options[:archive_path]
    archive_path = File.expand_path(options[:archive_path])
  else
    temp_root = Dir.mktmpdir("appship-")
    archive_path = File.join(temp_root, "#{scheme}.xcarchive")
    managed_paths << archive_path
  end

  if options[:export_path]
    export_path = File.expand_path(options[:export_path])
  else
    temp_root ||= Dir.mktmpdir("appship-")
    export_path = File.join(temp_root, "export")
    managed_paths << export_path
  end

  begin
    runner.require_command!("xcodebuild", "需要安装 Xcode 命令行工具")
    FileUtils.rm_rf(archive_path) if File.exist?(archive_path) && options.fetch(:clean, true)
    FileUtils.rm_rf(export_path) if File.exist?(export_path) && options.fetch(:clean, true)
    FileUtils.mkdir_p(File.dirname(archive_path))
    FileUtils.mkdir_p(export_path)

    archive!(target, scheme, configuration, sdk, archive_path, options)
    export!(archive_path, export_path, options)

    ipa = Dir[File.join(export_path, "*.ipa")].first
    raise CommandError, "导出完成但未找到 IPA: #{export_path}" unless ipa

    final_ipa = File.expand_path(ipa)
    if options[:badge]
      Ipa.apply_badge!(final_ipa, assets: options[:assets], app_icon: options[:app_icon], text: options[:badge], runner: runner)
    end

    FileUtils.mkdir_p(File.dirname(output))
    unless File.expand_path(output) == final_ipa
      FileUtils.rm_f(output) if File.exist?(output)
      FileUtils.mv(final_ipa, output)
    end
    puts "✅ IPA 已生成: #{output}"

    upload_if_requested!(output, options)
    output
  ensure
    FileUtils.rm_rf(temp_root) if temp_root && !options[:keep_temporary]
  end
end

#find_cached_app!(options) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/appship/builder.rb', line 68

def find_cached_app!(options)
  target = target_options(options)
  scheme = required!(options[:scheme], "--scheme 或缓存中的 project_name")
  sdk = options[:sdk] || "iphoneos"
  app_name = required!(options[:app_name], "缓存中的 app_name 或 --app-name")

  runner.require_command!("xcodebuild", "需要安装 Xcode 命令行工具")
  target_flag = target.keys.first == :workspace ? "-workspace" : "-project"
  configurations = [options[:configuration], "Debug", "Release"].compact.uniq
  configurations.each do |configuration|
    begin
      settings = runner.capture!(["xcodebuild", target_flag, target.values.first, "-scheme", scheme,
                                  "-configuration", configuration, "-sdk", sdk, "-showBuildSettings"])
    rescue CommandError
      next
    end
    products_dir = settings.lines.filter_map do |line|
      next unless line.include?("BUILT_PRODUCTS_DIR") && line.include?("=")

      line.split("=", 2).last.strip
    end.first
    next if products_dir.to_s.empty?

    app_path = File.join(products_dir, "#{app_name}.app")
    next unless File.directory?(app_path)

    options[:configuration] = configuration
    return app_path
  end

  raise CommandError, "未找到缓存 App(已检查配置: #{configurations.join(", ")}"
end

#package!(options) ⇒ Object

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/appship/builder.rb', line 101

def package!(options)
  app = File.expand_path(required!(options[:app], "--app"))
  raise ConfigurationError, ".app 不存在: #{app}" unless File.directory?(app)

  output = File.expand_path(options[:output] || File.join(Dir.pwd, "#{File.basename(app, ".app")}.ipa"))
  runner.require_command!("zip", "macOS 通常自带 zip")
  runner.require_command!("unzip", "macOS 通常自带 unzip") if options[:badge]
  Ipa.package!(app, output: output, badge: options[:badge], assets: options[:assets], app_icon: options[:app_icon], runner: runner)
  puts "✅ IPA 已生成: #{output}"
  upload_if_requested!(output, options)
  output
end

#upload!(options) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
# File 'lib/appship/builder.rb', line 114

def upload!(options)
  file = File.expand_path(required!(options[:file], "IPA 文件路径"))
  raise ConfigurationError, "文件不存在: #{file}" unless File.file?(file)

  result = Uploader.new(options).upload!(file)
  Display.upload_result(result, file: file, options: options)
  result
end