swift_generator 0.1.2

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.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.idea/.name +1 -0
  4. data/.idea/.rakeTasks +7 -0
  5. data/.idea/compiler.xml +22 -0
  6. data/.idea/copyright/profiles_settings.xml +3 -0
  7. data/.idea/dictionaries/ckornher.xml +7 -0
  8. data/.idea/encodings.xml +4 -0
  9. data/.idea/misc.xml +4 -0
  10. data/.idea/modules.xml +8 -0
  11. data/.idea/runConfigurations/Install_the_Gem.xml +26 -0
  12. data/.idea/runConfigurations/Ribosome.xml +26 -0
  13. data/.idea/runConfigurations/genswift_help.xml +26 -0
  14. data/.idea/runConfigurations/test1.xml +30 -0
  15. data/.idea/scopes/scope_settings.xml +5 -0
  16. data/.idea/swift_generator.iml +126 -0
  17. data/.idea/uiDesigner.xml +124 -0
  18. data/.idea/vcs.xml +6 -0
  19. data/.idea/workspace.xml +1356 -0
  20. data/.rspec +2 -0
  21. data/.travis.yml +3 -0
  22. data/CODE_OF_CONDUCT.md +13 -0
  23. data/Gemfile +7 -0
  24. data/LICENSE.txt +21 -0
  25. data/README.md +39 -0
  26. data/Rakefile +1 -0
  27. data/Tools/compile_templates.bash +3 -0
  28. data/Tools/help.rna +340 -0
  29. data/Tools/ribosome.rb +589 -0
  30. data/bin/console +14 -0
  31. data/bin/genswift +34 -0
  32. data/bin/setup +7 -0
  33. data/lib/swift_generator.rb +23 -0
  34. data/lib/swift_generator/code_generation/POSOTest.rb +8 -0
  35. data/lib/swift_generator/code_generation/SwiftFileTemplate.dna +157 -0
  36. data/lib/swift_generator/code_generation/ToRemove/generate.bash +41 -0
  37. data/lib/swift_generator/code_generation/code_generation_common.rb +6 -0
  38. data/lib/swift_generator/code_generation/swift_class_generation.rb +1589 -0
  39. data/lib/swift_generator/code_generation/swift_file_template.rb +499 -0
  40. data/lib/swift_generator/code_generation/swift_types.rb +106 -0
  41. data/lib/swift_generator/code_generation/usi_metaclasses.rb +23 -0
  42. data/lib/swift_generator/specfile_parser.rb +116 -0
  43. data/lib/swift_generator/version.rb +3 -0
  44. data/swift_generator.gemspec +31 -0
  45. metadata +118 -0
