objc2swift_assistant 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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/compiler.xml +22 -0
- data/.idea/copyright/profiles_settings.xml +3 -0
- data/.idea/dictionaries/ckornher.xml +7 -0
- data/.idea/encodings.xml +4 -0
- data/.idea/misc.xml +4 -0
- data/.idea/modules.xml +8 -0
- data/.idea/objc2swift_assistant.iml +103 -0
- data/.idea/runConfigurations/Console.xml +26 -0
- data/.idea/runConfigurations/assistant.xml +28 -0
- data/.idea/runConfigurations/help_text.xml +28 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/uiDesigner.xml +124 -0
- data/.idea/vcs.xml +6 -0
- data/.idea/workspace.xml +1484 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/objc2swift_assistant +50 -0
- data/bin/setup +7 -0
- data/lib/assistant.rb +110 -0
- data/lib/objc2swift_assistant/code_recognizer.rb +220 -0
- data/lib/objc2swift_assistant/file_hierarchical_config.rb +168 -0
- data/lib/objc2swift_assistant/file_sets.rb +577 -0
- data/lib/objc2swift_assistant/logging.rb +19 -0
- data/lib/objc2swift_assistant/objc_2_swift.rb +734 -0
- data/lib/objc2swift_assistant/objc_2_swift_block_conversion.rb +106 -0
- data/lib/objc2swift_assistant/objc_2_swift_configuration.rb +108 -0
- data/lib/objc2swift_assistant/objc_2_swift_type_mapping.rb +64 -0
- data/lib/objc2swift_assistant/objc_variable_types.rb +76 -0
- data/lib/objc2swift_assistant/processing_element.rb +32 -0
- data/lib/objc2swift_assistant/recognizers/at_sign_directives_recognizer.rb +41 -0
- data/lib/objc2swift_assistant/recognizers/category_recognizer.rb +72 -0
- data/lib/objc2swift_assistant/recognizers/enum_recognizer.rb +0 -0
- data/lib/objc2swift_assistant/recognizers/implementation_recognizer.rb +31 -0
- data/lib/objc2swift_assistant/recognizers/interface_recognizer.rb +37 -0
- data/lib/objc2swift_assistant/recognizers/method_recognizer.rb +190 -0
- data/lib/objc2swift_assistant/recognizers/pragma_mark_recognizer.rb +41 -0
- data/lib/objc2swift_assistant/recognizers/property_declaration_recognizer.rb +78 -0
- data/lib/objc2swift_assistant/recognizers/protocol_recognizer.rb +40 -0
- data/lib/objc2swift_assistant/recognizers/recognizer_keys.rb +26 -0
- data/lib/objc2swift_assistant/settings_file.rb +23 -0
- data/lib/objc2swift_assistant/swift_file_generator.rb +27 -0
- data/lib/objc2swift_assistant/version.rb +3 -0
- data/objc2swift_assistant.gemspec +31 -0
- metadata +128 -0
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Chris Kornher
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# objc2swift_assistant
|
2
|
+
|
3
|
+
This ruby gem is a command line tool to generate Swift files from Objective-C source files. The file structure of the original source is mirrored by the generated code by default.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'objc2swift_assistant'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install objc2swift_assistant
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
obj2swift generate --source SOURCE_PATH --dest GENERATED_CODE_PATH --config CONFIG_FILE
|
26
|
+
```
|
27
|
+
|
28
|
+
### Config Files
|
29
|
+
Config files can specify:
|
30
|
+
* Directories to ignore
|
31
|
+
* Source files to ignore
|
32
|
+
* **Coming Soon**: Mappings from Objective-C types to Swift types
|
33
|
+
|
34
|
+
An example configuration file:
|
35
|
+
```json
|
36
|
+
{
|
37
|
+
"company_name": "MyGreatCompany.com",
|
38
|
+
"ignore": [
|
39
|
+
"**/CoreData",
|
40
|
+
"**/Test",
|
41
|
+
"SourceToOmit.h",
|
42
|
+
"SourceToOmit.m"
|
43
|
+
],
|
44
|
+
"subdirs": [
|
45
|
+
{
|
46
|
+
"path": "Customers/Customer1",
|
47
|
+
"company_name": "Customer1 Inc."
|
48
|
+
}
|
49
|
+
]
|
50
|
+
}
|
51
|
+
```
|
52
|
+
#### Config File Structure
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
##### Config File Keys
|
57
|
+
The keys supported in config files at this time:
|
58
|
+
* **"path"** : The path of the current node, relative to the parent node. "**"
|
59
|
+
wildcards allowed in leftmost position. Ignored in root node.
|
60
|
+
* **"company_name"** : The company name to insert into the copyright and company fields in file comment blocks.
|
61
|
+
* **"subdirs"** : The list of sub-nodes under a node. Each must have a "path" specified. Wildcards are supported.
|
62
|
+
* **"ignore"** : An array of source-file paths to ignore at this place in the directory structure. Note that only files with the specific extension will be ignored. Note in the example that both the ".h" and the ".m" files must be specified to prevent the generation of code for classes defined in those files. "**" wildcard patterns are allowed.
|
63
|
+
* **"emit\_original\_code"** : Include original Objective-C method bodies and class variable declarations in the generated file as commented out bodies of the swift entities. Default: true.
|
64
|
+
* **"emit\_original\_signatures"** : Include original Objective-C method signatures (commented out) in the generated file. Ignored if emit\_converted\_code is false. Default: true.
|
65
|
+
|
66
|
+
Note that other options (including JSON serialization, generation of unit tests) are available through the Ruby API, but are not supported or documented at this time.
|
67
|
+
|
68
|
+
##### Wildcard Support
|
69
|
+
Only the **\*\*/...** wildcard pattern is currently supported. Full "file globbing" may be supported in the future.
|
70
|
+
|
71
|
+
## Development
|
72
|
+
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
74
|
+
|
75
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it ( https://github.com/[my-github-username]/objc2swift_assistant/fork )
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "assistant"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'commander/import'
|
4
|
+
require 'assistant'
|
5
|
+
require 'objc2swift_assistant/logging'
|
6
|
+
|
7
|
+
# assistant = Objc2swiftAssistant::Assistant.new
|
8
|
+
# assistant.runFromCommandLine
|
9
|
+
|
10
|
+
program :name, 'obj2swift'
|
11
|
+
program :version, '1.0.0'
|
12
|
+
program :description, 'Generates Swift class Skeletons from Objective-C files.'
|
13
|
+
|
14
|
+
# command :update do |c|
|
15
|
+
# c.syntax = 'obj2swift update [classnames]'
|
16
|
+
# c.description = 'Update Swift class skeletons from Objective-C'
|
17
|
+
# c.option '--source STRING', String, 'The root of the existing Objective-C source'
|
18
|
+
# c.option '--dest STRING', String, 'The root of the existing generated Swift source'
|
19
|
+
# c.action do |args, options|
|
20
|
+
# options.default :source => nil, :dest => nil
|
21
|
+
# # say "#{options.source}bar#{options.dest}"
|
22
|
+
# a = Objc2swiftAssistant::Assistant.new( options.source, options.dest ? options.dest : "" )
|
23
|
+
# a.update()
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
|
27
|
+
command :generate do |c|
|
28
|
+
c.syntax = 'obj2swift generate --source SOURCE_PATH --dest GENERATED_CODE_PATH --config CONFIG_FILE'
|
29
|
+
c.description = 'Generate Swift files and skeletons from Objective-C'
|
30
|
+
c.option '--source STRING', String, 'The root of the existing Objective-C source'
|
31
|
+
c.option '--dest STRING', String, 'The root of the generated Swift source'
|
32
|
+
c.option '--config STRING', String, 'Optional configuration file'
|
33
|
+
# c.option '--nocode STRING', String, 'Just concatenate the .h and .m commented source into one .sw file.'
|
34
|
+
c.option '--structure STRING', String, 'Dump the source file structure w/output location and errors encountered [all, errors, none]'
|
35
|
+
c.option '--log STRING', String, 'Log level: [error, warning, verbose, debug, none].'
|
36
|
+
c.action do |args, options|
|
37
|
+
# options.default :source => nil, :dest => nil, :nocode => false, :config => nil, :log => "warning", :structure => "errors"
|
38
|
+
options.default :source => nil, :dest => nil, :config => nil, :log => "warning", :structure => "all"
|
39
|
+
|
40
|
+
a = Objc2swiftAssistant::Assistant.new( options.source, options.dest, options.config, options.log, options.structure )
|
41
|
+
a.read_config_file
|
42
|
+
if a.configuration.log_level.nil?
|
43
|
+
puts( "#{options.log} is no a valid log level. The options are: error, warning, verbose, debug, none")
|
44
|
+
else
|
45
|
+
a.generate( false ) # TODO: nocde or remove the option
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
data/bin/setup
ADDED
data/lib/assistant.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require "objc2swift_assistant/version"
|
2
|
+
|
3
|
+
require 'commander'
|
4
|
+
require_relative 'objc2swift_assistant/file_sets'
|
5
|
+
require 'objc2swift_assistant/recognizers/implementation_recognizer'
|
6
|
+
require 'objc2swift_assistant/recognizers/property_declaration_recognizer'
|
7
|
+
require 'objc2swift_assistant/recognizers/interface_recognizer'
|
8
|
+
require 'objc2swift_assistant/recognizers/category_recognizer'
|
9
|
+
require 'objc2swift_assistant/recognizers/method_recognizer'
|
10
|
+
require 'objc2swift_assistant/recognizers/enum_recognizer'
|
11
|
+
require 'objc2swift_assistant/recognizers/protocol_recognizer'
|
12
|
+
require 'objc2swift_assistant/recognizers/pragma_mark_recognizer'
|
13
|
+
require 'objc2swift_assistant/recognizers/at_sign_directives_recognizer'
|
14
|
+
require 'objc2swift_assistant/objc_2_swift_configuration'
|
15
|
+
require 'objc2swift_assistant/logging'
|
16
|
+
|
17
|
+
module Objc2swiftAssistant
|
18
|
+
|
19
|
+
class Assistant
|
20
|
+
attr_accessor :source_root
|
21
|
+
attr_accessor :dest_root
|
22
|
+
attr_accessor :recognizers
|
23
|
+
attr_accessor :config_file_path
|
24
|
+
attr_accessor :config
|
25
|
+
attr_accessor :log_level_name
|
26
|
+
attr_accessor :configuration
|
27
|
+
attr_accessor :structure_option
|
28
|
+
|
29
|
+
def initialize( source, dest, config_file, log_level_name, structure_option)
|
30
|
+
@source_root = source
|
31
|
+
@dest_root = dest
|
32
|
+
@config_file_path = config_file.nil? ? nil : Pathname.new( config_file )
|
33
|
+
@log_level_name = log_level_name
|
34
|
+
@structure_option = structure_option
|
35
|
+
@recognizers = [
|
36
|
+
CategoryDeclarationRecognizer.new(),
|
37
|
+
CategoryImplRecognizer.new(),
|
38
|
+
ProtocolRecognizer.new(),
|
39
|
+
#EnumRecognizer.new(),
|
40
|
+
ImplementationRecognizer.new(),
|
41
|
+
InterfaceRecognizer.new(),
|
42
|
+
MethodRecognizer.new(),
|
43
|
+
PropertyDeclarationRecognizer.new(),
|
44
|
+
AtDirectiveRecognizer.new()
|
45
|
+
#PragmaMarkRecognizer.new()
|
46
|
+
]
|
47
|
+
end
|
48
|
+
|
49
|
+
def update
|
50
|
+
source_file_set = ObjCFileSet.new( @source_root, @config_hash )
|
51
|
+
source_file_set.scan
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate( dry_run )
|
56
|
+
unless @config_file_path.nil?
|
57
|
+
log_stage_start( 'Reading Configuration File' )
|
58
|
+
end
|
59
|
+
|
60
|
+
@recognizers.each do | recognizer |
|
61
|
+
recognizer.configuration = @configuration
|
62
|
+
end
|
63
|
+
|
64
|
+
source_file_set = ObjCFileSet.new( @source_root, @recognizers, configuration:@configuration)
|
65
|
+
|
66
|
+
log_stage_start( 'Scanning Objective-C Source Files' )
|
67
|
+
source_file_set.scan
|
68
|
+
|
69
|
+
log_stage_start( 'Recognizing code fragmants' )
|
70
|
+
source_file_set.recognize_code_fragments
|
71
|
+
|
72
|
+
log_stage_start( 'Converting to Swift' )
|
73
|
+
source_file_set.prepare_conversion
|
74
|
+
|
75
|
+
source_file_set.generate_swift_file_set( @dest_root, dry_run )
|
76
|
+
|
77
|
+
source_file_set.dump( '', ' ', @configuration.dump_only_errors ) if @configuration.dump_source
|
78
|
+
|
79
|
+
# TODO
|
80
|
+
# source_file_set.generate_swift_files(dry_run)
|
81
|
+
#
|
82
|
+
# generator = SwiftFileGenerator( @dest_root, source_file_set )
|
83
|
+
# generator.map_sources_to_destinations
|
84
|
+
|
85
|
+
# if writeCode
|
86
|
+
#
|
87
|
+
# else
|
88
|
+
|
89
|
+
# generator.concat_source_files()
|
90
|
+
end
|
91
|
+
|
92
|
+
def read_config_file
|
93
|
+
if @config_file_path.nil?
|
94
|
+
@configuration = ObjC2SwiftConfiguration.new( {}, @log_level_name, @structure_option )
|
95
|
+
else
|
96
|
+
file_data = @config_file_path.read
|
97
|
+
config_hash = JSON.parse(file_data)
|
98
|
+
@configuration = ObjC2SwiftConfiguration.new( config_hash, @log_level_name, @structure_option )
|
99
|
+
end
|
100
|
+
|
101
|
+
@configuration.load_configuration
|
102
|
+
end
|
103
|
+
|
104
|
+
def log_stage_start( text )
|
105
|
+
return unless @verbose
|
106
|
+
@configuration.log_verbose( "\n---------------------------------------- #{text} ----------------------------------------\n\n" )
|
107
|
+
end
|
108
|
+
|
109
|
+
end # class
|
110
|
+
end # module
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require "objc2swift_assistant/version"
|
2
|
+
|
3
|
+
require "objc2swift_assistant/objc_2_swift"
|
4
|
+
|
5
|
+
module Objc2swiftAssistant
|
6
|
+
def de_comment_line( line )
|
7
|
+
new_line = line
|
8
|
+
m = line.match( /^(?<uncommented>.*)\/\/(?<comment>.*)/ )
|
9
|
+
unless m.nil?
|
10
|
+
new_line = m[ 'uncommented' ].strip
|
11
|
+
end
|
12
|
+
|
13
|
+
return new_line
|
14
|
+
end
|
15
|
+
module_function :de_comment_line
|
16
|
+
|
17
|
+
def de_comment_lines( lines )
|
18
|
+
# Line Comments
|
19
|
+
return lines.map do |line|
|
20
|
+
Objc2swiftAssistant::de_comment_line( line )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
module_function :de_comment_lines
|
24
|
+
|
25
|
+
def is_getter_method_name( property_name, method_name )
|
26
|
+
#TODO: add boolean rules
|
27
|
+
return property_name == method_name
|
28
|
+
end
|
29
|
+
module_function :is_getter_method_name
|
30
|
+
|
31
|
+
|
32
|
+
def is_setter_method_name( property_name, method_name )
|
33
|
+
setter_name = "set" + property_name[0, 1].upcase + property_name[1..-1]
|
34
|
+
return method_name == setter_name
|
35
|
+
end
|
36
|
+
module_function :is_setter_method_name
|
37
|
+
|
38
|
+
class CodeRecognizer
|
39
|
+
attr_accessor :for_root_entities
|
40
|
+
attr_accessor :start_match_regex
|
41
|
+
attr_accessor :match_region_class
|
42
|
+
attr_accessor :source_file_type
|
43
|
+
attr_accessor :match_region_class
|
44
|
+
attr_accessor :voiding_match
|
45
|
+
attr_accessor :configuration # set after initialization
|
46
|
+
|
47
|
+
def initialize( start_test_regex, match_region_class, source_file_type, for_root_entities, include_preceeding_comment=for_root_entities, voiding_regex: nil)
|
48
|
+
@start_line_match_regex = start_test_regex
|
49
|
+
@match_region_class = match_region_class
|
50
|
+
@source_file_type = source_file_type
|
51
|
+
@for_root_entities = for_root_entities
|
52
|
+
@voiding_regex = voiding_regex
|
53
|
+
end
|
54
|
+
|
55
|
+
def matches( raw_file_lines )
|
56
|
+
|
57
|
+
file_lines = Objc2swiftAssistant::de_comment_lines( raw_file_lines )
|
58
|
+
|
59
|
+
matched_regions = []
|
60
|
+
file_lines.each_with_index do |line, index|
|
61
|
+
if @start_line_match_regex =~ line
|
62
|
+
unless @voiding_regex.nil?
|
63
|
+
if @voiding_regex =~ line
|
64
|
+
break
|
65
|
+
end
|
66
|
+
end
|
67
|
+
#print( line )
|
68
|
+
matched_region = @match_region_class.new( index, @for_root_entities )
|
69
|
+
matched_region.detection_line = line
|
70
|
+
matched_region.configuration = @configuration
|
71
|
+
matched_regions << matched_region
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
@configuration.log_verbose( "" ) if matched_regions.length > 0
|
77
|
+
matched_regions.each do |region|
|
78
|
+
# if region.region_type == 'at_directive'
|
79
|
+
@configuration.log_verbose( "#{region.region_type} #{region.detection_line}" )
|
80
|
+
# end
|
81
|
+
end
|
82
|
+
|
83
|
+
return matched_regions
|
84
|
+
end
|
85
|
+
|
86
|
+
def should_scan_file( file_type )
|
87
|
+
return true
|
88
|
+
# if @source_file_type == :all_source_files
|
89
|
+
# return [:header_file, :implementation_file].include?( file_type )
|
90
|
+
# else
|
91
|
+
# file_type == @source_file_type
|
92
|
+
# end
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
class MigrationRegion < FailableProcessingElement
|
99
|
+
|
100
|
+
attr_accessor :is_root_entity
|
101
|
+
attr_accessor :allowed_parent_region_types
|
102
|
+
attr_accessor :region_type
|
103
|
+
attr_accessor :can_occur_in_class_decl
|
104
|
+
|
105
|
+
attr_accessor :detection_line # For diagnostics
|
106
|
+
|
107
|
+
attr_accessor :region_identifier
|
108
|
+
|
109
|
+
attr_accessor :is_single_line
|
110
|
+
attr_accessor :starting_line_number # actually a zero-based index
|
111
|
+
attr_accessor :ending_line_number # actually a zero-based index
|
112
|
+
attr_accessor :ending_of_root_header # i.e. (ideally) The end of the class ivar declaration
|
113
|
+
attr_accessor :root_header # The content of the ivar declaration
|
114
|
+
# attr_accessor :consumed_line_count
|
115
|
+
attr_accessor :sub_regions
|
116
|
+
attr_accessor :parent_region
|
117
|
+
attr_accessor :configuration
|
118
|
+
|
119
|
+
def initialize( starting_line_number, is_root_entity, region_type, can_occur_in_class_decl:false )
|
120
|
+
@starting_line_number = starting_line_number
|
121
|
+
@is_root_entity = is_root_entity
|
122
|
+
@region_type = region_type
|
123
|
+
@detection_line = detection_line
|
124
|
+
@can_occur_in_class_decl = can_occur_in_class_decl
|
125
|
+
@sub_regions = []
|
126
|
+
@error_messages = []
|
127
|
+
@is_single_line = false
|
128
|
+
end
|
129
|
+
|
130
|
+
def contains_line( line_number )
|
131
|
+
line_number >= @starting_line_number && line_number <= @ending_line_number
|
132
|
+
end
|
133
|
+
|
134
|
+
def add_sub_region( sub_region )
|
135
|
+
@sub_regions << sub_region
|
136
|
+
sub_region.parent_region = self
|
137
|
+
end
|
138
|
+
|
139
|
+
def complete( file_lines )
|
140
|
+
# Delimit sub-regions
|
141
|
+
previous_sub_region = nil
|
142
|
+
@sub_regions.each do |sub_region|
|
143
|
+
resolve_ending_line_number( previous_sub_region, sub_region.starting_line_number - 1) unless previous_sub_region.nil?
|
144
|
+
previous_sub_region = sub_region
|
145
|
+
end
|
146
|
+
resolve_ending_line_number( previous_sub_region, self.ending_line_number ) unless previous_sub_region.nil?
|
147
|
+
|
148
|
+
self.extract_information( file_lines[ @starting_line_number..@ending_line_number] )
|
149
|
+
@sub_regions.each do |sub_region|
|
150
|
+
sub_region.complete( file_lines )
|
151
|
+
end
|
152
|
+
|
153
|
+
#Find the limits of the unrecognized code in the root migration region
|
154
|
+
@ending_of_root_header = nil
|
155
|
+
@sub_regions.each.with_index(0) do |sub_region, i|
|
156
|
+
if ! sub_region.can_occur_in_class_decl
|
157
|
+
@ending_of_root_header = sub_region.starting_line_number - 1
|
158
|
+
break
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
@ending_of_root_header = @ending_line_number if @ending_of_root_header.nil?
|
163
|
+
@root_header = file_lines[ @starting_line_number..@ending_of_root_header ]
|
164
|
+
|
165
|
+
# can_occur_in_class_decl
|
166
|
+
end
|
167
|
+
|
168
|
+
def resolve_ending_line_number( region, suggested_line_number )
|
169
|
+
if region.is_single_line
|
170
|
+
region.ending_line_number = region.starting_line_number
|
171
|
+
else
|
172
|
+
region.ending_line_number = suggested_line_number
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def extract_information( file_lines )
|
177
|
+
# Do nothing by default
|
178
|
+
end
|
179
|
+
|
180
|
+
def generic_description( detail )
|
181
|
+
return detail
|
182
|
+
end
|
183
|
+
|
184
|
+
def brief_description()
|
185
|
+
return ""
|
186
|
+
end
|
187
|
+
|
188
|
+
def has_failed
|
189
|
+
! @error_messages.length > 0
|
190
|
+
end
|
191
|
+
|
192
|
+
# Output
|
193
|
+
def dump( indent, tab, errors_only )
|
194
|
+
dump_region_info( indent, tab, errors_only )
|
195
|
+
@sub_regions.each do |sub_region|
|
196
|
+
sub_region.dump( indent+tab, tab, errors_only )
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def dump_region_info( indent, tab, errors_only )
|
201
|
+
# puts( indent + "#{@region_type} [lines: #{@starting_line_number || '?'}-#{@ending_line_number || '?'}]")
|
202
|
+
printf( "%s%-25s [lines: %3d-%-3d] %s %s\n", indent, @region_type, @starting_line_number+1|| -1, @ending_line_number+1 || -1,
|
203
|
+
brief_description(), @error_message.nil? ? '' : 'ERROR: ' + @error_message )
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
class ClassRootRegion < MigrationRegion
|
209
|
+
attr_accessor :class_name
|
210
|
+
|
211
|
+
def initialize( starting_line_number, is_root_entity, region_type_name )
|
212
|
+
super( starting_line_number, is_root_entity, region_type_name )
|
213
|
+
end
|
214
|
+
|
215
|
+
def description()
|
216
|
+
generic_description( "Class: #{@class_name}" )
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|