Repository navigation
Expand file tree
/
Copy pathrun.dart
More file actions
348 lines (320 loc) · 10.8 KB
/
Copy pathrun.dart
File metadata and controls
348 lines (320 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
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
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
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
161
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import 'dart:async';
import 'dart:io';
import 'scripts/android_emulator.dart';
const _defaultApiBaseUrl = 'http://10.0.2.2:8080/v1';
Future<void> main(List<String> args) async {
try {
final options = _parseArguments(args);
if (options.help) {
_printUsage();
return;
}
final projectRoot = File.fromUri(Platform.script).absolute.parent;
final backendRoot = Directory(
'${projectRoot.path}${Platform.pathSeparator}backend',
);
final env = await _readEnvFile(
File('${backendRoot.path}${Platform.pathSeparator}.env'),
);
final googleServerClientId = _resolveGoogleClientId(env);
final offlineAccessPublicKey = _resolveOfflineAccessPublicKey(env);
final apiBaseUrl =
options.apiBaseUrl ??
Platform.environment['API_BASE_URL'] ??
_defaultApiBaseUrl;
if (Uri.tryParse(apiBaseUrl)?.hasAuthority != true) {
throw const _LaunchException('API_BASE_URL tidak valid.');
}
final flutterDefines = <String>[
'--dart-define=API_BASE_URL=$apiBaseUrl',
'--dart-define=GOOGLE_SERVER_CLIENT_ID=$googleServerClientId',
if (offlineAccessPublicKey.isNotEmpty)
'--dart-define=OFFLINE_ACCESS_PUBLIC_KEY=$offlineAccessPublicKey',
];
final flutterArgs = <String>[
'run',
...flutterDefines,
...options.flutterArgs,
];
final selectedFlutterArgs = options.backendOnly
? flutterArgs
: <String>[
'run',
...flutterDefines,
...await prepareAndroidDeviceArgs(
projectRoot: projectRoot,
flutterArgs: options.flutterArgs,
),
];
Process? backend;
Directory? backendBuildDirectory;
try {
if (!options.skipBackend) {
final port = _backendPort(env);
stdout.writeln('Membangun backend Go ...');
backendBuildDirectory = await Directory.systemTemp.createTemp(
'nihongo-master-',
);
final executableName = Platform.isWindows
? 'nihongo-api.exe'
: 'nihongo-api';
final backendExecutable = File(
'${backendBuildDirectory.path}${Platform.pathSeparator}$executableName',
);
final build = await Process.start(
'go',
['build', '-o', backendExecutable.path, './cmd/api'],
workingDirectory: backendRoot.path,
runInShell: Platform.isWindows,
mode: ProcessStartMode.inheritStdio,
);
final buildExitCode = await build.exitCode;
if (buildExitCode != 0) {
throw _LaunchException(
'Build backend gagal (exit code $buildExitCode).',
);
}
stdout.writeln('Menjalankan backend di http://localhost:$port ...');
final backendEnvironment = Map<String, String>.of(Platform.environment)
..['GOOGLE_CLIENT_ID'] = googleServerClientId;
backend = await Process.start(
backendExecutable.path,
const [],
workingDirectory: backendRoot.path,
environment: backendEnvironment,
mode: ProcessStartMode.inheritStdio,
);
await _waitForBackend(backend, port);
stdout.writeln('Backend siap. Menjalankan Flutter ...');
}
if (options.backendOnly) {
stdout.writeln('Backend aktif. Tekan Ctrl+C untuk berhenti.');
exitCode = await backend!.exitCode;
return;
}
final flutter = await Process.start(
'flutter',
selectedFlutterArgs,
workingDirectory: projectRoot.path,
runInShell: Platform.isWindows,
mode: ProcessStartMode.inheritStdio,
);
exitCode = await flutter.exitCode;
} finally {
if (backend != null) {
backend.kill();
await backend.exitCode.timeout(
const Duration(seconds: 5),
onTimeout: () => -1,
);
}
if (backendBuildDirectory != null && backendBuildDirectory.existsSync()) {
await backendBuildDirectory.delete(recursive: true);
}
}
} on _LaunchException catch (error) {
stderr.writeln(error.message);
exitCode = 1;
} on ProcessException catch (error) {
stderr
..writeln('Tidak bisa menjalankan ${error.executable}.')
..writeln(
error.message.isNotEmpty
? error.message
: 'Pastikan Flutter SDK dan Go sudah terpasang serta ada di PATH.',
);
exitCode = 1;
} catch (error) {
stderr.writeln('Gagal menjalankan aplikasi: $error');
exitCode = 1;
}
}
Future<Map<String, String>> _readEnvFile(File file) async {
if (!file.existsSync()) {
throw const _LaunchException(
'File backend/.env tidak ditemukan. Buat file itu dari backend/.env.example lalu isi Google Web Client ID.',
);
}
final values = <String, String>{};
for (final rawLine in await file.readAsLines()) {
final line = rawLine.trim();
if (line.isEmpty || line.startsWith('#')) continue;
final separator = line.indexOf('=');
if (separator <= 0) continue;
final key = line.substring(0, separator).trim();
if (values.containsKey(key)) continue;
var value = line.substring(separator + 1).trim();
if (value.length >= 2 &&
((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'")))) {
value = value.substring(1, value.length - 1);
}
values[key] = value;
}
return values;
}
String _resolveGoogleClientId(Map<String, String> env) {
String configured(String key) {
final fromEnvironment = Platform.environment[key];
if (fromEnvironment != null && fromEnvironment.trim().isNotEmpty) {
return fromEnvironment;
}
return env[key] ?? '';
}
final serverClientId = configured('GOOGLE_SERVER_CLIENT_ID').trim();
final backendClientId = configured('GOOGLE_CLIENT_ID').trim();
if (serverClientId.isNotEmpty &&
backendClientId.isNotEmpty &&
serverClientId != backendClientId) {
throw const _LaunchException(
'GOOGLE_SERVER_CLIENT_ID dan GOOGLE_CLIENT_ID harus memakai Web Client ID yang sama di backend/.env.',
);
}
final clientId = serverClientId.isNotEmpty ? serverClientId : backendClientId;
if (!clientId.endsWith('.apps.googleusercontent.com')) {
throw const _LaunchException(
'Google Web Client ID tidak ditemukan atau formatnya salah di backend/.env.',
);
}
return clientId;
}
String _resolveOfflineAccessPublicKey(Map<String, String> env) {
final fromEnvironment = Platform.environment['OFFLINE_ACCESS_PUBLIC_KEY']
?.trim();
if (fromEnvironment != null && fromEnvironment.isNotEmpty) {
return fromEnvironment;
}
return env['OFFLINE_ACCESS_PUBLIC_KEY']?.trim() ?? '';
}
int _backendPort(Map<String, String> env) {
final environmentPort = Platform.environment['PORT'];
final rawPort = environmentPort == null || environmentPort.isEmpty
? env['PORT'] ?? '8080'
: environmentPort;
final port = int.tryParse(rawPort);
if (port == null || port < 1 || port > 65535) {
throw const _LaunchException(
'PORT di backend/.env harus berupa angka 1–65535.',
);
}
return port;
}
Future<void> _waitForBackend(Process backend, int port) async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 1);
final deadline = DateTime.now().add(const Duration(minutes: 2));
var backendFinished = false;
var backendExitCode = -1;
unawaited(
backend.exitCode.then((code) {
backendFinished = true;
backendExitCode = code;
}),
);
try {
while (DateTime.now().isBefore(deadline)) {
if (backendFinished) {
throw _LaunchException(
'Backend berhenti saat startup (exit code $backendExitCode).',
);
}
try {
final request = await client
.getUrl(Uri.parse('http://127.0.0.1:$port/healthz'))
.timeout(const Duration(seconds: 2));
final response = await request.close().timeout(
const Duration(seconds: 2),
);
await response.drain<void>();
if (response.statusCode == HttpStatus.ok) return;
} on SocketException {
// Backend may still be compiling or starting.
} on TimeoutException {
// Try again until startup deadline.
}
await Future<void>.delayed(const Duration(milliseconds: 500));
}
throw const _LaunchException(
'Backend belum siap setelah 2 menit. Periksa pesan error Go di terminal.',
);
} finally {
client.close(force: true);
}
}
_RunOptions _parseArguments(List<String> args) {
var help = false;
var skipBackend = false;
var backendOnly = false;
String? apiBaseUrl;
final flutterArgs = <String>[];
for (var index = 0; index < args.length; index++) {
final argument = args[index];
if (argument == '--help' || argument == '-h') {
help = true;
} else if (argument == '--no-backend') {
skipBackend = true;
} else if (argument == '--backend-only') {
backendOnly = true;
} else if (argument == '--api-base-url') {
if (index + 1 >= args.length) {
throw const _LaunchException('Isi URL setelah --api-base-url.');
}
apiBaseUrl = args[++index];
} else if (argument.startsWith('--api-base-url=')) {
apiBaseUrl = argument.substring('--api-base-url='.length);
} else {
flutterArgs.add(argument);
}
}
if (apiBaseUrl != null && Uri.tryParse(apiBaseUrl)?.hasAuthority != true) {
throw const _LaunchException('URL setelah --api-base-url tidak valid.');
}
if (skipBackend && backendOnly) {
throw const _LaunchException(
'--no-backend tidak bisa dipakai bersama --backend-only.',
);
}
return _RunOptions(
help: help,
skipBackend: skipBackend,
backendOnly: backendOnly,
apiBaseUrl: apiBaseUrl,
flutterArgs: flutterArgs,
);
}
void _printUsage() {
stdout.writeln('''
Jalankan dari folder utama project:
dart run.dart [opsi launcher] [opsi flutter run]
Contoh:
dart run.dart
dart run.dart -d emulator-5554
dart run.dart --api-base-url http://192.168.1.20:8080/v1 -d <DEVICE_ID>
Jika tidak ada perangkat Android aktif, launcher menyalakan AVD pertama.
Pilih AVD tertentu dengan ID dari `flutter emulators`, misalnya:
dart run.dart -d Pixel_8_API_35
Opsi launcher:
--api-base-url URL Alamat backend yang dipakai aplikasi
--no-backend Backend sudah berjalan di terminal lain
--backend-only Jalankan backend saja
-d, --device-id Pilih perangkat atau AVD Android
-h, --help Tampilkan bantuan
''');
}
class _RunOptions {
const _RunOptions({
required this.help,
required this.skipBackend,
required this.backendOnly,
required this.apiBaseUrl,
required this.flutterArgs,
});
final bool help;
final bool skipBackend;
final bool backendOnly;
final String? apiBaseUrl;
final List<String> flutterArgs;
}
class _LaunchException implements Exception {
const _LaunchException(this.message);
final String message;
}