tool.dart 3.4 KB

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