swift_generator 0.1.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -347,8 +347,6 @@ module SwiftGenerator
347
347
 
348
348
  def write_files_for_definition_set( definition_set )
349
349
  for _, f in definition_set.output_files
350
- path = Pathname.new( f.file_path )
351
- path.parent.mkpath
352
350
  SwiftGenerator::writeGeneratedFile( f )
353
351
  end
354
352
  end
@@ -360,10 +358,14 @@ module_function :write_files_for_definition_set
360
358
 
361
359
  # Templated Output Methods
362
360
  def write_element( element )
363
- if( element.kind_of? SwiftClass )
364
- SwiftGenerator::write_class( element )
361
+ if( element.kind_of? SwiftCategory )
362
+ SwiftGenerator::write_category( element )
363
+ elsif( element.kind_of? SwiftProtocol )
364
+ SwiftGenerator::write_protocol( element )
365
365
  elsif( element.kind_of? SwiftEnum )
366
366
  SwiftGenerator::write_enum( element )
367
+ elsif( element.kind_of? SwiftClass ) # Must be last as it is the superclass
368
+ SwiftGenerator::write_class( element )
367
369
  end
368
370
  end
369
371
 
@@ -372,10 +374,12 @@ module_function :write_element
372
374
  def writeGeneratedFile( f )
373
375
  return if ( f.is_user_file && File.exist?( f.file_path ))
374
376
 
377
+ Pathname.new( f.file_path ).parent().mkpath()
378
+
375
379
  Ribosome.output( f.file_path )
376
380
 
377
381
  Ribosome.dot("//", binding)
378
- Ribosome.dot("// @{f.file_name}", binding)
382
+ Ribosome.dot("// @{ Pathname.new( f.file_name ).basename }", binding)
379
383
  Ribosome.dot("// @{f.company_name}", binding)
380
384
  if $definition_file
381
385
  Ribosome.dot("//", binding)
@@ -431,7 +435,6 @@ Ribosome.dot(" &{declLine}", binding)
431
435
  SwiftGenerator::write_methods( e.methods )
432
436
  Ribosome.dot(" }", binding)
433
437
  end
434
-
435
438
  module_function :write_enum
436
439
 
437
440
 
@@ -444,21 +447,58 @@ Ribosome.add(" : @{c.inheritance_list.join( \", \" )}", binding)
444
447
  end
445
448
  Ribosome.add(" {", binding)
446
449
 
447
- SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
448
- SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
450
+ SwiftGenerator::write_class_body( c )
451
+ Ribosome.dot("}", binding)
452
+ end
453
+ module_function :write_class
449
454
 
450
- SwiftGenerator::write_methods( c.initializers )
451
- SwiftGenerator::write_methods( c.methods )
455
+
456
+ def write_category( c )
457
+ Ribosome.dot("", binding)
458
+ Ribosome.dot("", binding)
459
+ Ribosome.dot("&{c.access_control_modifier}extension @{c.categorized_class_name} /*@{c.type_name || ' '}*/ {", binding)
460
+ SwiftGenerator::write_class_body( c )
452
461
  Ribosome.dot("}", binding)
462
+ end
463
+ module_function :write_category
464
+
465
+ def write_class_body( c )
466
+ c.top_inner_comment_block().each do |line|
467
+ Ribosome.dot(" &{line}", binding)
468
+ end
453
469
 
470
+ # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
471
+ # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
472
+ SwiftGenerator::write_property_declarations( c.transient_properties )
473
+ SwiftGenerator::write_property_declarations( c.persistent_properties )
474
+
475
+ SwiftGenerator::write_methods( c.initializers )
476
+ SwiftGenerator::write_methods( c.methods )
454
477
  end
478
+ module_function :write_class_body
455
479
 
456
- module_function :write_class
480
+ def write_protocol( p )
481
+ Ribosome.dot("", binding)
482
+ Ribosome.dot("", binding)
483
+ Ribosome.dot("&{p.access_control_modifier}protocol @{p.type_name}", binding)
484
+ if p.inheritance_list.count > 0
485
+ Ribosome.add(" : @{p.inheritance_list.join( \", \" )}", binding)
486
+ end
487
+ Ribosome.add(" {", binding)
488
+ # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
489
+ # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
490
+ SwiftGenerator::write_property_declarations( p.transient_properties )
491
+ SwiftGenerator::write_property_declarations( p.persistent_properties )
457
492
 
