diff --git a/lib/flgen/context.rb b/lib/flgen/context.rb index 7438320..179fdb6 100644 --- a/lib/flgen/context.rb +++ b/lib/flgen/context.rb @@ -39,6 +39,10 @@ def define_macro(macro, value = nil) add_macro_definition(k, v, false) end + def undefine_macro(macro) + remove_macro(macro) + end + def macros @macros ||= {} end @@ -111,11 +115,19 @@ def add_macro_definition(macro, value, predefined) end def add_macro_argument(name, value) - arguments - .delete_if { |argument| argument.type == :define && argument.name == name } + arguments.delete_if { |arg| macro_definition?(arg, name) } add_compile_argument(Arguments::Define.new(name, value)) end + def remove_macro(macro) + name = macro.to_sym + arguments.reject! { |arg| macro_definition?(arg, name) } && macros.delete(name) + end + + def macro_definition?(arg, name) + arg.type == :define && arg.name == name + end + def directory_already_added?(type, path) arguments .any? { |argument| argument.type == type && argument.path == path } diff --git a/lib/flgen/file_list.rb b/lib/flgen/file_list.rb index 50a2f3e..496beca 100644 --- a/lib/flgen/file_list.rb +++ b/lib/flgen/file_list.rb @@ -49,6 +49,10 @@ def define_macro(macro, value = nil) @context.define_macro(macro, value) end + def undefine_macro(macro) + @context.undefine_macro(macro) + end + def macro?(macro) @context.macros.key?(macro.to_sym) end @@ -126,9 +130,7 @@ def add_entry(path, from, raise_error, method_name, location) end def raise_no_entry_error(path, location, raise_error) - return unless raise_error - - raise NoEntryError.new(path, location) + raise_error && (raise NoEntryError.new(path, location)) end def glob_files(patterns, from, method_name, location) diff --git a/spec/flgen/context_spec.rb b/spec/flgen/context_spec.rb index 32f07c3..4e0b160 100644 --- a/spec/flgen/context_spec.rb +++ b/spec/flgen/context_spec.rb @@ -242,6 +242,31 @@ module piyo; end end + describe '#undefine_macro' do + it '定義済みマクロを無効化する' do + context.define_macro(:FOO) + context.define_macro('BAR=1') + context.define_macro(:BAZ) + + context.undefine_macro(:QUX) + context.undefine_macro(:BAZ) + context.undefine_macro(:FOO) + + expect(context.macros).to match(BAR: '1') + end + + specify '事前定義済みマクロは無効化しない' do + options[:tool] = :vcs + + context.define_macro(:FOO) + context.define_macro(:BAR) + context.undefine_macro(:FOO) + context.undefine_macro(:VCS) + + expect(context.macros).to match(VCS: true, BAR: true) + end + end + describe '事前定義済みマクロ' do context '対象ツールがVCSの場合' do specify ':VCSが定義される' do diff --git a/spec/flgen/file_list_spec.rb b/spec/flgen/file_list_spec.rb index dde184c..4f07738 100644 --- a/spec/flgen/file_list_spec.rb +++ b/spec/flgen/file_list_spec.rb @@ -1390,6 +1390,18 @@ def mock_glob(pattens, results) end end + describe '#undefine_macro' do + it 'マクロを無効化する' do + file_list = described_class.new(context, path) + + expect(context).to receive(:undefine_macro).with(:FOO) + expect(context).to receive(:undefine_macro).with(:BAR) + + file_list.undefine_macro(:FOO) + file_list.undefine_macro(:BAR) + end + end + describe '#macro?/#macro_defined?' do let(:macros) do [:FOO, :BAR]