Module: Buildr::Generate

Defined in:
lib/buildr/core/generate.rb

Overview

:nodoc:

Constant Summary collapse

HEADER =
"# Generated by Buildr #{Buildr::VERSION}, change to your liking\n\n"

Class Method Summary collapse

Class Method Details

.compatibility_option(path) ⇒ Object



41
42
43
# File 'lib/buildr/core/generate.rb', line 41

def compatibility_option(path)
  # compile.options.target = '1.5'
end

.from_directory(path = Dir.pwd, root = true) ⇒ Object



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
# File 'lib/buildr/core/generate.rb', line 203

def from_directory(path = Dir.pwd, root = true)
  Dir.chdir(path) do
    name = File.basename(path)
    if root
      script = HEADER.split("\n")
      header = getEclipseBuildfileHeader(path, name)
      script += header.split("\n")
    else
      script = [ %{define "#{name}" do} ]
    end
    script <<  "  compile.with # Add classpath dependencies" if File.exist?("src/main/java")
    script <<  "  resources" if File.exist?("src/main/resources")
    script <<  "  test.compile.with # Add classpath dependencies" if File.exist?("src/test/java")
    script <<  "  test.resources" if File.exist?("src/test/resources")
    if File.exist?("src/main/webapp")
      script <<  "  package(:war)"
    elsif File.exist?("src/main/java")
      script <<  "  package(:jar)"
    end
    dirs = FileList["*"].exclude("src", "target", "report").
      select { |file| File.directory?(file) && File.exist?(File.join(file, "src")) }
    unless dirs.empty?
      script << ""
      dirs.sort.each do |dir|
        script << from_directory(dir, false).flatten.map { |line| "  " + line } << ""
      end
    end
    script << "end"
    script.flatten
  end
end

.from_eclipse(path = Dir.pwd, root = true) ⇒ Object



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
# File 'lib/buildr/core/generate.rb', line 123

