Class: App::Pom

Inherits:
Object
  • Object
show all
Defined in:
lib/core/pom.rb

Class Method Summary collapse

Class Method Details

.get_files_to_changeObject

Retrieves list of pom.xml files which need to be un-snapshotted

Returns:

  • Array



7
8
9
10
11
12
13
14
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
# File 'lib/core/pom.rb', line 7

def self.get_files_to_change

    command = "find #{ App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE)} -name 'pom.xml'"
    files = App::Terminal::command_capture(command)
    files = files[0].split("\n")

    unless files.any?
        App::Terminal::error('No files found', ["The command: #{App::Terminal::format_command(command)} didn't return any files.", 'It might be possible that the directory structure of our codebase has changed and this script needs to be updated.', nil, "Speak to #{App::Terminal::format_highlight('Albert')}."], true)
    end

    blank_line_inserted = false

    files_to_change = []
    files.each do |filename|
        file_lines = File.open(filename, 'rb').read
        file_lines.split("\n").each do |line|
            if line.match(/<version>\d+\.\d+\.\d+-(.*)-(snapshot)<\/version>/i)

                unless blank_line_inserted
                    puts
                    blank_line_inserted = true
                end

                puts "  \x1B[38;5;240mMatched line:\x1B[0m \x1B[38;5;11m#{line.strip}\x1B[0m"
                files_to_change << filename

            end
        end
    end

    files_to_change

end

.unsnapshot_files(files_to_change, current_branch_code, add_branch_tag = true, first_answer = nil, second_answer = nil, third_answer = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/core/pom.rb', line 41

def self.unsnapshot_files(files_to_change, current_branch_code, add_branch_tag = true, first_answer = nil, second_answer = nil, third_answer = nil)

    unless files_to_change.any?
        App::Terminal::info('No feature-tagged POMs found', 'Nothing to un-snapshot.')
        return
    end

    unless first_answer == App::Enum::YES
        if first_answer == App::Enum::NO || App::Terminal::prompt_yes_no('Un-snapshot POMs', generate_confirmation_text(files_to_change)) == false
            App::Terminal::abort
        end
    end

    files_to_change.each do |filename|
        App::Terminal::output("Un-snapshotting: #{App::Terminal::format_directory(filename)}")
        if current_branch_code == App::Git::MASTER || add_branch_tag == false
            new_snapshot_tag = '-SNAPSHOT'
        else
            new_snapshot_tag = "-#{current_branch_code}-SNAPSHOT"
        end
        contents = File.read(filename)
        contents_new = contents.gsub(/-(.*)-SNAPSHOT/, new_snapshot_tag)
        File.open(filename, 'w') { |file| file.puts contents_new }
    end

    red_versions = []
    green_versions = []

    diff_result = App::Terminal::command_capture('git diff | grep "+  \|-  "', App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
    puts
    diff_result[0].split("\n").each do |line|
        if line[0] == '+'
            puts "  \x1B[38;5;46m#{line.gsub(' ', '')}\x1B[0m"
            green_versions << get_version_from_line(line)
        else
            puts "  \x1B[38;5;196m#{line.gsub(' ', '')}\x1B[0m"
            red_versions << get_version_from_line(line)
        end
    end
    puts

    if red_versions != green_versions
        unless App::Terminal::prompt_yes_no('POM version conflict occurred', ['Unable to successfully Un-snapshot POMs.', 'Please fix the POM versions before continuing.'], 'POMs fixed and ready to continue?')
            App::Terminal::abort
        end
    end

    App::Terminal::command_capture(['git add .', 'git status'], App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))

    # Figure out the commit message.
    @git = App::Git.new
    current_branch_name = @git.current_branch_for_repo(App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE))
    if current_branch_name =~ /\A(bp|bug)-\d{4,5}\z/i
        jira_number = current_branch_name.gsub(/\D/, '')
    else
        jira_number = '0000'
    end

    if current_branch_code == App::Git::MASTER
        commit_message = "BP-#{jira_number} Removed POM tags."
    else
        commit_message = "BP-#{jira_number} Updated POM tags on #{current_branch_code}."
    end

    @git = App::Git.new

    if @git.current_branch_for_repo(App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_CODE)) == App::Git::MASTER
        App::Terminal::info('Un-snapshotting complete', ["Your POMs have been un-snapshotted. However, nothing has been committed to Git or pushed to origin because you are on #{App::Git::MASTER}.", 'You will need to do this manually.'], false)
    else
        if @git.changes_exist(App::Config.param(ConfigUnique::WORKSTATION_PATH_TO_BP_CODE))
            @git.ask_to_commit_to_git(commit_message, App::Config.param(App::Config::WORKSTATION_PATH_TO_BP_CODE), second_answer, third_answer)
        end
    end

end