tool.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. part of 'build_flowy.dart';
  2. enum _ScanMode {
  3. ignore,
  4. target,
  5. }
  6. enum _ModifyMode {
  7. include,
  8. exclude,
  9. }
  10. class _BuildTool {
  11. const _BuildTool({
  12. required this.repositoryRoot,
  13. required this.appVersion,
  14. });
  15. final String repositoryRoot;
  16. final String appVersion;
  17. String get projectRoot =>
  18. [repositoryRoot, 'appflowy_flutter'].join(Platform.pathSeparator);
  19. File get pubspec =>
  20. File([projectRoot, 'pubspec.yaml'].join(Platform.pathSeparator));
  21. Future<String> get _architecture async =>
  22. await Process.run('uname', ['-m']).then((value) => value.stdout.trim());
  23. Future<String> get _commandForOS async {
  24. // Check the operating system and CPU architecture
  25. var os = Platform.operatingSystem;
  26. var arch = Platform.isMacOS ? await _architecture : Platform.localHostname;
  27. // Determine the appropriate command based on the OS and architecture
  28. if (os == 'windows') {
  29. return 'cargo make --env APP_VERSION=$appVersion --profile production-windows-x86 appflowy';
  30. }
  31. if (os == 'linux') {
  32. return 'cargo make --env APP_VERSION=$appVersion --profile production-linux-x86_64 appflowy';
  33. }
  34. if (os == 'macos') {
  35. if (arch == 'x86_64') {
  36. return 'cargo make --env APP_VERSION=$appVersion --profile production-mac-x86_64 appflowy';
  37. }
  38. if (arch == 'arm64') {
  39. return 'cargo make --env APP_VERSION=$appVersion --profile production-mac-arm64 appflowy';
  40. }
  41. throw 'Unsupported CPU architecture: $arch';
  42. }
  43. throw 'Unsupported operating system: $os';
  44. }
  45. /// Scans a file for lines between # BEGIN: EXCLUDE_IN_RELEASE and
  46. /// END: EXCLUDE_IN_RELEASE. Will add a comment to remove those assets
  47. /// from the build.
  48. Future<void> _process_directives(
  49. File file, {
  50. required _ModifyMode mode,
  51. }) async {
  52. // Read the contents of the file into a list
  53. var lines = await file.readAsLines();
  54. // Find the lines between BEGIN: EXCLUDE_IN_RELEASE and END: EXCLUDE_IN_RELEASE
  55. var scanMode = _ScanMode.ignore;
  56. for (var i = 0; i < lines.length; i++) {
  57. var line = lines[i];
  58. if (line.contains(excludeTagBegin)) {
  59. scanMode = _ScanMode.target;
  60. } else if (line.contains(excludeTagEnd)) {
  61. scanMode = _ScanMode.ignore;
  62. } else if (scanMode == _ScanMode.target) {
  63. lines[i] = _modify(line, mode: mode);
  64. }
  65. }
  66. // Write the modified contents back to the file
  67. await file.writeAsString(lines.join('\n'));
  68. }
  69. String _modify(String line, {required _ModifyMode mode}) {
  70. switch (mode) {
  71. case _ModifyMode.include:
  72. return line.split('#').where((element) => element != '#').join();
  73. case _ModifyMode.exclude:
  74. return '#$line';
  75. }
  76. }
  77. Future<void> _build() async {
  78. final cwd = Directory.current;
  79. Directory.current = repositoryRoot;
  80. final cmd = await _commandForOS;
  81. // Run the command using the Process.run() function
  82. // final build = await Process.run('echo', ['hello'], runInShell: true);
  83. final build =
  84. await Process.start(cmd.split(' ')[0], cmd.split(' ').sublist(1));
  85. await stdout.addStream(build.stdout);
  86. await stderr.addStream(build.stderr);
  87. Directory.current = cwd;
  88. }
  89. Future<void> run() async {
  90. final pubspec = this.pubspec;
  91. await _process_directives(pubspec, mode: _ModifyMode.exclude);
  92. await _build();
  93. await _process_directives(pubspec, mode: _ModifyMode.include);
  94. }
  95. }