Class: SwiftGenerator::SwiftDefinitionSet

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_generator/code_generation/swift_class_generation.rb

Overview

The single coordinator object for a set of swift file generations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generated_root: nil, generated_user_root: nil, generated_test_root: nil) ⇒ SwiftDefinitionSet

Returns a new instance of SwiftDefinitionSet.



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1484

def initialize( generated_root:nil, generated_user_root:nil, generated_test_root:nil)
	@generated_root = File.expand_path(generated_root)
	@generated_test_root = File.expand_path(generated_test_root) unless generated_test_root.nil?
	@generated_user_root = File.expand_path(generated_user_root) unless generated_test_root.nil?

	@make_unknown_types = false

	@output_files = {}
	@elements_by_name = {}
	@make_more_supporting_elements = true

	@all_class_characteristics = [
		:json_serializable,
		:comparable,
		:make_test_class,
		:create_user_class
	]

	@types_by_symbol = {}

	initial_property_types.each do |propType|
		@types_by_symbol[propType.swift_type_symbol] = propType
   end

   @company_name = "<defined in SwiftDefinitionSet.company_name>"
end

Instance Attribute Details

#all_class_characteristicsObject

Returns the value of attribute all_class_characteristics.



1454
1455
1456
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1454

def all_class_characteristics
  @all_class_characteristics
end

#company_nameObject

Returns the value of attribute company_name.



1466
1467
1468
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1466

def company_name
  @company_name
end

#elements_by_nameObject

All non-primitives



1457
1458
1459
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1457

def elements_by_name
  @elements_by_name
end

#generated_rootObject

Returns the value of attribute generated_root.



1448
1449
1450
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1448

def generated_root
  @generated_root
end

#generated_test_rootObject

Returns the value of attribute generated_test_root.



1449
1450
1451
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1449

def generated_test_root
  @generated_test_root
end

#generated_user_rootObject

Returns the value of attribute generated_user_root.



1450
1451
1452
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1450

def generated_user_root
  @generated_user_root
end

#make_more_supporting_elementsObject

Returns the value of attribute make_more_supporting_elements.



1462
1463
1464
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1462

def make_more_supporting_elements
  @make_more_supporting_elements
end

#make_unknown_typesObject

Returns the value of attribute make_unknown_types.



1452
1453
1454
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1452

def make_unknown_types
  @make_unknown_types
end

#output_filesObject

Returns the value of attribute output_files.



1455
1456
1457
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1455

def output_files
  @output_files
end

#test_support_classObject

Returns the value of attribute test_support_class.



1464
1465
1466
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1464

def test_support_class
  @test_support_class
end

#types_by_symbolObject

attr_accessor :classes_by_name # Only classes attr_accessor :enums_by_name # Only enums



1461
1462
1463
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1461

def types_by_symbol
  @types_by_symbol
end

Class Method Details

.collection_typesObject



1477
1478
1479
1480
1481
1482
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1477

def self.collection_types
	{
		list: "list",
		idList: "idList"
	}
end

.mutability_typesObject



1468
1469
1470
1471
1472
1473
1474
1475
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1468

def self.mutability_types
	{
		let: MutabilityType.new(:let, 'let', ''),
		var: MutabilityType.new(:var, 'var', ''),
		optional: MutabilityType.new(:optional, 'var', '?', must_be_unwrapped: true),
		unwrapped: MutabilityType.new(:unwrapped, 'var', '!')
	}
end

Instance Method Details

#add_element(swift_element) ⇒ Object



1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1573

def add_element(swift_element)
	file_name = swift_element.file_name || swift_element.type_name
	the_file = file_for_name( file_name, swift_element.is_test_element, swift_element.is_user_editable )
	the_file.add_element(swift_element)
	swift_element.source_file = the_file

	# if swift_element.is_a? SwiftEnum
	# 	@enums_by_name[swift_element.type_name] = swift_element
	# elsif swift_element.is_a? SwiftClass
	# 	@elements_by_name[swift_element.type_name] = swift_element
	# end

	@elements_by_name[swift_element.type_name] = swift_element
	@make_more_supporting_elements = true

	# # Make a type for this class so that references to this class can be resolved
	# type_symbol = swift_element.type_name.to_sym
	# element_property_type = SwiftPropertyType.new( type_symbol, :NSDictionary, swift_kind: :class, test_value:nil )
	# @types_by_symbol[type_symbol] = element_property_type

	(type_symbol, property_type) = swift_element.make_property_type
	@types_by_symbol[type_symbol] = property_type
