Class: ProjectGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr-gen/xampl-cl-gen.rb

Constant Summary collapse

@@specialisation_file_content =
<<_EOF_
class ProjectGenerator

  def print_options
    # return an array containing any (or none) of:
    #    :schema    -- a schema-like xml representation of the generated code
    #    :graphml   -- a graphml file describing the class model (compatible with yEd)
    #    :yuml      -- a yuml file that represents a simplified class model (compatible with yUML)

    [ :yuml ]
  end

  def directory
    # return the path name to the generator's output directory
    File.join(%w{ . xampl-generated-code })
  end

  def resolve_namespaces
    # any array of arrays
    # each sub-array:
    #    0: a string or an array of strings, containing xml namespaces found
    #       in the example xml files an empty string is the default namespace
    #    1: a ruby Module name (get the character cases right)
    #    2: a namespace prefix used when writing xml, optional. A generated
    #       prefix will be used otherwise.

    [
{{MAPPING}}
    ]
  end
end
_EOF_

Instance Method Summary collapse

Constructor Details

#initialize(specialised = true) ⇒ ProjectGenerator

Returns a new instance of ProjectGenerator.



8
9
10
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 8

def initialize(specialised=true)
  @specialised = specialised
end

Instance Method Details

#directoryObject



12
13
14
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 12

def directory
  File.join(%w{ . xampl-generated-code })
end

#filenamesObject



16
17
18
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 16

def filenames
  Dir.glob("./xml/**/*.xml")
end

#generateObject



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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 115

def generate

  cl_options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: junk.rb [options]"

    opts.on("--download-yuml-png [FILENAME]", "Download the yuml png file if possible.") do | filename |
      cl_options[:download_yuml_png] = filename || true
    end
    opts.on("--download-yuml-pdf [FILENAME]", "Download the yuml pdf file if possible.") do | filename |
      cl_options[:download_yuml_pdf] = filename || true
    end
  end.parse!

#      Xampl.set_default_persister_kind(:simple)
  Xampl.set_default_persister_kind(:in_memory)
#      Xampl.set_default_persister_kind(:filesystem)
#      Xampl.set_default_persister_kind(:tokyo_cabinet)
#      Xampl.set_default_persister_format(:xml_format)

  dirname = self.directory
  
=begin

  if File.directory? dirname then
    begin
      FileUtils.rm_rf([ dirname ])
    rescue => e
      puts "could not clean up #{ dirname } -- #{ e }"
      return
    end
  elsif File.exists? dirname then
    puts "please move #{ dirname } out of the way of xampl-gen"
    return
  end