def from_eclipse(path = Dir.pwd, root = true)
  # We need two passes to be able to determine the dependencies correctly
  Dir.chdir(path) do
    name = File.basename(path)
    dot_projects = []
    mf = nil # avoid reloading manifest
    if root
      @@allProjects = Hash.new
      @@topDir = File.expand_path(Dir.pwd)
      script = HEADER.split("\n")
      script << "require 'buildr/ide/eclipse'"
      header = getEclipseBuildfileHeader(path, name)
      script += header.split("\n")
      script << "  # you may see hints about which jars are missing and should resolve them correctly"
      script << "  # dependencies  << 'junit should be commented out and replace by correct ARTIFACT definition. Eg"
      script << "  # dependencies  << 'junit:junit:jar:4.11'"
      script << setLayout('src', 'bin') # default values for eclipse
      dot_projects = Dir.glob('**/.project', File::FNM_DOTMATCH).find_all { |dot_project| get_project_natures(dot_project) }
      dot_projects.sort.each { |dot_project| from_eclipse(File.dirname(dot_project), false) } if dot_projects
    else
      # Skip fragments. Buildr cannot handle it without the help of buildr4osgi
      return [""] if File.exists?('fragment.xml')
      projectName = name
      version = ""
      mfName = File.join('META-INF', 'MANIFEST.MF')
      if File.exists?(mfName)
        mf = Packaging::Java::Manifest.parse(IO.readlines(mfName).join(''))
        if mf.main['Bundle-SymbolicName']
          projectName = mf.main['Bundle-SymbolicName'].split(';')[0]
          bundleVersion = mf.main['Bundle-Version']
          version = ", :version => \"#{bundleVersion}\"" unless "1.0.0".eql?(bundleVersion)
        end
      end
      # in the first run we just want to know that we exist
      unless @@allProjects[projectName]
        @@allProjects[projectName] = Dir.pwd
        return
      end
      base_dir = ""
      unless File.join(@@topDir, projectName).eql?(File.expand_path(Dir.pwd))
        base_dir = ", :base_dir => \"#{File.expand_path(Dir.pwd).sub(@@topDir+File::SEPARATOR, '')}\""
      end
      script = [%{define "#{projectName}"#{version}#{base_dir} do}]
    end
    natures = get_project_natures('.project')
    if natures && natures.index('org.eclipse.pde.PluginNature')
      script << "  package(:jar)"
    end
    script << "  dependencies ||= []"
    if mf && mf.main['Require-Bundle']
      mf.main['Require-Bundle'].split(',').each do
        |bundle|
        requiredName = bundle.split(';')[0]
        if @@allProjects.has_key?(requiredName)
          script << "  dependencies << projects(\"#{requiredName}\")"
        else
          script << "  # dependencies  << '#{requiredName}'"
        end
      end
    end
    script << "  compile.with dependencies # Add more classpath dependencies" if Dir.glob(File.join('src', '**', '*.java')).size > 0
    script << "  resources" if File.exist?("rsc")
    sourceProp = get_build_property('.', 'source..')
    outputProp = get_build_property('.', 'output..')
    if (sourceProp && !/src\/+/.match(sourceProp)) or (outputProp && !/bin\/+/.match(outputProp))
      setLayout(sourceProp, outputProp) # default values are overridden in this project
    end
    unless dot_projects.empty?
      script << ""
      dot_projects.sort.each do |dot_project|
        next if File.dirname(File.expand_path(dot_project)).eql?(File.expand_path(Dir.pwd))
        next unless get_project_natures(dot_project)
        script << from_eclipse(File.dirname(dot_project), false).flatten.map { |line| "  " + line } << ""
      end
    end
    script << "end\n\n"
    script.flatten
  end
end

.from_maven2_pom(path = 'pom.xml', root = true) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/buildr/core/generate.rb', line 235

def from_maven2_pom(path = 'pom.xml', root = true)
  pom = Buildr::POM.load(path)
  project = pom.project

  artifactId = project['artifactId'].first
  description = project['name'] || "The #{artifactId} project"
  project_name = File.basename(Dir.pwd)

  if root
    script = HEADER.split("\n")

    settings_file = ENV["M2_SETTINGS"] || File.join(ENV['HOME'], ".m2/settings.xml")
    settings = XmlSimple.xml_in(IO.read(settings_file)) if File.exists?(settings_file)

    if settings
      proxy = settings['proxies'].first['proxy'].find { |proxy|
        proxy["active"].nil? || proxy["active"].to_s =~ /true/
      } rescue nil

      if proxy
        url = %{#{proxy["protocol"].first}://#{proxy["host"].first}:#{proxy["port"].first}}
        exclude = proxy["nonProxyHosts"].to_s.gsub("|", ",") if proxy["nonProxyHosts"]
        script << "options.proxy.http = '#{url}'"
        script << "options.proxy.exclude << '#{exclude}'" if exclude
        script << ''
        # In addition, we need to use said proxies to download artifacts.
        Buildr.options.proxy.http = url
        Buildr.options.proxy.exclude << exclude if exclude
      end
    end

    repositories = project["repositories"].first["repository"].select { |repository|
      legacy = repository["layout"].to_s =~ /legacy/
      !legacy
    } rescue nil
    repositories = [{"name" => "Standard maven2 repository", "url" => "http://repo1.maven.org/maven2"}] if repositories.nil? || repositories.empty?
    repositories.each do |repository|
      name, url = repository["name"], repository["url"]
      script << "# #{name}"
      script << "repositories.remote << '#{url}'"
      # In addition we need to use said repositores to download artifacts.
      Buildr.repositories.remote << url.to_s
    end
    script << ""
  else
    script = []
  end

  script << "desc '#{description}'"
  script << "define '#{project_name}' do"

  groupId = project['groupId']
  script << "  project.group = '#{groupId}'" if groupId

  version = project['version']
  script << "  project.version = '#{version}'" if version

  #get plugins configurations
  plugins = project['build'].first['plugins'].first['plugin'] rescue {}
  if plugins
    compile_plugin = plugins.find{|pl| (pl['groupId'].nil? or pl['groupId'].first == 'org.apache.maven.plugins') and pl['artifactId'].first == 'maven-compiler-plugin'}
    if compile_plugin
      source = compile_plugin.first['configuration'].first['source'] rescue nil
      target = compile_plugin.first['configuration'].first['target'] rescue nil

      script << "  compile.options.source = '#{source}'" if source
      script << "  compile.options.target = '#{target}'" if target
    end
  end

  compile_dependencies = pom.dependencies
  dependencies = compile_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
  script <<  "  compile.with #{dependencies}" unless dependencies.empty?

  test_dependencies = (pom.dependencies(['test']) - compile_dependencies).reject{|d| d =~ /^junit:junit:jar:/ }
  #check if we have testng
  use_testng = test_dependencies.find{|d| d =~ /^org.testng:testng:jar:/}
  if use_testng
    script <<  "  test.using :testng"
    test_dependencies = pom.dependencies(['test']).reject{|d| d =~ /^org.testng:testng:jar:/ }
  end

  test_dependencies = test_dependencies.sort.map{|d| "'#{d}'"}.join(', ')
  script <<  "  test.with #{test_dependencies}" unless test_dependencies.empty?

  packaging = project['packaging'] ? project['packaging'].first : 'jar'
  if %w(jar war).include?(packaging)
    script <<  "  package :#{packaging}, :id => '#{artifactId}'"
  end

  modules = project['modules'].first['module'] rescue nil
  if modules
    script << ""
    modules.each do |mod|
      script << from_maven2_pom(File.join(File.dirname(path), mod, 'pom.xml'), false).flatten.map { |line| "  " + line } << ""
    end
  end
  script << "end"
  script.flatten
end

.get_build_property(path, propertyName) ⇒ Object



56
57
58
59
60
61
# File 'lib/buildr/core/generate.rb', line 56

def get_build_property(path, propertyName)
  propertiesFile = File.join(path, 'build.properties')
  return nil unless File.exists?(propertiesFile)
  inhalt = Hash.from_java_properties(File.read(propertiesFile))
  binDef = inhalt[propertyName]
end

.get_project_natures(projectFile) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/buildr/core/generate.rb', line 45

def get_project_natures(projectFile)
  return nil unless File.exists?(projectFile)
  File.open(projectFile) do |f|
    root = REXML::Document.new(f).root
    return nil if root == nil
    natures = root.elements.collect("natures/nature") { |n| n.text }
    return natures if natures
  end
  return nil
end

.getEclipseBuildfileHeader(path, name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/buildr/core/generate.rb', line 73

def getEclipseBuildfileHeader(path, name)
  x = "\#{\"require 'buildr/scala'\\n\" if Dir.glob(path + \"/**/*.scala\").size > 0}\n\#{\"require 'buildr/groovy'\\n\" if Dir.glob(path + \"/**/*.groovy\").size > 0}\n# Version number for this release\nVERSION_NUMBER = \"1.0.0\"\n# Group identifier for your projects\nGROUP = \"\#{name}\"\nCOPYRIGHT = \"\"\n\n# Specify Maven 2.0 remote repositories here, like this:\nrepositories.remote << \"http://repo1.maven.org/maven2\"\n\ndesc \"The \#{name.capitalize} project\"\ndefine \"\#{name}\" do\n\n  project.version = VERSION_NUMBER\n  project.group = GROUP\n  manifest[\"Implementation-Vendor\"] = COPYRIGHT\n      EOF\n  return x\nend\n"

.has_eclipse_project?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/buildr/core/generate.rb', line 63

def has_eclipse_project?
  candidates = Dir.glob("**/.project", File::FNM_DOTMATCH)
  return false if candidates.size == 0
  candidates.find { |x| get_project_natures(x) }
  return candidates.size > 0
end

.setLayout(source = nil, output = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/buildr/core/generate.rb', line 96

def setLayout(source=nil, output = nil)
  script = ""
  if source
    source = source.sub(/\/$/, '') # remove trailing /
    script += "  layout[:source, :main, :java] = \"\#{source}\"\n  layout[:source, :main, :scala] = \"\#{source}\"\n"
  end
  if output
    output = output.sub(/\/$/, '') # remove trailing /
    script += "  layout[:target, :main] = \"\#{output}\"\n  layout[:target, :main, :java] = \"\#{output}\"\n  layout[:target, :main, :scala] = \"\#{output}\"\n"
  end
  return script
end