flgen 0.17.0 → 0.19.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bf48956c855b1e0bef4cb503f0d587991d16ba1ff4f36c1aa3990ba7a343263
4
- data.tar.gz: ce22e515604e2790ce7ecba9d04eedfc16212ebfa5d7e20cf43a4ba233abf16c
3
+ metadata.gz: 93ba3a15351e9da39f2dfbbd23c54790140fe7134be768352aa2430533008194
4
+ data.tar.gz: 0e4cc12fae241384ec559682d965b68a63362ca0c934013c1312ac93eaec28cb
5
5
  SHA512:
6
- metadata.gz: 14eb5efde8db89f0c3bab5c380d482dd22d61172a2ad93e3654cd5edf46983dda17b4c0e164f42a1bb40dff785af9a9e21ef73c012d873d35f056be085949e3e
7
- data.tar.gz: bd522a28b24a175bc4f440af52fb548f6e01dddaa81b12e7ef3fa82f0d11b21956f7ade28a9bf3684e84e55b084b98868383c6f710d187bb27fd5e3b16de2337
6
+ metadata.gz: 01ffbee882b46664e9e77a63977abe0e5c1de2a7c75271a1ce6b0284ce7a0517d1785d8e14d95c524959aedfbc8482f6dba3174001311034fd88d863cb013cd3
7
+ data.tar.gz: 398adf788fa50f20d2cdda686b3364b8978a05884e554aecc19ae6c5beb908049a49e6de852b5d7ba41552e0c29310a9826c63c2e49fef2cd65899515133747f
data/README.md CHANGED
@@ -24,24 +24,29 @@ $ gem install flgen
24
24
 
25
25
  FLGen prives APIs listed below to describe your filelists.
26
26
 
27
- * `source_file(path, from: :current, base: nil)`
27
+ * `source_file(path, from: nil)`
28
28
  * Add the given source file to the current filelist.
29
- * `file_list(path, from: :root, base: nil)`
29
+ * `file_list(path, from: nil)`
30
30
  * Load the given filelist.
31
- * `library_file(path, from: :current, base: nil)`
31
+ * `library_file(path, from: nil)`
32
32
  * Add the given file to the list of library files.
33
- * `include_directory(path, from: :current, base: nil)`
33
+ * `include_directory(path, from: nil)`
34
34
  * Add the given directory to the list of include direcotries.
35
- * `library_directory(path, from: :current, base: nil)`
35
+ * `library_directory(path, from: nil)`
36
36
  * Add the given directory to the list of library directories.
