Skip to content

Instantly share code, notes, and snippets.

@pcreux
Last active November 1, 2024 17:51
Show Gist options
  • Save pcreux/ea884a138a8e1e6fac33b7228aebb4e2 to your computer and use it in GitHub Desktop.
Save pcreux/ea884a138a8e1e6fac33b7228aebb4e2 to your computer and use it in GitHub Desktop.
NAME = "forum"
MODULE_NAME = "Forum"
PATH = "aaa/forum"
def run(cmd)
puts cmd
unless system(cmd)
puts "Command failed"
exit(-1)
end
end
def write(path, content)
puts "Writing #{path}"
File.open(path, 'w') do |file|
file.puts content
end
end
def announce(txt)
puts txt
end
announce "Move app dirs..."
Dir["app/*/#{NAME}"].each do |path|
run "mkdir -p #{PATH}/#{path}"
run "mv #{path}/* #{PATH}/#{path}"
end
announce "Move test dirs..."
Dir["test/*/#{NAME}"].each do |path|
next if path.start_with?("test/fixtures")
run "mkdir -p #{PATH}/#{path}"
run "mv #{path}/* #{PATH}/#{path}"
end
announce "Move core model to lib"
run "mkdir -p #{PATH}/lib"
run "mv app/models/#{NAME}.rb #{PATH}/lib/"
announce "Fix core model"
GEM_RB = <<~RUBY
require "#{NAME}/engine"
#{File.read(PATH + "/lib/#{NAME}.rb")}
RUBY
write(PATH + "/lib/#{NAME}.rb", GEM_RB)
announce "Move app files..."
Dir["app/*/#{NAME}*"].each do |path|
next if File.directory?(path)
run "mkdir -p #{path.split('/')[0..-2].join('/')}"
run "mv #{path} #{PATH}/#{path}"
end
announce "Move test files..."
Dir["test/*/#{NAME}*"].each do |path|
next if File.directory?(path)
run "mkdir -p #{path.split('/')[0..-2].join('/')}"
run "mv #{path} #{PATH}/#{path}"
end
announce "Move migrations..."
run "mkdir -p #{PATH}/db/migrate"
run "mv db/migrate/*#{NAME}* #{PATH}/db/migrate"
announce "Add gem to Gemfile"
run %(echo "gem '#{NAME}', path: '#{PATH}'" >> Gemfile)
announce "Add test_helper"
File.open(File.join(PATH, "test", "test_helper.rb"), "w") do |f|
f.puts 'require_relative "../../../test/test_helper"'
end
announce "Add engine.rb"
ENGINE_RB = <<~RUBY
module #{MODULE_NAME}
class Engine < ::Rails::Engine
initializer :append_migrations do |app|
append_engine_paths("db/migrate", app)
end
private
def append_engine_paths(type, app)
return if app.root.to_s.match root.to_s
config.paths[type].expanded.each do |expanded_path|
app.config.paths[type] << expanded_path
end
end
end
end
RUBY
run "mkdir -p #{[PATH, "lib", NAME].join('/')}"
write(File.join(PATH, "lib", NAME, "engine.rb"), ENGINE_RB)
announce "Add gemspec"
GEMSPEC = <<~RUBY
$:.push File.expand_path("lib", __dir__)
Gem::Specification.new do |spec|
spec.name = "#{NAME}"
spec.version = "0.0.1"
spec.authors = ["AAA"]
spec.summary = "The #{NAME} AAA Engine"
end
RUBY
write(File.join(PATH, "#{NAME}.gemspec"), GEMSPEC)
puts <<STR
Done!
1. You now want to move the line `gem '#{NAME}'...` to the right spot.
2. Move config/routes/#{NAME} if any and update config/routes.rb to point to it
STR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment