diff --git a/Gemfile b/Gemfile index df64a48..a00d408 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,12 @@ source 'https://rubygems.org' # Specify your gem's dependencies in file_list_generator.gemspec gemspec + +group :developmend do + gem 'bump', '~> 0.10.0' + gem 'rake' + gem 'rspec', '~> 3.12.0' + gem 'rubocop', '~> 1.40.0' + gem 'simplecov', '~> 0.21.0' + gem 'simplecov-cobertura', '~> 2.1.0' +end diff --git a/Rakefile b/Rakefile index 60e83c3..eaef542 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'bundler/setup' require 'bundler/gem_tasks' require 'rspec/core/rake_task' require 'rubocop/rake_task' diff --git a/flgen.gemspec b/flgen.gemspec index 3f1272e..08d2f2c 100644 --- a/flgen.gemspec +++ b/flgen.gemspec @@ -25,12 +25,4 @@ Gem::Specification.new do |spec| spec.executables = `git ls-files -- exe/*`.split($RS).map(&File.method(:basename)) spec.require_paths = ['lib'] spec.required_ruby_version = '>= 3.0' - - spec.add_development_dependency 'bump', '~> 0.10.0' - spec.add_development_dependency 'bundler' - spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec', '~> 3.12.0' - spec.add_development_dependency 'rubocop', '~> 1.40.0' - spec.add_development_dependency 'simplecov', '~> 0.21.0' - spec.add_development_dependency 'simplecov-cobertura', '~> 2.1.0' end diff --git a/lib/flgen/file_list.rb b/lib/flgen/file_list.rb index 3b533e0..0d764bd 100644 --- a/lib/flgen/file_list.rb +++ b/lib/flgen/file_list.rb @@ -18,61 +18,42 @@ def reset_default_search_path(*target_types) end def file_list(path, from: nil, raise_error: true) - location = caller_location - load_file_list(path, from, location, raise_error) + load_file_list(path, from, raise_error, caller_location) end def source_file(path, from: nil, raise_error: true) - location = caller_location - add_file_entry(path, from, location, raise_error, :source_file) + add_entry(path, from, raise_error, __callee__, caller_location) end - def library_file(path, from: nil, raise_error: true) - location = caller_location - add_file_entry(path, from, location, raise_error, :library_file) - end - - def define_macro(macro, value = nil) - @context.define_macro(macro, value) - end - - def macro?(macro) - @context.macros.include?(macro.to_sym) - end - - alias_method :macro_defined?, :macro? - - def include_directory(path, from: nil, raise_error: true) - location = caller_location - add_directory_entry(path, from, location, raise_error, :include_directory) - end - - def library_directory(path, from: nil, raise_error: true) - location = caller_location - add_directory_entry(path, from, location, raise_error, :library_directory) - end + define_method(:library_file, instance_method(:source_file)) + define_method(:include_directory, instance_method(:source_file)) + define_method(:library_directory, instance_method(:source_file)) def find_files(*patterns, from: nil, &block) - location = caller_location - glob_files(patterns, __method__, from, location) + glob_files(patterns, from, __callee__, caller_location) .then { |e| block ? e.each(&block) : e.to_a } end def find_file(*patterns, from: nil) - location = caller_location - glob_files(patterns, __method__, from, location).first + glob_files(patterns, from, __callee__, caller_location).first end def file?(path, from: :current) - location = caller_location - !extract_file_path(path, from, location, :file).nil? + !extract_path(path, from, __callee__, caller_location).nil? end - def directory?(path, from: :current) - location = caller_location - !extract_directory_path(path, from, location, :directory).nil? + define_method(:directory?, instance_method(:file?)) + + def define_macro(macro, value = nil) + @context.define_macro(macro, value) end + def macro?(macro) + @context.macros.include?(macro.to_sym) + end + + alias_method :macro_defined?, :macro? + def env?(name) ENV.key?(name.to_s) end @@ -111,14 +92,14 @@ def init_default_search_path Hash.new { |_, key| key == :file_list ? :root : :current } end - def load_file_list(path, from, location, raise_error) - unless (list_path = extract_file_path(path, from, location, :file_list)) + def load_file_list(path, from, raise_error, location) + unless (extracted_path = extract_path(path, from, :file_list, location)) raise_no_entry_error(path, location, raise_error) return end # Need to File.realpath to resolve symblic link - list_path = File.realpath(list_path) + list_path = File.realpath(extracted_path) file_list_already_loaded?(list_path) && return @context.loaded_file_lists << list_path @@ -130,28 +111,23 @@ def file_list_already_loaded?(path) @context.loaded_file_lists.include?(path) end - def add_file_entry(path, from, location, raise_error, type) - unless (file_path = extract_file_path(path, from, location, type)) + def add_entry(path, from, raise_error, method_name, location) + unless (extracted_path = extract_path(path, from, method_name, location)) raise_no_entry_error(path, location, raise_error) return end - method = "add_#{type}".to_sym - @context.__send__(method, file_path) + @context.__send__("add_#{method_name}".to_sym, extracted_path) end - def add_directory_entry(path, from, location, raise_error, type) - unless (directory_path = extract_directory_path(path, from, location, type)) - raise_no_entry_error(path, location, raise_error) - return - end + def raise_no_entry_error(path, location, raise_error) + return unless raise_error - method = "add_#{type}".to_sym - @context.__send__(method, directory_path) + raise NoEntryError.new(path, location) end - def glob_files(patterns, method_name, from, location) - patterns.product(search_root('', from, location, method_name)) + def glob_files(patterns, from, method_name, location) + patterns.product(search_root('', from, method_name, location)) .lazy.flat_map { |patten, base| do_glob_files(patten, base) } end @@ -165,24 +141,16 @@ def caller_location caller_locations(2, 1).first end - def extract_file_path(path, from, location, type) - extract_path(path, from, location, type, :file?) - end - - def extract_directory_path(path, from, location, type) - extract_path(path, from, location, type, :directory?) - end - - def extract_path(path, from, location, type, checker) - search_root(path, from, location, type) + def extract_path(path, from, method_name, location) + search_root(path, from, method_name, location) .map { |root| File.expand_path(path, root) } - .find { |abs_path| File.__send__(checker, abs_path) && abs_path } + .find { |abs_path| exist_path?(abs_path, method_name) } end FROM_KEYWORDS = [:cwd, :current, :local_root, :root].freeze - def search_root(path, from, location, type) - search_path = from || @default_search_path[type] + def search_root(path, from, method_name, location) + search_path = from || @default_search_path[method_name] if absolute_path?(path) [''] elsif FROM_KEYWORDS.include?(search_path) @@ -213,10 +181,15 @@ def current_directory(location) File.dirname(path) end - def raise_no_entry_error(path, location, raise_error) - return unless raise_error + METHODS_TARGETING_DIRECTORY = + [:include_directory, :library_directory, :directory?].freeze - raise NoEntryError.new(path, location) + def exist_path?(path, method_name) + if METHODS_TARGETING_DIRECTORY.include?(method_name) + File.directory?(path) + else + File.file?(path) + end end end end diff --git a/spec/flgen/file_list_spec.rb b/spec/flgen/file_list_spec.rb index 7cd7e92..ad374f4 100644 --- a/spec/flgen/file_list_spec.rb +++ b/spec/flgen/file_list_spec.rb @@ -558,44 +558,6 @@ def setup_expectation(root, file_name, *invalid_roots) end end - describe '#define_macro' do - it 'マクロを定義する' do - file_list = described_class.new(context, path) - - expect(context).to receive(:define_macro).with(:FOO, nil) - expect(context).to receive(:define_macro).with(:BAR, 1) - - file_list.define_macro(:FOO) - file_list.define_macro(:BAR, 1) - end - end - - describe '#macro?/#macro_defined?' do - let(:macros) do - [:FOO, :BAR] - end - - it '定義済みマクロかどうかを示す' do - file_list = described_class.new(context, path) - context.macros << macros[0] - context.macros << macros[1] - - expect(file_list.macro?(:FOO)).to be true - expect(file_list.macro?('FOO')).to be true - expect(file_list.macro?(:BAR)).to be true - expect(file_list.macro?('BAR')).to be true - expect(file_list.macro?(:BAZ)).to be false - expect(file_list.macro?('BAZ')).to be false - - expect(file_list.macro_defined?(:FOO)).to be true - expect(file_list.macro_defined?('FOO')).to be true - expect(file_list.macro_defined?(:BAR)).to be true - expect(file_list.macro_defined?('BAR')).to be true - expect(file_list.macro_defined?(:BAZ)).to be false - expect(file_list.macro_defined?('BAZ')).to be false - end - end - describe '#include_directory' do let(:include_directories) do ['foo', 'bar/baz'] @@ -1386,6 +1348,44 @@ def mock_glab(patten, results) end end + describe '#define_macro' do + it 'マクロを定義する' do + file_list = described_class.new(context, path) + + expect(context).to receive(:define_macro).with(:FOO, nil) + expect(context).to receive(:define_macro).with(:BAR, 1) + + file_list.define_macro(:FOO) + file_list.define_macro(:BAR, 1) + end + end + + describe '#macro?/#macro_defined?' do + let(:macros) do + [:FOO, :BAR] + end + + it '定義済みマクロかどうかを示す' do + file_list = described_class.new(context, path) + context.macros << macros[0] + context.macros << macros[1] + + expect(file_list.macro?(:FOO)).to be true + expect(file_list.macro?('FOO')).to be true + expect(file_list.macro?(:BAR)).to be true + expect(file_list.macro?('BAR')).to be true + expect(file_list.macro?(:BAZ)).to be false + expect(file_list.macro?('BAZ')).to be false + + expect(file_list.macro_defined?(:FOO)).to be true + expect(file_list.macro_defined?('FOO')).to be true + expect(file_list.macro_defined?(:BAR)).to be true + expect(file_list.macro_defined?('BAR')).to be true + expect(file_list.macro_defined?(:BAZ)).to be false + expect(file_list.macro_defined?('BAZ')).to be false + end + end + describe '#env?' do it '指定された環境変数が定義されているかを返す' do file_list = described_class.new(context, path)