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, allow_prebuild: true) ⇒ 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)

  • allow_prebuild (Boolean) (defaults to: true)

    when false (doctor/validate and other read-only callers), an Expo managed project with no native folder is CLASSIFIED (framework: :expo, needs_prebuild: true) instead of having ‘npx expo prebuild` run against it. Detection must never mutate the working tree from a diagnostic path.



19
20
21
22
23
24
25
# File 'lib/mysigner/build/detector.rb', line 19

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

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

.detect_android(directory = Dir.pwd, allow_prebuild: true) ⇒ Object

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

Raises:



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
183
184
185
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
212
213
214
# File 'lib/mysigner/build/detector.rb', line 141

def self.detect_android(directory = Dir.pwd, allow_prebuild: true)
  # 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. Expo managed workflow (app.json/app.config.js + `expo` dependency,
  #    no android/ yet). Read-only callers pass allow_prebuild: false and
  #    get a non-mutating classification instead of a prebuild + possible
  #    raise.
  if expo_managed?(directory) && !Dir.exist?("#{directory}/android")
    return expo_managed_result(directory, :android) unless allow_prebuild

    run_expo_prebuild!(directory, :android)
    puts "\n✓ Android project generated successfully\n\n"
  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:



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/mysigner/build/detector.rb', line 367

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:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mysigner/build/detector.rb', line 271

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



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/mysigner/build/detector.rb', line 343

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:



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/mysigner/build/detector.rb', line 429

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:



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

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, allow_prebuild: true) ⇒ Object

Detect iOS project in directory (original detect behavior)

Raises:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/mysigner/build/detector.rb', line 217

def self.detect_ios(directory = Dir.pwd, allow_prebuild: true)
  # 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. Expo managed workflow (see detect_android for the allow_prebuild
  #    contract).
  if expo_managed?(directory) && !Dir.exist?("#{directory}/ios")
    return expo_managed_result(directory, :ios) unless allow_prebuild

    run_expo_prebuild!(directory, :ios)
    puts "\n✓ iOS project generated successfully\n\n"
  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_package_manager(directory) ⇒ Object

Detect the JS package manager from the lockfile so we suggest the EXACT install command for this project. Running the wrong one (e.g. ‘npm install` on a yarn/pnpm project) can corrupt the dependency tree, so we never guess blindly.



113
114
115
116
117
118
119
# File 'lib/mysigner/build/detector.rb', line 113

def self.detect_package_manager(directory)
  return :yarn if File.exist?("#{directory}/yarn.lock")
  return :pnpm if File.exist?("#{directory}/pnpm-lock.yaml")
  return :bun if File.exist?("#{directory}/bun.lockb") || File.exist?("#{directory}/bun.lock")

  :npm
end

.detect_react_native(directory) ⇒ Object

Raises:



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/mysigner/build/detector.rb', line 397

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:



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/mysigner/build/detector.rb', line 298

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

.ensure_expo_prereqs!(directory, platform) ⇒ Object

Fail fast (before shelling out) for the two common prebuild blockers.

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mysigner/build/detector.rb', line 84

def self.ensure_expo_prereqs!(directory, platform)
  unless Dir.exist?("#{directory}/node_modules/expo")
    install = install_command(detect_package_manager(directory))
    raise NoProjectError, expo_prebuild_failed_message(
      platform, hint: "JavaScript dependencies aren't installed. Run `#{install}` in the project first."
    )
  end

  major = node_major_version
  return unless major && major < 20

  raise NoProjectError, expo_prebuild_failed_message(
    platform, hint: "Node.js #{major}.x is too old for current Expo SDKs — " \
                    'install Node >= 20.19.4 (e.g. via nvm, mise, or nodejs.org).'
  )
end

.expo_managed?(directory) ⇒ Boolean

True when this looks like an Expo project: an app.json/app.config.js plus ‘expo` as an ACTUAL dependency in package.json. We parse the JSON rather than substring-scanning the file so an unrelated package like `eslint-config-expo`, or the word “expo” in a script, doesn’t trip detection (and the destructive prebuild it gates).

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mysigner/build/detector.rb', line 34

def self.expo_managed?(directory)
  return false unless File.exist?("#{directory}/app.json") || File.exist?("#{directory}/app.config.js")
  return false unless File.exist?("#{directory}/package.json")

  pkg = begin
    JSON.parse(File.read("#{directory}/package.json"))
  rescue StandardError
    nil
  end
  return false unless pkg.is_a?(Hash)

  deps = {}
  deps.merge!(pkg['dependencies']) if pkg['dependencies'].is_a?(Hash)
  deps.merge!(pkg['devDependencies']) if pkg['devDependencies'].is_a?(Hash)
  deps.key?('expo')
end

.expo_managed_result(directory, platform) ⇒ Object

Non-mutating classification for an Expo managed project that has no native folder yet. Callers must check :needs_prebuild before trying to read gradle/xcode paths off the result.



54
55
56
57
58
59
60
61
62
63
# File 'lib/mysigner/build/detector.rb', line 54

def self.expo_managed_result(directory, platform)
  {
    platform: platform,
    type: :expo_managed,
    framework: :expo,
    path: File.absolute_path(directory),
    directory: directory,
    needs_prebuild: true
  }
end

.expo_prebuild_failed_message(platform, hint: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mysigner/build/detector.rb', line 125

def self.expo_prebuild_failed_message(platform, hint: nil)
  native = platform == :android ? 'Android' : 'iOS'
  parts = ["Failed to generate #{native} project with expo prebuild."]
  parts << hint if hint
  parts << <<~ERROR.strip
    Try running manually:
      npx expo prebuild --platform #{platform}

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

.install_command(pkg_manager) ⇒ Object



121
122
123
# File 'lib/mysigner/build/detector.rb', line 121

def self.install_command(pkg_manager)
  { yarn: 'yarn install', pnpm: 'pnpm install', bun: 'bun install' }.fetch(pkg_manager, 'npm install')
end

.node_major_versionObject



101
102
103
104
105
106
107
# File 'lib/mysigner/build/detector.rb', line 101

def self.node_major_version
  out = `node --version 2>/dev/null`.to_s.strip
  m = out.match(/v?(\d+)\./)
  m && m[1].to_i
rescue StandardError
  nil
end

.run_expo_prebuild!(directory, platform) ⇒ Object

Materialise the native project with ‘npx expo prebuild`, after a precheck so the user gets an actionable message instead of expo’s raw ConfigError. Raises NoProjectError on any failure. The caller’s ‘!Dir.exist?(native/)` guard guarantees we never clobber an existing native folder.

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mysigner/build/detector.rb', line 70

def self.run_expo_prebuild!(directory, platform)
  ensure_expo_prereqs!(directory, platform)

  puts "\n📦 Expo managed workflow detected (no #{platform}/ folder)"
  puts "🔧 Running: npx expo prebuild --platform #{platform}\n\n"
  result = system("cd #{directory} && npx expo prebuild --platform #{platform}")

  native_dir = platform == :android ? 'android' : 'ios'
  return if result && Dir.exist?("#{directory}/#{native_dir}")

  raise NoProjectError, expo_prebuild_failed_message(platform)
end