@@ -0,0 +1,106 @@
1
+
2
+ # NSString, NSNumber, NSArray, NSDictionary or NSNull
3
+
4
+ $number_lambda = lambda{|num| return "#{num}" }
5
+ $string_lambda = lambda{|num| return "\"STR_#{num}\"" }
6
+ $date_lambda = lambda{|num| return "NSDate( timeIntervalSince1970:NSDate( timeIntervalSinceNow:-#{num}).timeIntervalSince1970 )" }
7
+
8
+ class SwiftPropertyType
9
+ attr_accessor :swift_type_symbol
10
+ attr_accessor :swift_type_name
11
+ attr_accessor :serialized_json_type
12
+ attr_accessor :auto_bridged
13
+ attr_accessor :swift_kind
14
+ attr_accessor :test_value_lambda
15
+ attr_accessor :custom_marshaling
16
+ attr_accessor :custom_unmarshaling
17
+ attr_accessor :custom_equality_test
18
+ attr_accessor :hashable_value_lambda
19
+
20
+ def initialize( swift_type_symbol, serialized_json_type, auto_bridged:false, swift_kind: :primitive,
21
+ test_value:lambda{|num| 'undefined' }, unmarshal_method: nil)
22
+ @swift_type_symbol = swift_type_symbol
23
+ @swift_type_name = swift_type_symbol.to_s
24
+ @serialized_json_type = serialized_json_type
25
+ @auto_bridged = auto_bridged
26
+ @swift_kind = swift_kind
27
+ @test_value_lambda = test_value
28
+ @custom_equality_test = nil
29
+ @hashable_value_lambda = nil
30
+ @unmarshal_method_lambda = unmarshal_method
31
+ end
32
+
33
+ def self.all_swift_kinds
34
+ {
35
+ primitive: 'Swift primitive, e.g. Int, String',
36
+ class: 'Swift Class',
37
+ struct: 'Swift Struct',
38
+ enum: 'Swift Enum'
39
+ }
40
+ end
41
+
42
+ def make_test_value( index )
43
+ @test_value_lambda.call( index )
44
+ end
45
+
46
+ def hashable_value( var_name, is_optional)
47
+ if @hashable_value_lambda.nil?
48
+ return var_name
49
+ else
50
+ return @hashable_value_lambda.call( var_name, is_optional )
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ class StringEnumPropertyType < SwiftPropertyType
57
+ attr_accessor :enum
58
+
59
+ def initialize( enum, serialized_json_type, test_value:lambda{|num| 'undefined' } )
60
+ super( enum.swift_type_symbol, serialized_json_type, swift_kind: :enum, test_value:test_value )
61
+ @enum = enum
62
+ end
63
+ end
64
+
65
+
66
+ class SwiftObjectPropertyType < SwiftPropertyType
67
+ attr_accessor :swift_class
68
+
69
+ def initialize( swift_class, serialized_json_type, test_value:lambda{|num| 'undefined' } )
70
+ super( swift_class.swift_type_symbol, serialized_json_type, swift_kind: :class, test_value:test_value )
71
+ @swift_class = swift_class
72
+ end
73
+ end
74
+
75
+
76
+ def initial_property_types
77
+
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} )" }
81
+ iso_date.custom_equality_test = lambda{|name, other| "optionalDatesEqual( #{name}, #{other})" }
82
+
83
+ int_type = SwiftPropertyType.new( :Int, :NSNumber, auto_bridged:true, test_value: $number_lambda )
84
+ uint_type = SwiftPropertyType.new( :UInt, :NSNumber, auto_bridged:true, test_value: $number_lambda )
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 )
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" }
89
+ uint_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.unsignedIntegerValue" }
90
+ float_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.floatValue" }
91
+ double_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.doubleValue" }
92
+ bool_type.custom_unmarshaling = lambda{|dest, value| "#{dest} = #{value}.boolValue" }
93
+
94
+ return [
95
+ int_type,
96
+ uint_type,
97
+ float_type,
98
+ double_type,
99
+ 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 ),
104
+ iso_date
105
+ ]
106
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'swift_class_generation'
2
+ # Standard USI Model settings
3
+ $default_uis_model_characteristics = [:json_serializable, :comparable, :make_test_class, :create_user_class]
4
+
5
+ # USI Model (Wire Data) settings
6
+ # defs = SwiftDefinitionSet.new( generated_root:model_generated_source,
7
+ # generated_user_root:model_user_source,
8
+ # generated_test_root:model_generated_test)
9
+
10
+
11
+
12
+
13
+ class USIWireDataClass < SwiftClass
14
+ attr_accessor :rest_endpoint
15
+
16
+ def initialize (definition_set, specified_type_name, endpoint:nil )
17
+
18
+ super(definition_set, specified_type_name, inheritance_list=['USIBaseModel'],
19
+ characteristics:$default_uis_model_characteristics, is_test_element: false, is_user_editable: false)
20
+
21
+ add_simple_class_property( "restEndpoint", :AnvilEndpoint, value:endpoint, override:true ) if !endpoint.nil?
22
+ end
23
+ end
@@ -0,0 +1,116 @@
1
+ require "swift_generator/version"
2
+
3
+ require 'json'
4
+
5
+ require "swift_generator/code_generation/swift_class_generation"
6
+ require "swift_generator/code_generation/swift_file_template"
7
+
8
+ #TODO: Use jamesbrooks/hash_validator to check for required and valid values
9
+
10
+ module SwiftGenerator
11
+
12
+ class SpecfileParser
13
+
14
+ attr_accessor :specfile_path
15
+
16
+ def initialize( specfile_path )
17
+ @specfile_path = specfile_path
18
+ end
19
+
20
+ def process_specfile( )
21
+ swift_definition_set = read_specfile
22
+ swift_definition_set.run_generation_sequence
23
+
24
+ SwiftGenerator::write_files_for_definition_set( swift_definition_set )
25
+
26
+ end
27
+
28
+ def read_specfile( )
29
+ file_data = @specfile_path.read
30
+ spec_hash = JSON.parse(file_data)
31
+
32
+ generated_source_root = spec_hash[ "sourceRoot" ]
33
+ generated_user_source_root = spec_hash[ "userClassSourceRoot" ]
34
+ generated_test_root = spec_hash[ "testRoot" ]
35
+
36
+ definition_set = SwiftDefinitionSet.new( generated_root:generated_source_root,
37
+ generated_user_root:generated_user_source_root,
38
+ generated_test_root:generated_test_root)
39
+
40
+ characteristics_by_name = spec_hash[ "characteristicSets" ] || {}
41
+ characteristics_by_name.each do |name, characteristics|
42
+ #TODO validate
43
+ end
44
+
45
+ classes = spec_hash[ "classes" ]
46
+ classes.each do | class_spec |
47
+ specified_type_name = class_spec[ "typeName" ]
48
+ inheritance_list = class_spec[ "inheritanceList" ]
49
+ file_name = class_spec[ "fileName" ]
50
+ characteristics_name = class_spec[ "characteristics" ]
51
+ is_test_element = class_spec[ "isTestElement" ]
52
+ is_user_editable = class_spec[ "isUserEditable" ] # not yet supported (?)
53
+
54
+ characteristics = characteristics_by_name[ characteristics_name ]
55
+ characteristics ||= $default_swift_class_characteristics
56
+
57
+ # def initialize (definition_set, specified_type_name, inheritance_list=[], file_name: nil,
58
+ # characteristics:default_swift_class_characteristics, is_test_element: false, is_user_editable: false)
59
+ swift_class = SwiftClass.new(
60
+ definition_set,
61
+ specified_type_name,
62
+ inheritance_list,
63
+ file_name: file_name,
64
+ characteristics: characteristics,
65
+ is_test_element: is_test_element,
66
+ is_user_editable: is_user_editable )
67
+
68
+ properties = class_spec[ "properties" ] || []
69
+ properties.each do | prop_spec |
70
+ add_property( swift_class, prop_spec )
71
+ end
72
+ end
73
+
74
+ return definition_set
75
+ end
76
+
77
+ def add_property( swift_class, prop_spec )
78
+ property_name = prop_spec[ "name" ]
79
+ type_symbol = prop_spec[ "type" ].to_sym
80
+ is_persistent = prop_spec[ "isPersistent" ]
81
+ mutability = prop_spec[ "mutability" ]
82
+ initialization_value = prop_spec[ "initializationValue" ]
83
+ collection_type = prop_spec[ "collectionType" ]
84
+ required = prop_spec[ "required" ]
85
+ rest_omit = prop_spec[ "restOmit" ]
86
+ json_key = prop_spec[ "jsonKey" ] # only used by SwiftPersistentProperty
87
+
88
+ mutability ||= "let"
89
+ mutability = mutability.to_sym
90
+
91
+ required ||= true
92
+
93
+ if( is_persistent )
94
+ SwiftPersistentProperty.new( swift_class,
95
+ property_name,
96
+ type_symbol,
97
+ mutability,
98
+ initialization_value,
99
+ collection_type:collection_type,
100
+ # required = required, ???
101
+ json_key:json_key,
102
+ rest_omit:rest_omit )
103
+ else
104
+ SwiftProperty.new( swift_class,
105
+ property_name,
106
+ property_type:type_symbol,
107
+ mutability:mutability,
108
+ initialization_value:initialization_value,
109
+ collection_type:collection_type,
110
+ required:required,
111
+ rest_omit:rest_omit )
112
+ end
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module SwiftGenerator
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swift_generator/version'
5
+
6
+ Gem::Specification.new do |spec|# for "generating swift classes"
7
+ spec.name = "swift_generator"
8
+ spec.version = SwiftGenerator::VERSION
9
+ spec.authors = ["Chris Kornher"]
10
+ spec.email = ["ckornher@mac.com"]
11
+
12
+ spec.summary = "Generate swift classes with JSON serialization, hashValue, copy and isEqual."
13
+ spec.description = %{Provides an API and command line tool for generating "pure" Swift classes,
14
+ enums either through a ruby API or command-line tool and a JSON spec file.}
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.required_ruby_version = '>= 2.0.0'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ # if spec.respond_to?(:metadata)
26
+ spec.metadata['allowed_push_host'] = "http://rubygems:n6uqyRnAqzQP3M@rubygems-noneventsystems.rhcloud.com/"
27
+ # end
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.9"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swift_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Chris Kornher
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: |-
42
+ Provides an API and command line tool for generating "pure" Swift classes,
43
+ enums either through a ruby API or command-line tool and a JSON spec file.
44
+ email:
45
+ - ckornher@mac.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".idea/.name"
52
+ - ".idea/.rakeTasks"
53
+ - ".idea/compiler.xml"
54
+ - ".idea/copyright/profiles_settings.xml"
55
+ - ".idea/dictionaries/ckornher.xml"
56
+ - ".idea/encodings.xml"
57
+ - ".idea/misc.xml"
58
+ - ".idea/modules.xml"
59
+ - ".idea/runConfigurations/Install_the_Gem.xml"
60
+ - ".idea/runConfigurations/Ribosome.xml"
61
+ - ".idea/runConfigurations/genswift_help.xml"
62
+ - ".idea/runConfigurations/test1.xml"
63
+ - ".idea/scopes/scope_settings.xml"
64
+ - ".idea/swift_generator.iml"
65
+ - ".idea/uiDesigner.xml"
66
+ - ".idea/vcs.xml"
67
+ - ".idea/workspace.xml"
68
+ - ".rspec"
69
+ - ".travis.yml"
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - Tools/compile_templates.bash
76
+ - Tools/help.rna
77
+ - Tools/ribosome.rb
78
+ - bin/console
79
+ - bin/genswift
80
+ - bin/setup
81
+ - lib/swift_generator.rb
82
+ - lib/swift_generator/code_generation/POSOTest.rb
83
+ - lib/swift_generator/code_generation/SwiftFileTemplate.dna
84
+ - lib/swift_generator/code_generation/ToRemove/generate.bash
85
+ - lib/swift_generator/code_generation/code_generation_common.rb
86
+ - lib/swift_generator/code_generation/swift_class_generation.rb
87
+ - lib/swift_generator/code_generation/swift_file_template.rb
88
+ - lib/swift_generator/code_generation/swift_types.rb
89
+ - lib/swift_generator/code_generation/usi_metaclasses.rb
90
+ - lib/swift_generator/specfile_parser.rb
91
+ - lib/swift_generator/version.rb
92
+ - swift_generator.gemspec
93
+ homepage: ''
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: http://rubygems:n6uqyRnAqzQP3M@rubygems-noneventsystems.rhcloud.com/
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.0.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Generate swift classes with JSON serialization, hashValue, copy and isEqual.
118
+ test_files: []