file-transaction 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +38 -0
- data/LICENSE +21 -0
- data/README.md +24 -0
- data/file-transaction.gemspec +25 -0
- data/lib/file-transaction.rb +54 -0
- data/lib/file-transaction/version.rb +3 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5c8da8359d8abcf3fe908a06823f670b66ed112faae1a0fd44fc9990ef8b9d08
|
4
|
+
data.tar.gz: 6f46a70a553a34c4edf7853dc3e002457f62d4123e43adf3866c2f2c6e79a366
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5183ac61f302f77b83e2e3bb09220274c8fb280c1979a8a1be9aab12e8f7667834a5214266544a95660f7fd82ac95bb9066d4428e78d0ca4775a1afa529c5727
|
7
|
+
data.tar.gz: 955fc9612e10a406db41fca89ff0f04503675c853cc5241c3f8918cbd68c05cff2754bceba850819fe743ebf2849fd22aeb193f963fa269ea22888cb9efe07b4
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
file-transaction (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.3)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
method_source (1.0.0)
|
12
|
+
pry (0.14.0)
|
13
|
+
coderay (~> 1.1)
|
14
|
+
method_source (~> 1.0)
|
15
|
+
rspec (3.10.0)
|
16
|
+
rspec-core (~> 3.10.0)
|
17
|
+
rspec-expectations (~> 3.10.0)
|
18
|
+
rspec-mocks (~> 3.10.0)
|
19
|
+
rspec-core (3.10.1)
|
20
|
+
rspec-support (~> 3.10.0)
|
21
|
+
rspec-expectations (3.10.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-mocks (3.10.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-support (3.10.2)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
arm64-darwin-20
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
file-transaction!
|
34
|
+
pry
|
35
|
+
rspec
|
36
|
+
|
37
|
+
BUNDLED WITH
|
38
|
+
2.2.5
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Daniel Inkpen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# File transaction
|
2
|
+
|
3
|
+
Copy a directory to a temp directory temporarily to perform mutations, to then copy it back.
|
4
|
+
|
5
|
+
If any exceptions are raised within the block, the changes will not be copied back.
|
6
|
+
|
7
|
+
# Usage
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
# Will create `a`, `b`, `c` in `/my/path`
|
11
|
+
File.transaction("/my/path") do |dir|
|
12
|
+
FileUtils.touch(dir.join("a"))
|
13
|
+
FileUtils.touch(dir.join("b"))
|
14
|
+
system("echo 'c' >#{dir}/c")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Will make no changes to `/my/path`
|
18
|
+
File.transaction("/my/path") do |dir|
|
19
|
+
FileUtils.touch(dir.join("a"))
|
20
|
+
FileUtils.touch(dir.join("b"))
|
21
|
+
system("echo 'c' >#{dir}/c")
|
22
|
+
raise "Any error" # <-------------
|
23
|
+
end
|
24
|
+
```
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "file-transaction/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "file-transaction"
|
7
|
+
spec.version = FileTransaction::VERSION
|
8
|
+
spec.authors = ["Daniel Inkpen"]
|
9
|
+
spec.email = ["dan2552@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "ActiveRecord-like transaction block for files in a directory"
|
12
|
+
spec.description = "Copy a directory to a temp directory temporarily to perform mutations, to then copy it back. If any exceptions are raised within the block, the changes will not be copied back."
|
13
|
+
spec.homepage = "https://github.com/Dan2552/file-transaction"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "pathname"
|
2
|
+
require "fileutils"
|
3
|
+
require "tmpdir"
|
4
|
+
|
5
|
+
module FileTransaction
|
6
|
+
# @internal
|
7
|
+
#
|
8
|
+
# Copies all contents of a directory instead of the directory itself.
|
9
|
+
#
|
10
|
+
def self.copy_files(directory, target)
|
11
|
+
if files_in(target).empty?
|
12
|
+
FileUtils.cp_r(File.join(directory, "."), target)
|
13
|
+
else
|
14
|
+
relative_files_in(directory).each do |file|
|
15
|
+
FileUtils.mkdir_p(File.join(target, file.dirname))
|
16
|
+
begin
|
17
|
+
FileUtils.cp(File.join(directory, file), File.join(target, file))
|
18
|
+
rescue Errno::EACCES
|
19
|
+
FileUtils.rm_rf(File.join(target, file))
|
20
|
+
FileUtils.cp(File.join(directory, file), File.join(target, file))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
files_for_deletion = relative_files_in(target) - relative_files_in(directory)
|
25
|
+
|
26
|
+
files_for_deletion.each do |file|
|
27
|
+
FileUtils.rm(File.join(target, file))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# @internal
|
33
|
+
#
|
34
|
+
def self.files_in(directory)
|
35
|
+
Dir.glob(File.join(directory, "**", "*"), File::FNM_DOTMATCH)
|
36
|
+
.reject { |file| file.end_with?(".") || file.end_with?("..") || File.directory?(file) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.relative_files_in(directory)
|
40
|
+
files_in(directory).map { |file| Pathname.new(file).relative_path_from(directory) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class File
|
45
|
+
def self.transaction(directory, &blk)
|
46
|
+
raise "#{directory} is not a directory" unless File.directory?(directory)
|
47
|
+
|
48
|
+
Dir.mktmpdir do |tmp_directory|
|
49
|
+
FileTransaction.copy_files(directory, tmp_directory)
|
50
|
+
blk.call(Pathname.new(tmp_directory))
|
51
|
+
FileTransaction.copy_files(tmp_directory, directory)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file-transaction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Inkpen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Copy a directory to a temp directory temporarily to perform mutations,
|
28
|
+
to then copy it back. If any exceptions are raised within the block, the changes
|
29
|
+
will not be copied back.
|
30
|
+
email:
|
31
|
+
- dan2552@gmail.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- ".rspec"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- file-transaction.gemspec
|
42
|
+
- lib/file-transaction.rb
|
43
|
+
- lib/file-transaction/version.rb
|
44
|
+
homepage: https://github.com/Dan2552/file-transaction
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.0.3
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: ActiveRecord-like transaction block for files in a directory
|
67
|
+
test_files: []
|