|
2 | 2 |
|
3 | 3 | # Handy methods that are not tied to one particular class
|
4 | 4 |
|
| 5 | +require 'fileutils' |
| 6 | + |
5 | 7 | module OctocatalogDiff
|
6 | 8 | module Util
|
7 | 9 | # Helper class to construct catalogs, performing all necessary steps such as
|
@@ -43,6 +45,38 @@ def self.deep_dup(object)
|
43 | 45 | safe_dup(object)
|
44 | 46 | end
|
45 | 47 | end
|
| 48 | + |
| 49 | + # Utility Method! |
| 50 | + # This creates a temporary directory. If the base directory is specified, then we |
| 51 | + # do not remove the temporary directory at exit, because we assume that something |
| 52 | + # else will remove the base directory. |
| 53 | + # |
| 54 | + # prefix - A String with the prefix for the temporary directory |
| 55 | + # basedir - A String with the directory in which to make the tempdir |
| 56 | + # |
| 57 | + # Returns the full path to the temporary directory. |
| 58 | + def self.temp_dir(prefix = 'ocd-', basedir = nil) |
| 59 | + # If the base directory is specified, make sure it exists, and then create the |
| 60 | + # temporary directory within it. |
| 61 | + if basedir |
| 62 | + unless File.directory?(basedir) |
| 63 | + raise Errno::ENOENT, "temp_dir: Base dir #{basedir.inspect} does not exist!" |
| 64 | + end |
| 65 | + return Dir.mktmpdir(prefix, basedir) |
| 66 | + end |
| 67 | + |
| 68 | + # If the base directory was not specified, then create a temporary directory, and |
| 69 | + # send the `at_exit` to clean it up at the conclusion. |
| 70 | + the_dir = Dir.mktmpdir(prefix) |
| 71 | + at_exit do |
| 72 | + begin |
| 73 | + FileUtils.remove_entry_secure the_dir |
| 74 | + rescue Errno::ENOENT # rubocop:disable Lint/HandleExceptions |
| 75 | + # OK if the directory doesn't exist since we're trying to remove it anyway |
| 76 | + end |
| 77 | + end |
| 78 | + the_dir |
| 79 | + end |
46 | 80 | end
|
47 | 81 | end
|
48 | 82 | end
|
0 commit comments