filename_increment 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ea5858882ee73443603452fcba99a4691bf7c69bf70f16eadeda434df02380fd
4
+ data.tar.gz: 2e0fd301ebde9d9abffa067979baedae51467d2da0bf28a2972e440d73b49cff
5
+ SHA512:
6
+ metadata.gz: 9ad677398faf5e60db470a6659685d44e25ecd6406103a2a5f5b5b55f07932c4722aa1719c8c91bd2d403b2fcf545e53f53eeebd1507c1ae427715dfcfaa9217
7
+ data.tar.gz: 23e17d53ea2ad3663e6ffcb36d33ff797d466d9f72f1ad0d990fcc978b3708ae113c18c094bf23bd74091aefc3d3d9e814fbf3c92b9b379fd9ca98406e3a5417
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea/
@@ -0,0 +1 @@
1
+ filename_increment
@@ -0,0 +1 @@
1
+ ruby-2.7.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dimidev@olympusone. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in filename_increment.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dimitrios Panagkasidis
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,113 @@
1
+ # filename_increment
2
+ Please consider starring the project to show your heart and support.
3
+
4
+ Project is based on this [repo](https://github.com/jonschlinkert/add-filename-increment), show him some love please ;).
5
+ >When copying or moving files, it's common for operating systems to automatically
6
+ >add an increment or 'copy' to duplicate file names. This does that for Ruby applications, with automatic platform
7
+ >detection and support for Linux, MacOs, and Windows conventions.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'filename_increment'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install filename_increment
24
+
25
+ ## Usage
26
+ All methods automatically detect the platform to use, unless platform is defined on the options.
27
+
28
+ ```ruby
29
+ FilenameIncrement.new('foo/bar.txt', platform: 'linux').to_s
30
+ # foo/bar (copy).txt
31
+ FilenameIncrement.new('foo/bar.txt', platform: 'win32').to_s
32
+ # foo/bar (2).txt
33
+ FilenameIncrement.new('foo/bar.txt', platform: 'darwin').to_s
34
+ # foo/bar copy.txt
35
+ ```
36
+
37
+ ## Options
38
+
39
+ ### options.fs
40
+
41
+ **Description**: Check the file system, and automatically increment the file based on existing files. Thus, if the file name is `foo.txt`, and `foo (2).txt` already exists, the file will automatically be renamed to `foo (3).txt`.
42
+
43
+ Also uses the correct conventions for Linux, Windows (win32), and MacOS (darwin).
44
+
45
+ **Type**: `boolean`
46
+
47
+ **Default**: `nil`
48
+
49
+ ### options.platform
50
+
51
+ **Description**: Specify the platform conventions to use.
52
+
53
+ **Type**: `String`
54
+
55
+ **Default**: Uses `Gem::Platform.local.os`. Valid values are `linux`, `win32` and `darwin`.
56
+
57
+ ## Operating Systems
58
+
59
+ * [Linux](#linux)
60
+ * [MacOS](#macos)
61
+ * [Windows](#windows)
62
+
63
+ **Supported Operating Systems**
64
+
65
+ Currently Windows, Darwin (MacOS), and Linux are supported. This library attempts to automatically use the correct conventions for each operating system. Please [create an issue](../../issues/new) if you ecounter a bug.
66
+
67
+ If you use an operating system with different conventions, and you would like for this library to add support, please [create an issue](../../issues/new) with a detailed description of those conventions, or feel free to do a [pull request](.github/contributing.md).
68
+
69
+ ### Linux
70
+
71
+ When a file is copied or moved, and the destination file path already exists, Linux uses the following conventions for incrementing the file name.
72
+
73
+ | **Source path** | **Destination path** | **Type** | **Directory<sup>1</sup>** |
74
+ | --- | --- | --- | --- |
75
+ | `foo.txt` | `foo (copy).txt`, `foo (another copy).txt`, `foo (3rd copy).txt`, ... | file | Same directory as source |
76
+ | `foo` | `foo (copy)`, `foo (another copy)`, `foo (3rd copy)`, ... | directory | Same directory as source |
77
+
78
+ <small><sup>1</sup> _On Linux, when a file or folder is copied or moved to a different directory and another file or folder with the same name exists in that directory, you are prompted to choose a new name for the file or folder, or to cancel or skip the operation._ </small>
79
+
80
+ ### MacOS
81
+
82
+ When a file is copied or moved, and the destination file path already exists, MacOS uses the following conventions for incrementing the file name.
83
+
84
+ | **Source path** | **Destination path** | **Type** | **Directory<sup>1</sup>** |
85
+ | --- | --- | --- | --- |
86
+ | `foo.txt` | `foo copy.txt`, `foo copy 2.txt`, ... | file | Same directory as source |
87
+ | `foo.txt` | `foo 2.txt`, `foo 3.txt`, ... | file | Different directory than source |
88
+ | `foo` | `foo copy`, `foo copy 2`, ... | directory | Same directory as source |
89
+
90
+ <small><sup>1</sup> _MacOS uses different conventions for incrementing file names when the source file is copied, moved or renamed to a different directory, versus when the file is copied into the same directory._ </small>
91
+
92
+ ### Windows
93
+
94
+ When a file is copied or moved, and the destination file path already exists, Windows uses the following conventions for incrementing the file name.
95
+
96
+ | **Source path** | **Destination path** | **Type** | **Directory<sup>1</sup>** |
97
+ | --- | --- | --- | --- |
98
+ | `foo.txt` | `foo - Copy.txt` | file | Same directory as source |
99
+ | `foo.txt` | `foo (2).txt` | file | Different directory than source |
100
+ | `foo (2).txt` | `foo (3).txt` | file | Different directory than source |
101
+ | `foo` | `foo - Copy` | directory | Same directory as source |
102
+ | `foo - Copy` | `foo - Copy (2)` | directory | Same directory as source |
103
+
104
+ <small><sup>1</sup> _Windows uses different conventions for incrementing file names when the source file is copied, moved or renamed to a different directory, versus when the file is copied into the same directory. Also, when a folder is copied to a new directory, and the new directory already has a folder with the same name, Windows just merges the folders automatically._ </small>
105
+
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
110
+
111
+ ## Code of Conduct
112
+
113
+ Everyone interacting in the FilenameIncrement project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/filename_increment/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/TODO.md ADDED
@@ -0,0 +1,3 @@
1
+ ## TODO
2
+ - [ ] Add more extensive test
3
+ - [ ] Add option to parameterize path (underscored)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "filename_increment"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/filename_increment/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "filename_increment"
5
+ spec.version = FilenameIncrement::VERSION
6
+ spec.authors = ["Dimitrios Panagkasidis"]
7
+ spec.email = ["dimidev@olympusone.com"]
8
+
9
+ spec.summary = %q{Automatically add an increment or 'copy' to duplicate file names}
10
+ spec.description = %q{When copying files, it's common for operating systems to append a numerical increment or
11
+ the word 'copy' to a file name to prevent the existing file from being overwritten.
12
+ This library allows you to do the same thing in your Ruby application.}
13
+ spec.homepage = "https://rubygems.org/gems/filename_increment"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/olympusone/filename_increment"
21
+ spec.metadata["changelog_uri"] = "https://github.com/olympusone/filename_increment/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
@@ -0,0 +1,102 @@
1
+ require "filename_increment/version"
2
+ require 'pathname'
3
+
4
+ class FilenameIncrement
5
+ class Error < StandardError; end
6
+
7
+ class FileNotFound < IOError; end
8
+
9
+ ORDINALS = %w[th st nd rd]
10
+
11
+ attr_reader :path
12
+
13
+ def initialize(filepath, options = {})
14
+ @options = options
15
+
16
+ @platform = @options[:platform] || Gem::Platform.local.os
17
+ @file = parse(filepath)
18
+ @start = @options[:start] || 1
19
+
20
+ @file = parse(filepath)
21
+
22
+ if @start == 1 && (@platform == 'win32' || @platform == 'windows')
23
+ @start += 1
24
+ end
25
+
26
+ if @options[:fs].is_a? TrueClass
27
+ if File.exist?(filepath)
28
+ name = @file[:name]
29
+ dest = filepath
30
+
31
+ while File.exist?(dest)
32
+ @start += 1
33
+ @file[:basename] = format(name, @start) + @file[:extname]
34
+
35
+ dest = File.join(@file[:dirname], @file[:basename])
36
+ end
37
+ else
38
+ # raise error if file not found
39
+ raise FileNotFound.new('File not found')
40
+ end
41
+ else
42
+ @file[:basename] = format(@file[:name], @start) + @file[:extname]
43
+ end
44
+
45
+ @path = Pathname.new(@file[:dirname]).join(@file[:basename])
46
+ end
47
+
48
+ def to_s
49
+ @path.cleanpath.to_s
50
+ end
51
+
52
+ private
53
+
54
+ def parse(filepath)
55
+ pathname = Pathname.new(filepath)
56
+
57
+ path = {path: filepath}
58
+ path[:dirname] = pathname.dirname
59
+ path[:basename] = pathname.basename
60
+ path[:name] = pathname.basename(pathname.extname)
61
+ path[:extname] = pathname.extname
62
+
63
+ path
64
+ end
65
+
66
+ def ordinal(number)
67
+ raise TypeError.new('expected a number') unless number.is_a? Numeric
68
+
69
+ ORDINALS[((number % 100) - 20) % 10] || ORDINALS[number % 100] || ORDINALS[0]
70
+ end
71
+
72
+ def to_ordinal(number)
73
+ raise TypeError.new('expected a number') unless number.is_a? Numeric
74
+
75
+ "#{number}#{ordinal(number)}"
76
+ end
77
+
78
+ def format(stem, number)
79
+ case @platform
80
+ when 'win32', 'windows'
81
+ number > 1 ? "#{stem} (#{number})" : stem
82
+ when 'linux'
83
+ if number == 0
84
+ stem
85
+ elsif number == 1
86
+ "#{stem} (copy)"
87
+ elsif number == 2
88
+ "#{stem} (another copy)"
89
+ else
90
+ "#{stem} (#{to_ordinal(number)} copy)"
91
+ end
92
+ when 'darwin'
93
+ if number == 1
94
+ "#{stem} copy"
95
+ elsif number > 1
96
+ "#{stem} (#{stem} copy #{number})"
97
+ end
98
+ else
99
+ number > 1 ? "#{stem} (#{number})" : stem
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ class FilenameIncrement
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filename_increment
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dimitrios Panagkasidis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ When copying files, it's common for operating systems to append a numerical increment or
15
+ the word 'copy' to a file name to prevent the existing file from being overwritten.
16
+ This library allows you to do the same thing in your Ruby application.
17
+ email:
18
+ - dimidev@olympusone.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".gitignore"
24
+ - ".ruby-gemset"
25
+ - ".ruby-version"
26
+ - ".travis.yml"
27
+ - CHANGELOG.md
28
+ - CODE_OF_CONDUCT.md
29
+ - Gemfile
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - TODO.md
34
+ - bin/console
35
+ - bin/setup
36
+ - filename_increment.gemspec
37
+ - lib/filename_increment.rb
38
+ - lib/filename_increment/version.rb
39
+ homepage: https://rubygems.org/gems/filename_increment
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://rubygems.org/gems/filename_increment
44
+ source_code_uri: https://github.com/olympusone/filename_increment
45
+ changelog_uri: https://github.com/olympusone/filename_increment/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.3.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Automatically add an increment or 'copy' to duplicate file names
65
+ test_files: []