null+****@clear*****
null+****@clear*****
Thu Feb 16 19:23:11 JST 2012
SUZUKI Miho 2012-02-16 19:23:11 +0900 (Thu, 16 Feb 2012) New Revision: 468c9a72d7813b9a8b7de910235823e56ef4b29c Log: Reduce dependency on LOGALING_CONFIG Modified files: lib/logaling/command/application.rb lib/logaling/config.rb spec/logaling/command_spec.rb Modified: lib/logaling/command/application.rb (+20 -12) =================================================================== --- lib/logaling/command/application.rb 2012-02-16 17:45:46 +0900 (4ed243d) +++ lib/logaling/command/application.rb 2012-02-16 19:23:11 +0900 (20ead5e) @@ -30,8 +30,9 @@ module Logaling::Command super @logaling_home = options["logaling-home"] ? options["logaling-home"] : LOGALING_HOME @repository = Logaling::Repository.new(@logaling_home) - project_config_path = File.join(find_dotfile, 'config') - @config = Logaling::Config.load_config_and_merge_options(project_config_path, @repository.config_path, options) + @dotfile_path = options["logaling-config"] ? options["logaling-config"] : find_dotfile + @project_config_path = File.join(@dotfile_path, 'config') + @config = Logaling::Config.load_config_and_merge_options(@project_config_path, @repository.config_path, options) rescue Logaling::CommandFailed @config = Logaling::Config.load_config_and_merge_options(nil, @repository.config_path, options) end @@ -52,20 +53,21 @@ module Logaling::Command class_option "source-language", type: :string, aliases: "-S" class_option "target-language", type: :string, aliases: "-T" class_option "logaling-home", type: :string, aliases: "-h" + class_option "logaling-config", type: :string, aliases: "-c" desc 'new [PROJECT NAME] [SOURCE LANGUAGE] [TARGET LANGUAGE(optional)]', 'Create .logaling' method_option "no-register", type: :boolean, default: false def new(project_name, source_language, target_language=nil) - unless File.exist?(LOGALING_CONFIG) - FileUtils.mkdir_p(File.join(LOGALING_CONFIG, "glossary")) + unless File.exist?(logaling_config_path) + FileUtils.mkdir_p(File.join(logaling_config_path, "glossary")) config = Logaling::Config.setup(project_name, source_language, target_language) - config.save(File.join(LOGALING_CONFIG, "config")) + config.save(File.join(logaling_config_path, "config")) register unless options["no-register"] - say "Successfully created #{LOGALING_CONFIG}" + say "Successfully created #{logaling_config_path}" else - say "#{LOGALING_CONFIG} already exists." + say "#{logaling_config_path} already exists." end end @@ -86,16 +88,14 @@ module Logaling::Command desc 'register', 'Register .logaling' def register - logaling_path = find_dotfile - @config.check_required_option("glossary" => "input glossary name '-g <glossary name>'") + raise Logaling::CommandFailed, "Try 'loga new' first." unless File.exist?(@dotfile_path) - @repository.register(logaling_path, @config.glossary) + @repository.register(@dotfile_path, @config.glossary) @repository.index say "#{@config.glossary} is now registered to logaling." rescue Logaling::CommandFailed => e say e.message - say "Try 'loga new' first." rescue Logaling::GlossaryAlreadyRegistered => e say "#{@config.glossary} is already registered." end @@ -116,7 +116,7 @@ module Logaling::Command desc 'config [KEY] [VALUE] [--global(optional)]', 'Set config.' method_option "global", type: :boolean, default: false def config(key, value) - config_path = options["global"] ? File.join(@logaling_home, "config") : File.join(find_dotfile, "config") + config_path = options["global"] ? File.join(@logaling_home, "config") : @project_config_path FileUtils.touch(config_path) unless File.exist?(config_path) Logaling::Config.add(config_path, key, value) say "Successfully set config." @@ -286,6 +286,14 @@ module Logaling::Command end end + def logaling_config_path + if options["logaling-config"] + options["logaling-config"] + else + File.join(Dir.pwd, LOGALING_CONFIG) + end + end + # http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby def run_pager return if options["no-pager"] Modified: lib/logaling/config.rb (+1 -1) =================================================================== --- lib/logaling/config.rb 2012-02-16 17:45:46 +0900 (8c21895) +++ lib/logaling/config.rb 2012-02-16 19:23:11 +0900 (f77284d) @@ -55,7 +55,7 @@ module Logaling def load_config(config_path=nil) config ||= {} - if config_path + if config_path && File.exist?(config_path) File.readlines(config_path).map{|l| l.chomp.split " "}.each do |option| key = option[0].sub(/^[\-]{2}/, "") value = option[1] Modified: spec/logaling/command_spec.rb (+16 -15) =================================================================== --- spec/logaling/command_spec.rb 2012-02-16 17:45:46 +0900 (d44076f) +++ spec/logaling/command_spec.rb 2012-02-16 19:23:11 +0900 (7c886c2) @@ -19,14 +19,19 @@ require File.join(File.dirname(__FILE__), "..", "spec_helper") describe Logaling::Command::Application do let(:logaling_home) { @logaling_home } - let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "target-language"=>"ja"} } + let(:logaling_config) { + # p dot_logaling = Dir.mktmpdir + # FileUtils.touch(File.join(dot_logaling, 'config')) + # dot_logaling + # } + File.join(File.dirname(__FILE__), "..", "tmp", ".logaling") } + let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "target-language"=>"ja", "logaling-config" => logaling_config} } let(:command) { Logaling::Command::Application.new([], base_options) } let(:glossary_path) { Logaling::Glossary.build_path('spec', 'en', 'ja', logaling_home) } let(:target_project_path) { File.join(logaling_home, "projects", "spec") } let(:repository) { Logaling::Repository.new(logaling_home) } before do - FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) FileUtils.remove_entry_secure(File.join(logaling_home, 'projects', 'spec'), true) end @@ -42,7 +47,7 @@ describe Logaling::Command::Application do end it 'print message \"<.logaling path> already exists.\"' do - @stdout.should include "#{Logaling::Command::LOGALING_CONFIG} already exists.\n" + @stdout.should include "#{logaling_config} already exists.\n" end end @@ -53,7 +58,7 @@ describe Logaling::Command::Application do end it 'should create .logaling' do - File.exist?(Logaling::Command::LOGALING_CONFIG).should be_true + File.exist?(logaling_config).should be_true end it 'should register .logaling as project' do @@ -69,7 +74,7 @@ describe Logaling::Command::Application do end it 'should create .logaling' do - File.exist?(Logaling::Command::LOGALING_CONFIG).should be_true + File.exist?(logaling_config).should be_true end it 'should not register .logaling as project' do @@ -88,7 +93,7 @@ describe Logaling::Command::Application do context "when can not find .logaling" do before(:all) do - FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) + FileUtils.remove_entry_secure(logaling_config, true) @stdout = capture(:stdout) {command.register} end @@ -111,10 +116,6 @@ describe Logaling::Command::Application do File.exist?(target_project_path).should be_true Dir[File.join(logaling_home, "projects", "*")].size.should == @project_counts + 1 end - - after do - FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) - end end end @@ -125,7 +126,7 @@ describe Logaling::Command::Application do context "when can not find .logaling" do before do - #FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) + #FileUtils.remove_entry_secure(logaling_config, true) end context "and call without option" do @@ -179,7 +180,7 @@ describe Logaling::Command::Application do end describe '#config' do - let(:project_config) { File.join(Logaling::Command::LOGALING_CONFIG, 'config') } + let(:project_config) { File.join(logaling_config, 'config') } let(:global_config) { File.join(logaling_home, 'config') } subject { File.read(project_config) } @@ -262,7 +263,7 @@ describe Logaling::Command::Application do context 'project config does not have TARGET-LANGUAGE' do let(:global_config) { File.join(logaling_home, 'config') } - let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "output" => "terminal"} } + let(:base_options) { {"glossary"=>"spec", "source-language"=>"en", "output" => "terminal", "logaling-config" => logaling_config} } before do # create global config file FileUtils.touch(global_config) @@ -396,7 +397,7 @@ describe Logaling::Command::Application do context "and called with '--force=true'" do before do - FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) + FileUtils.remove_entry_secure(logaling_config, true) FileUtils.remove_entry_secure(File.join(logaling_home, 'projects', 'spec'), true) command.options = base_options.merge("force" => true) command.new('spec', 'en', 'ja') @@ -475,7 +476,7 @@ describe Logaling::Command::Application do end after do - FileUtils.remove_entry_secure(Logaling::Command::LOGALING_CONFIG, true) + FileUtils.remove_entry_secure(logaling_config, true) FileUtils.remove_entry_secure(File.join(logaling_home, 'projects', 'spec'), true) end end