Class: Shadwire::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/shadwire/project.rb

Overview

Detects a consuming Rails app and its front-end stack by inspecting the Gemfile / Gemfile.lock and a few conventional files. Pure filesystem reads — never loads the app or Bundler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Project

Returns a new instance of Project.



10
11
12
# File 'lib/shadwire/project.rb', line 10

def initialize(root)
  @root = root.to_s
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/shadwire/project.rb', line 8

def root
  @root
end

Instance Method Details

#gem?(name) ⇒ Boolean

True when the gem is declared in Gemfile.lock (authoritative when present) or the Gemfile.

Returns:

  • (Boolean)


20
21
22
# File 'lib/shadwire/project.rb', line 20

def gem?(name)
  (File.exist?(lockfile_path) && lock_declares?(name)) || gemfile_declares?(name)
end

#importmap?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/shadwire/project.rb', line 24

def importmap?
  gem?("importmap-rails") || File.exist?(importmap_path)
end

#importmap_pathObject



36
37
38
# File 'lib/shadwire/project.rb', line 36

def importmap_path
  path("config/importmap.rb")
end

#include_all_helpers?Boolean

Rails includes every app/helpers/**/*_helper.rb module into views unless an app opts out. When it does, the Ui::*Helper modules need per-controller helper calls, so status surfaces it.

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/shadwire/project.rb', line 43

def include_all_helpers?
  config = path("config/application.rb")
  return true unless File.exist?(config)

  !File.read(config).match?(/include_all_helpers\s*=\s*false/)
end

#legacy_helper?Boolean

A monolithic app/helpers/ui_helper.rb left over from before helpers were split per item. No longer published; safe for the app to delete.

Returns:

  • (Boolean)


52
53
54
# File 'lib/shadwire/project.rb', line 52

def legacy_helper?
  File.exist?(path("app/helpers/ui_helper.rb"))
end

#rails?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/shadwire/project.rb', line 14

def rails?
  File.exist?(path("config/application.rb")) || gem?("rails")
end

#raise_unless_rails!Object

Raises ProjectError unless this looks like a Rails app.

Raises:



61
62
63
64
65
66
# File 'lib/shadwire/project.rb', line 61

def raise_unless_rails!
  return if rails?

  raise ProjectError,
        "Not a Rails app: no config/application.rb or `gem \"rails\"` found in #{@root}"
end

#stimulus?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/shadwire/project.rb', line 32

def stimulus?
  gem?("stimulus-rails") || File.exist?(path("app/javascript/controllers/index.js"))
end

#tailwind_css_path(relative = "app/assets/tailwind/application.css") ⇒ Object



56
57
58
# File 'lib/shadwire/project.rb', line 56

def tailwind_css_path(relative = "app/assets/tailwind/application.css")
  path(relative)
end

#tailwindcss_rails?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/shadwire/project.rb', line 28

def tailwindcss_rails?
  gem?("tailwindcss-rails")
end