Class: Danger::DangerSwiftformat

Inherits:
Plugin
  • Object
show all
Defined in:
lib/swiftformat/plugin.rb

Overview

A danger plugin to check Swift formatting using SwiftFormat.

Examples:

Check that the added and modified files are properly formatted:


swiftformat.check_format

See Also:

  • garriguv/danger-swiftformat

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#additional_argsString

Additional swiftformat command line arguments

Returns:

  • (String)


20
21
22
# File 'lib/swiftformat/plugin.rb', line 20

def additional_args
  @additional_args
end

#additional_messageString

Additional message to be appended the report

Returns:

  • (String)


25
26
27
# File 'lib/swiftformat/plugin.rb', line 25

def additional_message
  @additional_message
end

#binary_pathString

The path to SwiftFormat’s executable

Returns:

  • (String)


15
16
17
# File 'lib/swiftformat/plugin.rb', line 15

def binary_path
  @binary_path
end

#excludeArray<String>

An array of file and directory paths to exclude

Returns:

  • (Array<String>)


30
31
32
# File 'lib/swiftformat/plugin.rb', line 30

def exclude
  @exclude
end

#swiftversionString

The project Swift version

Returns:

  • (String)


35
36
37
# File 'lib/swiftformat/plugin.rb', line 35

def swiftversion
  @swiftversion
end

Instance Method Details

#check_format(fail_on_error: false) ⇒ void

This method returns an undefined value.

Runs swiftformat

Parameters:

  • fail_on_error (Boolean) (defaults to: false)


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
# File 'lib/swiftformat/plugin.rb', line 43

def check_format(fail_on_error: false)
  # Check if SwiftFormat is installed
  raise "Could not find SwiftFormat executable" unless swiftformat.installed?

  # Find Swift files
  swift_files = find_swift_files

  # Stop processing if there are no swift files
  return if swift_files.empty?

  # Run swiftformat
  results = swiftformat.check_format(swift_files, additional_args, swiftversion)

  # Stop processing if the errors array is empty
  return if results[:errors].empty?

  # Process the errors
  message = "### SwiftFormat found issues:\n\n"
  message << "| File | Rules |\n"
  message << "| ---- | ----- |\n"
  results[:errors].each do |error|
    message << "| #{error[:file].gsub("#{Dir.pwd}/", '')} | #{error[:rules].join(', ')} |\n"
  end

  unless additional_message.nil?
    message << "\n" << additional_message
  end

  markdown message

  if fail_on_error
    fail "SwiftFormat found issues"
  end
end

#find_swift_filesArray<String]

Find the files on which SwiftFormat should be run

Returns:

  • (Array<String])

    Array<String]



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/swiftformat/plugin.rb', line 81

def find_swift_files
  renamed_files_hash = git.renamed_files.map { |rename| [rename[:before], rename[:after]] }.to_h

  post_rename_modified_files = git.modified_files
    .map { |modified_file| renamed_files_hash[modified_file] || modified_file }

  files = (post_rename_modified_files - git.deleted_files) + git.added_files

  @exclude = %w() if @exclude.nil?

  files
    .select { |file| file.end_with?(".swift") }
    .reject { |file| @exclude.any? { |glob| File.fnmatch(glob, file) } }
    .uniq
    .sort
end

#swiftformatSwiftFormat

Constructs the SwiftFormat class

Returns:



101
102
103
# File 'lib/swiftformat/plugin.rb', line 101

def swiftformat
  SwiftFormat.new(binary_path)
end