Class: Taft
- Inherits:
-
Object
- Object
- Taft
- Defined in:
- lib/taft.rb
Constant Summary collapse
- TEMPLATE_PROJECT_NAME_REGEX =
e.g. redsky if one word, red_sky if two # regex to use when matching a template file name
/(zznamezz)/
- TEMPLATE_PROJECT_NAME_UPPERCASE_REGEX =
e.g. Redsky if one word, RedSky if two # regex to use when matching a template file name
/(ZZnamezz)/
- TEMPLATE_PROJECT_ABBREV_REGEX =
e.g. bs # regex to use when matching a template file name
/(xxabbrevxx)/
- TEMPLATE_PROJECT_ABBREV_UPPERCASE_REGEX =
e.g. BS # regex to use when matching some template text
/(xxabbrevupperxx)/i
- TEMPLATE_PROJECT_RAW_NAME_REGEX =
e.g. RED SKY # regex to use when matching some template text
/(yyrawnameyy)/i
- NAMES_AND_ABBREVS_REGEXES =
[TEMPLATE_PROJECT_ABBREV_REGEX, TEMPLATE_PROJECT_ABBREV_UPPERCASE_REGEX, TEMPLATE_PROJECT_NAME_REGEX, TEMPLATE_PROJECT_NAME_UPPERCASE_REGEX]
- TEMPLATE_GEM_NAME_REGEX =
/-\d+.\d+.\d+.gem/
- LANGUAGES =
, :java] # Java not yet supported
[:ruby]
Class Method Summary collapse
-
.adjust_file_contents(dest_base_folder, project_name, project_abbrev) ⇒ Object
Looks at the contents of each file within dest_base_folder and edits their contents, applying the project_name or project_abbrev as appropriate.
-
.adjust_file_names(dest_base_folder, project_name, project_abbrev) ⇒ Object
Looks at the names of each file & folder within dest_base_folder and renames them, applying the project_name or project_abbrev as appropriate.
- .delete_file(abs_file_path) ⇒ Object
- .install(lang = nil, debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
- .install_gems(bundled_gem_path) ⇒ Object
- .install_java(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
- .install_ruby(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
Class Method Details
.adjust_file_contents(dest_base_folder, project_name, project_abbrev) ⇒ Object
Looks at the contents of each file within dest_base_folder and edits their contents, applying the project_name or project_abbrev as appropriate
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/taft.rb', line 187 def self.adjust_file_contents(dest_base_folder, project_name, project_abbrev) pd "Now in #{__method__}; #{dest_base_folder}; #{project_name}; #{project_abbrev}" raw_name = project_name # should have been entered in form like RED SKY project_class_name_part = project_name.gsub(/[\s-]+/, "_").downcase # TODO use .snakecase ? project_class_name_uppercase_part = project_name.pascalcase project_class_abbrev_part = project_abbrev.gsub(/[\s-]+/, "_").delete("_").downcase # TODO use .snakecase ? pd "project_class_name_part : #{project_class_name_part}" # snakecase pd "project_class_name_uppercase_part : #{project_class_name_uppercase_part}" # pascalcase pd "project_class_abbrev_part : #{project_class_abbrev_part}" # lowercase Dir.chdir(dest_base_folder) do entries = Dir.entries(dest_base_folder) entries.delete(".") entries.delete("..") entries.delete(".git") entries.delete(".gitignore") entries.each do |f| f = File.(f) pd "Now looking at #{f}; is dir? #{Dir.exists?(f)}" if Dir.exists?(f) # if this is a dir, call recursively Taft.adjust_file_contents(f, project_name, project_abbrev) next end lines = [] File.open(f, "r") do |file_obj| lines = file_obj.readlines end # Each line may match more than one regex # Hence each line should be passed through all of the regexes, not stopping after the first one it matches # i.e. separate if-ends, not one big case-when or if-elsif routine lines.each do |line| if line =~ TEMPLATE_PROJECT_NAME_REGEX line.gsub!(TEMPLATE_PROJECT_NAME_REGEX, project_class_name_part) end if line =~ TEMPLATE_PROJECT_NAME_UPPERCASE_REGEX line.gsub!(TEMPLATE_PROJECT_NAME_UPPERCASE_REGEX, project_class_name_uppercase_part) end if line =~ TEMPLATE_PROJECT_ABBREV_REGEX line.gsub!(TEMPLATE_PROJECT_ABBREV_REGEX, project_class_abbrev_part) end if line =~ TEMPLATE_PROJECT_ABBREV_UPPERCASE_REGEX line.gsub!(TEMPLATE_PROJECT_ABBREV_UPPERCASE_REGEX, project_class_abbrev_part.upcase) end if line =~ TEMPLATE_PROJECT_RAW_NAME_REGEX line.gsub!(TEMPLATE_PROJECT_RAW_NAME_REGEX, raw_name) end end File.open(f, "w") do |file_obj| file_obj.puts lines end end end end |
.adjust_file_names(dest_base_folder, project_name, project_abbrev) ⇒ Object
Looks at the names of each file & folder within dest_base_folder and renames them, applying the project_name or project_abbrev as appropriate
135 136 137 138 139 140 141 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/taft.rb', line 135 def self.adjust_file_names(dest_base_folder, project_name, project_abbrev) pd "Now in #{__method__}; #{dest_base_folder}; #{project_name}; #{project_abbrev}" project_file_name_part = project_name.gsub(/[\s-]+/, "_").downcase # TODO use .snakecase ? project_file_abbrev_part = project_abbrev.gsub(/[\s-]+/, "_").delete("_").downcase # TODO use .snakecase ? pd "project_file_name_part : #{project_file_name_part}" # snakecase pd "project_file_abbrev_part : #{project_file_abbrev_part}" # lowercase Dir.chdir(dest_base_folder) do entries = Dir.entries(dest_base_folder) entries.delete(".") entries.delete("..") entries.each do |f| f = File.(f) pd "Now looking at #{f}; is dir? #{Dir.exists?(f)}" if Dir.exists?(f) # if this is a dir, call recursively Taft.adjust_file_names(f, project_name, project_abbrev) end # After processing the contents of the directory, rename it # Or if the entry was actually a file, rename it basename = File.basename(f, ".*") NAMES_AND_ABBREVS_REGEXES.each do |regex| if basename =~ regex ext = File.extname(f) dir = File.split(f)[0] # For the given regex, work out what the replacement text should be case regex.inspect when TEMPLATE_PROJECT_NAME_REGEX replacement = @project_name_part when TEMPLATE_PROJECT_NAME_UPPERCASE_REGEX replacement = @project_name_uppercase_part when TEMPLATE_PROJECT_ABBREV_REGEX replacement = @project_abbrev_part when TEMPLATE_PROJECT_ABBREV_UPPERCASE_REGEX replacement = @project_abbrev_uppercase_part end new_f = basename.gsub(regex, replacement) new_f += ext # must add the extension back on new_f = File.join(dir, new_f) # this is the full path pd "Will rename #{f}\n to #{new_f}" Taft.delete_file(new_f) File.rename(f, new_f) end end end end end |
.delete_file(abs_file_path) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/taft.rb', line 246 def self.delete_file(abs_file_path) if Dir.exists?(abs_file_path) FileUtils.remove_dir(abs_file_path) elsif File.exists?(abs_file_path) File.delete(abs_file_path) end end |
.install(lang = nil, debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/taft.rb', line 34 def self.install(lang = nil, debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") $TAFT_DEBUGGING = true if debugging unless dest.empty? puts "Taft.install has been called with the following parameters :" puts "Language : #{lang}" puts "Project name : #{project_name}" puts "Project abbreviation : #{project_abbrev}" puts "Destination folder (relative to #{Dir.getwd} if a relative path) : #{dest}" puts "Press Enter to continue..." gets end base_wd = Dir.getwd dest_base_folder = "" if lang == nil puts "\nPlease enter the language of this project : #{LANGUAGES.join(", ")}" lang = gets.chomp.downcase.to_sym raise "TAFT cannot install a new project in that language" unless LANGUAGES.include?(lang) end if dest.empty? puts "\nPlease enter the path of the base folder that TAFT should create, up to and including the name of the base folder" puts "TAFT will create this folder if it does not exist" puts "If a relative path is entered, it will be taken as being relative to path : #{Dir.getwd}" dest = gets.chomp raise "The base folder path entered was empty" if dest.empty? puts "Does this folder already exist? Entering Y will grant TAFT permission to write into that folder, overwriting any files with matching names that are aready present." folder_exists = gets.chomp overwrite_ok = (folder_exists == "Y") end # if dest has been specified in the call, then overwrite_ok has as well case lang when :ruby raw_file_path = "#{File.(File.dirname(__FILE__))}/taft_files" # TODO determine list of gems & put in folder # Might be better to output a list of gem install commands instead? What if people are using earlier/later versions of Ruby? # bundled_gem_path = "#{File.expand_path(File.dirname(__FILE__))}/bundled_gems" # install_gems(bundled_gem_path) when :java raw_file_path = "#{File.(File.dirname(__FILE__))}/java_taft_files" end # TODO does this handle dests like "~/foo" ? if (Pathname.new dest).absolute? dest_base_folder = dest else dest_base_folder = File.join(Dir.getwd, dest) end dest_base_folder = File.(dest_base_folder) puts "TAFT will install to #{dest_base_folder}" raise "Folder #{dest_base_folder} already exists, and you did not grant TAFT permission to write into the folder" if Dir.exists?(dest_base_folder) && !overwrite_ok # Create the base folder begin Dir.mkdir(dest_base_folder) unless Dir.exists?(dest_base_folder) ensure raise "The base folder '#{dest_base_folder}' did not exist" if folder_exists && !Dir.exists?(dest_base_folder) raise "The base folder '#{dest_base_folder}' could not be created" unless Dir.exists?(dest_base_folder) end # Copy the raw files into the base folder, preserving the structure FileUtils.copy_entry(raw_file_path, dest_base_folder, false, false, true) puts "\nFiles have been copied" if project_name.empty? puts "\nPlease enter the name of your project (e.g. RED SKY) :" project_name = gets.chomp raise "The project name entered was empty" if project_name.empty? end if project_abbrev.empty? puts "\nPlease enter the abbreviation of your project (e.g. RS) :" project_abbrev = gets.chomp raise "The project abbreviation entered was empty" if project_abbrev.empty? end @project_name_part = project_name.gsub(/[\s-]+/, "_").downcase # TODO use .snakecase ? @project_name_uppercase_part = @project_name_part.upcase @project_abbrev_part = project_abbrev.gsub(/[\s-]+/, "_").delete("_").downcase # TODO use .snakecase ? @project_abbrev_uppercase_part = @project_abbrev_part.upcase # Now sweep over the copied files, adjusting the names accordingly Taft.adjust_file_names(dest_base_folder, project_name, project_abbrev) # Now sweep over the copied files, adjusting the contents accordingly Taft.adjust_file_contents(dest_base_folder, project_name, project_abbrev) puts "\nFiles have been tailored to your project." puts "\nTAFT has installed a tailored copy of the #{lang.capitalize} Automation Framework to #{dest_base_folder}" puts "Installation complete." end |
.install_gems(bundled_gem_path) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/taft.rb', line 254 def self.install_gems(bundled_gem_path) puts "\nWill first install all gems bundled in with the TAFT gem." gem_list = Dir.entries(bundled_gem_path) gem_list.delete(".") gem_list.delete("..") gems_to_install = [] gem_list.each do |gem_name| # For each gem, try to require it. Only install those that couldn't be required. begin gem_base_name = gem_name.gsub(TEMPLATE_GEM_NAME_REGEX, "") require gem_base_name rescue LoadError puts "#{gem_base_name} could not be required; will install #{gem_name}" gems_to_install << gem_name end end if gems_to_install.empty? puts "All required gems are already installed" else puts "Will install the following gems :" gems_to_install.each {|gem_name| puts gem_name } Dir.chdir(bundled_gem_path) # set this to be the working directory while installing the gems gems_to_install.each do |gem_name| puts "\nNow installing #{gem_name}..." system("gem install #{gem_name}") puts "Gem installed." end Dir.chdir(base_wd) # reset the working directory end end |
.install_java(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
30 31 32 |
# File 'lib/taft.rb', line 30 def self.install_java(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") install(:java, debugging, dest, overwrite_ok, project_name, project_abbrev) end |
.install_ruby(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") ⇒ Object
26 27 28 |
# File 'lib/taft.rb', line 26 def self.install_ruby(debugging = false, dest = "", overwrite_ok = false, project_name = "", project_abbrev = "") install(:ruby, debugging, dest, overwrite_ok, project_name, project_abbrev) end |