37
+ * `find_files(patterns, from: nil)`
38
+ * Return an array of filenames matching the given patterns strings.
39
+ * See [here](https://docs.ruby-lang.org/en/3.2/Dir.html#method-c-glob) for the format of a pattern string.
40
+ * `find_file(patterns, from: nil)`
41
+ * Return the first filename matching the given pattern strings.
42
+ * `file?(path, from: :current)`
43
+ * Return `true` if the given file exists.
44
+ * `directory?(path, from: :current)`
45
+ * Return `true` if the given directory exists.
37
46
  * `define_macro(name, value = nil)`
38
47
  * Define a text macro.
39
48
  * `macro?(name)`/`macro_defined?(name)`
40
49
  * Return `true` if the given macro is defined.
41
- * `file?(path, from: :current, base: nil)`
42
- * Return `treu` if the given file exists.
43
- * `directory?(path, from: :current, base: nil)`
44
- * Return `true` if the given directory exists.
45
50
  * `env?(name)`
46
51
  * Return `true` if the givne environment variable is defined.
47
52
  * `env(name)`
@@ -54,6 +59,10 @@ FLGen prives APIs listed below to describe your filelists.
54
59
  * If `tool` is specified the given argument is added only when `tool` is matched with the targe tool.
55
60
  * `target_tool?(tool)`
56
61
  * Return `true` if the given tool is matched with the targe tool.
62
+ * `default_search_path(**search_paths)`
63
+ * Change the default behavior when the `from` argument is not specified.
64
+ * `reset_default_search_path(*target_types)`
65
+ * Reset the default behavior when the `from` argument is not specified.
57
66
 
58
67
  FLGen's filelist is designed as an inernal DSL with Ruby. Therefore you can use Ruby's syntax. For example:
59
68
 
@@ -65,10 +74,14 @@ else
65
74
  end
66
75
  ```
67
76
 
68
- ### About `from`/`base` arguments
77
+ ### About the `from` argument
69
78
 
70
79
  The `from` argument is to specify how to search the given file or directory. You can specify one of three below.
71
80
 
81
+ * a directory path
82
+ * Search the given file or directory from the directory path specified by the `from` argument.
83
+ * `:cwd`
84
+ * Search the given file or directory from the current working directory.
72
85
  * `:current`
73
86
  * Search the given file or directory from the directory where the current filelist is.
74
87
  * `:root`
@@ -78,9 +91,31 @@ The `from` argument is to specify how to search the given file or directory. You
78
91
  * `:local_root`
79
92
  * Search the given file or directory from the repository root directory where the current filelist belongs to.
80
93
 
81
- The `from` argument is ignored if the given path is an absolute path or the `base` argument is specified.
94
+ Default behaviors when the `from` argument is not spcified are listed below:
95
+
96
+
97
+ | API name | Default `from` argument |
98
+ |:------------------|:------------------------|
99
+ | source_file | :current |
100
+ | file_list | :root |
101
+ | library_file | :current |
102
+ | include_directory | :current |
103
+ | library_directory | :current |
104
+ | find_files | :current |
105
+ | find_file | :current |
82
106
 
83
- The `base` argument is to specify the serach direcotry for the given file or directory.
107
+ You can change the above default behaviors by using the `default_search_path` API.
108
+ In addition, you can reset the default behaviors by using the `reset_default_search_path` API.
109
+
110
+ ```ruby
111
+ default_search_path source_file: :root, file_list: :current
112
+ source_file 'foo.sv' # FLGen will search the 'foo.sv' file from the root directories.
113
+ file_list 'bar.list.rb' # FLGen will eaarch the 'bar.list.rb' file from the directory where this file list is.
114
+
115
+ reset_default_search_path :source_file, :file_list
116
+ source_file 'baz.sv' # FLGen will search the 'baz.sv' file from the directory where this file list is.
117
+ file_list 'qux.list.rb' # FLGen will eaarch the 'qux.list.rb' file from the root directories.
118
+ ```
84
119
 
85
120
  #### Example
86
121
 
data/lib/flgen/context.rb CHANGED
@@ -12,18 +12,18 @@ module FLGen
12
12
  @source_files ||= []
13
13
  end
14
14
 
15
- def add_source_file(root, path)
15
+ def add_source_file(path)
16
16
  return if runtime?
17
17
 
18
- file = SourceFile.new(root, path)
18
+ file = SourceFile.new(path)
19
19
  add_source_file?(file) &&
20
20
  (source_files << file.remove_ext(@options[:rm_ext]))
21
21
  end
22
22
 
23
- def add_library_file(root, path)
23
+ def add_library_file(path)
24
24
  return if runtime?
25
25
 
26
- file = SourceFile.new(root, path)
26
+ file = SourceFile.new(path)
27
27
  add_library_file?(file) &&
28
28
  add_compile_argument(Arguments::LibraryFile.new(file))
29
29
  end
@@ -6,53 +6,54 @@ module FLGen
6
6
  @context = context
7
7
  @path = path
8
8
  @root_directories = extract_root
9
+ @default_search_path = init_default_search_path
9
10
  end
10
11
 
11
- def file_list(path, from: :root, base: nil, raise_error: true)
12
- location = caller_location
13
- load_file_list(path, from, base, location, raise_error)
12
+ def default_search_path(**search_paths)
13
+ @default_search_path.update(search_paths)
14
14
  end
15
15
 
16
- def source_file(path, from: :current, base: nil, raise_error: true)
17
- location = caller_location
18
- add_file_entry(path, from, base, location, raise_error, :add_source_file)
16
+ def reset_default_search_path(*target_types)
17
+ target_types.each { |type| @default_search_path.delete(type) }
19
18
  end
20
19
 
21
- def library_file(path, from: :current, base: nil, raise_error: true)
22
- location = caller_location
23
- add_file_entry(path, from, base, location, raise_error, :add_library_file)
20
+ def file_list(path, from: nil, raise_error: true)
21
+ load_file_list(path, from, raise_error, caller_location)
24
22
  end
25
23
 
26
- def define_macro(macro, value = nil)
27
- @context.define_macro(macro, value)
24
+ def source_file(path, from: nil, raise_error: true)
25
+ add_entry(path, from, raise_error, __callee__, caller_location)
28
26
  end
29
27
 
30
- def macro?(macro)
31
- @context.macros.include?(macro.to_sym)
32
- end
28
+ define_method(:library_file, instance_method(:source_file))
29
+ define_method(:include_directory, instance_method(:source_file))
30
+ define_method(:library_directory, instance_method(:source_file))
33
31
 
34
- alias_method :macro_defined?, :macro?
32
+ def find_files(patterns, from: nil, &block)
33
+ glob_files(patterns, from, __callee__, caller_location)
34
+ .then { |e| block ? e.each(&block) : e.to_a }
35
+ end
35
36
 
36
- def include_directory(path, from: :current, base: nil, raise_error: true)
37
- location = caller_location
38
- add_directory_entry(path, from, base, location, raise_error, :add_include_directory)
37
+ def find_file(patterns, from: nil)
38
+ glob_files(patterns, from, __callee__, caller_location).first
39
39
  end
40
40
 
41
- def library_directory(path, from: :current, base: nil, raise_error: true)
42
- location = caller_location
43
- add_directory_entry(path, from, base, location, raise_error, :add_library_directory)
41
+ def file?(path, from: :current)
42
+ !extract_path(path, from, __callee__, caller_location).nil?
44
43
  end
45
44
 
46
- def file?(path, from: :current, base: nil)
47
- location = caller_location
48
- !lookup_root(path, from, base, location, :file?).nil?
45
+ define_method(:directory?, instance_method(:file?))
46
+
47
+ def define_macro(macro, value = nil)
48
+ @context.define_macro(macro, value)
49
49
  end
50
50
 
51
- def directory?(path, from: :current, base: nil)
52
- location = caller_location
53
- !lookup_root(path, from, base, location, :directory?).nil?
51
+ def macro?(macro)
52
+ @context.macros.include?(macro.to_sym)
54
53
  end
55
54
 
55
+ alias_method :macro_defined?, :macro?
56
+
56
57
  def env?(name)
57
58
  ENV.key?(name.to_s)
58
59
  end
@@ -80,25 +81,25 @@ module FLGen
80
81
  return nil if @path.empty?
81
82
 
82
83
  Pathname
83
- .new(@path)
84
- .dirname
85
- .descend
86
- .select(&method(:repository_root?))
87
- .map(&:to_s)
84
+ .new(@path).dirname.descend.select(&method(:repository_root?)).map(&:to_s)
88
85
  end
89
86
 
90
87
  def repository_root?(path)
91
88
  File.exist?(path.join('.git').to_s)
92
89
  end
93
90
 
94
- def load_file_list(path, from, base, location, raise_error)
95
- unless (root = lookup_root(path, from, base, location, :file?))
91
+ def init_default_search_path
92
+ Hash.new { |_, key| key == :file_list ? :root : :current }
93
+ end
94
+
95
+ def load_file_list(path, from, raise_error, location)
96
+ unless (extracted_path = extract_path(path, from, :file_list, location))
96
97
  raise_no_entry_error(path, location, raise_error)
97
98
  return
98
99
  end
99
100
 
100
101
  # Need to File.realpath to resolve symblic link
101
- list_path = File.realpath(concat_path(root, path))
102
+ list_path = File.realpath(extracted_path)
102
103
  file_list_already_loaded?(list_path) && return
103
104
 
104
105
  @context.loaded_file_lists << list_path
@@ -110,49 +111,52 @@ module FLGen
110
111
  @context.loaded_file_lists.include?(path)
111
112
  end
112
113
 
113
- # rubocop:disable Metrics/ParameterLists
114
-
115
- def add_file_entry(path, from, base, location, raise_error, method)
116
- unless (root = lookup_root(path, from, base, location, :file?))
114
+ def add_entry(path, from, raise_error, method_name, location)
115
+ unless (extracted_path = extract_path(path, from, method_name, location))
117
116
  raise_no_entry_error(path, location, raise_error)
118
117
  return
119
118
  end
120
119
 
121
- @context.__send__(method, root, path)
120
+ @context.__send__("add_#{method_name}".to_sym, extracted_path)
122
121
  end
123
122
 
124
- def add_directory_entry(path, from, base, location, raise_error, method)
125
- unless (root = lookup_root(path, from, base, location, :directory?))
126
- raise_no_entry_error(path, location, raise_error)
127
- return
128
- end
123
+ def raise_no_entry_error(path, location, raise_error)
124
+ return unless raise_error
129
125
 
130
- directory_path = concat_path(root, path)
131
- @context.__send__(method, directory_path)
126
+ raise NoEntryError.new(path, location)
132
127
  end
133
128
 
134
- # rubocop:enable Metrics/ParameterLists
129
+ def glob_files(patterns, from, method_name, location)
130
+ search_root('', from, method_name, location)
131
+ .lazy.flat_map { |base| do_glob_files(patterns, base) }
132
+ end
133
+
134
+ def do_glob_files(patterns, base)
135
+ Dir.glob(Array(patterns), base: base)
136
+ .map { |path| File.join(base, path) }
137
+ .select(&File.method(:file?))
138
+ end
135
139
 
136
140
  def caller_location
137
141
  caller_locations(2, 1).first
138
142
  end
139
143
 
140
- def lookup_root(path, from, base, location, checker)
141
- search_root(path, from, base, location)
142
- .find { |root| File.__send__(checker, concat_path(root, path)) }
144
+ def extract_path(path, from, method_name, location)
145
+ search_root(path, from, method_name, location)
146
+ .map { |root| File.expand_path(path, root) }
147
+ .find { |abs_path| exist_path?(abs_path, method_name) }
143
148
  end
144
149
 
145
- def search_root(path, from, base, location)
150
+ FROM_KEYWORDS = [:cwd, :current, :local_root, :root].freeze
151
+
152
+ def search_root(path, from, method_name, location)
153
+ search_path = from || @default_search_path[method_name]
146
154
  if absolute_path?(path)
147
155
  ['']
148
- elsif !base.nil?
149
- [base]
150
- elsif from == :current
151
- [current_directory(location)]
152
- elsif from == :local_root
153
- [@root_directories.last]
156
+ elsif FROM_KEYWORDS.include?(search_path)
157
+ search_root_specified_by_keyword(search_path, location)
154
158
  else
155
- @root_directories
159
+ [search_path]
156
160
  end
157
161
  end
158
162
 
@@ -160,6 +164,15 @@ module FLGen
160
164
  Pathname.new(path).absolute?
161
165
  end
162
166
 
167
+ def search_root_specified_by_keyword(from_keyword, location)
168
+ case from_keyword
169
+ when :cwd then [Dir.pwd]
170
+ when :current then [current_directory(location)]
171
+ when :local_root then [@root_directories.last]
172
+ else @root_directories
173
+ end
174
+ end
175
+
163
176
  def current_directory(location)
164
177
  # From Ruby 3.1 Thread::Backtrace::Location#absolute_path returns nil
165
178
  # for code string evaluated by eval methods
@@ -168,14 +181,15 @@ module FLGen
168
181
  File.dirname(path)
169
182
  end
170
183
 
171
- def concat_path(root, path)
172
- File.expand_path(path, root)
173
- end
184
+ METHODS_TARGETING_DIRECTORY =
185
+ [:include_directory, :library_directory, :directory?].freeze
174
186
 
175
- def raise_no_entry_error(path, location, raise_error)
176
- return unless raise_error
177
-
178
- raise NoEntryError.new(path, location)
187
+ def exist_path?(path, method_name)
188
+ if METHODS_TARGETING_DIRECTORY.include?(method_name)
189
+ File.directory?(path)
190
+ else
191
+ File.file?(path)
192
+ end
179
193
  end
180
194
  end
181
195
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module FLGen
4
4
  class SourceFile
5
- def initialize(root, path, checksum = nil)
6
- @path = File.join(root, path)
5
+ def initialize(path, checksum = nil)
6
+ @path = path
7
7
  @checksum = checksum
8
8
  end
9
9
 
@@ -27,8 +27,8 @@ module FLGen
27
27
  def remove_ext(ext_list)
28
28
  return self unless match_ext?(ext_list)
29
29
 
30
- path = Pathname.new(@path).sub_ext('').to_s
31
- self.class.new('', path, checksum)
30
+ path_no_ext = Pathname.new(path).sub_ext('').to_s
31
+ self.class.new(path_no_ext, checksum)
32
32
  end
33
33
 
34
34
  def checksum
data/lib/flgen/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FLGen
4
- VERSION = '0.17.0'
4
+ VERSION = '0.19.0'
5
5
  end
metadata CHANGED
@@ -1,113 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taichi Ishitani
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-13 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bump
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.10.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.10.0
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 3.12.0
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: 3.12.0
69
- - !ruby/object:Gem::Dependency
70
- name: rubocop
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.40.0
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.40.0
83
- - !ruby/object:Gem::Dependency
84
- name: simplecov
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: 0.21.0
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: 0.21.0
97
- - !ruby/object:Gem::Dependency
98
- name: simplecov-cobertura
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 2.1.0
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 2.1.0
11
+ date: 2023-09-15 00:00:00.000000000 Z
12
+ dependencies: []
111
13
  description: Filelist generator
112
14
  email:
113
15
  - ishitani@pezy.co.jp
@@ -162,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
64
  - !ruby/object:Gem::Version
163
65
  version: '0'
164
66
  requirements: []
165
- rubygems_version: 3.4.5
67
+ rubygems_version: 3.4.19
166
68
  signing_key:
167
69
  specification_version: 4
168
70
  summary: Filelist generator