Module: Ruflet::CLI::BuildCommand

Includes:
FlutterSdk
Included in:
Ruflet::CLI
Defined in:
lib/ruflet/cli/build_command.rb

Constant Summary collapse

CLIENT_EXTENSION_MAP =
{
  "audio" => { package: "flet_audio", alias: "ruflet_audio" },
  "audio_recorder" => { package: "flet_audio_recorder", alias: "ruflet_audio_recorder" },
  "camera" => { package: "flet_camera", alias: "ruflet_camera" },
  "charts" => { package: "flet_charts", alias: "ruflet_charts" },
  "code_editor" => { package: "flet_code_editor", alias: "ruflet_code_editor" },
  "color_pickers" => { package: "flet_color_pickers", alias: "ruflet_color_picker" },
  "datatable2" => { package: "flet_datatable2", alias: "ruflet_datatable2" },
  "flashlight" => { package: "flet_flashlight", alias: "ruflet_flashlight" },
  "geolocator" => { package: "flet_geolocator", alias: "ruflet_geolocator" },
  "lottie" => { package: "flet_lottie", alias: "ruflet_lottie" },
  "map" => { package: "flet_map", alias: "ruflet_map" },
  "permission_handler" => { package: "flet_permission_handler", alias: "ruflet_permission_handler" },
  "qrcode_scanner" => { package: "ruflet_qrcode_scanner", alias: "ruflet_qrcode_scanner" },
  "rive" => { package: "flet_rive", alias: "ruflet_rive" },
  "secure_storage" => { package: "flet_secure_storage", alias: "ruflet_secure_storage" },
  "video" => { package: "flet_video", alias: "ruflet_video" },
  "webview" => { package: "flet_webview", alias: "ruflet_webview" }
}.freeze
PROTECTED_SERVICE_EXTENSIONS =
{
  "camera" => %w[camera permission_handler],
  "microphone" => %w[audio_recorder permission_handler],
  "location" => %w[geolocator permission_handler],
  "motion" => %w[permission_handler]
}.freeze
EXTENSION_REQUIRED_SERVICES =
{
  "qrcode_scanner" => %w[camera]
}.freeze
ANDROID_SERVICE_PERMISSIONS =
{
  "camera" => %w[android.permission.CAMERA],
  "microphone" => %w[android.permission.RECORD_AUDIO],
  "location" => %w[android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION],
  "motion" => %w[android.permission.ACTIVITY_RECOGNITION android.permission.HIGH_SAMPLING_RATE_SENSORS]
}.freeze
IOS_SERVICE_USAGE_KEYS =
{
  "camera" => "NSCameraUsageDescription",
  "microphone" => "NSMicrophoneUsageDescription",
  "location" => "NSLocationWhenInUseUsageDescription",
  "motion" => "NSMotionUsageDescription"
}.freeze
RUNTIME_RESOLVED_BACKEND_PLATFORMS =

Clients that are told which server to use at launch rather than at build time, so they may be built without a configured backend_url.

%w[web macos windows linux].freeze
PLATFORM_ASSET_SUPPORT =

What the underlying generators can actually produce per platform: flutter_native_splash covers android/ios/web, flutter_launcher_icons covers android/ios/web/windows/macos. Linux has neither.

{
  "android" => { splash: true, icon: true },
  "ios" => { splash: true, icon: true },
  "web" => { splash: true, icon: true },
  "macos" => { splash: false, icon: true },
  "windows" => { splash: false, icon: true },
  "linux" => { splash: false, icon: false }
}.freeze

Constants included from FlutterSdk

FlutterSdk::DEFAULT_FLUTTER_CHANNEL, FlutterSdk::RELEASES_BASE

Instance Method Summary collapse

Methods included from FlutterSdk

#ensure_flutter!, #flutter_version_summary

Instance Method Details

#command_build(args) ⇒ Object



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruflet/cli/build_command.rb', line 71