493
+ SwiftGenerator::write_methods( p.initializers, for_protocol=true )
494
+ SwiftGenerator::write_methods( p.methods, for_protocol=true )
495
+ Ribosome.dot("}", binding)
496
+ end
497
+ module_function :write_protocol
458
498
 
459
- def write_property_declarations( properties, property_type_label )
499
+ def write_property_declarations( properties, property_type_label=nil )
460
500
  properties.each_with_index do |prop, i|
461
- if i == 0
501
+ if i == 0 && ! property_type_label.nil?
462
502
  Ribosome.dot("", binding)
463
503
  Ribosome.dot(" // MARK: @{property_type_label} Properties", binding)
464
504
  end
@@ -466,13 +506,12 @@ Ribosome.dot(" // MARK: @{property_type_label} Properties", binding)
466
506
  Ribosome.dot(" &{declLine}", binding)
467
507
  end
468
508
  end
469
-
470
509
  end
471
510
 
472
511
  module_function :write_property_declarations
473
512
 
474
513
 
475
- def write_methods( methods )
514
+ def write_methods( methods, for_protocol=false )
476
515
  methods.each_with_index do |m, i|
477
516
  overrideString = m.override ? 'override ' : ''
478
517
  Ribosome.dot("", binding)
@@ -480,15 +519,18 @@ Ribosome.dot("", binding)
480
519
  Ribosome.dot(" &{m.comment}", binding)
481
520
  end
482
521
  args = m.argStr.nil? || m.argStr.empty? ? m.argStr : ' ' + m.argStr + ' '
483
- Ribosome.dot(" &{overrideString}&{m.access_control_modifier}&{m.func_fragment} @{m.name}(&{args})", binding)
522
+ access_control_modifier_str = m.access_control_modifiers.length > 0 ? m.access_control_modifiers.join( ", " ) + " " : ""
523
+ Ribosome.dot(" &{overrideString}&{access_control_modifier_str}&{m.func_fragment + \" \" unless m.func_fragment.length == 0}@{m.name}(&{args})", binding)
484
524
  if ! (m.returns.nil? || m.returns.length == 0 )
485
525
  Ribosome.add(" -> @{m.returns}", binding)
486
526
  end
527
+ unless for_protocol
487
528
  Ribosome.add(" {", binding)
488
- m.bodyLines.each do |line|
489
- Ribosome.dot(" &{line}", binding)
490
- end
529
+ m.bodyLines.each do |line|
530
+ Ribosome.dot(" &{line}", binding)
531
+ end
491
532
  Ribosome.dot(" }", binding)
533
+ end
492
534
  end
493
535
  end
494
536
 
@@ -76,31 +76,64 @@ end
76
76
  def initial_property_types
77
77
 
78
78
  iso_date = SwiftPropertyType.new( :NSDate, :String, auto_bridged:false, test_value: $date_lambda )
79
- iso_date.custom_marshaling = lambda{|dest, value| "#{dest} = DateFormatting.isoStringFromDate( #{value} )" }
80
- iso_date.custom_unmarshaling = lambda{|dest, value| "#{dest} = DateFormatting.dateFromISOString( #{value} )" }
79
+ iso_date.custom_marshaling = lambda{|dest, value| "#{dest} = DateFormatting.isoStringFromDate( #{value} )" }
80
+ iso_date.custom_unmarshaling = lambda{|dest, value| "#{dest} = DateFormatting.dateFromISOString( #{value} )" }
81
81
  iso_date.custom_equality_test = lambda{|name, other| "optionalDatesEqual( #{name}, #{other})" }
82
82
 
83
- int_type = SwiftPropertyType.new( :Int, :NSNumber, auto_bridged:true, test_value: $number_lambda )
83
+ int_type = SwiftPropertyType.new( :Int, :NSNumber, auto_bridged:true, test_value: $number_lambda )
84
84
  uint_type = SwiftPropertyType.new( :UInt, :NSNumber, auto_bridged:true, test_value: $number_lambda )
85
85
  float_type = SwiftPropertyType.new( :Float, :NSNumber, auto_bridged:true, test_value: $number_lambda )
86
- double_type = SwiftPropertyType.new( :Double, :NSNumber, auto_bridged:true, test_value: $number_lambda )
86
+ double_type = SwiftPropertyType.new( :Double, :NSNumber, auto_bridged:true, test_value: $number_lambda )
87
87
  bool_type = SwiftPropertyType.new( :Bool, :NSNumber, auto_bridged:true, test_value: lambda{|num| return num.even? ? 'true' : 'false' })
