Module: Eco::Data::Files::Folder
- Includes:
- RelativePath, Language::AuxiliarLogger
- Included in:
- ClassMethods
- Defined in:
- lib/eco/data/files/folder.rb
Constant Summary collapse
- PRESERVED_FILES =
[ /.*\.rb$/, /.*\.sh$/, /.*\.ps1$/, /.*\.ya?ml$/, /.*\.md$/, /.*\.gemspec$/, /Gem/, /Rake/, /LICENSE/, /(?:^|[\\\/])\.[^\\\/]+$/ # i.e. `.env`, `.gitignore` ].freeze
Instance Attribute Summary
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
- #archive_file(filename, subfolder: 'Archive') ⇒ Object
- #clear_folder(folder, pattern = '*', regexp: nil, older_than: nil, newer_than: nil, timeout: 3) ⇒ Object
- #csv_files(folder = '.', regexp: nil, older_than: nil, newer_than: nil, &block) ⇒ Object
-
#ensure_file_path!(filename) ⇒ Object
It ensure that the path to the file exists.
- #ensure_folder!(path) ⇒ Object
- #folder_files(folder = '.', pattern = '*', regexp: nil, older_than: nil, newer_than: nil, &block) ⇒ Object
Methods included from RelativePath
Methods included from Language::AuxiliarLogger
Instance Method Details
#archive_file(filename, subfolder: 'Archive') ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/eco/data/files/folder.rb', line 178 def archive_file(filename, subfolder: 'Archive') src_folder = File.dirname(filename) subpath = File.join(src_folder, subfolder) FileUtils.mkdir_p(subpath) basename = File.basename(filename) archived_file = to_relative_path(File.join(subpath, basename)) log(:info) { msg = [] msg << "Moving file:" msg << " • from: '#{to_relative_path(filename)}'" msg << " • to: '#{archived_file}'" msg.join("\n") } File.rename(filename, archived_file) archived_file end |
#clear_folder(folder, pattern = '*', regexp: nil, older_than: nil, newer_than: nil, timeout: 3) ⇒ Object
Note:
it will exclude files matching any of the patterns defined
in PRESERVED_FILES
Note:
never make folder an argument with default
(force it to be explicitly declared)
127 128 129 130 131 132 133 134 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 |
# File 'lib/eco/data/files/folder.rb', line 127 def clear_folder( folder, pattern = '*', regexp: nil, older_than: nil, newer_than: nil, timeout: 3 ) target_files = folder_files( folder, pattern, regexp: regexp, older_than: older_than, newer_than: newer_than ).select do |file| File.file?(file) end # safety check (exclude certain files) PRESERVED_FILES.reduce(target_files) do |mem, rex| mem.grep_v(rex) # excluse those matching pattern end.tap do |files| next if files.empty? msg = ' • ' msg << files.join("\n • ") msg << "\n" if respond_to?(:session, true) msg = "The following files will be removed:\n#{msg}" send(:session).prompt_user( 'Do you want to proceed? (Y/n):', explanation: msg, default: 'Y', timeout: timeout ) do |response| next unless response.upcase.start_with?('Y') File.delete(*files) end else msg = "The following files have been removed:\n#{msg}" File.delete(*files) log(:info) { msg } end end end |
#csv_files(folder = '.', regexp: nil, older_than: nil, newer_than: nil, &block) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/eco/data/files/folder.rb', line 106 def csv_files( folder = '.', regexp: nil, older_than: nil, newer_than: nil, &block ) folder_files( folder, '*.csv', regexp: regexp, older_than: older_than, newer_than: newer_than, &block ) end |
#ensure_file_path!(filename) ⇒ Object
It ensure that the path to the file exists
22 23 24 25 26 |
# File 'lib/eco/data/files/folder.rb', line 22 def ensure_file_path!(filename) return if File.exist?(filename) ensure_folder!(File.dirname(filename)) end |
#ensure_folder!(path) ⇒ Object
28 29 30 |
# File 'lib/eco/data/files/folder.rb', line 28 def ensure_folder!(path) FileUtils.mkdir_p(path) end |
#folder_files(folder = '.', pattern = '*', regexp: nil, older_than: nil, newer_than: nil, &block) ⇒ Object
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 |
# File 'lib/eco/data/files/folder.rb', line 40 def folder_files( # rubocop:disable Metrics/AbcSize folder = '.', pattern = '*', regexp: nil, older_than: nil, newer_than: nil, &block ) return [] unless folder.is_a?(String) unless Dir.exist?(folder) log(:error) { "Folder '#{folder}' does not exist" } return [] end folder = to_relative_path(folder) pattern ||= '.' pattern = "*.#{pattern}" if pattern.is_a?(Symbol) target = File.join(File.(folder), pattern) to_relative_path( Dir[target] ).tap do |dir_files| dir_files.select! {|f| File.file?(f)} if older_than dir_files.select! do |f| File.mtime(f) < (Time.now - (60*60*24*older_than)) end end if newer_than dir_files.select! do |f| File.mtime(f) > (Time.now - (60*60*24*newer_than)) end end if regexp.is_a?(Regexp) dir_files.select! do |f| File.basename(f).match(regexp) end end end.sort.tap do |files| if files.empty? matchers = [pattern, regexp].compact log(:info) { "Couldn't find local files (#{matchers}): in folder '#{folder}'" } else log(:info) { msg = 'Found local files: ' msg << "\n • " msg << files.join("\n • ") msg << "\n" msg } end next unless block_given? files.each(&block) end end |