Last active
July 20, 2017 10:52
-
-
Save schrockwell/430037d511cbfac61802208c70817078 to your computer and use it in GitHub Desktop.
Ruby script for restructuring Phoenix 1.3 apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# SET OPTIONS HERE | |
# | |
# True means that no files will actually be moved | |
@dry_run = false | |
# False keeps the same controller and view file names, whereas | |
# true renames them to controller.ex and view.ex, respectively. | |
@shorten_names = false | |
# | |
# SCRIPT STARTS HERE | |
# | |
require 'pathname' | |
require 'fileutils' | |
@web_path = ARGV.first | |
unless @web_path | |
puts 'Usage: drawers.rb [path to web/ folder]' | |
exit | |
end | |
@web_path = Pathname.new(@web_path) | |
if @web_path.basename.to_s != 'web' | |
puts "This script expects a web/ directory, but instead it got #{@web_path.basename}/. Is that okay? (y to continue)" | |
exit unless STDIN.gets.downcase.strip == 'y' | |
end | |
@app_name = @web_path.parent.basename.to_s | |
@old_controllers_path = @web_path.join('controllers') | |
@old_views_path = @web_path.join('views') | |
@old_templates_path = @web_path.join('templates') | |
@controller_paths = Pathname.glob(@web_path.join('controllers', '**', '*_controller.ex')) | |
@view_paths = Pathname.glob(@web_path.join('views', '**', '*_view.ex')) | |
@template_paths = Pathname.glob(@web_path.join('templates', '**', '*.eex')) | |
def move(old_path, new_path) | |
puts "Moving #{old_path.relative_path_from(@web_path.parent)}" | |
puts " to #{new_path.relative_path_from(@web_path.parent)}" | |
unless @dry_run | |
FileUtils.mkdir_p(new_path.dirname) | |
FileUtils.mv(old_path, new_path) | |
end | |
end | |
@controller_paths.each do |old_path| | |
rel_path = old_path.relative_path_from(@old_controllers_path) | |
name = @shorten_names ? old_path.basename.to_s.gsub(/_controller.ex$/, '') : old_path.basename.to_s | |
new_path = @web_path.join(rel_path.dirname.join(name, 'controller.ex')) | |
move(old_path, new_path) | |
end | |
@view_paths.each do |old_path| | |
rel_path = old_path.relative_path_from(@old_views_path) | |
name = @shorten_names ? old_path.basename.to_s.gsub(/_view.ex$/, '') : old_path.basename.to_s | |
new_path = @web_path.join(rel_path.dirname.join(name, 'view.ex')) | |
move(old_path, new_path) | |
end | |
@template_paths.each do |old_path| | |
rel_path = old_path.relative_path_from(@old_templates_path) | |
dirname, basename = rel_path.split | |
new_path = @web_path.join(dirname.join('templates', basename)) | |
move(old_path, new_path) | |
end | |
puts <<DIRECTIONS | |
Moved #{@controller_paths.count + @view_paths.count + @template_paths.count} files | |
1.) Update lib/#{@app_name}/web.ex to use this in each view: | |
use Phoenix.View, root: "lib/#{@app_name}/web/\#{Regex.replace(~r/View$/, __MODULE__ |> Module.split |> Enum.slice(2..-1) |> Enum.join("."), "") |> Macro.underscore}/templates", path: "", namespace: MyApp.Web | |
2.) Update the :live_reload option in config/dev.exs: | |
config :#{@app_name}, MyApp.Web.Endpoint, | |
live_reload: [ | |
patterns: [ | |
~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$}, | |
~r{priv/gettext/.*(po)$}, | |
~r{lib/#{@app_name}/web/.*/view.ex$}, # <-- changed | |
~r{lib/#{@app_name}/web/.*/templates/.*(eex)$} # <-- changed | |
] | |
DIRECTIONS | |
if @dry_run | |
puts "\n--- DRY RUN: NO FILES WERE ACTUALLY MOVED ---" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment