Skip to content

Commit 7affad0

Browse files
committed
Refactor api and release 1.2.0
1 parent faf2a36 commit 7affad0

12 files changed

Lines changed: 66 additions & 60 deletions

File tree

.code_manifest_test.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
code_manifest (1.1.1)
4+
code_manifest (1.2.0)
55

66
GEM
77
remote: https://rubygems.org/

lib/code_manifest.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,47 @@
88
module CodeManifest
99
class Error < StandardError; end
1010

11-
DOTFILE = '.code_manifest.yml'
11+
MANIFEST_FILE = '.code_manifest.yml'
1212
KEY_PATTERN = /[a-z_0-9]+/.freeze
1313

1414
class << self
1515
def [](name)
1616
manifests[name.to_s]
1717
end
1818

19+
def root(start_path: Dir.pwd, reset: false)
20+
@root = nil if reset
21+
@root ||= find_root(start_path)
22+
end
23+
1924
private
2025

2126
def manifests
2227
@manifests ||= begin
23-
manifest_file = traverse_files(DOTFILE, Dir.pwd)
24-
25-
unless manifest_file
26-
raise "#{DOTFILE} was not found in your project directory, please check README for instructions."
27-
end
28-
29-
root = Pathname.new(manifest_file).dirname
28+
manifest_file = root.join(MANIFEST_FILE)
3029

3130
load_manifest(manifest_file).each_with_object({}) do |(name, patterns), collection|
3231
next unless name.match?(KEY_PATTERN)
3332

34-
raise ArgumentError, "#{name} defined multiple times in #{DOTFILE}" if collection.key?(name)
33+
raise ArgumentError, "#{name} defined multiple times in #{MANIFEST_FILE}" if collection.key?(name)
3534

36-
collection[name] = Manifest.new(root, patterns.flatten)
35+
collection[name] = Manifest.new(patterns.flatten)
3736
end
3837
end
3938
end
4039

41-
def traverse_files(filename, start_dir)
42-
Pathname.new(start_dir).expand_path.ascend do |dir|
43-
file = dir.join(filename)
44-
return file.to_s if file.exist?
40+
def find_root(path)
41+
Pathname.new(path).expand_path.ascend do |dir|
42+
return dir if dir.join(MANIFEST_FILE).exist?
4543
end
44+
45+
raise "#{MANIFEST_FILE} was not found in your project directory, please check README for instructions."
4646
end
4747

4848
# https://stackoverflow.com/a/71192990
4949
def load_manifest(file)
5050
YAML.load_file(file, aliases: true)
5151
rescue ArgumentError
52-
puts YAML.load_file(file)
5352
YAML.load_file(file)
5453
end
5554
end

lib/code_manifest/manifest.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ module CodeManifest
77
class Manifest
88
GLOB_OPTIONS = File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB
99

10-
attr_reader :root, :rules
10+
attr_reader :rules
1111

12-
def initialize(root, patterns)
13-
@root = root
12+
def initialize(patterns)
1413
@rules ||= patterns.map do |pattern|
15-
Rule.new(root, pattern)
14+
Rule.new(CodeManifest.root, pattern)
1615
end
1716
end
1817

@@ -22,16 +21,20 @@ def files
2221

2322
def digest
2423
@digest ||= begin
25-
digests = files.map { |file| Digest::MD5.file(root.join(file)).hexdigest }
24+
digests = files.map { |file| Digest::MD5.file(CodeManifest.root.join(file)).hexdigest }
2625
Digest::MD5.hexdigest(digests.join).freeze
2726
end
2827
end
2928

3029
def matches(paths)
31-
result_paths = paths.select { |path| inclusion_rules.any? { |rule| rule.match?(path) } }
32-
result_paths.reject! { |path| exclusion_files.any? { |rule| rule.match?(path) } }
30+
result_paths = paths.select do |path|
31+
inclusion_rules.any? { |rule| rule.match?(path) }
32+
end
33+
result_paths.reject! do |path|
34+
exclusion_files.any? { |rule| rule.match?(path) }
35+
end
3336

34-
result_paths
37+
result_paths.sort!
3538
end
3639

3740
private
@@ -56,7 +59,8 @@ def files_with_relative_path(files)
5659
files.map do |file|
5760
pathname = Pathname.new(file)
5861
next if pathname.directory?
59-
pathname.relative_path_from(root.expand_path).to_s
62+
63+
pathname.relative_path_from(CodeManifest.root).to_s
6064
end.compact
6165
end
6266
end

