18 Commits

Author SHA1 Message Date
Issy Long
e4156909d4
Only take overrideable stanzas into account
- This skips over stanza names that are not overrideable in `on_*`
  blocks, with the positive side effect that `on_*` blocks themselves
  aren't in the list so we can get rid of another conditional.
- Stanzas overrideable in blocks are defined in `Cask::DSL` by each of
  the methods calling `set_unique_stanza`.
2023-03-23 18:59:29 +00:00
Issy Long
d4d413db62
String interpolations in regexps should also be allowed
- This came up in Cask `simply-fortran`:

```
Scanning /opt/homebrew/Library/Taps/homebrew/homebrew-cask/Casks/simply-fortran.rb
send_node: s(:send, nil, :arch), send_node.parent: s(:begin,
  s(:send, nil, :arch)), send_node.parent.parent: (regexp
  (str "href=.*?simplyfortran[._-]v?(\\d+(?:\\.\\d+)+)")
  (begin
    (send nil :arch))
  (str "\\.dmg")
  (regopt :i))
Casks/simply-fortran.rb:2:3: C: Cask/NoOverrides: Do not use a top-level arch stanza as the default. Add it to an on_{system} block instead.
Use :or_older or :or_newer to specify a range of macOS versions.

  arch arm: "-arm64", intel: "-x86_64"
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```
2023-03-23 13:55:00 +00:00
Issy Long
c1de3dfb90
Fix string interpolation detection again to be even broader
- My approach wasn't quite good enough. Replace it with what RuboCop
  itself does to detect all forms of string interpolation.
2023-03-23 13:17:09 +00:00
Issy Long
e66226aefc
Fix skipping of livecheck block contents in on_* blocks
- This passes the previously failing test for `on_*` blocks with
  `livecheck` blocks with multiple stanzas inside them (eg `url` and
  `strategy`) that weren't being correctly skipped because we weren't
  detecting high enough up the ancestry.
2023-03-22 23:17:32 +00:00
Issy Long
b82c5f1d36
Fix detection of string interpolation
- This was almost there but not quite and the lack of tests lulled me into a false sense of security.
2023-03-22 19:13:47 +00:00
Issy Long
8091e603df
rubocops/cask: Ignore livecheck blocks within on_* blocks
- The Cask `sip`, to give a random example, was failing this RuboCop
  because it has a `livecheck` block within an `on_*` block and the
  livecheck block and the top-level Cask both have `url` stanzas. This
  is a legitimate use of `livecheck` blocks because the cask software
  download URL and the livecheck version check URL are not the same
  thing, so let's skip over `livecheck` blocks and their contents.
2023-03-21 23:49:54 +00:00
Issy Long
794cd37dbb
Remove unnecessary skip since toplevel_stanzas doesn't look in blocks
Co-authored-by: Markus Reiter <me@reitermark.us>
2023-03-21 22:51:28 +00:00
Issy Long
7a4a579847
rubocops/cask: Run on_system_stanzas(on_block) once in the beginning
Co-authored-by: Markus Reiter <me@reitermark.us>
2023-03-21 22:51:27 +00:00
Issy Long
c337dc4c81
rubocops/cask: Skip string interpolations when determining inner stanzas
- Otherwise syntax like this, where `#{arch}` is an interpolation,
  causes an offense:

```ruby
cask "transcribe" do
  arch arm: "_arm"

  on_catalina :or_older do
    version "8.75.2"
    sha256 "f01781100cd3b9987c8f8892145a2eaa358df07b92e10e26f30b6a877f5b352c"

    url "https://www.seventhstring.com/xscribe/downmo/transcribe#{version.no_dots}.dmg"

    livecheck_version = "10"
  end
  on_big_sur :or_newer do
    livecheck_version = "11"

    version "9.21"
    sha256 :no_check

    url "https://www.seventhstring.com/xscribe/transcribe#{arch}.dmg"
  end
  [...]
end
```

```
Casks/transcribe.rb:2:3: C: Cask/NoOverrides: Do not use a top-level arch stanza as the default. Add it to an on_{system} block instead.
Use :or_older or :or_newer to specify a range of macOS versions.

  arch arm: "_arm"
  ^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```
2023-03-21 22:51:26 +00:00
Markus Reiter
e04101007f
Fix check for Set. 2023-03-21 22:51:25 +00:00
Issy Long
deb8a13a73
rubocops/cask: Make names, the list of method names in on_*, a Set
Co-authored-by: Markus Reiter <me@reitermark.us>
2023-03-21 22:51:24 +00:00
Issy Long
0b7bd81959
rubocops/cask: No need for the first return if, [].each works OK 2023-03-21 22:51:24 +00:00
Issy Long
35cd764261
rubocops/cask: Tweak offense message to read better 2023-03-21 22:51:20 +00:00
Issy Long
2c5165850a
rubocops/cask: Check ON_SYSTEM_METHODS instead of STANZA_ORDER
- This is already here and used in multiple places. Continuing to use
  `STANZA_ORDER` will break this cop when we add `ON_SYSTEM_METHODS`
  to that list.
2023-03-21 22:51:19 +00:00
Issy Long
ff2fad6bcf
rubocops/cask: Don't force on_* blocks for all stanzas
- In the event that there's only one common stanza within the `on_*`
  blocks (eg, `url`) with a generic `version` that doesn't change per-OS,
  let's not force adding `version` to each `on_*` block as well.
2023-03-21 22:51:18 +00:00
Issy Long
dec1309140
rubocops/cask: Casks with zero on_* blocks are still valid 2023-03-21 22:51:17 +00:00
Issy Long
5ce4966f4a
rubocops/cask: Use ON_SYSTEM_METHODS for skipping
Co-authored-by: Rylan Polster <rslpolster@gmail.com>
2023-03-21 22:51:16 +00:00
Issy Long
9cc046bc60
rubocops/cask: Enforce the use of on_{system} blocks
- As discussed in
  https://github.com/Homebrew/brew/pull/14976#issuecomment-1474544569
  and further comments, this is needed because in order to enforce the
  order of `on_{arch,system}` blocks we need to have everything
  consistently within one of those blocks.
- We previously allowed overrides where the top-level `version` stanza
  would be the default, unless on an OS that had an `on_system` block
  with a `version` specified. But this breaks down when we try to order
  the `on_system` blocks because if a `url` at the top-level has a
  `version` interpolated in it, then the `version` stanza needs to be
  above the `url` stanza. But it could be that `version` is OS-specific.
- Let's stop allowing overrides and require that everything be in an
  `on_system` block. This will make it easier to enforce the order of
  `on_system` blocks in a future PR (14976).
2023-03-21 22:51:12 +00:00