Module: AppCopyr

Defined in:
lib/app_copyr.rb,
lib/app_copyr/version.rb

Defined Under Namespace

Classes: DirMissing, DistinationExists

Constant Summary collapse

VERSION =
"0.0.6"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#structure_builtObject (readonly)

Returns the value of attribute structure_built.



15
16
17
# File 'lib/app_copyr.rb', line 15

def structure_built
  @structure_built
end

Instance Method Details

#all_filesObject



118
119
120
# 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) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
# 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_destObject



29
30
31
# File 'lib/app_copyr.rb', line 29

def build_dest
  @build_dest ||= options[:dest]
end

#build_sourceObject



25
26
27
# File 'lib/app_copyr.rb', line 25

def build_source
  @build_source ||= options[:source]
end

#build_structureObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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_nameObject



21
22
23
# File 'lib/app_copyr.rb', line 21

def get_app_name
  new_app_name
end

#log(msg) ⇒ Object



17
18
19
# File 'lib/app_copyr.rb', line 17

def log(msg)
  puts msg if options[:verbose]
end

#mainObject



132
133
134
135
136
137
138
139
140
# 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_baseObject

Spits out directory of the new app



57
58
59
# File 'lib/app_copyr.rb', line 57

def new_app_base
  File.basename(build_dest)
end

#new_app_nameObject



42
43
44
45
46
47
48
49
# 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_rootObject

Spits out path to the new app



52
53
54
# File 'lib/app_copyr.rb', line 52

def new_app_root
  File.dirname(build_dest)
end

#old_app_nameObject



33
34
35
36
37
38
39
40
# 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_copyObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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