69 Commits

Author SHA1 Message Date
apainintheneck
9567d57e39 Add missing utils/service require
This was originally added in https://github.com/Homebrew/brew/pull/18865.

```console
$ brew ruby -e 'p "activemq".f.service.to_systemd_unit'
/usr/local/Homebrew/Library/Homebrew/service.rb:458:in `block in to_systemd_unit': uninitialized constant Utils::Service (NameError)
        from /usr/local/Homebrew/Library/Homebrew/service.rb:458:in `map'
        from /usr/local/Homebrew/Library/Homebrew/service.rb:458:in `to_systemd_unit'
        from /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11685/lib/types/private/methods/call_validation.rb:278:in `bind_call'
        from /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11685/lib/types/private/methods/call_validation.rb:278:in `validate_call'
        from /usr/local/Homebrew/Library/Homebrew/vendor/bundle/ruby/3.3.0/gems/sorbet-runtime-0.5.11685/lib/types/private/methods/_methods.rb:277:in `block in _on_method_added'
        from -e:1:in `<main>'
```

```console
$ brew ruby -e 'p "activemq".f.service.to_systemd_unit'
"[Unit]\nDescription=Homebrew generated unit for activemq\n\n[Install]\nWantedBy=default.target\n\n[Service]\nType=simple\nExecStart=\"/usr/local/opt/activemq/bin/activemq\" \"console\"\nWorkingDirectory=/usr/local/opt/activemq/libexec\n"
```
2024-12-07 10:35:33 -08:00
Ruoyu Zhong
36d06c5075
Merge remote-tracking branch 'origin/master' into systemd-quote 2024-12-04 18:01:10 +08:00
Ruoyu Zhong
bba67ec02e
service: fix systemd command line quoting
Fixes #18802 (affected formulae still need rebuilding).
2024-12-04 02:57:11 +08:00
Ruoyu Zhong
21cd8e92c3
service: end systemd configs with a new line
systemd configs, like all UNIX text files, should end with a new line.
2024-12-04 02:46:52 +08:00
Issy Long
45978435e7
rubocop: Use Sorbet/StrictSigil as it's better than comments
- Previously I thought that comments were fine to discourage people from
  wasting their time trying to bump things that used `undef` that Sorbet
  didn't support. But RuboCop is better at this since it'll complain if
  the comments are unnecessary.

- Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501.

- I've gone for a mixture of `rubocop:disable` for the files that can't
  be `typed: strict` (use of undef, required before everything else, etc)
  and `rubocop:todo` for everything else that should be tried to make
  strictly typed. There's no functional difference between the two as
  `rubocop:todo` is `rubocop:disable` with a different name.

- And I entirely disabled the cop for the docs/ directory since
  `typed: strict` isn't going to gain us anything for some Markdown
  linting config files.

- This means that now it's easier to track what needs to be done rather
  than relying on checklists of files in our big Sorbet issue:

```shell
$ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l
    268
```

- And this is confirmed working for new files:

```shell
$ git status
On branch use-rubocop-for-sorbet-strict-sigils
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Library/Homebrew/bad.rb
        Library/Homebrew/good.rb

nothing added to commit but untracked files present (use "git add" to track)

$ brew style
Offenses:

bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true.
^^^^^^^^^^^^^

