Module: Retrospec::Plugins::V1::ModuleHelpers
- Included in:
- Config
- Defined in:
- lib/retrospec/plugins/v1/module_helpers.rb
Instance Method Summary collapse
-
#default_retrospec_dir ⇒ Object
the directory where the config, repos, and other info are saved.
- #retrospec_repos_dir ⇒ Object
-
#safe_copy_file(src, dest) ⇒ Object
safely copy and existing file to another dest.
-
#safe_create_file(filepath, content) ⇒ Object
safely creates a file and does not override the existing file.
-
#safe_create_module_files(template_dir, module_path, spec_object) ⇒ Object
creates any file that is contained in the templates/modules_files directory structure loops through the directory looking for erb files or other files.
-
#safe_create_symlink(src, dest) ⇒ Object
copy the symlink and preserve the link.
-
#safe_create_template_file(path, template, spec_object) ⇒ Object
path is the full path of the file to create template is the full path to the template file spec_object is any bindable object which the templates uses for context.
-
#safe_mkdir(dir) ⇒ Object
only creates a directory if the directory doesn’t already exist.
-
#safe_touch(file) ⇒ Object
touch a file, this is useful for setting up trigger files.
Instance Method Details
#default_retrospec_dir ⇒ Object
the directory where the config, repos, and other info are saved
75 76 77 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 75 def default_retrospec_dir File.(File.join(ENV['HOME'], '.retrospec' )) end |
#retrospec_repos_dir ⇒ Object
79 80 81 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 79 def retrospec_repos_dir File.join(default_retrospec_dir, 'repos') end |
#safe_copy_file(src, dest) ⇒ Object
safely copy and existing file to another dest
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 31 def safe_copy_file(src, dest) if File.exists?(dest) and not File.zero?(dest) $stderr.puts "!! #{dest} already exists".warning else if not File.exists?(src) safe_touch(src) else safe_mkdir(File.dirname(dest)) FileUtils.cp(src,dest) end puts " + #{dest}".info end end |
#safe_create_file(filepath, content) ⇒ Object
safely creates a file and does not override the existing file
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 58 def safe_create_file(filepath, content) if File.exists? filepath old_content = File.read(filepath) # if we did a better comparison of content we could be smarter about when we create files if old_content != content or not File.zero?(filepath) $stderr.puts "!! #{filepath} already exists and differs from template".warning end else safe_mkdir(File.dirname(filepath)) unless File.exists? File.dirname(filepath) File.open(filepath, 'w') do |f| f.puts content end puts " + #{filepath}".info end end |
#safe_create_module_files(template_dir, module_path, spec_object) ⇒ Object
creates any file that is contained in the templates/modules_files directory structure loops through the directory looking for erb files or other files. strips the erb extension and renders the template to the current module path filenames must named how they would appear in the normal module path. The directory structure where the file is contained
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 105 def safe_create_module_files(template_dir, module_path, spec_object) templates = Find.find(File.join(template_dir,'module_files')).find_all.sort templates.each do |template| dest = template.gsub(File.join(template_dir,'module_files'), module_path).gsub('.erb', '') if File.symlink?(template) safe_create_symlink(template, dest) elsif File.directory?(template) safe_mkdir(dest) else safe_create_template_file(dest, template, spec_object) end end end |
#safe_create_symlink(src, dest) ⇒ Object
copy the symlink and preserve the link
21 22 23 24 25 26 27 28 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 21 def safe_create_symlink(src,dest) if File.exists? dest $stderr.puts "!! #{dest} already exists and differs from template".warning else FileUtils.copy_entry(src,dest) puts " + #{dest}".info end end |
#safe_create_template_file(path, template, spec_object) ⇒ Object
path is the full path of the file to create template is the full path to the template file spec_object is any bindable object which the templates uses for context
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 86 def safe_create_template_file(path, template, spec_object) # check to ensure parent directory exists file_dir_path = File.(File.dirname(path)) if ! File.exists?(file_dir_path) safe_mkdir(file_dir_path) end File.open(template) do |file| renderer = ERB.new(file.read, 0, '-') content = renderer.result spec_object.get_binding dest_path = File.(path) safe_create_file(dest_path, content) end end |
#safe_mkdir(dir) ⇒ Object
only creates a directory if the directory doesn’t already exist
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 9 def safe_mkdir(dir) if File.exists? dir unless File.directory? dir $stderr.puts "!! #{dir} already exists and is not a directory".fatal end else FileUtils.mkdir_p dir puts " + #{dir}/".info end end |
#safe_touch(file) ⇒ Object
touch a file, this is useful for setting up trigger files
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/retrospec/plugins/v1/module_helpers.rb', line 46 def safe_touch(file) if File.exists? file unless File.file? file $stderr.puts "!! #{file} already exists and is not a regular file".fatal end else FileUtils.touch file puts " + #{file}".info end end |