def command_build(args)
  self_contained = args.delete("--self")
  verbose = args.delete("--verbose") || args.delete("-v")
  platform = (args.shift || "").downcase
  if platform.empty?
    warn "Usage: ruflet build <apk|android|aab|ios|ipa|web|macos|windows|linux> [--self] [--verbose]"
    return 1
  end

  flutter_cmd = flutter_build_command(platform)
  unless flutter_cmd
    warn "Unsupported build target: #{platform}"
    return 1
  end

  # `ipa` produces the uploadable archive; every other step — pods,
  # signing, icons, package name — is the same as a plain iOS build.
  requested_platform = platform
  platform = "ios" if platform == "ipa"

  # The embedded Ruby VM is a native plugin with no browser
  # implementation, so a self-contained web build produces an app that
  # cannot start. Say so rather than shipping one that hangs.
  if self_contained && platform == "web"
    warn "build config error: --self is not supported for web"
    warn "A web client runs no embedded Ruby; build it with `ruflet build web`."
    return 1
  end

  ensure_ruflet_build_assets(verbose: !!verbose)
  client_dir = ensure_flutter_client_dir(verbose: !!verbose)
  unless client_dir
    warn "Could not find Flutter client directory."
    warn "Set RUFLET_CLIENT_DIR or let Ruflet manage the client under ./build/client"
    return 1
  end

  build_note("Preparing #{platform} build (#{self_contained ? 'self-contained' : 'server-driven'})")
  config = load_ruflet_config
  tools = ensure_flutter!("build", client_dir: client_dir)
  command_env = build_tool_env(tools[:env], platform, client_dir)
  ok = prepare_flutter_client(
    client_dir,
    platform: platform,
    tools: tools.merge(env: command_env),
    config: config,
    self_contained: !!self_contained,
    verbose: !!verbose
  )
  return 1 unless ok

  build_args = [*flutter_cmd, *args]
  build_args << "--codesign" if ios_device_build_needs_codesign_flag?(requested_platform, build_args)
  target_entrypoint = flutter_target_entrypoint(client_dir, self_contained: !!self_contained)
  build_args += ["--target", target_entrypoint] if target_entrypoint
  backend_url = configured_backend_url(config)
  if self_contained
    build_args += ["--dart-define", "RUFLET_BACKEND_URL=#{backend_url}"] if backend_url
    # Pin the embedded project so main.self.dart extracts assets/<name>/
    # deterministically instead of inferring from a single main.rb — the
    # app tree now ships many main.rb files (standalone_apps/*/main.rb).
    build_args += ["--dart-define", "RUFLET_EMBEDDED_PROJECT=#{self_contained_project_name}"]
  elsif backend_url
    build_args += ["--dart-define", "RUFLET_BACKEND_URL=#{backend_url}"]
  elsif RUNTIME_RESOLVED_BACKEND_PLATFORMS.include?(platform)
    # These clients learn their server at launch: a web client from the
    # origin it is served from, a desktop client from the URL the launcher
    # passes. Baking one in would pin them to a single host and port,
    # which a preview client cannot use.
    build_note("No backend_url configured; the #{platform} client will resolve its server at launch")
  else
    warn "build config error: backend_url is required for server-driven builds"
    warn "Set app.backend_url or backend_url in ruflet.yaml"
    return 1
  end
  build_args << "-v" if verbose
  stage_ios_simulator_ruby_runtime(client_dir, build_args, verbose: !!verbose) if self_contained

  build_log(verbose, "mode=#{self_contained ? 'self' : 'server'}")
  build_log(verbose, "client_dir=#{client_dir}")
  build_log(verbose, "flutter=#{tools[:flutter]}")
  build_log(verbose, "dart=#{tools[:dart]}")
  build_log(verbose, "target=#{target_entrypoint}") if target_entrypoint
  build_log(verbose, "command=#{([tools[:flutter]] + build_args).join(' ')}")

  build_note("Running Flutter #{build_args.join(' ')}")
  ok = run_external_command(command_env, tools[:flutter], *build_args, chdir: client_dir, unbundled: true)
  export_platform_build_outputs(client_dir, platform, verbose: !!verbose) if ok
  ok ? 0 : 1
end

#command_install(args) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ruflet/cli/build_command.rb', line 162

def command_install(args)
  verbose = args.delete("--verbose") || args.delete("-v")
  device_id = extract_option_value!(args, "--device", "-d")

  client_dir = ensure_flutter_client_dir(verbose: !!verbose)
  unless client_dir
    warn "Could not find Flutter client directory."
    warn "Set RUFLET_CLIENT_DIR or let Ruflet manage the client under ./build/client"
    return 1
  end

  tools = ensure_flutter!("install", client_dir: client_dir)
  command_env = install_tool_env(tools[:env], client_dir)
  install_platform = install_platform_for_device(device_id)
  unless sync_built_outputs_for_install(client_dir, platform: install_platform, verbose: !!verbose)
    warn "Could not find built app outputs under ./build"
    warn "Run `ruflet build ...` first, then `ruflet install`."
    return 1
  end
  unless validate_install_artifacts(client_dir, platform: install_platform, device_id: device_id)
    return 1
  end

  install_args = ["install"]
  install_args += ["-d", device_id] if device_id
  install_args << "-v" if verbose

  build_log(verbose, "client_dir=#{client_dir}")
  build_log(verbose, "flutter=#{tools[:flutter]}")
  build_log(verbose, "dart=#{tools[:dart]}")
  build_log(verbose, "install_command=#{([tools[:flutter]] + install_args).join(' ')}")
  build_note("Installing app#{device_id ? " to device #{device_id}" : ""}")

  ok = run_external_command(command_env, tools[:flutter], *install_args, chdir: client_dir, unbundled: true)
  ok ? 0 : 1
end