Class: FastlaneFlutterFlavor::SetupLanes

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_setup.rb

Overview

Helper class containing maintenance and environment setup logic

Instance Method Summary collapse

Constructor Details

#initialize(root_folder:, status_manager:) ⇒ SetupLanes

(upgrade_setup and cleanup) are inherently cross-platform.



8
9
10
11
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_setup.rb', line 8

def initialize(root_folder:, status_manager:)
  @root_folder = root_folder
  @status_manager = status_manager
end

Instance Method Details

#cleanup(force: false) ⇒ Object


Core Cleanup




107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_setup.rb', line 107

def cleanup(force: false)
  begin
    Fastlane::UI.message("🧹 Cleaning build artifacts...")

    # 1. Standard Flutter clean (Root task)
    Fastlane::UI.message("-> Running 'flutter clean'...")
    Dir.chdir @root_folder do
        Fastlane::Actions::sh("flutter", "clean")
    end

    if force
      Fastlane::UI.message("-> Performing aggressive cleanup (deleting platform-specific build folders)...")
      # 2. Aggressive cleanup of general Flutter build folder and platform-specific folders
      dirs_to_delete = [
        "build", # General Flutter build
        "android/.gradle",
        "android/build",
        "ios/Pods",
        "ios/Podfile.lock", # Crucial for forcing a fresh dependency graph on next install
        "ios/build",
        "web/build"
      ]

      # Change directory temporarily to simplify paths for deletion
      Dir.chdir @root_folder do
        dirs_to_delete.each do |dir|
          full_path = File.join(dir)

          # Only delete if the file/directory exists
          if File.exist?(full_path)
            Fastlane::UI.message("Deleting artifact: #{dir}")
            FileUtils.rm_rf(full_path)
            Fastlane::UI.success("Successfully deleted: #{dir}")
          end
        end
      end
      Fastlane::UI.success("Successfully performed aggressive cleanup.")
    else
        Fastlane::UI.important("Skipped aggressive deletion of build directories and native lock files. Pass 'force: true' to perform full cleanup.")
    end

    # Pass nil for platform since this is a cross-platform action
    @status_manager.logSuccess nil, "cleanup", nil
    return true
  rescue => e
    Fastlane::UI.error "❌ Error during cleanup of build artifacts: #{e}"
    Fastlane::UI.error e
    # Pass nil for platform since this is a cross-platform action
    @status_manager.logError nil, "cleanup", nil, e
    return false
  end
end

#upgrade_setup(force: false) ⇒ Object


Core Maintenance and Setup




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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fastlane/plugin/ann_flutter_flavor/helper/lanes_setup.rb', line 17

def upgrade_setup(force: false)
  begin
    Fastlane::UI.message("🚀 Starting maintenance and environment checks...")

    # 1. Update Flutter SDK
    Fastlane::UI.message("-> Step 1: Running 'flutter upgrade' to update SDK.")
    Fastlane::Actions::sh("flutter", "upgrade")

    # 2. Aggressive Cleanup: Delete lock files and call cleanup lane to remove build artifacts
    if force
        Fastlane::UI.message("-> Step 2: Running aggressive pre-resolution cleanup (deleting lock files and build artifacts)...")
        Dir.chdir @root_folder do
            pubspec_lock_path = File.join(@root_folder, "pubspec.lock")
            gemfile_lock_path = File.join(@root_folder, "Gemfile.lock")

            # Delete Dart lock file
            if File.exist?(pubspec_lock_path)
                Fastlane::UI.message("   -> Deleting pubspec.lock to force full Flutter dependency re-resolution.")
                FileUtils.rm_f(pubspec_lock_path)
            end

            # Delete Ruby lock file
            if File.exist?(gemfile_lock_path)
                Fastlane::UI.message("   -> Deleting Gemfile.lock to force full Ruby dependency re-resolution.")
                FileUtils.rm_f(gemfile_lock_path)
            end
        end

        # Call cleanup which, with force: true, deletes ios/Podfile.lock, ios/Pods, and all build folders
        cleanup(force: force)
    end

    # 3. Resolve Dependencies: Ruby first, then Flutter (Root task)
    Dir.chdir @root_folder do
      Fastlane::UI.message("-> Step 3: Resolving Ruby and Dart dependencies...")

      # Resolve Ruby dependencies (Gemfile.lock is regenerated here)
      bundle_install_command = ["bundle", "install"]
      bundle_update_command = ["bundle", "update"]
      bundle_install_command << "--force" if force
      bundle_update_command << "--force" if force

      Fastlane::UI.message("   -> Running 'bundle install' and 'bundle update'#{force ? ' (with --force)' : ''}...")
      Fastlane::Actions::sh(*bundle_install_command)
      Fastlane::Actions::sh(*bundle_update_command)

      # Resolve Dart dependencies (pubspec.lock is regenerated here)
      Fastlane::UI.message("   -> Running 'flutter pub get' to re-resolve Dart dependencies.")
      Fastlane::Actions::sh("flutter", "pub", "get")
    end

    # 4. Resolve Native iOS Dependencies
    ios_dir = File.join(@root_folder, "ios")
    if File.directory?(ios_dir)
      Fastlane::UI.message("-> Step 4: Running 'pod install' in the 'ios' directory to ensure native dependencies are linked.")

      # Use --repo-update when force is true to fix "CocoaPods could not find compatible versions" errors.
      pod_command = ["bundle", "exec", "pod", "install"]
      if force
        pod_command << "--repo-update"
        Fastlane::UI.message("   -> Adding '--repo-update' flag to pod install for aggressive source update.")
      end

      Dir.chdir ios_dir do
        Fastlane::Actions::sh(*pod_command)
      end
    else
      Fastlane::UI.important("   -> Skipped 'pod install': 'ios' directory not found (likely not an iOS-enabled project).")
    end

    # 5. Doctor check (Root task)
    Fastlane::UI.message("-> Step 5: Running 'flutter doctor' for final verification.")
    Fastlane::Actions::sh("flutter", "doctor")

    # Pass nil for platform since this is a cross-platform action
    @status_manager.logSuccess nil, "upgrade_setup", nil
    return true
  rescue => e
    Fastlane::UI.error "❌ Error during environment setup or upgrade: #{e}"
    Fastlane::UI.error e
    # Pass nil for platform since this is a cross-platform action
    @status_manager.logError nil, "upgrade_setup", nil, e
    return false
  end
end