Module: Ruflet::CLI::BuildCommand

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

Constant Summary collapse

CLIENT_EXTENSION_MAP =
{
  "ads" => { package: "flet_ads", alias: "ruflet_ads" },
  "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" },
  "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

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



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
67
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
# File 'lib/ruflet/cli/build_command.rb', line 34

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|ios|aab|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

  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?(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
  else
    unless backend_url
      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 += ["--dart-define", "RUFLET_BACKEND_URL=#{backend_url}"]
  end
  build_args << "-v" if verbose

  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



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
# File 'lib/ruflet/cli/build_command.rb', line 100

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