| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  | part of 'build_flowy.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum _ScanMode { | 
					
						
							|  |  |  |   ignore, | 
					
						
							|  |  |  |   target, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  | enum ModifyMode { | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   include, | 
					
						
							|  |  |  |   exclude, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  | class BuildTool { | 
					
						
							|  |  |  |   const BuildTool({ | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |     required this.repositoryRoot, | 
					
						
							|  |  |  |     required this.appVersion, | 
					
						
							| 
									
										
										
										
											2023-05-15 11:18:08 +08:00
										 |  |  |     this.arch, | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final String repositoryRoot; | 
					
						
							|  |  |  |   final String appVersion; | 
					
						
							| 
									
										
										
										
											2023-05-15 11:18:08 +08:00
										 |  |  |   final String? arch; | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  | 
 | 
					
						
							|  |  |  |   String get projectRoot => | 
					
						
							|  |  |  |       [repositoryRoot, 'appflowy_flutter'].join(Platform.pathSeparator); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   File get pubspec => | 
					
						
							|  |  |  |       File([projectRoot, 'pubspec.yaml'].join(Platform.pathSeparator)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<String> get _architecture async => | 
					
						
							|  |  |  |       await Process.run('uname', ['-m']).then((value) => value.stdout.trim()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<String> get _commandForOS async { | 
					
						
							|  |  |  |     // Check the operating system and CPU architecture
 | 
					
						
							| 
									
										
										
										
											2023-05-15 11:18:08 +08:00
										 |  |  |     final os = Platform.operatingSystem; | 
					
						
							|  |  |  |     final arch = this.arch ?? | 
					
						
							|  |  |  |         (Platform.isMacOS ? await _architecture : Platform.localHostname); | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Determine the appropriate command based on the OS and architecture
 | 
					
						
							|  |  |  |     if (os == 'windows') { | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |       return 'cargo make --env APP_VERSION=$appVersion --profile production-windows-x86 appflowy --verbose'; | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (os == 'linux') { | 
					
						
							|  |  |  |       return 'cargo make --env APP_VERSION=$appVersion --profile production-linux-x86_64 appflowy'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (os == 'macos') { | 
					
						
							|  |  |  |       if (arch == 'x86_64') { | 
					
						
							|  |  |  |         return 'cargo make --env APP_VERSION=$appVersion --profile production-mac-x86_64 appflowy'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (arch == 'arm64') { | 
					
						
							|  |  |  |         return 'cargo make --env APP_VERSION=$appVersion --profile production-mac-arm64 appflowy'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       throw 'Unsupported CPU architecture: $arch'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     throw 'Unsupported operating system: $os'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Scans a file for lines between # BEGIN: EXCLUDE_IN_RELEASE and
 | 
					
						
							|  |  |  |   /// END: EXCLUDE_IN_RELEASE. Will add a comment to remove those assets
 | 
					
						
							|  |  |  |   /// from the build.
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |   Future<void> process_directives( | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |     File file, { | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |     required ModifyMode mode, | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   }) async { | 
					
						
							|  |  |  |     // Read the contents of the file into a list
 | 
					
						
							|  |  |  |     var lines = await file.readAsLines(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Find the lines between BEGIN: EXCLUDE_IN_RELEASE and END: EXCLUDE_IN_RELEASE
 | 
					
						
							|  |  |  |     var scanMode = _ScanMode.ignore; | 
					
						
							|  |  |  |     for (var i = 0; i < lines.length; i++) { | 
					
						
							|  |  |  |       var line = lines[i]; | 
					
						
							|  |  |  |       if (line.contains(excludeTagBegin)) { | 
					
						
							|  |  |  |         scanMode = _ScanMode.target; | 
					
						
							|  |  |  |       } else if (line.contains(excludeTagEnd)) { | 
					
						
							|  |  |  |         scanMode = _ScanMode.ignore; | 
					
						
							|  |  |  |       } else if (scanMode == _ScanMode.target) { | 
					
						
							|  |  |  |         lines[i] = _modify(line, mode: mode); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Write the modified contents back to the file
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |     await file.writeAsString(lines.join('\n'), flush: true); | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |   String _modify(String line, {required ModifyMode mode}) { | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |     switch (mode) { | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |       case ModifyMode.include: | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |         return line.split('#').where((element) => element != '#').join(); | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |       case ModifyMode.exclude: | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |         return '#$line'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |   Future<void> build() async { | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |     final cwd = Directory.current; | 
					
						
							|  |  |  |     Directory.current = repositoryRoot; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     final cmd = await _commandForOS; | 
					
						
							|  |  |  |     // Run the command using the Process.run() function
 | 
					
						
							|  |  |  |     // final build = await Process.run('echo', ['hello'], runInShell: true);
 | 
					
						
							|  |  |  |     final build = | 
					
						
							|  |  |  |         await Process.start(cmd.split(' ')[0], cmd.split(' ').sublist(1)); | 
					
						
							|  |  |  |     await stdout.addStream(build.stdout); | 
					
						
							|  |  |  |     await stderr.addStream(build.stderr); | 
					
						
							|  |  |  |     Directory.current = cwd; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |   Future<void> directives(ModifyMode mode) async { | 
					
						
							|  |  |  |     await process_directives(pubspec, mode: mode); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   Future<void> run() async { | 
					
						
							|  |  |  |     final pubspec = this.pubspec; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-17 06:03:29 -07:00
										 |  |  |     await process_directives(pubspec, mode: ModifyMode.exclude); | 
					
						
							|  |  |  |     await build(); | 
					
						
							|  |  |  |     await process_directives(pubspec, mode: ModifyMode.include); | 
					
						
							| 
									
										
										
										
											2023-04-03 18:50:22 -10:00
										 |  |  |   } | 
					
						
							|  |  |  | } |