Class: Postnhost::Generators::Tailwindcss::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/postnhost/tailwindcss/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_host_stylesheetObject



16
17
18
19
20
21
# File 'lib/generators/postnhost/tailwindcss/install_generator.rb', line 16

def create_host_stylesheet
  path = Rails.root.join("app/assets/stylesheets/postnhost/host.tailwind.css")
  return if path.exist?

  copy_file "host.tailwind.css", path
end

#create_stylesheet_directoryObject



12
13
14
# File 'lib/generators/postnhost/tailwindcss/install_generator.rb', line 12

def create_stylesheet_directory
  empty_directory "app/assets/stylesheets/postnhost"
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/postnhost/tailwindcss/install_generator.rb', line 62

def print_next_steps
  say ""
  say "PostnHost host Tailwind support installed.", :green
  say "Next steps:"
  step = 1

  if @package_json_present
    say "  #{step}. Install dependencies with the JavaScript package manager used by this app."
    step += 1
  end

  if Rails.root.join("bin/dev").exist?
    say "  #{step}. Run bin/dev; the PostnHost watcher is wired in Procfile.dev."
  else
    say "  #{step}. Run bin/rails postnhost:tailwindcss:watch alongside your Rails server."
  end

  say "  #{step + 1}. Use normal Tailwind classes in copied PostnHost views."
  say "  #{step + 2}. Keep the default layout tags; the combined build overrides postnhost/application automatically."
  say ""
end

#update_package_jsonObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/postnhost/tailwindcss/install_generator.rb', line 23

def update_package_json
  path = Rails.root.join("package.json")
  return say_package_json_instructions unless path.exist?

  @package_json_present = true

  json_data = JSON.parse(File.read(path))
  json_data["scripts"] ||= {}
  json_data["devDependencies"] ||= {}

  json_data["scripts"]["postnhost:tailwindcss"] = "bundle exec rails postnhost:tailwindcss:build"
  json_data["scripts"]["postnhost:tailwindcss:watch"] = "bundle exec rails postnhost:tailwindcss:watch"

  ensure_dependency(json_data, "tailwindcss", "^4.1.12")
  ensure_dependency(json_data, "@tailwindcss/cli", "^4.1.12")
  ensure_dependency(json_data, "@tailwindcss/typography", "^0.5.16")

  File.write(path, "#{JSON.pretty_generate(json_data)}\n")

  say "Updated package.json with PostnHost Tailwind scripts and dependencies.", :green
end

#wire_procfile_devObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/postnhost/tailwindcss/install_generator.rb', line 45

def wire_procfile_dev
  path = Rails.root.join("Procfile.dev")
  entry = "postnhost_css: bundle exec rails postnhost:tailwindcss:watch"

  if path.exist?
    if File.read(path).include?("postnhost:tailwindcss:watch")
      say "Procfile.dev already includes the PostnHost watcher.", :green
    else
      append_to_file path, "\n#{entry}\n"
      say "Added the PostnHost watcher to Procfile.dev.", :green
    end
  else
    create_file "Procfile.dev", "#{entry}\n"
    say "Created Procfile.dev with the PostnHost watcher.", :green
  end
end