marklarr / mayday

Easily add custom warnings and errors to your Xcode project's build process
MIT License
260 stars 7 forks source link

Main file loop doesn't include custom globs #19

Closed keith closed 6 years ago

keith commented 9 years ago

When specifying a match with a custom files: ["glob"] the specified glob doesn't seem to be included in the script's output. This means that files not ending with *.{m,h,swift} are not considered.

Here's an example, when trying to specify regex for storyboards:

warning_regex "Misplaced views", /misplaced/, files: ["*.storyboard"]

The (abridged) output looks like this:

def abstract_flag_matcher_9955(file_path, file_contents)
  return unless File.fnmatch("*.storyboard", file_path)
  return if File.fnmatch("*Pods/*", file_path)

  line_number_to_warning_hash = lambda do
    lambda do |file_contents|
      hash = {}
      lines = file_contents.split("
                                  ")
      lines.each_with_index do |line, line_number| 
        message = lambda do |line|
          line =~ Regexp.new('(?-mix:YES)') ? "Misplaced views" : nil
        end
        .call(line)
        hash[line_number + 1] = message if message
      end
      hash
    end
    .call(file_contents)
  end.call

  if line_number_to_warning_hash && line_number_to_warning_hash.keys.count > 0
    final_warning_array = []
    line_number_to_warning_hash.map do |line_number, warning_str|
      final_warning_array << "#{file_path}:#{line_number}: warning: #{warning_str} [Wmayday]"
    end
    final_warning_array.join("")
  else
    false
  end
end

Dir[ENV["SRCROOT"] + "/**/*.{m,h,swift}"].each do |filename|
  # Could be a dir with .m, like Underscore.m's dir
  if (File.file?(filename))
    file = File.open(filename, 'r')
    file_contents = file.read
    abstract_flag_output = abstract_flag_matcher_9955(file.path, file_contents)
    if abstract_flag_output && true
      puts abstract_flag_output
    end
  end
end

My understanding here is that:

Dir[ENV["SRCROOT"] + "/**/*.{m,h,swift}"].each do |filename|

Will not include *.storyboard files, meaning my custom matcher will never get called.

Let me know if I'm totally off here!

keith commented 9 years ago

By editing the gem locally and adding ,storyboard to that glob, it seems to work as expected.

marklarr commented 9 years ago

You're right! I didn't really even realize you could have warning in storyboard file. Should we be adding xib also?

keith commented 9 years ago

Probably. But maybe we shouldn't be restricting that glob at all. The problem then would be needing to merge there's and may days. Because I could also see wanting to lint even more arbitrary stuff such as strings files, maybe even xcassets JSON to enforce file names. But either way adding storyboard and xib would probably be an easy quick fix.

marklarr commented 9 years ago

Looking at more files means the build phase would run more slowly, but I wonder if the difference would be negligible?

keith commented 9 years ago

Yea, I guess part of the problem is that if people aren't specifying a language right now, adding those to the glob could break some stuff. Especially line length like tests.

marklarr commented 9 years ago

Maybe it tries to be smart and includes any globs they've put into any warnings/errors into that final Dir[ENV["SRCROOT"] + "/**/*.{m,h,swift}"].each do |filename| glob?

keith commented 9 years ago

Yep, I think that would work