88
- int_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.integerValue" }
88
+
89
+ int_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.integerValue" }
89
90
  uint_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.unsignedIntegerValue" }
91
+
90
92
  float_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.floatValue" }
91
93
  double_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.doubleValue" }
92
94
  bool_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.boolValue" }
93
95
 
96
+ # Specified-Precision numeric types
97
+ int8_type = SwiftPropertyType.new( :Int8, :NSNumber, auto_bridged:true, test_value: $number_lambda )
98
+ int16_type = SwiftPropertyType.new( :Int16, :NSNumber, auto_bridged:true, test_value: $number_lambda )
99
+ int32_type = SwiftPropertyType.new( :Int32, :NSNumber, auto_bridged:true, test_value: $number_lambda )
100
+ int64_type = SwiftPropertyType.new( :Int64, :NSNumber, auto_bridged:true, test_value: $number_lambda )
101
+ uint8_type = SwiftPropertyType.new( :UInt8, :NSNumber, auto_bridged:true, test_value: $number_lambda )
102
+ uint16_type = SwiftPropertyType.new( :UInt16, :NSNumber, auto_bridged:true, test_value: $number_lambda )
103
+ uint32_type = SwiftPropertyType.new( :UInt32, :NSNumber, auto_bridged:true, test_value: $number_lambda )
104
+ uint64_type = SwiftPropertyType.new( :UInt64, :NSNumber, auto_bridged:true, test_value: $number_lambda )
105
+
106
+ float80_type = SwiftPropertyType.new( :Float80, :NSNumber, auto_bridged:true, test_value: $number_lambda )
107
+ # TODO: unmarshaling for specified-precision numeric types
108
+ # int8_type.custom_unmarshaling
109
+ # int16_type.custom_unmarshaling
110
+ # int32_type.custom_unmarshaling
111
+ # int64_type.custom_unmarshaling
112
+ # uint8_type.custom_unmarshaling
113
+ # uint16_type.custom_unmarshaling
114
+ # uint32_type.custom_unmarshaling
115
+ # uint64_type.custom_unmarshaling
116
+
117
+ # float80_type.custom_unmarshaling
118
+
94
119
  return [
95
120
  int_type,
96
121
  uint_type,
97
122
  float_type,
98
123
  double_type,
99
124
  bool_type,
100
- SwiftPropertyType.new( :String, :NSString, auto_bridged:true, test_value: $string_lambda ),
101
- SwiftPropertyType.new( :NSString, :NSString, auto_bridged:true, test_value: $string_lambda ),
102
- SwiftPropertyType.new( :USIObjectID, :NSString, auto_bridged:true, test_value: $string_lambda ),
103
- SwiftPropertyType.new( :AnvilEndpoint, :AnvilEndpoint, auto_bridged:true, test_value: nil ),
125
+ int8_type,
126
+ int16_type,
127
+ int32_type,
128
+ int64_type,
129
+ uint8_type,
130
+ uint16_type,
131
+ uint32_type,
132
+ uint64_type,
133
+ SwiftPropertyType.new( :String, :NSString, auto_bridged:true, test_value: $string_lambda ),
134
+ SwiftPropertyType.new( :NSString, :NSString, auto_bridged:true, test_value: $string_lambda ),
135
+ SwiftPropertyType.new( :USIObjectID, :NSString, auto_bridged:true, test_value: $string_lambda ),
136
+ SwiftPropertyType.new( :AnvilEndpoint, :AnvilEndpoint, auto_bridged:true, test_value: nil ),
104
137
  iso_date
105
138
  ]
106
139
  end
@@ -48,8 +48,8 @@ module SwiftGenerator
48
48
  inheritance_list = class_spec[ "inheritanceList" ]
49
49
  file_name = class_spec[ "fileName" ]
50
50
  characteristics_name = class_spec[ "characteristics" ]
51
- is_test_element = class_spec[ "isTestElement" ]
52
- is_user_editable = class_spec[ "isUserEditable" ] # not yet supported (?)
51
+ is_test_element = class_spec[ "isTestElement" ] || false
52
+ is_user_editable = class_spec[ "isUserEditable" ] || false
53
53
 
54
54
  characteristics = characteristics_by_name[ characteristics_name ]
55
55
  characteristics ||= $default_swift_class_characteristics
