diff --git a/CHANGELOG.md b/CHANGELOG.md index 7521ab7..7d67d0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## master +* Improve error and warning messages when targetting an xcdatamodeld. + [Steven Watremez](https://github.com/StevenWatremez) + [#53](https://github.com/NijiDigital/gyro/pull/53) + ## 1.4.0 * Order the Primary Key (identity attribute) first when generating the class for the entity. diff --git a/bin/gyro b/bin/gyro index f5db5fd..dcdf98e 100755 --- a/bin/gyro +++ b/bin/gyro @@ -76,6 +76,7 @@ rescue OptionParser::ParseError => e exit 1 end +# Model directory parsing if options[:model].nil? Gyro::Log.info('No model provided, trying to find one in the local directory…') options[:model] = Gyro::Parser::XCDataModel.find_in_dir(dir) @@ -90,6 +91,8 @@ else Gyro::Log.success("Using #{basename} in #{dirname}") end +xcdatamodel_dir = Pathname.new(options[:model]) + # Liquid Templates if options[:template].nil? # Generate JSON if no -t is specified @@ -114,12 +117,13 @@ end puts <<-INFO.gsub(/ \|/, '') | |#=================================== + |# Model : #{xcdatamodel_dir} |# Template : #{template_dir} |# Output Dir : #{output_dir} |# Params : #{options[:params].inspect} |#=================================== INFO -xcdatamodel = Gyro::Parser::XCDataModel::XCDataModel.new(options[:model]) +xcdatamodel = Gyro::Parser::XCDataModel::XCDataModel.new(xcdatamodel_dir) gen = Gyro::Generator::Liquid.new(template_dir, output_dir, options[:params]) gen.generate(xcdatamodel) diff --git a/lib/gyro/log.rb b/lib/gyro/log.rb index 912af46..af16c5c 100644 --- a/lib/gyro/log.rb +++ b/lib/gyro/log.rb @@ -16,30 +16,30 @@ module Gyro # Print nice and colored output for various error/success/title messages of Gyro # module Log - @quiet = false - def self.quiet=(value) - @quiet = value + @in_test_context = false + def self.in_test_context=(value) + @in_test_context = value end def self.title(str) # bg yellow - puts "\n#{str.colorize(:gray, :blue)}" unless @quiet + puts "\n#{str.colorize(:gray, :blue)}" unless @in_test_context end def self.error(str) - puts "! #{str}".colorize(:red, :bold) unless @quiet + puts "! #{str}".colorize(:red, :bold) unless @in_test_context end def self.info(str) - puts "> #{str}".colorize(:yellow, :bold) unless @quiet + puts "> #{str}".colorize(:yellow, :bold) unless @in_test_context end def self.success(str) - puts "√ #{str}".colorize(:green, :bold) unless @quiet + puts "√ #{str}".colorize(:green, :bold) unless @in_test_context end - def self.fail!(message, stacktrace: false) + def self.fail!(message) Gyro::Log.error message - raise message if stacktrace + raise message if @in_test_context exit 1 end end diff --git a/lib/gyro/parser/xcdatamodel/attribute.rb b/lib/gyro/parser/xcdatamodel/attribute.rb index 32e69a0..23a92db 100644 --- a/lib/gyro/parser/xcdatamodel/attribute.rb +++ b/lib/gyro/parser/xcdatamodel/attribute.rb @@ -116,13 +116,13 @@ def search_for_error # rubocop:disable Style/GuardClause if @type == :undefined || @type.empty? msg = %(The attribute "#{@name}" from "#{@entity_name}" has no type - please fix it) - Gyro::Log.fail!(msg, stacktrace: true) + Gyro::Log.fail!(msg) end if !@json_key_path.empty? && !@enum_values.empty? && (@enum_values.size != @json_values.size) message = %(The attribute "#{@name}" from "#{@entity_name}" is wrongly annotated:) + %(when declaring an type with enum and JSONKeyPath, you must have the same number of items) + %(in the 'enumValues' and 'JSONValues' annotations.) - Gyro::Log.fail!(message, stacktrace: true) + Gyro::Log.fail!(message) end # rubocop:enable Style/GuardClause end diff --git a/lib/gyro/parser/xcdatamodel/relationship.rb b/lib/gyro/parser/xcdatamodel/relationship.rb index c972333..781adb6 100644 --- a/lib/gyro/parser/xcdatamodel/relationship.rb +++ b/lib/gyro/parser/xcdatamodel/relationship.rb @@ -72,12 +72,12 @@ def search_for_error # rubocop:disable Style/GuardClause if inverse_type.empty? && destination.empty? message = %(The relationship "#{@name}" from "#{@entity_name}" is wrong - please fix it) - Gyro::Log.fail!(message, stacktrace: true) + Gyro::Log.fail!(message) end if !destination.empty? && type != :to_many message = %(The relationship "#{@name}" from "#{@entity_name}" is wrong - ) + %(please set a 'No Value' relationship as 'To Many') - Gyro::Log.fail!(message, stacktrace: true) + Gyro::Log.fail!(message) end # rubocop:enable Style/GuardClause end diff --git a/lib/gyro/parser/xcdatamodel/xcdatamodel.rb b/lib/gyro/parser/xcdatamodel/xcdatamodel.rb index 850ccde..77e6806 100644 --- a/lib/gyro/parser/xcdatamodel/xcdatamodel.rb +++ b/lib/gyro/parser/xcdatamodel/xcdatamodel.rb @@ -36,12 +36,18 @@ class XCDataModel attr_accessor :entities def initialize(xcdatamodel_dir) - contents_file = File.join(xcdatamodel_dir, 'contents') - Gyro::Log.fail!('Unable to find contents of xcdatamodel', stacktrace: true) unless File.exist?(contents_file) + is_xcdatamodeld = xcdatamodel_dir.extname != '.xcdatamodeld' + Gyro::Log.fail!(%(Please target an '.xcdatamodel' file inside your xcdatamodeld)) unless is_xcdatamodeld + if xcdatamodel_dir.parent.extname == '.xcdatamodeld' + xcdatamodeld_info_message = 'You are using an xcdatamodeld, ' \ + 'please be sure you target the correct version of your xcdatamodel.' \ + ' Current version used by gyro is: ' + xcdatamodel_dir.basename.to_path + Gyro::Log.info(xcdatamodeld_info_message) + end + file_contents = xcdatamodel_dir + 'contents' + Gyro::Log.fail!('Unable to find contents of xcdatamodel') unless file_contents.exist? @entities = {} - file = File.open(contents_file) - document_xml = Document.new(file) - file.close + document_xml = File.open(file_contents) { |file| Document.new(file) } load_entities(document_xml) end diff --git a/spec/fixtures/xcdatamodel/Model.xcdatamodeld/Model.xcdatamodel/contents b/spec/fixtures/xcdatamodel/Model.xcdatamodeld/Model.xcdatamodel/contents new file mode 100644 index 0000000..6e56d57 --- /dev/null +++ b/spec/fixtures/xcdatamodel/Model.xcdatamodeld/Model.xcdatamodel/contents @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/spec/gyro/liquid_generator_java_spec.rb b/spec/gyro/liquid_generator_java_spec.rb index 960ed5f..aa24d7c 100644 --- a/spec/gyro/liquid_generator_java_spec.rb +++ b/spec/gyro/liquid_generator_java_spec.rb @@ -20,7 +20,7 @@ module Gyro describe 'Liquid' do describe 'Java' do before do - Gyro::Log.quiet = true + Gyro::Log.in_test_context = true end JAVA_MODELS.each do |datamodel| it datamodel do diff --git a/spec/gyro/liquid_generator_kotlin_spec.rb b/spec/gyro/liquid_generator_kotlin_spec.rb index 78e00c7..a1402c9 100644 --- a/spec/gyro/liquid_generator_kotlin_spec.rb +++ b/spec/gyro/liquid_generator_kotlin_spec.rb @@ -23,7 +23,7 @@ module Gyro describe 'Liquid' do describe 'Kotlin' do before do - Gyro::Log.quiet = true + Gyro::Log.in_test_context = true end KOTLIN_MODELS.each do |datamodel| diff --git a/spec/gyro/liquid_generator_swift_spec.rb b/spec/gyro/liquid_generator_swift_spec.rb index fffa574..f9ec7b1 100644 --- a/spec/gyro/liquid_generator_swift_spec.rb +++ b/spec/gyro/liquid_generator_swift_spec.rb @@ -20,7 +20,7 @@ module Gyro describe 'Liquid' do describe 'Swift' do before do - Gyro::Log.quiet = true + Gyro::Log.in_test_context = true end %w[default realm primary ignored inverse enum optional].each do |datamodel| diff --git a/spec/gyro/xcdatamodel_spec.rb b/spec/gyro/xcdatamodel_spec.rb index 96b6a23..b0e03e1 100644 --- a/spec/gyro/xcdatamodel_spec.rb +++ b/spec/gyro/xcdatamodel_spec.rb @@ -15,7 +15,7 @@ module Gyro describe Parser::XCDataModel do before do - Gyro::Log.quiet = true + Gyro::Log.in_test_context = true end it 'check raise an error for file' do @@ -24,6 +24,12 @@ module Gyro .to raise_error 'Unable to find contents of xcdatamodel' end + it 'check raise an error for xcdatamodeld' do + xcdatamodel_dir = fixture('xcdatamodel', 'Model.xcdatamodeld') + expect { Parser::XCDataModel::XCDataModel.new(xcdatamodel_dir) } + .to raise_error %(Please target an '.xcdatamodel' file inside your xcdatamodeld) + end + it 'check raising relationship error' do xcdatamodel_dir = fixture('xcdatamodel', 'error_relationship.xcdatamodel') expect { Parser::XCDataModel::XCDataModel.new(xcdatamodel_dir) }