Browse Source

fix: windows release build (#3168)

* refactor: build tool

* chore: update release.yml

* chore: skip creating release if already created
Alex Wallen 1 year ago
parent
commit
103f56922f

+ 6 - 2
.github/workflows/release.yml

@@ -26,6 +26,7 @@ jobs:
           cat CHANGELOG.md | sed -e '/./{H;$!d;}' -e "x;/##\ Version\ ${{ github.ref_name }}/"'!d;' >> ${{ env.RELEASE_NOTES_PATH }}
 
       - name: Create release
+        if: github.run_number == 1
         id: create_release
         uses: actions/create-release@v1
         env:
@@ -82,9 +83,12 @@ jobs:
 
       - name: Build Windows app
         working-directory: frontend
+        # the cargo make script has to be run separately because of file locking issues
         run: |
           flutter config --enable-windows-desktop
+          dart ./scripts/flutter_release_build/build_flowy.dart exclude-directives . ${{ github.ref_name }}
           cargo make --env APP_VERSION=${{ github.ref_name }} --profile production-windows-x86 appflowy
+          dart ./scripts/flutter_release_build/build_flowy.dart include-directives . ${{ github.ref_name }}
 
       - name: Archive Asset
         uses: vimtor/action-zip@v1
@@ -172,7 +176,7 @@ jobs:
         working-directory: frontend
         run: |
           flutter config --enable-macos-desktop
-          dart ./scripts/flutter_release_build/build_flowy.dart . ${{ github.ref_name }}
+          dart ./scripts/flutter_release_build/build_flowy.dart run . ${{ github.ref_name }}
 
       - name: Codesign AppFlowy
         run: |
@@ -397,7 +401,7 @@ jobs:
         working-directory: frontend
         run: |
           flutter config --enable-linux-desktop
-          dart ./scripts/flutter_release_build/build_flowy.dart . ${{ github.ref_name }}
+          dart ./scripts/flutter_release_build/build_flowy.dart run . ${{ github.ref_name }}
 
       - name: Archive Assert
         working-directory: ${{ env.LINUX_APP_RELEASE_PATH }}

+ 39 - 12
frontend/scripts/flutter_release_build/build_flowy.dart

@@ -9,25 +9,52 @@ Future<void> main(List<String> args) async {
   const help = '''
 A build script that modifies build assets before building the release version of AppFlowy.
 
-args[0]: The directory that contains the AppFlowy git repository. Should be the parent to appflowy_flutter. (absolute path)
-args[1]: The appflowy version to be built (github ref_name).
+args[0] (required): The subcommand to use (build, include-directives, exclude-directives, run).
+  - run: calls exclude-directives, build, include-directives.
+  - build: builds the release version of AppFlowy.
+  - include-directives: adds the directives from pubspec.yaml.
+  - exclude-directives: removes the directives from pubspec.yaml.
+
+args[1] (required): The repository root for appflowy (the directory containing pubspec.yaml).
+
+args[2] (required): version (only relevant for build). The version of the app to build.
+
 ''';
-  const numArgs = 2;
+  const numArgs = 3;
   assert(args.length == numArgs,
       'Expected ${numArgs}, got ${args.length}. Read the following for instructions about how to use this script.\n\n$help');
   if (args[0] == '-h' || args[0] == '--help') {
     stdout.write(help);
     stdout.flush();
   }
-  final repositoryRoot = Directory(args[0]);
+
+  // parse the vesrion
+  final version = args[2];
+
+  // parse the first required argument
+  final repositoryRoot = Directory(args[1]);
   assert(await repositoryRoot.exists(),
       '$repositoryRoot is an invalid directory. Please try again with a valid directory.\n\n$help');
-  final appVersion = args[1];
-  String? arch;
-  if (args.length > 2) arch = args[2];
-  await _BuildTool(
-    repositoryRoot: repositoryRoot.path,
-    appVersion: appVersion,
-    arch: arch,
-  ).run();
+
+  // parse the command
+  final command = args[0];
+  final tool =
+      BuildTool(repositoryRoot: repositoryRoot.path, appVersion: version);
+
+  switch (command) {
+    case 'run':
+      await tool.run();
+      break;
+    case 'build':
+      await tool.build();
+      break;
+    case 'include-directives':
+      await tool.directives(ModifyMode.include);
+      break;
+    case 'exclude-directives':
+      await tool.directives(ModifyMode.exclude);
+      break;
+    default:
+      throw StateError('Invalid command: $command');
+  }
 }

+ 18 - 14
frontend/scripts/flutter_release_build/tool.dart

@@ -5,13 +5,13 @@ enum _ScanMode {
   target,
 }
 
-enum _ModifyMode {
+enum ModifyMode {
   include,
   exclude,
 }
 
-class _BuildTool {
-  const _BuildTool({
+class BuildTool {
+  const BuildTool({
     required this.repositoryRoot,
     required this.appVersion,
     this.arch,
@@ -38,7 +38,7 @@ class _BuildTool {
 
     // Determine the appropriate command based on the OS and architecture
     if (os == 'windows') {
-      return 'cargo make --env APP_VERSION=$appVersion --profile production-windows-x86 appflowy';
+      return 'cargo make --env APP_VERSION=$appVersion --profile production-windows-x86 appflowy --verbose';
     }
 
     if (os == 'linux') {
@@ -61,9 +61,9 @@ class _BuildTool {
   /// 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.
-  Future<void> _process_directives(
+  Future<void> process_directives(
     File file, {
-    required _ModifyMode mode,
+    required ModifyMode mode,
   }) async {
     // Read the contents of the file into a list
     var lines = await file.readAsLines();
@@ -82,19 +82,19 @@ class _BuildTool {
     }
 
     // Write the modified contents back to the file
-    await file.writeAsString(lines.join('\n'));
+    await file.writeAsString(lines.join('\n'), flush: true);
   }
 
-  String _modify(String line, {required _ModifyMode mode}) {
+  String _modify(String line, {required ModifyMode mode}) {
     switch (mode) {
-      case _ModifyMode.include:
+      case ModifyMode.include:
         return line.split('#').where((element) => element != '#').join();
-      case _ModifyMode.exclude:
+      case ModifyMode.exclude:
         return '#$line';
     }
   }
 
-  Future<void> _build() async {
+  Future<void> build() async {
     final cwd = Directory.current;
     Directory.current = repositoryRoot;
 
@@ -108,11 +108,15 @@ class _BuildTool {
     Directory.current = cwd;
   }
 
+  Future<void> directives(ModifyMode mode) async {
+    await process_directives(pubspec, mode: mode);
+  }
+
   Future<void> run() async {
     final pubspec = this.pubspec;
 
-    await _process_directives(pubspec, mode: _ModifyMode.exclude);
-    await _build();
-    await _process_directives(pubspec, mode: _ModifyMode.include);
+    await process_directives(pubspec, mode: ModifyMode.exclude);
+    await build();
+    await process_directives(pubspec, mode: ModifyMode.include);
   }
 }