Class: Mysigner::Build::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/mysigner/build/detector.rb

Defined Under Namespace

Classes: NoProjectError

Class Method Summary collapse

Class Method Details

.detect(directory = Dir.pwd, platform: nil) ⇒ Object

Detect project in directory Returns: { platform: :ios/:android, type: :workspace/:project/:gradle, path: String, framework: :capacitor/:react_native/:flutter/:native }

Parameters:

  • directory (String) (defaults to: Dir.pwd)

    Directory to search in

  • platform (Symbol, nil) (defaults to: nil)

    Force detection for specific platform (:ios, :android, or nil for auto-detect iOS)



12
13
14
15
16
17
18
# File 'lib/mysigner/build/detector.rb', line 12

def self.detect(directory = Dir.pwd, platform: nil)
  # If platform is explicitly android, detect android
  return detect_android(directory) if platform == :android

  # Default behavior: detect iOS (backwards compatible)
  detect_ios(directory)
end

.detect_android(directory = Dir.pwd) ⇒ Object

Detect Android project in directory Returns: { platform: :android, type: :gradle, path: String, framework: :capacitor/:react_native/:flutter/:native }

Raises:



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
102
103
104
105
106
107
108
109
110
111
# File 'lib/mysigner/build/detector.rb', line 22

def self.detect_android(directory = Dir.pwd)
  # 1. Check for Capacitor (most specific first)
  if File.exist?("#{directory}/capacitor.config.json") ||
     File.exist?("#{directory}/capacitor.config.ts")
    return detect_capacitor_android(directory)
  end

  # 2. Check for Expo (managed workflow - no android folder)
  if (File.exist?("#{directory}/app.json") || File.exist?("#{directory}/app.config.js")) &&
     File.exist?("#{directory}/package.json")
    content = File.read("#{directory}/package.json")
    if content.include?('expo') && !Dir.exist?("#{directory}/android")
      # Auto-run expo prebuild
      puts "\nšŸ“¦ Expo managed workflow detected (no android/ folder)"
      puts "šŸ”§ Running: npx expo prebuild --platform android\n\n"

      result = system("cd #{directory} && npx expo prebuild --platform android")

      unless result && Dir.exist?("#{directory}/android")
        raise NoProjectError, <<~ERROR
          Failed to generate Android project with expo prebuild.

          Try running manually:
            npx expo prebuild --platform android

          Alternative: Use EAS Build (Expo's cloud service)
          Learn more: https://docs.expo.dev/bare/overview/
        ERROR
      end

      puts "\nāœ“ Android project generated successfully\n\n"
    end
  end

  # 3. Check for React Native
  if File.exist?("#{directory}/package.json") && Dir.exist?("#{directory}/android")
    content = File.read("#{directory}/package.json")
    return detect_react_native_android(directory) if content.include?('react-native')
  end

  # 3. Check for Flutter
  return detect_flutter_android(directory) if File.exist?("#{directory}/pubspec.yaml")

  # 4. Check for .NET MAUI / Xamarin
  maui_project = Dir.glob("#{directory}/*.csproj").first
  if maui_project
    content = File.read(maui_project)
    if content.include?('Maui') || content.include?('Xamarin.Forms') || content.include?('Xamarin.Android')
      return detect_dotnet_android(directory, maui_project)
    end
  end

  # 5. Check for native Android project
  # Look for app/build.gradle (standard Android project structure)
  app_gradle = "#{directory}/app/build.gradle"
  app_gradle_kts = "#{directory}/app/build.gradle.kts"
  root_gradle = "#{directory}/build.gradle"
  root_gradle_kts = "#{directory}/build.gradle.kts"

  if File.exist?(app_gradle) || File.exist?(app_gradle_kts)
    return {
      platform: :android,
      type: :gradle,
      path: File.absolute_path(directory),
      framework: :native,
      directory: directory,
      android_directory: directory,
      app_build_gradle: File.exist?(app_gradle) ? app_gradle : app_gradle_kts
    }
  end

  # Check for single-module project (build.gradle at root with android block)
  if File.exist?(root_gradle) || File.exist?(root_gradle_kts)
    gradle_file = File.exist?(root_gradle) ? root_gradle : root_gradle_kts
    content = File.read(gradle_file)
    if content.include?('android {') || content.include?('android{')
      return {
        platform: :android,
        type: :gradle,
        path: File.absolute_path(directory),
        framework: :native,
        directory: directory,
        android_directory: directory,
        app_build_gradle: gradle_file
      }
    end
  end

  raise NoProjectError, "No Android project found in #{directory}. Run in an Android project directory."
end

.detect_capacitor(directory) ⇒ Object

iOS detection for cross-platform frameworks (existing methods)

Raises:



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/mysigner/build/detector.rb', line 282

def self.detect_capacitor(directory)
  ios_dir = "#{directory}/ios/App"

  # Capacitor always creates App.xcworkspace
  workspace = "#{ios_dir}/App.xcworkspace"
  project = "#{ios_dir}/App.xcodeproj"

  if File.exist?(workspace)
    return {
      platform: :ios,
      type: :workspace,
      path: File.absolute_path(workspace),
      framework: :capacitor,
      directory: directory,
      ios_directory: ios_dir
    }
  elsif File.exist?(project)
    return {
      platform: :ios,
      type: :project,
      path: File.absolute_path(project),
      framework: :capacitor,
      directory: directory,
      ios_directory: ios_dir
    }
  end

  raise NoProjectError, "Capacitor project detected but no Xcode project found. Run 'npx cap sync ios' first."
end

.detect_capacitor_android(directory) ⇒ Object

Android detection for cross-platform frameworks

Raises:



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mysigner/build/detector.rb', line 186

def self.detect_capacitor_android(directory)
  android_dir = "#{directory}/android"

  unless Dir.exist?(android_dir)
    raise NoProjectError,
          "Capacitor project detected but no android/ folder found. Run 'npx cap add android' first."
  end

  app_gradle = "#{android_dir}/app/build.gradle"
  app_gradle_kts = "#{android_dir}/app/build.gradle.kts"

  if File.exist?(app_gradle) || File.exist?(app_gradle_kts)
    return {
      platform: :android,
      type: :gradle,
      path: File.absolute_path(android_dir),
      framework: :capacitor,
      directory: directory,
      android_directory: android_dir,
      app_build_gradle: File.exist?(app_gradle) ? app_gradle : app_gradle_kts
    }
  end

  raise NoProjectError,
        "Capacitor project detected but no Android build.gradle found. Run 'npx cap sync android' first."
end

.detect_dotnet_android(directory, csproj_path) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/mysigner/build/detector.rb', line 258

def self.detect_dotnet_android(directory, csproj_path)
  content = File.read(csproj_path)

  framework_type = if content.include?('Maui')
                     :maui
                   elsif content.include?('Xamarin.Forms')
                     :xamarin_forms
                   else
                     :xamarin
                   end

  {
    platform: :android,
    type: :dotnet,
    path: File.absolute_path(csproj_path),
    framework: framework_type,
    directory: directory,
    android_directory: directory,
    csproj_path: csproj_path
  }
end

.detect_flutter(directory) ⇒ Object

Raises:



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/mysigner/build/detector.rb', line 344

def self.detect_flutter(directory)
  ios_dir = "#{directory}/ios"

  # Flutter typically uses Runner.xcworkspace
  workspace = "#{ios_dir}/Runner.xcworkspace"
  if File.exist?(workspace)
    return {
      platform: :ios,
      type: :workspace,
      path: File.absolute_path(workspace),
      framework: :flutter,
      directory: directory,
      ios_directory: ios_dir
    }
  end

  # Fall back to Runner.xcodeproj
  project = "#{ios_dir}/Runner.xcodeproj"
  if File.exist?(project)
    return {
      platform: :ios,
      type: :project,
      path: File.absolute_path(project),
      framework: :flutter,
      directory: directory,
      ios_directory: ios_dir
    }
  end

  raise NoProjectError, "Flutter project detected but no Xcode project found. Run 'flutter build ios' first."
end

.detect_flutter_android(directory) ⇒ Object

Raises:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/mysigner/build/detector.rb', line 234

def self.detect_flutter_android(directory)
  android_dir = "#{directory}/android"

  raise NoProjectError, "Flutter project detected but no android/ folder found. Run 'flutter create .' first." unless Dir.exist?(android_dir)

  app_gradle = "#{android_dir}/app/build.gradle"
  app_gradle_kts = "#{android_dir}/app/build.gradle.kts"

  if File.exist?(app_gradle) || File.exist?(app_gradle_kts)
    return {
      platform: :android,
      type: :gradle,
      path: File.absolute_path(android_dir),
      framework: :flutter,
      directory: directory,
      android_directory: android_dir,
      app_build_gradle: File.exist?(app_gradle) ? app_gradle : app_gradle_kts
    }
  end

  raise NoProjectError,
        "Flutter project detected but no Android build.gradle found. Run 'flutter build apk' first."
end

.detect_ios(directory = Dir.pwd) ⇒ Object

Detect iOS project in directory (original detect behavior)

Raises:



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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mysigner/build/detector.rb', line 114

def self.detect_ios(directory = Dir.pwd)
  # 1. Check for Capacitor (most specific first)
  if File.exist?("#{directory}/capacitor.config.json") ||
     File.exist?("#{directory}/capacitor.config.ts")
    return detect_capacitor(directory)
  end

  # 2. Check for Expo (managed workflow)
  if (File.exist?("#{directory}/app.json") || File.exist?("#{directory}/app.config.js")) &&
     File.exist?("#{directory}/package.json")
    content = File.read("#{directory}/package.json")
    if content.include?('expo') && !Dir.exist?("#{directory}/ios")
      # Auto-run expo prebuild
      puts "\nšŸ“¦ Expo managed workflow detected (no ios/ folder)"
      puts "šŸ”§ Running: npx expo prebuild --platform ios\n\n"

      result = system("cd #{directory} && npx expo prebuild --platform ios")

      unless result && Dir.exist?("#{directory}/ios")
        raise NoProjectError, <<~ERROR
          Failed to generate iOS project with expo prebuild.

          Try running manually:
            npx expo prebuild --platform ios

          Alternative: Use EAS Build (Expo's cloud service)
          Learn more: https://docs.expo.dev/bare/overview/
        ERROR
      end

      puts "\nāœ“ iOS project generated successfully\n\n"
    end
  end

  # 3. Check for React Native
  if File.exist?("#{directory}/package.json") && Dir.exist?("#{directory}/ios")
    content = File.read("#{directory}/package.json")
    return detect_react_native(directory) if content.include?('react-native')
  end

  # 4. Check for Flutter
  return detect_flutter(directory) if File.exist?("#{directory}/pubspec.yaml")

  # 5. Check for native iOS project (workspace first, then project)
  workspace = Dir.glob("#{directory}/*.xcworkspace").first
  if workspace
    return {
      platform: :ios,
      type: :workspace,
      path: File.absolute_path(workspace),
      framework: :native,
      directory: directory
    }
  end

  project = Dir.glob("#{directory}/*.xcodeproj").first
  if project
    return {
      platform: :ios,
      type: :project,
      path: File.absolute_path(project),
      framework: :native,
      directory: directory
    }
  end

  raise NoProjectError,
        "No Xcode project found in #{directory}. Run in a project directory or try 'mysigner init' first."
end

.detect_react_native(directory) ⇒ Object

Raises:



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/mysigner/build/detector.rb', line 312

def self.detect_react_native(directory)
  ios_dir = "#{directory}/ios"

  # Look for workspace first (CocoaPods)
  workspace = Dir.glob("#{ios_dir}/*.xcworkspace").first
  if workspace
    return {
      platform: :ios,
      type: :workspace,
      path: File.absolute_path(workspace),
      framework: :react_native,
      directory: directory,
      ios_directory: ios_dir
    }
  end

  # Fall back to project
  project = Dir.glob("#{ios_dir}/*.xcodeproj").first
  if project
    return {
      platform: :ios,
      type: :project,
      path: File.absolute_path(project),
      framework: :react_native,
      directory: directory,
      ios_directory: ios_dir
    }
  end

  raise NoProjectError, 'React Native project detected but no Xcode project found in ios/ directory.'
end

.detect_react_native_android(directory) ⇒ Object

Raises:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mysigner/build/detector.rb', line 213

def self.detect_react_native_android(directory)
  android_dir = "#{directory}/android"

  app_gradle = "#{android_dir}/app/build.gradle"
  app_gradle_kts = "#{android_dir}/app/build.gradle.kts"

  if File.exist?(app_gradle) || File.exist?(app_gradle_kts)
    return {
      platform: :android,
      type: :gradle,
      path: File.absolute_path(android_dir),
      framework: :react_native,
      directory: directory,
      android_directory: android_dir,
      app_build_gradle: File.exist?(app_gradle) ? app_gradle : app_gradle_kts
    }
  end

  raise NoProjectError, 'React Native project detected but no Android build.gradle found in android/ directory.'
end