Permalink
Browse files

refactored ruby-maven and split into three files

  • Loading branch information...
1 parent 68635f0 commit 838591b39b9822df47118932de70a7612174b655 @mkristian committed Aug 12, 2012
View
8 bin/rmvn
@@ -1,9 +1,11 @@
#!/usr/bin/env ruby
-require 'ruby_maven'
+require 'maven/ruby/cli'
+require 'maven/ruby/pom_magic'
-mvn = Maven::RubyMaven.new
+mvn = Maven::Ruby::Cli.new
+magic = Maven::Ruby::PomMagic.new
args = ARGV.dup
ARGV.clear # clean up in case another script gets executed it gets clear ARGV
-mvn.exec(*args)
+mvn.exec(magic.generate_pom(*args))
View
161 lib/ruby/maven/cucumber_steps.rb
@@ -1,161 +0,0 @@
-require 'fileutils'
-require 'ruby-maven'
-module Maven
- class CucumberSteps
-
- def initialize(options = {})
- @options = {:ruby_version => RUBY_VERSION }
- @options[:jruby_version] = JRUBY_VERSION if defined? JRUBY_VERSION
-
- @options.merge!(options || {})
- end
-
- def rmvn
- @rmvn ||= Maven::RubyMaven.new
- end
-
- def copy_tests(tests)
- FileUtils.mkdir_p(@app_directory)
- FileUtils.cp_r(File.join('templates', "tests-#{tests}", "."),
- File.join(@app_directory, 'test'),
- :remove_destination => true)
- end
-
- def copy_specs(specs)
- FileUtils.mkdir_p(@app_directory)
- FileUtils.cp_r(File.join('templates', "specs-#{specs}", "."),
- File.join(@app_directory, 'spec'),
- :remove_destination => true)
- end
-
- def copy_files(files)
- FileUtils.mkdir_p(@app_directory)
- FileUtils.cp_r(File.join('templates', "files-#{files}", "."),
- @app_directory,
- :remove_destination => true)
- end
-
- def create_rails_application(template)
- name = template.sub(/.template$/, '')
- @app_directory = File.join('target', name)
-
- # rails version from gemspec
- gemspec = File.read(Dir.glob("*.gemspec")[0])
- rails_version = gemspec.split("\n").detect { |l| l =~ /development_dep.*rails/ }.sub(/'$/, '').sub(/.*'/, '')
-
- rmvn.options['-Dplugin.version'] = @options[:plugin_version] if @options[:plugin_version]
- rmvn.options['-Djruby.version'] = @options[:jruby_version] if @options[:jruby_version]
- if @options[:ruby_version]
- rversion = @options[:ruby_version] =~ /^1.8./ ? '--1.8': '--1.9'
- rmvn.options['-Djruby.switches'] = rversion
- end
-
- rmvn.options['-Drails.version'] = rails_version
- rmvn.options['-Dgem.home'] = ENV['GEM_HOME']
- rmvn.options['-Dgem.path'] = ENV['GEM_PATH']
- rmvn.options['-o'] = nil
-
- FileUtils.rm_rf(@app_directory)
-
- template_file = File.expand_path("templates/#{template}")
- rmvn.exec("rails", "new", @app_directory, "-f", '--', '-e', "-Dtemplate=#{template_file}")
- end
-
- def given_template(template)
- create_rails_application(template)
- end
-
- def given_template_and_tests(template, tests)
- create_rails_application(template)
- copy_tests(tests)
- end
-
- def given_template_and_specs(template, specs)
- create_rails_application(template)
- copy_specs(specs)
- end
-
- def given_template_and_files(template, files)
- create_rails_application(template)
- copy_files(files)
- end
-
- def given_application(name)
- @app_directory = File.join('target', name)
- end
-
- def given_application_and_tests(name, tests)
- @app_directory = File.join('target', name)
- copy_tests(tests)
- end
-
- def given_application_and_specs(name, specs)
- @app_directory = File.join('target', name)
- copy_specs(specs)
- end
-
- def given_application_and_files(name, files)
- @app_directory = File.join('target', name)
- copy_files(files)
- end
-
- def execute(args)
- rmvn.options['-l'] = "output.log"
- rmvn.exec_in(@app_directory, args.split(' '))
- end
-
- def expected_output(expected)
- result = File.read(File.join(@app_directory, "output.log"))
- expected.split(/\"?\s+and\s+\"?/).each do |exp|
- puts exp
- yield(result =~ /.*#{exp}.*/)
- end
- end
- end
-end
-
-
-steps = Maven::CucumberSteps.new(:plugin_version => '0.28.5-SNAPSHOT')
-
-Given /^I create new rails application with template "(.*)"$/ do |template|
- steps.given_template(template)
-end
-
-Given /^I create new rails application with template "(.*)" and "(.*)" tests$/ do |template, tests|
- steps.given_template_and_tests(template, tests)
-end
-
-Given /^I create new rails application with template "(.*)" and "(.*)" specs$/ do |template, specs|
- steps.given_template_and_specs(template, specs)
-end
-
-Given /^I create new rails application with template "(.*)" and "(.*)" files$/ do |template, files|
- steps.given_template_and_files(template, files)
-end
-
-Given /^me an existing rails application "(.*)"$/ do |name|
- steps.given_application(name)
-end
-
-Given /^me an existing rails application "(.*)" and "(.*)" tests$/ do |name, tests|
- steps.given_application_and_tests(name, tests)
-end
-
-Given /^me an existing rails application "(.*)" and "(.*)" specs$/ do |name, specs|
- steps.given_application_and_specs(name, specs)
-end
-
-Given /^me an existing rails application "(.*)" and "(.*)" files$/ do |name, files|
- steps.given_application_and_files(name, files)
-end
-
-And /^I execute \"(.*)\"$/ do |args|
- steps.execute(args)
-end
-
-Then /^the output should contain \"(.*)\"$/ do |expected|
- steps.expected_output(expected) do |exp|
- exp.should_not be_nil
- end
-end
-
View
1 lib/ruby/maven/ruby.rb
@@ -0,0 +1 @@
+require 'maven/ruby/maven'
View
124 lib/ruby/maven/ruby/cli.rb
@@ -0,0 +1,124 @@
+module Maven
+ module Ruby
+ class Cli
+
+ private
+
+ # make the command line for the goals of the jruby-maven-plugins nicer
+ PLUGINS = {
+ :rake => [:rake],
+ :jruby => [:jruby, :compile],
+ :gem => [:package, :install, :push, :exec, :pom, :initialize, :irb],
+ :rails3 => [:new, :generate, :rake, :server, :console, :dbconsole, :pom, :initialize],
+ :cucumber => [:test],
+ :rspec => [:test],
+ :runit => [:test],
+ :mini => [:test,:spec],
+ :bundler => [:install, :update]
+ }
+ ALIASES = {
+ :ruby => :jruby,
+ :spec => :rspec,
+ :rails => :rails3,
+ :bundle => :bundler
+ }
+
+ def prepare(args)
+ if args.size > 0
+ name = args[0].to_sym
+ name = ALIASES[name] || name
+ if PLUGINS.member?(name)
+ start = 1
+ if args.size > 1
+ if PLUGINS[name].member? args[1].to_sym
+ goal = args[1].to_sym
+ start = 2
+ else
+ goal = PLUGINS[name][0]
+ end
+ else
+ goal = PLUGINS[name][0]
+ end
+ # determine the version and delete from args if given
+ version = args.detect do |a|
+ a =~ /^-Dplugin.version=/
+ end
+ version ||= options['-Dplugin.version']
+
+ if version
+ args.delete(version)
+ version = ":" + version.sub(/^-Dplugin.version=/, '')
+ end
+ aa = if index = args.index("--")
+ args[(index + 1)..-1]
+ else
+ []
+ end
+ ruby_args = (args[start, (index || 1000) - start] || []).join(' ')
+
+ aa << "de.saumya.mojo:#{name}-maven-plugin#{version}:#{goal}"
+ aa << "-Dargs=\"#{ruby_args}\"" if ruby_args.size > 0
+ args.replace(aa)
+ else
+ args.delete("--")
+ end
+ end
+ args
+ end
+
+ def log(args)
+ log = File.join('log', 'rmvn.log')
+ if File.exists? File.dirname(log)
+ File.open(log, 'a') do |f|
+ f.puts "#{$0.sub(/.*\//, '')} #{args.join ' '}"
+ end
+ end
+ end
+
+ def maybe_print_help(args)
+ if args.size == 0 || args[0] == "--help"
+ puts "usage: rmvn [<plugin-name> [<args>] [-- <maven-options>]] | [<maven-goal>|<maven-phase> <maven-options>] | --help"
+ PLUGINS.each do |name, goals|
+ puts
+ print "plugin #{name}"
+ print " - alias: #{ALIASES.invert[name]}" if ALIASES.invert[name]
+ puts
+ if goals.size > 1
+ print "\tgoals : #{goals.join(',')}"
+ puts
+ end
+ print "\tdefault goal: #{goals[0]}"
+ puts
+ end
+ puts
+ ["--help"]
+ else
+ args
+ end
+ end
+
+ def command_line(args)
+ args = prepare(args)
+ args = maybe_print_help(args)
+ args
+ end
+
+ def setup(*args)
+ log(args)
+ command_line(args.dup.flatten)
+ end
+
+ public
+
+ def exec(*args)
+ mvn = RubyMaven.new
+ mvn.exec(setup(args))
+ end
+
+ def exec_in(launchdirectory, *args)
+ mvn = RubyMaven.new
+ mvn.exec(launchdirectory, setup(args))
+ end
+ end
+ end
+end
View
96 lib/ruby/maven/ruby/maven.rb
@@ -0,0 +1,96 @@
+require 'fileutils'
+require 'java' if defined? JRUBY_VERSION
+
+module Maven
+ module Ruby
+ class Maven
+
+ private
+
+ def launch_jruby(args)
+ classpath_array.each do |path|
+ require path
+ end
+
+ java.lang.System.setProperty("classworlds.conf",
+ File.join(@maven_home, 'bin', "m2.conf"))
+
+ java.lang.System.setProperty("maven.home", @maven_home)
+
+ org.codehaus.plexus.classworlds.launcher.Launcher.main(args)
+ end
+
+ def classpath_array
+ Dir.glob(File.join(@maven_home, "boot", "*jar"))
+ end
+
+ def launch_java(*args)
+ system "java -cp #{classpath_array.join(':')} -Dmaven.home=#{File.expand_path(@maven_home)} -Dclassworlds.conf=#{File.expand_path(File.join(@maven_home, 'bin', 'm2.conf'))} org.codehaus.plexus.classworlds.launcher.Launcher #{args.join ' '}"
+ end
+
+ def options_string
+ options_array.join ' '
+ end
+
+ def options_array
+ options.collect do |k,v|
+ if k =~ /^-D/
+ v = "=#{v}" unless v.nil?
+ else
+ v = " #{v}" unless v.nil?
+ end
+ "#{k}#{v}"
+ end
+ end
+
+ public
+
+ def maven_home
+ @maven_home = File.expand_path(File.join(File.dirname(__FILE__),
+ '..',
+ '..',
+ '..',
+ '..'))
+ end
+
+ def options
+ @options ||= {}
+ end
+
+ def verbose= v
+ @verbose = v
+ end
+
+ def property(key, value = nil)
+ options["-D#{key}"] = value
+ end
+
+ def verbose
+ if @verbose.nil?
+ options.delete('--verbose').to_s == 'true'
+ else
+ @verbose
+ end
+ end
+
+ def exec(*args)
+ a = args.dup + options_array
+ a.flatten!
+ puts "mvn #{a.join(' ')}" if verbose
+ if defined? JRUBY_VERSION
+ launch_jruby(a)
+ else
+ launch_java(a)
+ end
+ end
+
+ def exec_in(launchdirectory, *args)
+ succeeded = nil
+ FileUtils.cd(launchdirectory) do
+ succeeded = exec(args)
+ end
+ succeeded
+ end
+ end
+ end
+end
View
59 lib/ruby/maven/ruby/pom_magic.rb
@@ -0,0 +1,59 @@
+require 'fileutils'
+
+module Maven
+ module Ruby
+ class PomMagic
+
+ def new_rails_project
+ Maven::Tools::RailsProject.new
+ end
+
+ def pom_xml
+ ".pom.xml"
+ end
+
+ def generate_pom(*args)
+ unless args.member?("-f") || args.member?("--file")
+ gemfiles = Dir["*Gemfile"]
+ gemfiles.delete_if {|g| g =~ /.pom/}
+ if gemfiles.size > 0
+ proj =
+ if File.exists? File.join( 'config', 'application.rb' )
+ new_rails_project
+ else
+ Maven::Tools::GemProject.new
+ end
+ filename = gemfiles[0]
+ proj.load_gemfile(filename)
+ else
+ gemspecs = Dir["*.gemspec"]
+ gemspecs.delete_if {|g| g =~ /.pom/}
+ if gemspecs.size > 0
+ proj = Maven::Tools::GemProject.new
+ filename = gemspecs[0]
+ proj.load_gemspec(filename)
+ end
+ end
+ if proc
+ proj.load_jarfile(File.join(File.dirname(filename), 'Jarfile'))
+ proj.load_gemfile(File.join(File.dirname(filename), 'Mavenfile'))
+ proj.add_defaults
+ File.open(pom_xml, 'w') do |f|
+ f.puts proj.to_xml
+ end
+ args << '-f'
+ args << pom_xml
+ end
+ end
+ args
+ end
+
+ def dump_pom(force = false, file = 'pom.xml')
+ if force || !File.exists?(file)
+ generate_pom
+ FileUtils.cp(pom_xml, file)
+ end
+ end
+ end
+ end
+end
View
5 lib/ruby/maven/ruby/version.rb
@@ -0,0 +1,5 @@
+module Maven
+ module Ruby
+ VERSION = '3.0.4.1'
+ end
+end
View
228 lib/ruby/ruby_maven.rb
@@ -1,227 +1 @@
-require 'fileutils'
-require 'maven/tools/rails_project'
-require 'java' if defined? JRUBY_VERSION
-
-module Maven
- class RubyMaven
-
- # make the command line for the goals of the jruby-maven-plugins nicer
- PLUGINS = {
- :rake => [:rake],
- :jruby => [:jruby, :compile],
- :gem => [:package, :install, :push, :exec, :pom, :initialize, :irb],
- :rails3 => [:new, :generate, :rake, :server, :console, :dbconsole, :pom, :initialize],
- :cucumber => [:test],
- :rspec => [:test],
- :runit => [:test],
- :mini => [:test,:spec],
- :bundler => [:install, :update]
- }
- ALIASES = {
- :ruby => :jruby,
- :spec => :rspec,
- :rails => :rails3,
- :bundle => :bundler
- }
-
- def initialize
- @maven_home = File.expand_path(File.join(File.dirname(__FILE__),'..','..'))
- end
-
- def launch_jruby(args)
- classpath_array.each do |path|
- require path
- end
-
- java.lang.System.setProperty("classworlds.conf",
- File.join(@maven_home, 'bin', "m2.conf"))
-
- java.lang.System.setProperty("maven.home", @maven_home)
-
- org.codehaus.plexus.classworlds.launcher.Launcher.main(args)
- end
-
- def classpath_array
- Dir.glob(File.join(@maven_home, "boot", "*jar")) +
- Dir.glob(File.join(@maven_home, "ext", "ruby-tools*jar"))
- end
-
- def launch_java(*args)
- system "java -cp #{classpath_array.join(':')} -Dmaven.home=#{File.expand_path(@maven_home)} -Dclassworlds.conf=#{File.expand_path(File.join(@maven_home, 'bin', 'm2.conf'))} org.codehaus.plexus.classworlds.launcher.Launcher #{args.join ' '}"
- end
-
- def prepare(args)
- if args.size > 0
- name = args[0].to_sym
- name = ALIASES[name] || name
- if PLUGINS.member?(name)
- start = 1
- if args.size > 1
- if PLUGINS[name].member? args[1].to_sym
- goal = args[1].to_sym
- start = 2
- else
- goal = PLUGINS[name][0]
- end
- else
- goal = PLUGINS[name][0]
- end
- # determine the version and delete from args if given
- version = args.detect do |a|
- a =~ /^-Dplugin.version=/
- end
- version ||= options['-Dplugin.version']
-
- if version
- args.delete(version)
- version = ":" + version.sub(/^-Dplugin.version=/, '')
- end
- aa = if index = args.index("--")
- args[(index + 1)..-1]
- else
- []
- end
- ruby_args = (args[start, (index || 1000) - start] || []).join(' ')
-
- aa << "de.saumya.mojo:#{name}-maven-plugin#{version}:#{goal}"
- aa << "-Dargs=\"#{ruby_args}\"" if ruby_args.size > 0
- args.replace(aa)
- else
- args.delete("--")
- end
- end
- args
- end
-
- def log(args)
- log = File.join('log', 'rmvn.log')
- if File.exists? File.dirname(log)
- File.open(log, 'a') do |f|
- f.puts "#{$0.sub(/.*\//, '')} #{args.join ' '}"
- end
- end
- end
-
- def maybe_print_help(args)
- if args.size == 0 || args[0] == "--help"
- puts "usage: rmvn [<plugin-name> [<args>] [-- <maven-options>]] | [<maven-goal>|<maven-phase> <maven-options>] | --help"
- PLUGINS.each do |name, goals|
- puts
- print "plugin #{name}"
- print " - alias: #{ALIASES.invert[name]}" if ALIASES.invert[name]
- puts
- if goals.size > 1
- print "\tgoals : #{goals.join(',')}"
- puts
- end
- print "\tdefault goal: #{goals[0]}"
- puts
- end
- puts
- ["--help"]
- else
- args
- end
- end
-
- def options
- @options ||= {}
- end
-
- def options_string
- options_array.join ' '
- end
-
- def options_array
- options.collect do |k,v|
- if k =~ /^-D/
- v = "=#{v}" unless v.nil?
- else
- v = " #{v}" unless v.nil?
- end
- "#{k}#{v}"
- end
- end
-
- def command_line(args)
- args = prepare(args)
- args = maybe_print_help(args)
- args
- end
-
- def new_rails_project
- Maven::Tools::RailsProject.new
- end
-
- def generate_pom(*args)
- unless args.member?("-f") || args.member?("--file")
- gemfiles = Dir["*Gemfile"]
- gemfiles.delete_if {|g| g =~ /.pom/}
- if gemfiles.size > 0
- proj =
- if File.exists? File.join( 'config', 'application.rb' )
- new_rails_project
- else
- Maven::Tools::GemProject.new
- end
- filename = gemfiles[0]
- proj.load_gemfile(filename)
- else
- gemspecs = Dir["*.gemspec"]
- gemspecs.delete_if {|g| g =~ /.pom/}
- if gemspecs.size > 0
- proj = Maven::Tools::GemProject.new
- filename = gemspecs[0]
- proj.load_gemspec(filename)
- end
- end
- if filename
- proj.load_jarfile(File.join(File.dirname(filename), 'Jarfile'))
- proj.load_gemfile(File.join(File.dirname(filename), 'Mavenfile'))
- proj.add_defaults
- #target = File.join(File.dirname(filename), 'target')
- #FileUtils.mkdir_p target
- pom = ".pom.xml"
- File.open(pom, 'w') do |f|
- f.puts proj.to_xml
- end
- args << '-f'
- args << pom
- end
- end
- args
- end
-
- def exec(*args)
- log(args)
- a = command_line(args.dup.flatten)
- verbose = options.delete('--verbose')
- no_pom = options.delete('--no-pom')
- a << options_array
- a.flatten!
- a = generate_pom(*a) unless no_pom
- puts "mvn #{a.join(' ')}" if verbose
- if defined? JRUBY_VERSION
- # TODO use a setup like maven_gemify from jruby to launch maven
- launch_java(a)
- else
- launch_java(a)
- end
- end
-
- def exec_in(launchdirectory, *args)
- succeeded = nil
- FileUtils.cd(launchdirectory) do
- succeeded = exec(args)
- end
- succeeded
- end
-
- def dump_pom(force = false, file = 'pom.xml')
- if force || !File.exists?(file)
- generate_pom
- FileUtils.cp(".pom.xml", file)
- end
- end
- end
-end
+require 'maven/ruby/maven'
View
8 pom.xml
@@ -34,6 +34,7 @@
</repositories>
<dependencies>
+ <!-- get the gems so `gem build` works -->
<dependency>
<groupId>rubygems</groupId>
<artifactId>thor</artifactId>
@@ -50,12 +51,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <!-- if some more gem releases are needed you can append some number
- here with the gem.version -->
- <jruby.plugins.version>0.29.1</jruby.plugins.version>
- <!--jruby.plugins.version.postfix>-SNAPSHOT</jruby.plugins.version.postfix-->
<maven.version>3.0.4</maven.version>
- <gem.basedir>${project.build.directory}/ruby-maven-${project.version}</gem.basedir>
</properties>
<build>
<resources>
@@ -90,7 +86,6 @@
</execution>
</executions>
</plugin>
-
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
@@ -133,5 +128,4 @@
</plugin>
</plugins>
</build>
-
</project>
View
3 ruby-maven.gemspec
@@ -1,7 +1,8 @@
require 'fileutils'
+require 'lib/ruby/maven/ruby/version'
Gem::Specification.new do |s|
s.name = %q{ruby-maven}
- s.version = "3.0.4.1"
+ s.version = Maven::Ruby::VERSION
s.authors = ["mkristian"]
s.description = %q{maven support for rubygems based on maven 3.0. it allows to use xyz.gemspec file as pom file or the usual pom.xml files. with a rails3 application with a Gemfile (suitable for jruby). you need java installed or jruby but it will run with MRI (without installed jruby) since the maven will take care of the jruby to use.}

0 comments on commit 838591b

Please sign in to comment.