Skip to content

Instantly share code, notes, and snippets.

@sleepingkingstudios
Created September 11, 2017 06:48
Show Gist options
  • Save sleepingkingstudios/ec7294d92e21d07f7fd5dcd50abd6187 to your computer and use it in GitHub Desktop.
Save sleepingkingstudios/ec7294d92e21d07f7fd5dcd50abd6187 to your computer and use it in GitHub Desktop.
$LOAD_PATH.unshift './lib'
source_file = ARGV[0]
target_file = ARGV[1]
puts "Source: #{source_file}"
puts "Target: #{target_file}"
require 'fileutils'
require 'sleeping_king_studios/tasks/file'
module SleepingKingStudios::Tasks::File
class FilePath
def initialize file_path
@full_path = file_path
split_file_path
end # constructor
attr_reader \
:full_path,
:relative_path,
:relative_fragments,
:source_directory,
:extension,
:file_name
def from_file_name file_name
copy = dup
copy.extension = File.extname(file_name)
copy.file_name =
File.basename(file_name.split(File::SEPARATOR).last, copy.extension)
copy.join_full_path
copy
end # method from_file_name
def from_source_directory directory
copy = dup
copy.source_directory = directory
copy.join_full_path
copy
end # method from_source_directory
def to_spec_file
copy = dup
copy.source_directory = 'spec'
copy.file_name = "#{file_name}_spec"
copy.join_full_path
copy
end # method to_spec_file
protected
attr_writer :file_name, :extension, :source_directory
def join_full_path
@full_path =
File.join(
@source_directory,
@relative_path,
"#{file_name}#{extension}"
) # end join
end # method join_full_path
private
def split_file_path
@source_directory = nil
@extension = File.extname(full_path)
fragments = full_path.split(File::SEPARATOR)
@file_name = File.basename(fragments.pop, extension)
split_relative_path fragments
end # method split_file_path
def split_relative_path fragments
if %w(app apps lib spec tmp vendor).include?(fragments.first)
@source_directory = fragments.shift
end # if
@relative_fragments = fragments
@relative_path = File.join fragments
end # method split_relative_path
end # class
class MoveTask < SleepingKingStudios::Tasks::Task
def call old_path, new_path
@old_file = FilePath.new(old_path)
if new_path.include? File::SEPARATOR
@new_file = FilePath.new(new_path)
else
@new_file = @old_file.from_file_name(new_path)
end # if-else
move_file(old_file, new_file)
move_file(old_file.to_spec_file, new_file.to_spec_file)
end # method call
private
attr_reader :old_file, :new_file
def check_for_existing_source _; end
def check_for_existing_target _; end
def create_directories *directory_names
FileUtils.mkdir_p directory_names.compact.join(File::SEPARATOR)
end # method create_directories
def move_file src, tgt
check_for_existing_source(src)
check_for_existing_target(tgt)
create_directories(
tgt.source_directory,
*tgt.relative_fragments
) # end create_directories
in_git = !`git ls-files #{src.full_path}`.empty?
puts "Moving:\n #{src.full_path}\nto:\n #{tgt.full_path}\n\n"
`#{in_git ? 'git ' : ''}mv #{src.full_path} #{tgt.full_path}`
end # method move_file
end # class
end # module
SleepingKingStudios::Tasks::File::MoveTask.new({}).
call(source_file, target_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment