Top Level Namespace

Defined Under Namespace

Classes: Property

Instance Method Summary collapse

Instance Method Details

#make_models_public(source_file, destination_file, reserved_keywords = []) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/swift_republic.rb', line 66

def make_models_public(source_file, destination_file, reserved_keywords = [])
	# source_file = "/A/B/C/D.swift"
	# destination_file = "/W/X/Y/Z.swift"

    lines = []
    bracket_stack = Array.new # keeps track of brackets... they must be balanced.
    struct_stack = Array.new # keeps track of latest struct containing current line.
    property_stack = Array.new
    protocol_stack = Array.new
    can_update_property_scope = false # helps in skipping updates to local variables.
    can_update_function_scope = true # helps in skipping updates to func inside protocols.
    # reserved_keywords = ["reserved_keywords or file names"] # skip ENTIRE FILES containing these keywords.

	file_name = File.basename(source_file)
	p "Parsing #{file_name} to output --> #{destination_file}"

	dirname = File.dirname(destination_file)
	unless File.directory?(dirname)
	  FileUtils.mkdir_p(dirname)
	end

	t_file = File.new(destination_file, "w")

	File.open(source_file) { |file|  
		lines = file.readlines
	}

	lines.each_with_index { |line, source_line_number|
		indent_offset = line =~ /\S/

		if reserved_keywords.any? { |keyword| line.include? keyword }
			t_file.close
			p "Could NOT parse #{file_name}. Dependencies/restricted keywords detected in line number #{source_line_number}: #{line}"
			File.delete(destination_file)
			break

		elsif (line.include? "private") || (line.include? "fileprivate") || (line.include? "public")
			if (line.include? "\{")
				if not (line.include? "\}")
					bracket_stack.push "other"
				end
			end				
			t_file.puts "#{line}"
			# Script is intended to handle only "internal" for now.

		elsif line.strip.start_with?("\/\/")
			# commented out code
			t_file.puts "#{line}"
			# pending check for /* XXX */

		elsif (line.include? "struct") && (line.include? "\{")
			line["struct"] = "public struct"		
			t_file.puts "#{line}"
			bracket_stack.push "#{line}"
			struct_stack.push "#{line}"

		elsif (line.include? "enum") && (line.include? "\{")
			line["enum"] = "public enum"
			t_file.puts "#{line}"
			bracket_stack.push line	 # doesn't need initilizer.

		elsif (line.include? "protocol ") && (line.include? "\{")
			line["protocol"] = "public protocol"
			t_file.puts "#{line}"
			bracket_stack.push line
			protocol_stack.push line

		elsif line.include? " func "
			if can_update_function_scope # if NOT in protocol:
				line["func"] = "public func"
			end
			if line.include? "\{"
				bracket_stack.push line
			end
			t_file.puts "#{line}"

		elsif can_update_property_scope && (line.strip.start_with?("let"))
			property_details = Property.new(struct_stack.last, line)
			property_stack.push property_details
			line.insert indent_offset, "public "
			t_file.puts "#{line}"

		elsif can_update_property_scope && (line.strip.start_with?("var"))
			property_details = Property.new(struct_stack.last, line)
			if line.strip.end_with?("\{")
				# computed property:
				bracket_stack.push line					
			else				
				property_stack.push property_details
			end
			line.insert indent_offset, "public "
			t_file.puts "#{line}"

		elsif can_update_property_scope && (line.strip.start_with?("static"))
			line.insert indent_offset, "public "
			t_file.puts "#{line}"
			if (line.include? "\{")
				if not (line.include? "\}")
					bracket_stack.push "other"
				end
			end

		elsif line.strip.start_with?("init(")
			line["init("] = "public init("
			if (line.include? "\{")
				if not (line.include? "\}")
					bracket_stack.push "other"
				end
			end
			t_file.puts "#{line}"

		elsif (line.include? "\{") # Eg. guard statements, etc.
			if not (line.include? "\}")
				bracket_stack.push "other"
			end
			t_file.puts "#{line}"

		elsif (line.strip.length == 1) && (line.include? "\}")
		# If end of struct, add its initializer.
			top_object = bracket_stack.pop
			if can_update_property_scope && (top_object.include? "struct")
				latest_struct = struct_stack.pop
				properties_array = Array.new

				while (property_stack.size > 0) && (property_stack.last.associated_struct == latest_struct)
					current_property = property_stack.pop
					properties_array.push current_property
				end

				if properties_array.size > 0
					# for structs which have both req and response inner structs
					# i.e. skip structs with no properties.
					struct_initializer = create_initializer(properties_array.reverse!)
					t_file.puts struct_initializer
				end

				t_file.puts "#{line}"
				# binding.pry
			elsif top_object.include? "protocol"
				protocol_stack.pop
				t_file.puts "#{line}"				
			else
				t_file.puts "#{line}"
			end

		elsif (line.include? "\}")
			bracket_stack.pop
			t_file.puts "#{line}   <<--Please fix formatting there, and run again."

		else
			t_file.puts "#{line}"
		end

		can_update_property_scope = (struct_stack.size > 0) && ((bracket_stack.last.include? "struct") || (bracket_stack.last.include? "enum")) # skip func and protocol
		can_update_function_scope = (protocol_stack.size == 0) # func in protocol are not to be made public.
	}

	t_file.close
end

#make_models_public_test(source_file, destination_file) ⇒ Object



227
228
229
230
# File 'lib/swift_republic.rb', line 227

def make_models_public_test(source_file, destination_file)
	p source_file
	p destination_file
end