Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Manage xcdatamodeld #53

Merged
merged 6 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 5 additions & 1 deletion bin/gyro
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
18 changes: 9 additions & 9 deletions lib/gyro/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/gyro/parser/xcdatamodel/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/gyro/parser/xcdatamodel/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions lib/gyro/parser/xcdatamodel/xcdatamodel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="16G1212" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<elements/>
</model>
2 changes: 1 addition & 1 deletion spec/gyro/liquid_generator_java_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/gyro/liquid_generator_kotlin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion spec/gyro/liquid_generator_swift_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
8 changes: 7 additions & 1 deletion spec/gyro/xcdatamodel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
Expand Down