Rakefile in ably-1.0.7 vs Rakefile in ably-1.1.0
- old
+ new
@@ -27,8 +27,40 @@
).join(' ')
Rake::Task[:spec].invoke
end
end
+
+ desc 'Generate error code constants from ably-common: https://github.com/ably/ably-common/issues/32'
+ task :generate_error_codes do
+ errors_json_path = File.join(File.dirname(__FILE__), 'lib/submodules/ably-common/protocol/errors.json')
+ module_path = File.join(File.dirname(__FILE__), 'lib/ably/modules/exception_codes.rb')
+ max_length = 0
+
+ errors = JSON.parse(File.read(errors_json_path)).each_with_object({}) do |(key, val), hash|
+ hash[key] = val.split(/\s+/).map { |d| d.upcase.gsub(/[^a-zA-Z]+/, '') }.join('_')
+ end.each do |code, const_name|
+ max_length = [const_name.length, max_length].max
+ end.map do |code, const_name|
+ " #{const_name.ljust(max_length, ' ')} = #{code}"
+ end.join("\n")
+ module_content = <<-EOF
+# This file is generated by running `rake :generate_error_codes`
+# Do not manually modify this file
+# Generated at: #{Time.now.utc}
+#
+module Ably
+ module Exceptions
+ module Codes
+#{errors}
+ end
+ end
+end
+EOF
+ File.open(module_path, 'w') { |file| file.write module_content }
+
+ puts "Error code constants have been generated into #{module_path}"
+ puts "Warning: Search for any constants referenced in this library if their name has changed as a result of this constant generation!"
+ end
rescue LoadError
# RSpec not available
end