1340 files inspected, 1 offense detected
```
2024-08-12 15:24:27 +01:00
Douglas Eichelberger
179419d8bc rm unnecessary casts 2024-05-31 15:49:12 -07:00
Douglas Eichelberger
c59d9fa833 Prefer numbered block params over proc conversion, cont'd 2024-04-08 09:47:27 -07:00
Michael Cho
de00f76115
service: allow Pathname in array for #run
Signed-off-by: Michael Cho <michael@michaelcho.dev>
2024-04-01 14:30:10 -04:00
Mike McQuaid
ea2892f8ee
brew.rb: handle missing args. 2024-03-07 16:20:20 +00:00
apainintheneck
3c503cdf56
Normalize service serialization method names 2024-02-04 13:50:57 +01:00
Issy Long
f682147598
Fix RuboCop Style/RedundantFreeze offenses 2024-01-18 22:20:01 +00:00
apainintheneck
215419daa5 service: provide backwards compatibility for socket strings
The previous PR changed how sockets were represented in the JSON
API for formulae and that would cause problems when trying to install
packages with service sockets. This provides backwards compatibility
until all users have upgraded to versions of homebrew that can deserialize
sockets hashes (maybe a couple weeks). Essentially, we store the
socket string when serializing sockets that were originally defined with
only the string parameter otherwise we serialize it to a hash.
2023-10-07 14:22:38 -07:00
Kevin
043aca2df4
Revert "Revert "service: support multiple sockets in DSL"" 2023-09-29 19:10:13 -07:00
Kevin
653f333d73
Revert "service: support multiple sockets in DSL" 2023-09-28 09:58:03 -07:00
apainintheneck
5fb9f90457 service: prefer symbols over strings in DSL
This is more in keeping with the other DSL methods and Ruby
convention along with the fact that these socket names are
just used internally by launchd.
2023-09-27 22:30:30 -07:00
apainintheneck
ae5e9387b9 service: support multiple sockets in DSL
This adds support for multiple named sockets to the service DSL.
It also retains backwards compatibility with the previous DSL
where you can declare one socket and it is always just named
Listener by default.
2023-09-23 19:29:21 -07:00
Mike McQuaid
8775465872
Enable Sorbet after running a developer command
We have no commands with Sorbet disabled and have had Sorbet enabled
for developers for a decent amount of time. As a result, we can enable
it for everyone who has run a developer command.

This also allows a bunch of `raise TypeError`s to be removed in favour
of relying on Sorbet here instead.
2023-09-02 08:39:28 -04:00
Mike McQuaid
6cb8f3d7e0
service: handle quoting in service file, too.
Follow-up to https://github.com/Homebrew/brew/pull/15875 based on
https://github.com/Homebrew/homebrew-core/pull/139680#discussion_r1295770047
2023-08-16 13:58:03 +01:00
Mike McQuaid
6d65ec3afb
service: correctly quote arguments.
If you have a run argument with a space in it: it will need to be quoted
to be passed through correctly when run as a manual command.

Fixes #15871.
2023-08-16 12:03:40 +01:00
Mike McQuaid
c940e15a5c
Use HOMEBREW_CELLAR_PLACEHOLDER
Similarly to HOMEBREW_PREFIX_PLACEHOLDER, this allows the substitution
of HOMEBREW_CELLAR in JSON output when using the API.

Fixes #15668.
2023-07-18 10:59:27 +01:00
Markus Reiter
c12ea0bc56
Also expand paths in command. 2023-05-18 20:00:46 +02:00
Markus Reiter
7eff8957c4
Expand service paths. 2023-05-18 19:59:32 +02:00
Bo Anderson
a3c96126cf
service: fix API compatibility break 2023-05-18 14:57:47 +01:00
apainintheneck
f2adbf6613 service: change custom name DSL
After some discussion, we decided to change the DSL to get rid of
the `plist_name` and `service_name` methods which aren't meaningful
for most users.

The new DSL looks like this:

```rb
service do
  name macos: "name", linux: "name"
end
```

I also updated some specs here to reflect these changes.

There was some talk about maybe deprecating `plist_name` and `service_name`
but I think that's outside of the scope of this PR so I'm leaving
them as is for now. One benefit of this is that everything here is backwards
compatible.
2023-05-17 21:54:32 -07:00
apainintheneck
1111706378 service: add custom service name DSL
The main thing is that this DSL allows us to provide an
interface that can be serialized to the JSON API.

Changes:
- Homebrew::Service
  - Adds `#service_name` and `#plist_name` methods
    - Each is now included in the `#serialize` method as well
  - Eval block on instantiation
    - Before we lazy evaluated this but the cost is not significant
      and it complicated the code a bunch. This only gets called
      during install, when evaluating caveats and in the `brew service`
      command. It skips this evaluation if the service block isn't there.
  - Add `#command?` helper to avoid `#command.blank?` and `#command.present?`
- Formula
  - `#service` now returns a service whenever it's called. This call is
    hidden behind a call to `#service?` most of the time anyway so this
    should be fine.
  - `#plist_name` and `#service_name` now call the methods of the same name
    on the service class. This should have already been in the service object
    to begin with and keeping these methods here helps preserve backwards
    compatibility with people who were overwriting these methods before.
