Module: Mvnizer::TaskHelper
- Included in:
- Command::AddDependency, Command::NewProject
- Defined in:
- lib/mvnizer/task_helper.rb
Overview
The TaskHelper provides different functions that can be used in tasks, such as function to create directories, generate files from templates, etc.
Constant Summary collapse
- TEMPLATE_DIR =
path to the template location.
File.(File.join(File.dirname(__FILE__), 'templates'))
Instance Method Summary collapse
-
#add_dependency(dependency, pom_location = File.join(Dir.pwd, "pom.xml")) ⇒ Object
adds a list of dependencies to the pom file whose location can be given as
pom_location. -
#create_dir(*dir) ⇒ Object
creates recursively all directories passed as a param.
-
#generate_file(template, destination_file, binding) ⇒ Object
generates the file
destination_filefrom templatetemplateand objectbinding.
Instance Method Details
#add_dependency(dependency, pom_location = File.join(Dir.pwd, "pom.xml")) ⇒ Object
adds a list of dependencies to the pom file whose location can be given as pom_location. By default, this function looks up the pom in the current directory.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mvnizer/task_helper.rb', line 36 def add_dependency(dependency, pom_location = File.join(Dir.pwd, "pom.xml")) raise FileNotFoundError, "The pom.xml file cannot be found." unless File.exists?(pom_location) coordinate_parser = CoordinateParser.new pom = Nokogiri::XML(File.open(pom_location)) do |c| c.noblanks end dependencies_node = pom.xpath("/pom:project/pom:dependencies", {"pom" => "http://maven.apache.org/POM/4.0.0"}).first dependency.each do |d| # First parse the dependency coordinates dep_project = coordinate_parser.parse_scoped_coordinates(d) Nokogiri::XML::Builder.with(dependencies_node) do |xml| xml.dependency { xml.groupId dep_project.group_id xml.artifactId dep_project.artifact_id xml.version dep_project.version xml.scope dep_project.scope if dep_project.scope != nil && dep_project.scope != "compile" xml.type dep_project.type if dep_project.type != "jar" } end end # Requires sparklemotion/nokogiri#772 to produce # identical pom files in both MRI and JRuby. pom.to_xml(indent: 2) end |
#create_dir(*dir) ⇒ Object
creates recursively all directories passed as a param.
12 13 14 |
# File 'lib/mvnizer/task_helper.rb', line 12 def create_dir(*dir) dir.each { |d| FileUtils.mkdir_p d } end |
#generate_file(template, destination_file, binding) ⇒ Object
generates the file destination_file from template template and object binding. binding must be an ERB binding. destination_file must be the aboslute location to the generated file. If the output folder in which the file gets generated does not exist, it automatically gets created.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mvnizer/task_helper.rb', line 22 def generate_file(template, destination_file, binding) dir = File.dirname(destination_file) create_dir(dir) unless File.exists?(dir) template = File.open(template, 'r').read File.open(destination_file, "w") do |f| f.write(ERB.new(template).result(binding.get_binding)) end end |