end

General purpose



1637
1638
1639
1640
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1637

def characteristics_are_legal( characteristics )
	unknown_characteristics = (characteristics - @all_class_characteristics)
	unknown_characteristics.empty?
end

#file_for_name(name, is_test_element, is_user_editable) ⇒ Object

def add_json_serializable_class(serializable_class) @json_serializable_classes << serializable_class end



1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1601

def file_for_name(name, is_test_element, is_user_editable)
	if( is_test_element )
		abort( "User-modifiable test classes are not generated" ) if is_user_editable
		file_dir = @generated_test_root
	else
		if( is_user_editable )
			file_dir = @generated_user_root
		else
			file_dir = @generated_root
		end
	end

	@output_files[name] ||= SwiftFile.new(name, file_dir, is_user_file:is_user_editable, company_name:@company_name)
	return @output_files[name]
end

#make_unknown_type(symbol) ⇒ Object



1630
1631
1632
1633
1634
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1630

def make_unknown_type( symbol )
	property_type = SwiftPropertyType.new( symbol, :NSDictionary )		#TODO: NSDictionary? for unknown type?
	@types_by_symbol[ symbol ] = property_type
	property_type
end

#prepare_for_generationObject



1567
1568
1569
1570
1571
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1567

def prepare_for_generation
	@output_files.each do |_, swiftFile|
		swiftFile.prepare_for_generation
	end
end

#prepare_output_pathsObject



1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1521

def prepare_output_paths
	# Create / empty the generated output directories
	paths = []
	paths << @generated_root unless @generated_root.nil?
	paths << @generated_test_root unless @generated_test_root.nil?
	paths.each do |path|
		FileUtils.mkdir_p(path)
		Dir.foreach(path) do |entry|
			file_path = File.join(path, entry)
			if (File::ftype(file_path) == "file")
				puts "deleting " + file_path
				File.delete(file_path)
			end
		end
	end
end

#prepare_supporting_elementsObject

Called first during code generation. Note that this method may lead to the creation of new files



1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1539

def prepare_supporting_elements()
   unless @generated_test_root.nil?
     @test_support_class = SwiftClass.new(self, 'AutoGeneratedTestSupport', [], characteristics:[], is_test_element: true ) unless

         while @make_more_supporting_elements do
           @make_more_supporting_elements = false
           original_output_files = @output_files.dup
           original_output_files.each do |_, swiftFile|
             swiftFile.prepare_supporting_elements
           end
         end
   end

end

#property_type_for_symbol(symbol) ⇒ Object



1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1617

def property_type_for_symbol(symbol)
	resolved = @types_by_symbol[symbol]
	if resolved.nil?
		if @make_unknown_types
			resolved = make_unknown_type( symbol  )
		else
			puts( "ERROR: Unknown symbol: #{symbol.to_s}")
		end
	end

	resolved
end

#resolve_inheritanceObject

Called during code generation.



1555
1556
1557
1558
1559
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1555

def resolve_inheritance()
	@elements_by_name.each do |_, element|
		element.resolve_inheritance if element.respond_to? :resolve_inheritance
	end
end

#resolve_property_typesObject



1561
1562
1563
1564
1565
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1561

def resolve_property_types()
	@elements_by_name.each do |_, element|
		element.resolve_property_types if element.respond_to? :resolve_property_types
	end
end

#run_generation_sequenceObject



1511
1512
1513
1514
1515
1516
1517
1518
1519
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1511

def run_generation_sequence
	prepare_output_paths
	prepare_supporting_elements
	resolve_property_types
	resolve_inheritance

	prepare_for_generation
	resolve_property_types
end