ashfurrow / danger-ruby-swiftlint

A Danger plugin for SwiftLint.
https://rubygems.org/gems/danger-swiftlint
MIT License
203 stars 80 forks source link

[Question] How to have a "Dynamic" Binary path ? #185

Closed alexanderwe closed 2 years ago

alexanderwe commented 2 years ago

Hey,

I am currently trying to let danger-ruby-swiftlint use a dynamic binary path by configuring it like the following:

swiftlint.config_file = './.swiftlint.yml'
swiftlint.binary_path = %x( which swiftlint )
swiftlint.lint_files

But it seems this is not working, I always get an error message that SwiftLint is not installed. But when I directly use the value calculated by %x( which swiftlint ) it is working

swiftlint.config_file = './.swiftlint.yml'
swiftlint.binary_path = '/opt/homebrew/bin/swiftlint'
swiftlint.lint_files

The reason why I am doing this is two fold:

  1. I do not want to rely on the SwiftLint version that is installed by danger-ruby-swiftlint
  2. The Homebrew path for SwiftLint is different on M1 and Intel machines and in the team and on CI we are using them both. So with the command I could always get the correct path to SwiftLint.

Do you maybe have an idea what I am doing wrong or if there is another way to make it possible what I try to achieve ?

Thanks in advance - and thanks a lot of course for maintaining this library ! :)

ashfurrow commented 2 years ago

Hi! Thanks for the detailed question. I agree that if which swiftlint yields the string /opt/homebrew/bin/swiftlint then this should work. Could you add some logging here so we could learn more?

binary_path = %x( which swiftlint )
puts "binary_path: #{binary_path}"
swiftlint.config_file = './.swiftlint.yml'
swiftlint.binary_path = binary_path
swiftlint.lint_files

When I just tested how %x( ) works in irb and got a string with \n at the end:

2.6.6 :001 > %x( which yarn )
 => "/usr/local/bin/yarn\n" # I'm on an unusual machine without SwiftLint installed right now 😅

So maybe adding a .strip would fix this?

swiftlint.config_file = './.swiftlint.yml'
swiftlint.binary_path = %x( which swiftlint ).strip
swiftlint.lint_files

Let us know how it goes!

alexanderwe commented 2 years ago

Hey @ashfurrow - thanks for the fast reply. At first, your suggestion with .strip works, so thanks a lot! 👍 Danger now picks up the correct path locally AND on the CI. But what's interesting though, when I add logging I do not see the \n at the end, but still .strip did the trick and it works now

You used `puts` in your Dangerfile. To print out text to GitHub use `message` instead
binary_path: /opt/homebrew/bin/swiftlint
[!] The exception involves the following plugins:
 -  danger-swiftlint
ashfurrow commented 2 years ago

Ah, right 😅

Yeah I'm not sure about formatting the \n newline. puts doesn't seem distinguish the two, but irb does:

2.6.0 :001 > "Hello, world\n"
 => "Hello, world\n"
2.6.0 :002 > "Hello, world"
 => "Hello, world"
2.6.0 :003 > puts "Hello, world\n"
Hello, world
 => nil
2.6.0 :004 > puts "Hello, world"
Hello, world
 => nil
2.6.0 :005 >

🤷 I'm glad it's working, though!