module AppCopyr

Constants

VERSION

Attributes

structure_built[R]

Public Instance Methods

all_files() click to toggle source
# File lib/app_copyr.rb, line 118
def all_files
  @all_files ||= Dir.glob(File.join(options[:source], "/**/**/*")).reject { |f| f.match(/\/vendor\/bundle\//) }
end
binary?(filename) click to toggle source
# File lib/app_copyr.rb, line 122
def binary?(filename)
  return false if File.size(filename) == 0
  begin
    fm = FileMagic.new(FileMagic::MAGIC_MIME)
    !(fm.file(filename)=~ /^text\/|^application\/(xml|json)/)
  ensure
    fm.close
  end
end
build_dest() click to toggle source
# File lib/app_copyr.rb, line 29
def build_dest
  @build_dest ||= options[:dest]
end
build_source() click to toggle source
# File lib/app_copyr.rb, line 25
def build_source
  @build_source ||= options[:source]
end
build_structure() click to toggle source
# File lib/app_copyr.rb, line 61
def build_structure
  puts "Building structure..."
  _created_dirs = []
  dirs = all_files.reject { |f| not File.directory?(f) }
  dirs.each do |dir|
    _new_file = dir.gsub(build_source, build_dest)
    # make the directory only and next if dir
    next if _created_dirs.include?(_new_file)
    printf "."
    _created_dirs << _new_file
    log "Making directory #{_new_file}"
    FileUtils.mkdir_p(_new_file)
  end
  @structure_built = true
end
get_app_name() click to toggle source
# File lib/app_copyr.rb, line 21
def get_app_name
  new_app_name
end
log(msg) click to toggle source
# File lib/app_copyr.rb, line 17
def log(msg)
  puts msg if options[:verbose]
end
main() click to toggle source
# File lib/app_copyr.rb, line 132
def main
  unless Dir.exists?(build_source)
    raise DirMissing.new("Unable to find source directory #{build_source}")
  end
  if Dir.exists?(build_dest)
    raise DistinationExists.new "Destination already exists"
  end
  run_copy
end
new_app_base() click to toggle source

Spits out directory of the new app

# File lib/app_copyr.rb, line 57
def new_app_base
  File.basename(build_dest)
end
new_app_name() click to toggle source
# File lib/app_copyr.rb, line 42
def new_app_name
  @new_app_name ||=
      if build_dest.match('/')
        build_dest.split('/').last.camelize
      else
        build_dest.camelize
      end
end
new_app_root() click to toggle source

Spits out path to the new app

# File lib/app_copyr.rb, line 52
def new_app_root
  File.dirname(build_dest)
end
old_app_name() click to toggle source
# File lib/app_copyr.rb, line 33
def old_app_name
  @old_app_name ||=
      if build_source.match('/')
        build_source.split('/').last.camelize
      else
        build_source.camelize
      end
end
run_copy() click to toggle source
# File lib/app_copyr.rb, line 77
def run_copy
  # loop thru each directory and substring out the old app name for the new
  puts "Running file copies..."
  build_structure unless structure_built
  files = all_files.select { |f| File.file?(f) && !binary?(f) }
  bin_files = all_files.select { |f| File.file?(f) && binary?(f) }
  bin_files.each do |bin_file|
    # get rid of the old path
    new_name = bin_file.gsub(/#{old_app_name}/, new_app_name)
    unless File.exists?(new_name)
      FileUtils.cp(bin_file, new_name)
    end
  end
  files.each do |source_file|
    next if source_file =~ /\.log\z/
    _new_file = source_file.gsub(build_source, build_dest)
    # make the directory only and next if dir
    _dest_file = File.open(_new_file, 'w+')
    log "Searching #{source_file}"
    if File.size(source_file) == 0
      # skip empty files, but still create them
      FileUtils.touch(_new_file)
      next
    end

    File.open(source_file, 'r').each do |line|
      if line.match(/(^|\s|_)#{old_app_name}|#{old_app_name.underscore}/)
        log "Removing #{old_app_name} from #{_new_file}"
        line = line.gsub(old_app_name, new_app_name)
        log "Removing #{old_app_name.underscore} from #{_new_file}"
        line = line.gsub(/#{old_app_name.underscore}/, "#{new_app_name.underscore}")
        _dest_file.print line
      else
        _dest_file.print line
      end
    end
    _dest_file.close
    printf "."
  end
end