1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
# pathspec-ruby
[](https://badge.fury.io/rb/pathspec) [](https://github.com/highb/pathspec-ruby/actions/workflows/ruby.yml) [](https://codeclimate.com/github/highb/pathspec-ruby/maintainability)
[man Page as HTML](http://highb.github.io/pathspec-ruby/)
[Supported Rubies](https://www.ruby-lang.org/en/downloads/):
- 3.1 (Stable, Tested)
- 3.2 (Stable, Tested)
- 3.3 (Stable, Tested)
Match Path Specifications, such as .gitignore, in Ruby!
Follows .gitignore syntax defined on [gitscm](http://git-scm.com/docs/gitignore)
.gitignore functionality ported from [Python pathspec](https://pypi.python.org/pypi/pathspec/0.2.2) by [@cpburnz](https://github.com/cpburnz/python-path-specification)
## Build/Install from Rubygems
```shell
gem install pathspec
```
## CLI Usage
```bash
➜ cat .gitignore
*.swp
/coverage/
➜ bundle exec pathspec-rb specs_match "coverage/foo"
/coverage/
➜ bundle exec pathspec-rb specs_match "file.swp"
*.swp
➜ bundle exec pathspec-rb match "file.swp"
➜ echo $?
0
➜ ls
Gemfile Gemfile.lock coverage file.swp source.rb
➜ bundle exec pathspec-rb tree .
./coverage
./coverage/index.html
./file.swp
```
## Usage
```ruby
require 'pathspec'
# Create a .gitignore-style Pathspec by giving it newline separated gitignore
# lines, an array of gitignore lines, or any other enumable object that will
# give strings matching the .gitignore-style (File, etc.)
gitignore = PathSpec.from_filename('spec/files/gitignore_readme')
# Our .gitignore in this example contains:
# !**/important.txt
# abc/**
# true, matches "abc/**"
gitignore.match 'abc/def.rb'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/def.rb'
# false, because it has been negated using the line "!**/important.txt"
gitignore.match 'abc/important.txt'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme match 'abc/important.txt'
# Give a path somewhere in the filesystem, and the Pathspec will return all
# matching files underneath.
# Returns ['/src/repo/abc/', '/src/repo/abc/123']
gitignore.match_tree '/src/repo'
# CLI equivalent: pathspec.rb -f spec/files/gitignore_readme tree /src/repo
# Give an enumerable of paths, and Pathspec will return the ones that match.
# Returns ['/abc/123', '/abc/']
gitignore.match_paths ['/abc/123', '/abc/important.txt', '/abc/']
# There is no CLI equivalent to this.
```
## Example Usage in Gemspec
```
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "gemspec_pathspec_test/version"
require 'pathspec'
Gem::Specification.new do |spec|
spec.name = "gemspec_pathspec_test"
spec.version = GemspecPathspecTest::VERSION
spec.authors = ["Brandon High"]
spec.email = ["highb@users.noreply.github.com"]
spec.summary = "whatever"
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
ps = PathSpec.from_filename('.gitignore')
spec.files = Dir['lib/*.rb'].reject { |f| ps.match(f) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
end
```
## Building/Installing from Source
```shell
git clone git@github.com:highb/pathspec-ruby.git
cd pathspec-ruby && bash ./build_from_source.sh
```
## Contributing
Pull requests, bug reports, and feature requests welcome! :smile: I've tried to write exhaustive tests but who knows what cases I've missed.
## Releasing
This is mainly a reminder to myself but the release process is:
1. Make sure CI is passing
2. Update the CHANGELOG with relevant changes to Gem consumers
3. Update version in gemspec with correct SemVer bump for scope of changes
4. Tag/release using GitHub UI and the Build & Push workflow should do the rest.
|