=end

  Xampl.transaction("project-generation") do

    options = Options.new do | opts |
      persisted_attributes.each do | pattr |
        opts.new_index_attribute(pattr).persisted = true
      end

      indexed_attributes.each do | iattr |
        opts.new_index_attribute(iattr)
      end

      resolve_namespaces.each do | namespace, ruby_module_name, output_ns_prefix |
        opts.resolve(namespace, ruby_module_name, output_ns_prefix)
      end

    end

    generator = Generator.new('generator')
    okay = generator.go(:options => options,
                        :filenames => filenames,
                        :directory => directory)

    if okay
      puts generator.print_elements("#{ directory }/#{ print_base_filename }", print_options)
      self.write_specialisation_file(generator.elements_map)

      generated_files = Dir.glob("#{ self.directory }/*.rb")
      if 0 < generated_files.size then
        all = []
        #abs = []
        generated_files.each do | filename |
          all << "require \"\#\{ File.dirname __FILE__ \}/#{ File.basename(filename, '.rb') }\""
          #all << "require '#{ File.dirname(filename)[2..-1] }/#{ File.basename(filename, '.rb') }'"
          #abs_filename = File.expand_path(filename)
          #abs << "require '#{ File.dirname(abs_filename) }/#{ File.basename(abs_filename, '.rb') }'"
        end

        out_filename = "#{ self.directory }/all.rb"
        File.open(out_filename, 'w') do | out |
          out.puts all.join("\n")
        end
        puts "WRITE TO FILE: #{ out_filename }"

        #out_filename = "#{ self.directory }/all-absolute.rb"
        #File.open(out_filename, 'w') do | out |
        #  out.puts abs.join("\n")
        #end
        #puts "WRITE TO FILE: #{ out_filename }"

        if File.exists?("#{ self.directory }/generated.yuml") && (cl_options[:download_yuml_png] || cl_options[:download_yuml_pdf]) then
          diagram = ""
          File.open("#{ self.directory }/generated.yuml") do | f |
            f.each do | line |
              diagram << line.chomp
            end
          end

          okay = false
          if cl_options[:download_yuml_png] then
            begin
              filename = (true == cl_options[:download_yuml_png]) ?  'generated.png' : cl_options[:download_yuml_png]
              wget = "wget 'http://yuml.me/diagram/scruffy/class/#{diagram}' -O '#{ self.directory }/#{filename}'"
              okay = system(wget)
              if okay then
                puts "downloaded yuml png"
              else
                puts "could not get the yuml png file -- #{ $? }"
              end
            rescue => e
              puts "could not get the yuml png file -- #{ e }"
            end
          end

          if cl_options[:download_yuml_pdf] then
            begin
              filename = (true == cl_options[:download_yuml_png]) ?  'generated.png' : cl_options[:download_yuml_png]
              wget = "wget 'http://yuml.me/diagram/scruffy/class/#{diagram}.pdf' -O '#{filename}'"
              okay = system(wget)
              puts "downloaded yuml pdf"
              if okay then
                puts "downloaded yuml pdf"
              else
                puts "could not get the yuml pdf file -- #{ $? }"
              end
            rescue => e
              puts "could not get the yuml pdf file -- #{ e }"
            end
          end
        end
      end
    end
    Xampl.rollback

  end
end

#indexed_attributesObject



43
44
45
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 43

def indexed_attributes
  %w{ id }
end

#persisted_attributesObject



39
40
41
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 39

def persisted_attributes
  %w{ pid }
end


35
36
37
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 35

def print
  nil
end


20
21
22
23
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 20

def print_base_filename
  #File.join(%w{ . generated })
  'generated'
end


25
26
27
28
29
30
31
32
33
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 25

def print_options
  # return an array containing any (or none) of:
  #    :schema    -- a schema-like xml representation of the generated code
  #    :graphml   -- a graphml file describing the class model (compatible with yEd)
  #    :yuml      -- a yuml file that represents a simplified class model (compatible with yUML)

  # [:schema, :graphml, :yuml]
  []
end

#resolve_namespacesObject



47
48
49
50
51
52
53
54
55
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 47

def resolve_namespaces
  # any array of arrays
  # each sub-array:
  #    0: a string or an array of strings, containing xml namespaces found in the example xml files
  #       an empty string is the default namespace
  #    1: a ruby Module name (get the cases right)
  #    2: a namespace prefix used when writing xml, optional. A generated prefix will be used otherwise.
  []
end

#write_specialisation_file(element_map) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xamplr-gen/xampl-cl-gen.rb', line 90

def write_specialisation_file(element_map)
  filename = './project-generator.rb'
  return if File.exists? filename

  mappings = []
  count = 0
  element_map.each do | ns, elements |
    module_name = elements.element.first.package || 'XamplAdHoc'
    count += 1
    mappings << [ ns, module_name, "ns#{ count }" ]
  end

  text =  @@specialisation_file_content.gsub(/{{MAPPING}}/) do
    insert = []
    mappings.each do | ns, module_name, prefix |
      insert << "            ['#{ ns }', '#{ module_name }', '#{ prefix }'],"
    end
    insert.join("\n")
  end

  File.open(filename, 'w') do | out |
    out.write text
  end
end