lib/code_manifest/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module CodeManifest
4-
VERSION = '1.1.1'
4+
VERSION = '1.2.0'
55
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
foo: &foo
2+
- "/foo/**/*"
3+
bar:
4+
- "/foo/bar.txt"
5+
no_bar:
6+
- "*foo"
7+
- "!spec/fixtures/foo/bar*"
File renamed without changes.

spec/lib/code_manifest/manifest_spec.rb

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,31 @@
44
require 'fileutils'
55

66
RSpec.describe CodeManifest::Manifest do
7-
let(:root) { Pathname.new('tmp/manifest_spec').expand_path }
7+
let(:root) { CodeManifest.root }
88

99
describe '#files' do
10-
let(:patterns) { ['/foo', 'bar/*', '!bar/exclude'] }
11-
let(:manifest) { described_class.new(root.expand_path, patterns) }
10+
let(:patterns) { ['/foo/foo.md', 'bar/*', '!bar/exclude'] }
11+
let(:manifest) { described_class.new(patterns) }
1212

1313
around do |example|
14-
root.mkpath
15-
FileUtils.touch(root.join('foo'))
1614
root.join('bar').mkpath
1715
FileUtils.touch(root.join('bar/include'))
1816
FileUtils.touch(root.join('bar/exclude'))
1917
example.run
20-
root.rmtree
2118
end
2219

2320
it 'returns only included files' do
24-
expect(manifest.files).to match_array(['bar/include', 'foo'])
21+
expect(manifest.files).to match_array(['bar/include', 'foo/foo.md'])
2522
end
2623

2724
context 'with different type of globs' do
2825
let(:patterns) { ['dir_bar/**/*'] }
29-
let(:manifest) { described_class.new(root.expand_path, patterns) }
26+
let(:manifest) { described_class.new(patterns) }
3027

3128
around do |example|
32-
Dir.mktmpdir do |tmp_dir|
33-
Dir.chdir(tmp_dir) do
34-
FileUtils.mkdir(root.join('dir_bar'))
35-
FileUtils.touch(root.join('dir_bar/foo'))
36-
example.run
37-
end
38-
end
29+
FileUtils.mkdir(root.join('dir_bar'))
30+
FileUtils.touch(root.join('dir_bar/foo'))
31+
example.run
3932
end
4033

4134
it 'excludes directories' do
@@ -63,7 +56,7 @@
6356

6457
expected = [
6558
'dir_bar/.bar',
66-
'dir_bar/foo',
59+
'dir_bar/foo'
6760
]
6861

6962
expect(manifest.files).to match_array(expected)
@@ -80,7 +73,7 @@
8073

8174
expected = [
8275
'foo.x',
83-
'foo.y',
76+
'foo.y'
8477
]
8578

8679
expect(manifest.files).to match_array(expected)
@@ -91,7 +84,7 @@
9184

9285
describe '#digest' do
9386
let(:patterns) { ['/foo', 'bar/*', '!bar/exclude'] }
94-
let(:manifest) { described_class.new(root.expand_path, patterns) }
87+
let(:manifest) { described_class.new(patterns) }
9588

9689
around do |example|
9790
root.mkpath
@@ -104,13 +97,13 @@
10497
end
10598

10699
it 'returns only included files' do
107-
expect(manifest.digest).to eq('020eb29b524d7ba672d9d48bc72db455')
100+
expect(manifest.digest).to eq('74be16979710d4c4e7c6647856088456')
108101
end
109102
end
110103

111104
describe '#matches' do
112105
let(:patterns) { ['/foo', 'bar/*', '!bar/exclude'] }
113-
let(:manifest) { described_class.new(root.expand_path, patterns) }
106+
let(:manifest) { described_class.new(patterns) }
114107
let(:paths) do
115108
[
116109
'bar/exclude',
@@ -135,9 +128,9 @@
135128

136129
it 'returns matched paths' do
137130
expect(manifest.matches(paths)).to match_array([
138-
'bar/iniclude',
139-
'foo',
140-
])
131+
'bar/iniclude',
132+
'foo'
133+
])
141134
end
142135
end
143136
end

0 commit comments

Comments
 (0)