Class: Hokkaido::GemModifier

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_modifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info) ⇒ GemModifier

Returns a new instance of GemModifier.



38
39
40
41
# File 'lib/gem_modifier.rb', line 38

def initialize(info)
  @gem_name, @init_lib, @lib_folder = info
  @require_libs = [File.join(@lib_folder, @init_lib)]
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



36
37
38
# File 'lib/gem_modifier.rb', line 36

def gem_name
  @gem_name
end

#init_libObject (readonly)

Returns the value of attribute init_lib.



36
37
38
# File 'lib/gem_modifier.rb', line 36

def init_lib
  @init_lib
end

#lib_folderObject (readonly)

Returns the value of attribute lib_folder.



36
37
38
# File 'lib/gem_modifier.rb', line 36

def lib_folder
  @lib_folder
end

Instance Method Details

#modify!Object



43
44
45
46
# File 'lib/gem_modifier.rb', line 43

def modify!
  parse_gem(File.join(@lib_folder, @init_lib))
  write_manifest
end

#parse_gem(init_lib) ⇒ Object

def simulate! puts "simulator not implemented..." end



52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/gem_modifier.rb', line 52

def parse_gem(init_lib)
  # puts "Processing: #{init_lib}"
  # don't ask
  init_path = init_lib

  init_file = File.read(init_lib)
  current_file = ""

  init_file.each_line do |line|
    if line.strip =~ /^require/
      parser = RubyParser.new
      sexp = parser.parse(line)
      call = sexp[2]

      unless call == :require
        # WEIRD SHIT IS HAPPENING
        current_file += line
        next
      end

      require_type = sexp[3][1][0]
      library = sexp[3][1][1]
      #p library

      if require_type == :str && library.match(@gem_name)
        # fold in
        full_rb_path = File.join([@lib_folder, "#{library}.rb"])
        unless @require_libs.include?(full_rb_path)
          file_index = @require_libs.index(init_lib)
          insert_index = file_index
          @require_libs.insert insert_index, full_rb_path
          parse_gem(full_rb_path)
        end
      else
        current_file += "# FIXME: #require is not supported in RubyMotion\n"
        current_file += "# #{line}"
        next
      end
      # comment it out
      current_file += "# #{line}"
      next
    elsif line.strip =~ /^autoload/
      # same as require
      parser = RubyParser.new
      sexp = parser.parse(line)
      call = sexp[2]

      unless call == :autoload
        # WEIRD SHIT IS HAPPENING
        current_file += line
        next
      end

      require_type = sexp[3][2][0]
      library = sexp[3][2][1]

      full_rb_path = File.join([@lib_folder, "#{library}.rb"])
      unless @require_libs.include?(full_rb_path)
        file_index = @require_libs.index(init_lib)
        insert_index = file_index
        @require_libs.insert insert_index, full_rb_path
        parse_gem(full_rb_path)
      end
      # comment it out
      current_file += "# #{line}"
      next

    
    elsif line.strip =~ /^eval/
        # reprint as a block
        # parser = RubyParser.new
        # sexp = parser.parse(line)
        # code_str = sexp[3][1][1]
        # new_eval = "eval do\n  #{code_str}\nend\n"
        # current_file += "#{new_eval}"
        # next

        # produce a fixme

        current_file += "# FIXME: Cannot eval strings in RubyMotion. \n"
        current_file += "# Rewrite to use a block.. inbuilt hack will run \n"
        current_file += "# Change this: \n"
        current_file += "# eval %Q{ \n" 
        current_file += '# def #{c}(string = nil) ' + "\n"
        current_file += "# To this \n"
        current_file += "# eval do \n"
        current_file += "# define_method(c) do |string| \n"
        current_file += "#{line}"
        next
    elsif line.strip =~ /^binding/
        current_file += "# FIXME: binding is not supported in RubyMotion.\n"
        current_file += "#{line}"
        next
    end

    # dont intefere
    current_file += line
  end

  # replace file
  File.open(init_lib, 'w') {|f| f.write(current_file) } #unless TEST_MODE

end

#write_manifestObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gem_modifier.rb', line 156

def write_manifest

  #puts @require_libs

  @manifest_files = @require_libs.collect do |lib|

    lib = lib.gsub("#{@lib_folder}/", "")

    INCLUDE_STRING.gsub("RELATIVE_LIBRARY_PATH", lib)

  end

  # creates config manifest
  @manifest = RUBYMOTION_GEM_CONFIG.gsub("MAIN_CONFIG_FILES", @manifest_files.join("\n"))

  File.prepend(File.join(@lib_folder, @init_lib), @manifest)

  File.prepend(File.join(@lib_folder, @init_lib), EVAL_HACK)

end