/
localeapp
executable file
·163 lines (141 loc) · 4.3 KB
/
localeapp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
require 'localeapp'
# Don't connect to the net if we're running under cucumber for testing
# Use FakeWeb to simulate api.localeapp.com
if ENV['FAKE_WEB_DURING_CUCUMBER_RUN'] && fake_data_as_json = ENV['FAKE_WEB_FAKES']
require 'fakeweb'
FakeWeb.allow_net_connect = false
fakes = JSON.parse(fake_data_as_json)
fakes.each do |fake|
FakeWeb.register_uri fake['method'].to_sym, fake['uri'], { :body => fake['body'], :status => fake['status'] }.merge(fake['headers'])
end
end
require 'gli'
gli2 = GLI::VERSION >= '2.0.0'
if gli2
include GLI::App
else
include GLI
end
pre do |global_options, command, options, args|
global_options[:k] = if global_options[:k]
global_options[:k]
elsif ENV['LOCALEAPP_API_KEY']
ENV['LOCALEAPP_API_KEY']
elsif File.exist?('.env') && IO.read('.env') =~ /^LOCALEAPP_API_KEY=(\w+)$/
$1
else
nil
end
if Localeapp.has_config_file? || !global_options[:k].nil?
true
else
puts "Could not load config file and no key specified"
exit 1
end
end
desc "API Key (for when there is no configuration file)"
flag [:k, 'api-key']
version Localeapp::VERSION
desc "Creates new configuration files and confirms key works"
skips_pre
arg_name "<api_key>"
command :install do |c|
c.desc "install configuration files in .localeapp/"
c.switch [:s, 'standalone']
c.desc "create configuration when using localeapp via a heroku addon (PRE ALPHA)"
c.switch [:h, 'heroku']
c.desc "install a skeleton project suitable for Github"
c.switch [:g, 'github']
c.action do |global_options, options, args|
key = args.first
installer = Localeapp::CLI::Install.new
installer.config_type = :standalone if options[:standalone]
installer.config_type = :heroku if options[:heroku]
installer.config_type = :github if options[:github]
installer.config_type ||= :rails
unless installer.execute(key)
exit_now! "", 1
end
end
end
desc "Sends the key and content to localeapp.com"
arg_name "<key> <locale:content> (<locale:content> ...)"
command :add do |c|
c.action do |global_options, options, args|
key = args.shift
if key.nil? || args.size.zero?
exit_now! "localeapp add requires a key name and at least one translation", 1
else
Localeapp::CLI::Add.new(global_options).execute(key, *args)
end
end
end
desc "removes a key from the project"
arg_name "<key>"
command :rm do |c|
c.action do |global_options, options, args|
key = args.shift
if key.nil?
exit_now! "localeapp rm requires a key name", 1
else
Localeapp::CLI::Remove.new(global_options).execute(key, *args)
end
end
end
desc "renames a key in the project"
arg_name "<current key name> <new key name>"
command :mv do |c|
c.action do |global_options, options, args|
current_name = args.shift
new_name = args.shift
if current_name.nil? || new_name.nil?
exit_now! "localeapp mv requires a current key name and a new key name", 1
else
Localeapp::CLI::Rename.new(global_options).execute(current_name, new_name, *args)
end
end
end
desc "Pulls all translations from localeapp.com"
command :pull do |c|
c.action do |global_options, options, args|
Localeapp::CLI::Pull.new(global_options).execute
end
end
desc "Pushes a translation file or directory to localeapp.com"
arg_name "<file>"
command :push do |c|
c.action do |global_options, options, args|
if args.empty?
exit_now! "localeapp push requires an file or directory to push", 1
else
path = args.first
pusher = Localeapp::CLI::Push.new(global_options)
pusher.execute(path)
end
end
end
desc "Gets any changes since the last poll and updates the yml"
command :update do |c|
c.action do |global_options, options, args|
Localeapp::CLI::Update.new(global_options).execute
end
end
desc "Simple daemon (checks for new translations in the background)"
command :daemon do |c|
c.desc "Interval to wait between checks"
c.arg_name 'interval'
c.default_value 5
c.flag [:i, :interval]
c.desc "run the daemon in the background"
c.switch [:b, 'background']
c.action do |global_options, options, args|
Localeapp::CLI::Daemon.new(global_options).execute(options)
end
end
if gli2
exit run(ARGV)
else
exit GLI.run(ARGV)
end