Class: AppMap::Swagger::RakeDiffTask::Command

Inherits:
Struct
  • Object
show all
Defined in:
lib/appmap/swagger/rake_diff_task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#baseObject

Returns the value of attribute base

Returns:

  • (Object)

    the current value of base



16
17
18
# File 'lib/appmap/swagger/rake_diff_task.rb', line 16

def base
  @base
end

#git_commandObject

Returns the value of attribute git_command

Returns:

  • (Object)

    the current value of git_command



16
17
18
# File 'lib/appmap/swagger/rake_diff_task.rb', line 16

def git_command
  @git_command
end

#swagger_fileObject

Returns the value of attribute swagger_file

Returns:

  • (Object)

    the current value of swagger_file



16
17
18
# File 'lib/appmap/swagger/rake_diff_task.rb', line 16

def swagger_file
  @swagger_file
end

Instance Method Details

#performObject



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
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
# File 'lib/appmap/swagger/rake_diff_task.rb', line 21

def perform
  do_fail = lambda do |msg|
    warn msg
    exit $?.exitstatus || 1
  end
  
  return do_fail.(%Q('#{git_command}' not found; please install git)) unless system('git --version 2>&1 > /dev/null')

  fetch_command = 'git fetch'
  warn fetch_command if verbose
  do_fail.(%Q(#{fetch_command} failed)) unless system(fetch_command)

  show_command = %Q(git show #{base}:#{swagger_file})
  warn show_command if verbose
  base_content = `#{show_command}`
  do_fail.(%Q(#{show_command} failed)) unless $?.exitstatus == 0

  base_content = YAML.load(base_content)
  head_content = YAML.load(File.read(swagger_file))

  format_path = lambda do |tokens|
    tokens.map do |token|
      if token =~ /^[a-zA-Z0-9_-]+$/
        token
      else
        %Q("#{token}")
      end
    end.join('.')
  end

  format_change = lambda do |entry|
    path, old_v, new_v = entry
    [
      "changed       @ #{format_path.call path.split('.')}",
      "old value     : #{old_v}",
      "new value     : #{new_v}" 
    ].join("\n")
  end

  format_deletion = lambda do |entry|
    path, old_v = entry
    path_tokens = path.split('.')
    removed_key = path_tokens.pop
    [
      "removed       @ #{format_path.call path_tokens}",
      "removed key   : #{removed_key}",
      "removed value : #{old_v}" 
    ].join("\n")
  end

  format_addition = lambda do |entry|
    path, new_v = entry
    path_tokens = path.split('.')
    added_key = path_tokens.pop
    [
      "added         @ #{format_path.call path_tokens}",
      "added key     : #{added_key}",
      "added value   : #{new_v}" 
    ].join("\n")
  end

  formatters = {
    '~' => format_change,
    '-' => format_deletion,
    '+' => format_addition,
  }

  diff = Hashdiff.diff(base_content, head_content)
  diff.each_with_index do |entry, index|
    puts unless index == 0
    warn YAML.dump(entry) if verbose
    puts formatters[entry[0]].(entry[1..-1])
  end
end

#verboseObject



17
18
19
# File 'lib/appmap/swagger/rake_diff_task.rb', line 17

def verbose
  Rake.verbose == true
end