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.



1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1429

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.



1399
1400
1401
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1399

def all_class_characteristics
  @all_class_characteristics
end

#company_nameObject

Returns the value of attribute company_name.



1411
1412
1413
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1411

def company_name
  @company_name
end

#elements_by_nameObject

All non-primitives



1402
1403
1404
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1402

def elements_by_name
  @elements_by_name
end

#generated_rootObject

Returns the value of attribute generated_root.



1393
1394
1395
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1393

def generated_root
  @generated_root
end

#generated_test_rootObject

Returns the value of attribute generated_test_root.



1394
1395
1396
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1394

def generated_test_root
  @generated_test_root
end

#generated_user_rootObject

Returns the value of attribute generated_user_root.



1395
1396
1397
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1395

def generated_user_root
  @generated_user_root
end

#make_more_supporting_elementsObject

Returns the value of attribute make_more_supporting_elements.



1407
1408
1409
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1407

def make_more_supporting_elements
  @make_more_supporting_elements
end

#make_unknown_typesObject

Returns the value of attribute make_unknown_types.



1397
1398
1399
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1397

def make_unknown_types
  @make_unknown_types
end

#output_filesObject

Returns the value of attribute output_files.



1400
1401
1402
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1400

def output_files
  @output_files
end

#test_support_classObject

Returns the value of attribute test_support_class.



1409
1410
1411
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1409

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



1406
1407
1408
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1406

def types_by_symbol
  @types_by_symbol
end

Class Method Details

.collection_typesObject



1422
1423
1424
1425
1426
1427
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1422

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

.mutability_typesObject



1413
1414
1415
1416
1417
1418
1419
1420
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1413

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



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

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



1582
1583
1584
1585
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1582

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



1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1546

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



1575
1576
1577
1578
1579
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1575

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



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

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

#prepare_output_pathsObject



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1466

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



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1484

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



1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1562

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.



1500
1501
1502
1503
1504
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1500

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

#resolve_property_typesObject



1506
1507
1508
1509
1510
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1506

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



1456
1457
1458
1459
1460
1461
1462
1463
1464
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 1456

def run_generation_sequence
	prepare_output_paths
	prepare_supporting_elements
	resolve_property_types
	resolve_inheritance

	prepare_for_generation
	resolve_property_types
end