Class: Maven::Tools::POM

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/maven/tools/pom.rb

Instance Method Summary collapse

Methods included from DSL

#activation, #add_execute_task, #archives, #args_and_options, #artifact, #basedir, #build, #contributor, #dependency, #dependency!, #dependency?, #dependency_container, #dependency_set, #developer, #distribution, #do_dependency, #do_gem, #do_jruby_plugin, #eval_pom, #excludes, #execute, #execute_goal, #execute_goals, #extension, #file, #fill, #fill_options, #find_dependency, #gem, #gem!, #gem?, #gemfile, #gemspec, #git, #group, #id, #includes, #inherit, #issue_management, #jar!, #jarfile, #jruby_plugin, #jruby_plugin!, #license, #local, #mailing_list, #maven, #method_missing, #model, #organization, #other_archives, #overrides, #path, #phase, #platforms, #plugin, #plugin!, #plugin_repository, #prerequisites, #profile, #project, #properties, #property, #releases, #report_set, #reporting, #repository, #repository_policy, #resource, #retrieve_dependency, #roles, #ruby, #scope, #set_config, #setup_jruby_plugins_version, #site, #snapshot_repository, #snapshots, #source, #source_control, #spec, #tesla, #test_resource, #xml

Constructor Details

#initialize(file = nil) ⇒ POM

Returns a new instance of POM.



63
64
65
66
67
68
69
# File 'lib/maven/tools/pom.rb', line 63

def initialize( file = nil )
  if file.is_a? Gem::Specification
    eval_spec( file )
  else
    eval_file( file )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Maven::Tools::DSL

Instance Method Details

#eval_file(file) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/maven/tools/pom.rb', line 40

def eval_file( file )
  if file && File.directory?( file )
    dir = file
    file = nil
  else
    dir = '.'
  end

  unless file
    file = pom_file( 'pom.rb', dir )
    file ||= pom_file( 'Mavenfile', dir )
    file ||= pom_file( 'Gemfile', dir )
    #file ||= pom_file( 'Jarfile', dir )
    file ||= pom_file( '*.gemspec', dir )
  end

  if file
    FileUtils.cd( dir ) do
      @model = to_model( File.basename( file ) )
    end
  end
end

#eval_spec(s) ⇒ Object



34
35
36
37
38
# File 'lib/maven/tools/pom.rb', line 34

def eval_spec( s )
  @model = tesla do
    spec s
  end
end

#pom_file(pom, dir = '.') ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/maven/tools/pom.rb', line 71

def pom_file( pom, dir = '.' )
  files = Dir[ File.join( dir, pom ) ]
  case files.size
  when 0
  when 1
    files.first
  else
    warn 'more than one pom file found'
  end
end

#to_file(file) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/maven/tools/pom.rb', line 91

def to_file( file )
  if @model
    v = ::Maven::Tools::Visitor.new( File.open( file, 'w' ) )
    v.accept_project( @model )
    true
  end
end

#to_model(file) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/maven/tools/pom.rb', line 99

def to_model( file )
  if File.exists?( file )
    case file
    when /pom.rb/
      eval_pom( "tesla do\n#{ File.read( file ) }\nend", file )
    when /(Maven|Gem|Jar)file/
      eval_pom( "tesla do\n#{ File.read( file ) }\nend", file )
    when /.+\.gemspec/
      eval_pom( "tesla do\ngemspec( '#{ File.basename( file ) }' )\nend", file )
    end
  else
    eval_pom( "tesla do\n#{file}\nend", nil )
  end
rescue ArgumentError => e
  warn 'fallback to old maven model'
  puts e.message
  puts e.backtrace.join("\n\t")
  raise 'TODO old maven model'
end

#to_sObject



82
83
84
85
86
87
88
89
# File 'lib/maven/tools/pom.rb', line 82

def to_s
  if @model
    io = StringIO.new
    v = ::Maven::Tools::Visitor.new( io )
    v.accept_project( @model )
    io.string
  end
end