@@ -78,18 +78,15 @@ module SwiftGenerator
78
78
  property_name = prop_spec[ "name" ]
79
79
  type_symbol = prop_spec[ "type" ].to_sym
80
80
  is_persistent = prop_spec[ "isPersistent" ]
81
- mutability = prop_spec[ "mutability" ]
81
+ mutability = prop_spec[ "mutability" ] || "let"
82
82
  initialization_value = prop_spec[ "initializationValue" ]
83
83
  collection_type = prop_spec[ "collectionType" ]
84
- required = prop_spec[ "required" ]
84
+ required = prop_spec[ "required" ] || true
85
85
  rest_omit = prop_spec[ "restOmit" ]
86
86
  json_key = prop_spec[ "jsonKey" ] # only used by SwiftPersistentProperty
87
87
 
88
- mutability ||= "let"
89
88
  mutability = mutability.to_sym
90
89
 
91
- required ||= true
92
-
93
90
  if( is_persistent )
94
91
  SwiftPersistentProperty.new( swift_class,
95
92
  property_name,
@@ -1,3 +1,3 @@
1
1
  module SwiftGenerator
2
- VERSION = "0.1.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|# for "generating swift classes"
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  # if spec.respond_to?(:metadata)
26
- spec.metadata['allowed_push_host'] = "http://rubygems:n6uqyRnAqzQP3M@rubygems-noneventsystems.rhcloud.com/"
26
+ # spec.metadata['allowed_push_host'] = "http://rubygems:n6uqyRnAqzQP3M@rubygems-noneventsystems.rhcloud.com/"
27
27
  # end
28
28
 
29
29
  spec.add_development_dependency "bundler", "~> 1.9"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swift_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Kornher
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-09 00:00:00.000000000 Z
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,10 +78,9 @@ files:
78
78
  - bin/console
79
79
  - bin/genswift
80
80
  - bin/setup
81
+ - examples/ReadmeExample.rb
81
82
  - lib/swift_generator.rb
82
- - lib/swift_generator/code_generation/POSOTest.rb
83
83
  - lib/swift_generator/code_generation/SwiftFileTemplate.dna
84
- - lib/swift_generator/code_generation/ToRemove/generate.bash
85
84
  - lib/swift_generator/code_generation/code_generation_common.rb
86
85
  - lib/swift_generator/code_generation/swift_class_generation.rb
87
86
  - lib/swift_generator/code_generation/swift_file_template.rb
@@ -93,8 +92,7 @@ files:
93
92
  homepage: ''
94
93
  licenses:
95
94
  - MIT
96
- metadata:
97
- allowed_push_host: http://rubygems:n6uqyRnAqzQP3M@rubygems-noneventsystems.rhcloud.com/
95
+ metadata: {}
98
96
  post_install_message:
99
97
  rdoc_options: []
100
98
  require_paths:
@@ -1,8 +0,0 @@
1
- require_relative 'swift_class_generation'
2
-
3
- gen = SwiftDefinitionSet.new( 'output' )
4
-
5
- c1 = swift_class.new( gen, 'User', ['USIWireData'] )
6
-
7
- puts c1
8
- puts gen
@@ -1,41 +0,0 @@
1
- #!/bin/bash
2
- cd "$(dirname "$0")"
3
- pwd
4
-
5
- SOURCE_FILES=../Useful/Shared/Models/Generated/*.swift
6
- for f in $SOURCE_FILES
7
- do
8
- rm $f
9
- done
10
-
11
- TEST_FILES=../UsefulTests/Shared/Models/Generated/*.swift
12
- for f in $TEST_FILES
13
- do
14
- rm $f
15
- done
16
-
17
- ./Tools/ribosome --rna PlainOldSwiftObjects.dna > PlainOldSwiftObjects.rna
18
- ./PlainOldSwiftObjects.rna
19
-
20
-
21
- for f in $SOURCE_FILES
22
- do
23
- echo " "
24
- echo "--------------------------------------------------------------------------------"
25
- echo " "
26
- echo "Generated Source File: $f"
27
- echo " "
28
- # take action on each file. $f store current file name
29
- cat $f
30
- done
31
-
32
- for f in $TEST_FILES
33
- do
34
- echo " "
35
- echo "--------------------------------------------------------------------------------"
36
- echo " "
37
- echo "Generated Test File: $f"
38
- echo " "
39
- # take action on each file. $f store current file name
40
- cat $f
41
- done