- Caveats
  - Prefer `service#command?`
  - Add helpers for checking on service commands
    - This duplicates some of the work in `brew services`. Maybe we should
      merge that repo in at some point.
  - Check for installed service at `#plist_name` or `#service_name`. I think
    this should be used instead of `Keg#plist_installed?` which checked for any plist file.
    We should think about deprecating `#plist_installed?` in the future.
  - Stop using `ps aux | grep #{formula.plist_name}` to check for service files
    because it was inaccurate (it always returns true on my machine) because the grep
    process is started before the ps process.
  - Note: The behavior is the same as it was before. This means that caveats
    only show up for custom service files on install or if they're already installed.
    Otherwise it won't show up in `brew info`. This is because it has to check
    first if the service file has been installed.
- Utils::Service
  - Add utils for evaluating if a service is installed and running. This duplicates
    some of the work already found in `brew services`. We should seriously consider
    merging `brew services` with the main brew repo in the future since it's already
    tightly coupled to the code in the main repo.
- Formulary.load_formula_from_api
  - Be more explicit about which types can be deserialized into run params since
    it is now possible for run params to be nil.
- Update and add tests
2023-05-13 14:53:01 -07:00
Douglas Eichelberger
24cf6076e8 brew style --fix 2023-04-24 20:42:39 -07:00
Mike McQuaid
85a1b79154
service: fix typo. 2023-04-03 08:41:21 +01:00
apainintheneck
39092fa629 service: handle string run cmd
This was not handled at all during deserialization.
The string argument gets turned into an array internally
but we skip that to preserve all args in the @run_params
variable. That means that we have to handle strings when
deserializing too.
2023-03-31 20:33:21 -07:00
apainintheneck
801ee5e474 Address feedback
- style nits
- better comments for tests that are not idempotent
- moved appdir placeholder constant to global.rb
2023-03-22 19:43:49 -07:00
apainintheneck
bc85857e13 service: make serialization non-recursive
This helps with type annotations and makes it easier to
reason about the code as well.
2023-03-21 23:20:49 -07:00
apainintheneck
db8d488db4 service: add info to API json and load from api
As a part of serializing the hash, certain path
placeholders are used for the HOMEBREW_PREFIX and $HOME
directories. Beyond that certain elements need to be
turned back into strings like cron and sockets and symbols
need to be preserved as well.

The run command accepts either an arg or kwargs so it
has to be treated specially here.
2023-03-19 00:09:10 -07:00
DenizUgur
f4ed502350 run_at_load set to true by default 2023-02-23 10:15:06 -08:00
DenizUgur
66fe6d4169 remove onActiveSec 2023-02-15 09:49:08 -08:00
DenizUgur
16b51d2a43 revert type check 2023-02-14 04:24:26 -08:00
DenizUgur
d80d94ab28 comments 2023-02-14 03:55:45 -08:00
DenizUgur
a9d1ee44c8 suggestion for linux compatibility 2023-02-14 02:24:20 -08:00
DenizUgur
ff748ea030 change the default run_at_load value 2023-02-14 01:04:34 -08:00
DenizUgur
7d19ef7070 dedicated 'RunAtLoad' property 2023-02-13 23:19:34 -08:00
Dawid Dziurla
0d0c98de29
Allow defining service run command per platform 2023-01-19 11:36:37 +01:00
Sean Molenaar
d477d1663a
Service: add method to define a root requirement 2022-11-06 13:52:53 +01:00
Sean Molenaar
d70d837687
service: use default.target on Linux 2022-10-07 20:22:12 +02:00
Mike McQuaid
59692b5bf4
service: provide formula accessor.
Use `f` because it's generally recognised as a formula and this
shouldn't be widely needed/used.
2022-08-16 08:35:33 +01:00
Mike McQuaid
6b697470fd
brew style --fix
For https://github.com/Homebrew/brew/pull/13671
2022-08-10 14:19:33 +01:00
GottemHams
dec8577d18 Comply with style checks 2022-07-03 16:24:05 +02:00
GottemHams
dca1d8b52c Added session type to macOS service plist 2022-07-03 15:56:49 +02:00
Mike McQuaid
2d5eab2e1c
brew style --fix 2022-06-30 08:56:21 +01:00
Sean Molenaar
3d5d12e8b9
service: add sockets and keepalive variants 2022-04-12 12:17:27 +02:00
apainintheneck
62de156c40 Added Service#keep_alive? method 2022-03-11 12:42:41 -08:00
NickHackman
12bf07a6ba service: systemd launch_only_once
Set the type of service to oneshot if launch_only_once is provided; otherwise, simple.
2022-02-12 17:26:09 -05:00
NickHackman
9f4949c8c6 service: LaunchOnlyOnce plist test 2022-02-11 14:46:47 -06:00