-
Notifications
You must be signed in to change notification settings - Fork 7
/
deploy_web.dart
73 lines (65 loc) · 1.83 KB
/
deploy_web.dart
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
// ignore_for_file: avoid_print
import 'dart:io';
void main() {
_log('Building flutter web');
final output = Process.runSync(
'flutter',
['build', 'web'],
runInShell: true,
);
_handleProcessResult(output);
// Extract remote url
final result = Process.runSync('git', ['remote', 'get-url', 'origin']);
final remoteUrl = _handleProcessResult(result);
if (remoteUrl == null || remoteUrl.isEmpty) {
throw Exception('No git remote');
}
_log('Remote result: $remoteUrl');
_cd('build/web');
try {
_handleProcessResult(
Process.runSync('git', ['init']),
);
_handleProcessResult(
Process.runSync('git', ['branch', '-m', 'web']),
);
_handleProcessResult(
Process.runSync('git', ['remote', 'add', 'web', remoteUrl]),
);
_handleProcessResult(
Process.runSync('git', ['add', '--all']),
);
_handleProcessResult(
Process.runSync('git', ['commit', '-m', 'Deploy web']),
);
_handleProcessResult(
Process.runSync('git', ['push', '-f', 'web', 'web']),
);
_log('Web deployed. Check your repository Actions page.');
} finally {
_rmDir('.git');
_cd('../..');
}
}
String? _handleProcessResult(ProcessResult result) {
final out = (result.stdout as String?)?.trim();
final err = (result.stdout as String?)?.trim();
if (result.exitCode == 0) {
_log('Output: $out');
} else {
_log('Error: $err');
throw Exception('Exit code: ${result.exitCode}\n$out\n$err\n');
}
return out;
}
void _log(String message) => print(message);
String _path(String path) => path.replaceAll('/', Platform.pathSeparator);
void _cd(String path) {
Directory.current = Directory(
'${Directory.current.path}${Platform.pathSeparator}${_path(path)}',
);
}
void _rmDir(String path) {
final dir = Directory(_path(path));
dir.deleteSync(recursive: true);
}