Class: YJNCopycat::XcodeProjectCloner

Inherits:
Object
  • Object
show all
Defined in:
lib/yjncopycat/xcodeprojectcloner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ XcodeProjectCloner

Returns a new instance of XcodeProjectCloner.



11
12
13
14
15
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 11

def initialize(options)
    @url = options[:url]
    @new_name = options[:name]
    @target_path = "./#{@new_name}"
end

Instance Attribute Details

#new_nameObject (readonly)

Returns the value of attribute new_name.



9
10
11
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 9

def new_name
  @new_name
end

#old_nameObject (readonly)

Returns the value of attribute old_name.



9
10
11
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 9

def old_name
  @old_name
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



9
10
11
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 9

def target_path
  @target_path
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 9

def url
  @url
end

#xcodeproj_pathObject (readonly)

Returns the value of attribute xcodeproj_path.



9
10
11
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 9

def xcodeproj_path
  @xcodeproj_path
end

Instance Method Details

#createCloneObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 17

def createClone
    createDirectory
    downloadRepo
    removePods
    replaceOccurencesOfOldProjectNameWithNewNameInFiles
    renameDirectories
    renameFiles
    reinstallPods
    reinitializeGit
    openProject
end

#createDirectoryObject



29
30
31
32
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 29

def createDirectory
    system 'mkdir', '-p', target_path
    puts "New folder created at: #{target_path}.".yellow
end

#downloadRepoObject



34
35
36
37
38
39
40
41
42
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 34

def downloadRepo
    downloadOptions = Pod::Downloader.preprocess_options({ :git => @url })
    downloader = Pod::Downloader.for_target(@target_path, downloadOptions)
    puts ''
    puts "Will start downloading project. This may take a while. Please wait.".yellow
    downloader.download
    puts "Download complete.".yellow
    puts ''
end

#openProjectObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 111

def openProject
    new_xcodeproj_file_path = Find.find(@target_path).select { |p| /.*\.xcodeproj$/ =~ p }.first
    new_xcworkspace_file_path = Find.find(@target_path).select { |p| /.*\.xcworkspace$/ =~ p }.first
    if new_xcworkspace_file_path.nil?
        puts "Will open xcproj file at: " + new_xcodeproj_file_path
        system "open '#{new_xcodeproj_file_path}'"
    else 
        puts "Will open xcworkspace file at: " + new_xcworkspace_file_path
        system "open '#{new_xcworkspace_file_path}'"
    end
end

#reinitializeGitObject



103
104
105
106
107
108
109
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 103

def reinitializeGit
    Dir.chdir(target_path) do
        `rm -rf .git`
        `git init`
        `git add -A`
    end
end

#reinstallPodsObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 92

def reinstallPods
    Dir.glob("#{@target_path}/**/Podfile") do |podfile|
        file = File.new(podfile)
        podfile_dir = File.dirname(file)
        Dir.chdir(podfile_dir) do
            puts "Installing CocoaPods dependencies...".yellow
            system "pod install"
        end
    end
end

#removePodsObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 44

def removePods
    Dir.glob("#{@target_path}/**/Podfile.lock") do |lock_file|
        `rm -rf #{lock_file}`
    end
    Dir.glob("#{@target_path}/**/*.xcworkspace") do |xcworkspace_file|
        `rm -rf #{xcworkspace_file}`
    end
    Dir.glob("#{@target_path}/**/Pods/") do |pods_dir|
        `rm -rf #{pods_dir}`
    end
end

#renameDirectoriesObject



74
75
76
77
78
79
80
81
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 74

def renameDirectories
    Dir.glob("#{@target_path}/**/#{@old_name}*").select { |file| File.directory? file }.reverse.each do |filename|
        file = File.new(filename)
        pattern = /(.*)#{@old_name}/
        new_name = filename.gsub(pattern, "\\1#{@new_name}")
        File.rename filename, new_name
    end
end

#renameFilesObject



83
84
85
86
87
88
89
90
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 83

def renameFiles 
    Dir.glob("#{@target_path}/**/*#{@old_name}*").reverse.each do |filename|
        file = File.new(filename)
        pattern = /(.*)#{@old_name}/
        new_name = filename.gsub(pattern, "\\1#{@new_name}")
        File.rename filename, new_name
    end
end

#replaceOccurencesOfOldProjectNameWithNewNameInFilesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/yjncopycat/xcodeprojectcloner.rb', line 56

def replaceOccurencesOfOldProjectNameWithNewNameInFiles
    @xcodeproj_path = Find.find(target_path).select { |p| /.*\.xcodeproj$/ =~ p }.first
    if @xcodeproj_path.nil?
        puts "ERROR: Unable to locate xcodeproj file.".red
        exit 1
    end
    @old_name = File.basename(@xcodeproj_path, ".xcodeproj")

    Dir.glob(@target_path + "/**/**/**/**").each do |filename|
        next if Dir.exists? filename
        text = File.read(filename)
        for search, replace in { @old_name => @new_name }
            text = text.gsub(search, replace)
        end
        File.open(filename, "w") { |file| file.puts text }
    end
end