Config changes - Redone from current master #20

Merged
merged 7 commits into from Feb 24, 2012
View
@@ -1,3 +1,8 @@
+# HEAD
+
+* Add --standalone option to install to generate a .localeapp/ config
+ directory. This enables usage outside of rails.
+
# Version 0.4.0
* Use Psych to generate the yaml if it's available (This will completely change
View
@@ -39,6 +39,14 @@ Install the gem:
Create a project on localeapp.com and get the api key. Then run:
localeapp install <YOUR_API_KEY>
+
+### Non rails projects
+
+Install the gem and run:
+
+ localeapp install --standalone <YOUR_API_KEY>
+
+This will create a `.localeapp` directory for your configuration files.
## Importing existing content
View
@@ -14,8 +14,8 @@ if ENV['FAKE_WEB_DURING_CUCUMBER_RUN'] && fake_data_as_json = ENV['FAKE_WEB_FAKE
end
end
-def include_config_file
- unless Localeapp.include_config_file
+def initialize_config
+ unless Localeapp.initialize_config
puts "Could not load config file"
exit 1
end
@@ -29,9 +29,13 @@ version Localeapp::VERSION
desc "Creates new configuration files and confirms key works"
arg_name "<api_key>"
command :install do |c|
+ c.desc "install configuration files in .localeapp/"
+ c.switch [:s, 'standalone']
+
c.action do |global_options, options, args|
key = args.first
installer = Localeapp::CLI::Install.new
+ installer.config_type = options[:standalone] ? :dot_file : :rails
unless installer.execute(key)
exit_now! "", 1
end
@@ -46,7 +50,7 @@ command :add do |c|
if key.nil? || args.size.zero?
exit_now! "localeapp add requires a key name and at least one translation", 1
else
- include_config_file
+ initialize_config
Localeapp::CLI::Add.new.execute(key, *args)
end
end
@@ -55,7 +59,7 @@ end
desc "Pulls all translations from localeapp.com"
command :pull do |c|
c.action do |global_options, options, args|
- include_config_file
+ initialize_config
Localeapp::CLI::Pull.new.execute
end
end
@@ -67,8 +71,8 @@ command :push do |c|
if args.empty?
exit_now! "localeapp push requires an file or directory to push", 1
else
- include_config_file
path = args.first
+ initialize_config
pusher = Localeapp::CLI::Push.new
pusher.execute(path)
end
@@ -78,7 +82,7 @@ end
desc "Gets any changes since the last poll and updates the yml"
command :update do |c|
c.action do |global_options, options, args|
- include_config_file
+ initialize_config
Localeapp::CLI::Update.new.execute
end
end
@@ -94,7 +98,7 @@ command :daemon do |c|
c.switch [:b, 'background']
c.action do |global_options, options, args|
- include_config_file
+ initialize_config
interval = options[:interval].to_i
@@ -16,7 +16,7 @@ Feature: localeapp executable
error: Unknown command 'foo'. Use 'localeapp help' for a list of commands
"""
- Scenario: Running install
+ Scenario: Running Rails install
In order to configure my project and check my api key is correct
When I have a valid project on localeapp.com with api key "MYAPIKEY"
And I run `localeapp install MYAPIKEY`
@@ -33,6 +33,24 @@ Feature: localeapp executable
And a file named "config/initializers/localeapp.rb" should exist
And the exit status should be 0
+ Scenario: Running standalone install
+ In order to configure my non rails project and check my api key is correct
+ When I have a valid project on localeapp.com with api key "MYAPIKEY"
+ And I run `localeapp install --standalone MYAPIKEY`
+ Then the output should contain:
+ """
+ Localeapp Install
+
+ Checking API key: MYAPIKEY
+ Success!
+ Project: Test Project
+ Default Locale: en (English)
+ """
+ And help should not be displayed
+ And a file named ".localeapp/config.rb" should exist
+ And the exit status should be 0
+
+
Scenario: Running install with bad api key
In order to configure my project and check my api key is correct
When I have a valid project on localeapp.com but an incorrect api key "BADAPIKEY"
View
@@ -95,14 +95,20 @@ def configure
end
# requires the Localeapp configuration
- def include_config_file(file_path=nil)
- file_path ||= File.join(Dir.pwd, 'config', 'initializers', 'localeapp')
- begin
- require file_path
- true
- rescue
- false
+ def initialize_config(file_path=nil)
+ file_paths = [ File.join(Dir.pwd, '.localeapp', 'config.rb'),
+ File.join(Dir.pwd, 'config', 'initializers', 'localeapp.rb') ]
+ file_paths << file_path if file_path
+ file_paths.each do |path|
+ next unless File.exists? path
+ begin
+ require path
+ return true
+ rescue
+ end
end
+ false
end
+
end
end
@@ -1,6 +1,12 @@
module Localeapp
module CLI
class Install
+ attr_accessor :config_type
+
+ def initialize
+ @config_type = :rails
+ end
+
def execute(key, output = $stdout)
output.puts "Localeapp Install"
output.puts ""
@@ -15,12 +21,24 @@ def execute(key, output = $stdout)
output.puts "Project: #{project_data['name']}"
localeapp_default_code = project_data['default_locale']['code']
output.puts "Default Locale: #{localeapp_default_code} (#{project_data['default_locale']['name']})"
- if I18n.default_locale.to_s != localeapp_default_code
- output.puts "WARNING: I18n.default_locale is #{I18n.default_locale}, change in config/environment.rb (Rails 2) or config/application.rb (Rails 3)"
+
+ if config_type == :rails
+ if I18n.default_locale.to_s != localeapp_default_code
+ output.puts "WARNING: I18n.default_locale is #{I18n.default_locale}, change in config/environment.rb (Rails 2) or config/application.rb (Rails 3)"
+ end
+ config_file_path = "config/initializers/localeapp.rb"
+ data_directory = "config/locales"
+ else
+ output.puts "NOTICE: you probably want to add .localeapp to your .gitignore file"
+ config_file_path = ".localeapp/config.rb"
+ data_directory = "locales"
end
- config_file_path = "config/initializers/localeapp.rb"
output.puts "Writing configuration file to #{config_file_path}"
write_configuration_file config_file_path
+
+ unless Dir.exist?(data_directory)
+ output.puts "WARNING: please create the #{data_directory} directory. Your translation data will be stored there."
+ end
true
else
output.puts "ERROR: Project not found"
@@ -34,7 +52,11 @@ def check_key(key)
end
def write_configuration_file(path)
- Localeapp.configuration.write_initial(path)
+ if config_type == :rails
+ Localeapp.configuration.write_rails_configuration(path)
+ else
+ Localeapp.configuration.write_dot_file_configuration(path)
+ end
end
end
end
@@ -146,7 +146,7 @@ def sending_disabled?
end
end
- def write_initial(path)
+ def write_rails_configuration(path)
dir = File.dirname(path)
FileUtils.mkdir_p(dir)
File.open(path, 'w+') do |file|
@@ -159,5 +159,21 @@ def write_initial(path)
CONTENT
end
end
+
+ def write_dot_file_configuration(path)
+ dir = File.dirname(path)
+ FileUtils.mkdir_p(dir)
+ File.open(path, 'w+') do |file|
+ file.write <<-CONTENT
+Localeapp.configure do |config|
+ config.api_key = '#{@api_key}'
+ config.translation_data_directory = 'locales'
+ config.synchronization_data_file = '.localeapp/log.yml'
+ config.daemon_pid_file = '.localeapp/localeapp.pid'
+end
+CONTENT
+ end
+ end
+
end
end
@@ -39,4 +39,26 @@
@command.should_receive(:write_configuration_file).with('config/initializers/localeapp.rb')
@command.execute('API_KEY', @output)
end
+
+ it "asks the configuration to write itself to .localeapp when the --not-rails switch is set" do
+ @command.stub!(:check_key).and_return([true, valid_project_data])
+ @command.config_type = :dot_file
+ @command.should_receive(:write_configuration_file).with('.localeapp/config.rb')
+ @command.execute('API_KEY', @output)
+ end
+
+ it "displays warning if config.translation_data_directory doesn't exist" do
+ @command.stub!(:check_key).and_return([true, valid_project_data])
+ @command.stub!(:write_configuration_file)
+ @command.execute('API_KEY', @output)
+ @output.string.should match(/Your translation data will be stored there./)
+ end
+
+ it "doesn't display a warning if translation_data_directory exists" do
+ @command.stub!(:check_key).and_return([true, valid_project_data])
+ @command.stub!(:write_configuration_file)
+ Dir.should_receive(:exist?).and_return(true)
+ @command.execute('API_KEY', @output)
+ @output.string.should_not match(/Your translation data will be stored there./)
+ end
end
@@ -303,7 +303,7 @@
end
end
-describe Localeapp::Configuration, "#write_initial(path)" do
+describe Localeapp::Configuration, "#write_rails_configuration(path)" do
it "creates a configuration file containing just the api key at the given path" do
configuration = Localeapp::Configuration.new
configuration.api_key = "APIKEY"
@@ -317,6 +317,26 @@
end
CONTENT
File.should_receive(:open).with(path, 'w+').and_yield(file)
- configuration.write_initial(path)
+ configuration.write_rails_configuration(path)
+ end
+end
+
+
+describe Localeapp::Configuration, "#write_dot_file_configuration(path)" do
+ it "creates a configuration file containing the dot file configuration at the given path" do
+ configuration = Localeapp::Configuration.new
+ configuration.api_key = "APIKEY"
+ path = 'test_path'
+ file = stub('file')
+ file.should_receive(:write).with <<-CONTENT
+Localeapp.configure do |config|
+ config.api_key = 'APIKEY'
+ config.translation_data_directory = 'locales'
+ config.synchronization_data_file = '.localeapp/log.yml'
+ config.daemon_pid_file = '.localeapp/localeapp.pid'
+end
+CONTENT
+ File.should_receive(:open).with(path, 'w+').and_yield(file)
+ configuration.write_dot_file_configuration(path)
end
end