Class: Toxic::Project::Create
- Inherits:
-
Object
- Object
- Toxic::Project::Create
- Defined in:
- lib/toxic/source/project.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#name ⇒ Object
Returns the value of attribute name.
-
#organization ⇒ Object
Returns the value of attribute organization.
-
#repository_address ⇒ Object
Returns the value of attribute repository_address.
-
#template_author ⇒ Object
Returns the value of attribute template_author.
-
#template_name ⇒ Object
Returns the value of attribute template_name.
-
#template_name_other ⇒ Object
Returns the value of attribute template_name_other.
-
#template_organization ⇒ Object
Returns the value of attribute template_organization.
-
#template_url ⇒ Object
Returns the value of attribute template_url.
Instance Method Summary collapse
- #add_fastlane ⇒ Object
- #add_git_repository ⇒ Object
- #ask_info_for_new ⇒ Object
- #clone_template ⇒ Object
- #configure_template ⇒ Object
- #get_template_info ⇒ Object
-
#initialize(name, template_url) ⇒ Create
constructor
A new instance of Create.
- #open_project ⇒ Object
- #pod_install ⇒ Object
- #remove_useless ⇒ Object
- #rename(original_name) ⇒ Object
- #run ⇒ Object
- #set_bundle_identifiers ⇒ Object
- #template_author_organization ⇒ Object
- #traverse_dir(file_path) ⇒ Object
- #update_content(file_path) ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(name, template_url) ⇒ Create
Returns a new instance of Create.
13 14 15 16 |
# File 'lib/toxic/source/project.rb', line 13 def initialize(name, template_url) @name = name @template_url = template_url end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
9 10 11 |
# File 'lib/toxic/source/project.rb', line 9 def @author end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/toxic/source/project.rb', line 9 def name @name end |
#organization ⇒ Object
Returns the value of attribute organization.
9 10 11 |
# File 'lib/toxic/source/project.rb', line 9 def organization @organization end |
#repository_address ⇒ Object
Returns the value of attribute repository_address.
9 10 11 |
# File 'lib/toxic/source/project.rb', line 9 def repository_address @repository_address end |
#template_author ⇒ Object
Returns the value of attribute template_author.
10 11 12 |
# File 'lib/toxic/source/project.rb', line 10 def @template_author end |
#template_name ⇒ Object
Returns the value of attribute template_name.
10 11 12 |
# File 'lib/toxic/source/project.rb', line 10 def template_name @template_name end |
#template_name_other ⇒ Object
Returns the value of attribute template_name_other.
11 12 13 |
# File 'lib/toxic/source/project.rb', line 11 def template_name_other @template_name_other end |
#template_organization ⇒ Object
Returns the value of attribute template_organization.
10 11 12 |
# File 'lib/toxic/source/project.rb', line 10 def template_organization @template_organization end |
#template_url ⇒ Object
Returns the value of attribute template_url.
9 10 11 |
# File 'lib/toxic/source/project.rb', line 9 def template_url @template_url end |
Instance Method Details
#add_fastlane ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/toxic/source/project.rb', line 115 def add_fastlane return nil unless system "which fastlane > /dev/null" decision = CLI::UI.ask("do you want to add fastlane to your project? (y/n)", default: 'y') return nil unless decision == 'y' Dir.chdir("#{name}") do |_| system "fastlane init" end end |
#add_git_repository ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/toxic/source/project.rb', line 93 def add_git_repository Dir.chdir("#{name}") do |_| system "git init > /dev/null" puts CLI::UI.fmt("{{green: Initialized empty Git repository}}") @repository_address = CLI::UI.ask('repository address for the project:(enter to skip)') system "git remote add origin #{repository_address}" unless repository_address.empty? end end |
#ask_info_for_new ⇒ Object
60 61 62 63 64 65 |
# File 'lib/toxic/source/project.rb', line 60 def ask_info_for_new puts CLI::UI.fmt("{{green: Let's go over some question to create your base project code!}}") @author = CLI::UI.ask('author for the project:') @organization = CLI::UI.ask('organization for the project:') end |
#clone_template ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/toxic/source/project.rb', line 39 def clone_template if Dir.exist?(Pathname("./#{name}")) question = CLI::UI.fmt("{{red: Folder #{name} already exists, do you want to overwrite it? (y/n)}}") override = CLI::UI.ask(question, default: 'n') if override == 'y' puts CLI::UI.fmt("deleting #{name}") system "rm -rf ./#{name}" else exit(0) end end system "git clone #{@template_url} #{name}" end |
#configure_template ⇒ Object
74 75 76 |
# File 'lib/toxic/source/project.rb', line 74 def configure_template traverse_dir(Pathname("./#{name}")) end |
#get_template_info ⇒ Object
53 54 55 56 57 58 |
# File 'lib/toxic/source/project.rb', line 53 def get_template_info template_path = Dir.glob("./#{name}/**/**/*.xcodeproj").first @template_name = File.basename(template_path, '.xcodeproj') @template_name_other = template_name.gsub(/[^a-zA-Z0-9]/, '_') @template_author, @template_organization = end |
#open_project ⇒ Object
124 125 126 127 128 |
# File 'lib/toxic/source/project.rb', line 124 def open_project project = Dir.glob("./**/**/#{name}.xcworkspace").first project = Dir.glob("./**/**/#{name}.xcodeproj") unless Dir.glob(project).any? system "open #{project}" end |
#pod_install ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/toxic/source/project.rb', line 102 def pod_install Dir.chdir("#{name}") do |_| if File.exists?('Podfile') decision = CLI::UI.ask("Podfile detected, do you want to exec 'pod install' ?", options: %w(install later)) case decision when 'install' system "pod install" else break end end end end |
#remove_useless ⇒ Object
67 68 69 70 71 72 |
# File 'lib/toxic/source/project.rb', line 67 def remove_useless system "rm -rf ./#{name}/.git" system "rm -rf ./#{name}/**/xcuserdata/" system "rm -rf ./#{name}/**/**/xcuserdata/" system "rm -rf ./#{name}/**/**/xcshareddata" end |
#rename(original_name) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/toxic/source/project.rb', line 168 def rename(original_name) name_new = original_name.sub(Regexp.new(Regexp.escape(template_name), Regexp::IGNORECASE), name) name_new = name_new.sub(Regexp.new(Regexp.escape(template_name_other), Regexp::IGNORECASE), name) File.rename(original_name, name_new) name_new end |
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/toxic/source/project.rb', line 18 def run validate! clone_template get_template_info remove_useless ask_info_for_new configure_template set_bundle_identifiers add_git_repository pod_install add_fastlane open_project end |
#set_bundle_identifiers ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/toxic/source/project.rb', line 78 def set_bundle_identifiers puts CLI::UI.fmt("{{cyan: Let's setup your bundle identifiers}}") project_path = Dir.glob("./#{name}/**/**/#{name}.xcodeproj").first project = Xcodeproj::Project.open(project_path) project.targets.each do |target| target.build_configurations.each do |config| original_bundle_identifier = config.build_settings["PRODUCT_BUNDLE_IDENTIFIER"] question = CLI::UI.fmt("target {{green:#{target}}} under {{green:#{config}}} configuration") answer = CLI::UI.ask(question, default: original_bundle_identifier) config.build_settings["PRODUCT_BUNDLE_IDENTIFIER"] = answer end end project.save end |
#template_author_organization ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/toxic/source/project.rb', line 142 def app_delegate_swift_path = Dir.glob("./#{name}/**/**/*AppDelegate*.swift").last raise "Can't find your AppDelegate file" if app_delegate_swift_path.nil? = File.open(app_delegate_swift_path) do |file| file.each_line.with_index do |line, _| break line if /^\/\/ {2}Created by/ =~ line end end organization = File.open(app_delegate_swift_path) do |file| file.each_line do |line| break line if /^\/\/ {2}Copyright ©/ =~ line end end index1 = .index 'by' index2 = .index 'on' = [index1+3 ... index2] index3 = organization.index '©' index4 = organization.index '.' organization = organization[index3+7 ... index4] [, organization] end |
#traverse_dir(file_path) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/toxic/source/project.rb', line 130 def traverse_dir(file_path) puts "updating #{file_path}" file_path = rename(file_path) if File.directory?(file_path) Dir.each_child(file_path) do |file| traverse_dir(file_path + file) end else update_content(file_path) end end |
#update_content(file_path) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/toxic/source/project.rb', line 175 def update_content(file_path) begin file = File.new("#{file_path}_new", "w+") origin = File.open(file_path, "r:UTF-8" ) origin.each do |line| line = "// Created by #{} on #{Date.today}." if /^\/\/ {2}Created by/ =~ line line = "// Copyright © 2018 #{organization}. All rights reserved." if /^\/\/ {2}Copyright ©/ =~ line line.gsub!(Regexp.new(Regexp.escape(template_name), Regexp::IGNORECASE), name) line.gsub!(Regexp.new(Regexp.escape(template_name_other), Regexp::IGNORECASE), name) line.gsub!(Regexp.new(Regexp.escape(template_organization), Regexp::IGNORECASE), organization) line.gsub!(Regexp.new(Regexp.escape(), Regexp::IGNORECASE), ) file.puts line end origin.close file.close File.delete(origin) File.rename("#{file_path}_new", file_path) rescue Exception # ignored end end |
#validate! ⇒ Object
32 33 34 35 36 37 |
# File 'lib/toxic/source/project.rb', line 32 def validate! raise "A name for the project is required." unless name raise "The project name cannot contain spaces." if name =~ /\s/ raise "The project name cannot begin with a '.'" if name[0, 1] == '.' raise "The project name should only contain number and character" if name =~ /[^a-zA-Z0-9]/ end |