Class: Gjp::ScriptGenerator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gjp/script_generator.rb

Overview

generates build scripts from bash_history

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(project, history_path) ⇒ ScriptGenerator



8
9
10
11
12
13
# File 'lib/gjp/script_generator.rb', line 8

def initialize(project, history_path)
  @project = project
  @ant_runner = Gjp::AntRunner.new(project)
  @maven_runner = Gjp::MavenRunner.new(project)
  @history_path = history_path
end

Instance Method Details

#generate_build_script(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gjp/script_generator.rb', line 15

def generate_build_script(name)
  @project.from_directory do
    history_lines = File.readlines(@history_path).map { |e| e.strip }
    relevant_lines =
      history_lines
        .reverse
        .take_while { |e| e.match(/gjp +dry-run/).nil? }
        .reverse
        .take_while { |e| e.match(/gjp +finish/).nil? }
        .select { |e| e.match(/^#/).nil? }

    script_lines = [
      "#!/bin/bash",
      "PROJECT_PREFIX=`readlink -e .`",
      "cd #{@project.latest_dry_run_directory}"
    ] +
    relevant_lines.map do |line|
      if line =~ /gjp +mvn/
        line.gsub(/gjp +mvn/, "#{@maven_runner.get_maven_commandline("$PROJECT_PREFIX", ["-o"])}")
      elsif line =~ /gjp +ant/
        line.gsub(/gjp +ant/, "#{@ant_runner.get_ant_commandline("$PROJECT_PREFIX")}")
      else
        line
      end
    end

    new_content = script_lines.join("\n") + "\n"

    script_name = "build.sh"
    result_path = File.join("src", name, script_name)
    conflict_count = @project.merge_new_content(new_content, result_path, "Build script generated",
                                                "generate_#{name}_build_script")

    destination_dir = File.join("output", name)
    FileUtils.mkdir_p(destination_dir)
    destination_script_path =  File.join(destination_dir, script_name)
    FileUtils.symlink(File.expand_path(result_path), destination_script_path, force: true)

    [result_path, conflict_count]
  end
end