Class: Develoz::Generators::Base

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/develoz/generators/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_timestampObject



10
11
12
13
# File 'lib/develoz/generators/base.rb', line 10

def self.next_migration_timestamp
  self.migration_counter += 1
  (Time.now.utc + migration_counter).strftime("%Y%m%d%H%M%S")
end

.source_rootObject



17
18
19
# File 'lib/develoz/generators/base.rb', line 17

def self.source_root
  File.expand_path("../../../templates", __dir__)
end

Instance Method Details

#add_gem(name, version = nil, group: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/develoz/generators/base.rb', line 54

def add_gem(name, version = nil, group: nil, **)
  file_path = File.join(destination_root, "Gemfile")
  return unless File.exist?(file_path)

  gemfile_content = File.read(file_path)

  if gemfile_content.match?(/^\s*gem\s+["']#{Regexp.escape(name)}["']/)
    say "Gemfile: gem '#{name}' already present, skipping", :green
    return
  end

  gem(name, version, group: group, **)
  gemfile = File.read(file_path)
  formatted_gemfile = gemfile.gsub(/group: \[([^\]\n]+)\]/, 'group: [ \1 ]')
  File.write(file_path, formatted_gemfile) if formatted_gemfile != gemfile
end

#app_classObject



25
26
27
# File 'lib/develoz/generators/base.rb', line 25

def app_class
  app_name.camelize
end

#app_nameObject



21
22
23
# File 'lib/develoz/generators/base.rb', line 21

def app_name
  File.basename(destination_root)
end

#append_env(key, value, example: true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/develoz/generators/base.rb', line 85

def append_env(key, value, example: true)
  env_file = File.join(destination_root, ".env")
  env_example_file = File.join(destination_root, ".env.example")

  if File.exist?(env_file)
    env_content = File.read(env_file)
    if env_content.include?("#{key}=")
      say ".env: key '#{key}' already present, skipping", :green
    else
      append_to_file(".env", "#{key}=#{value}\n")
    end
  end

  return unless example && File.exist?(env_example_file)

  example_content = File.read(env_example_file)
  if example_content.include?("#{key}=")
    say ".env.example: key '#{key}' already present, skipping", :green
  else
    append_to_file(".env.example", "#{key}=\n")
  end
end

#apply_template(name, destination) ⇒ Object



126
127
128
129
130
131
# File 'lib/develoz/generators/base.rb', line 126

def apply_template(name, destination)
  template_file = File.join(self.class.source_root, name)
  return unless File.exist?(template_file)

  copy_file(name, destination)
end

#ensure_gitignore(pattern) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/develoz/generators/base.rb', line 108

def ensure_gitignore(pattern)
  file_path = File.join(destination_root, ".gitignore")
  return unless File.exist?(file_path)

  gitignore_content = File.read(file_path)

  if gitignore_content.include?(pattern)
    say ".gitignore: pattern '#{pattern}' already present, skipping", :green
    return
  end

  append_to_file(".gitignore", "#{pattern}\n")
end

#inject_once(into:, content:, after: nil, before: nil, marker: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/develoz/generators/base.rb', line 29

def inject_once(into:, content:, after: nil, before: nil, marker: nil)
  file_path = File.join(destination_root, into)
  return unless File.exist?(file_path)

  file_content = File.read(file_path)

  if marker && file_content.include?(marker)
    say "#{into}: marker '#{marker}' already present, skipping", :green
    return
  end

  if file_content.include?(content)
    say "#{into}: content already present, skipping", :green
    return
  end

  if after
    inject_into_file(into, "#{content}\n", after: after)
  elsif before
    inject_into_file(into, "#{content}\n", before: before)
  else
    inject_into_file(into, "#{content}\n")
  end
end

#insert_route(route_line) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/develoz/generators/base.rb', line 71

def insert_route(route_line)
  file_path = File.join(destination_root, "config/routes.rb")
  return unless File.exist?(file_path)

  routes_content = File.read(file_path)

  if routes_content.include?(route_line)
    say "config/routes.rb: route already present, skipping", :green
    return
  end

  inject_into_file("config/routes.rb", "  #{route_line}\n", before: /^end\s*$/)
end

#migration_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/develoz/generators/base.rb', line 122

def migration_exists?(name)
  Dir.glob(File.join(destination_root, "db/migrate/*_#{name}.rb")).any?
end