DSL Code to Retrieve a Formula Resource's Staged Path Inside a Block?


(Bryce Glover) #1

     After working on an OpenJFX cask to address my own needs and an earlier thread here on Discourse and being directed to change this submission to concern a formula in Homebrew/homebrew-core instead, I got a bit stuck writing some of the code for said new formula submission. So far, my ‘def install’ block reads a little something like this:

⋮

  def install
    resources.each do |resource|
      resource.stage do |resource|
        system "mvn", "install:install-file", "-Dfile=#{resource.<something goes here…>}", …
      end
    end
  end

⋮

The problem lies, naturally enough, in that I don’t know what to put in place of the ‘<something goes here…>’ part in order to fetch the staging path for the resource currently being processed in each invocation of this block of Ruby code. Looking at the API documentation for the Resource class, it has a cached_download’ method I could call, but that doesn’t strike me as being quite what I’d want here. One can do something similar with virtualenv-using Python packages installable using pip by calling virtualenv_install_with_resources, but that doesn’t exactly parallel what I want, either, I think, as it involves installing dependencies into a ‘vendor’ folder inside a formula’s prefix, rather than installing directly into the latter. What’s the recommended way to do this? Alternatively, is there a better way to?


(Bryce Glover) #2

     Additionally, another argument that I’ll need to pass to ‘mvn install:install-file’ is ‘-DartifactId=<resource name>,’ but it isn’t clear how I’d pass that in. The Resource class is also described by its documentation as having a name attribute, but its description reads as follows:

Formula name must be set after the DSL, as we have no access to the formula name before initialization of the formula.

Wouldn’t Resource#name have more to do with a resource’s name than with its containing formula’s…? This ‘explanation’ doesn’t really match that expectation/intuition.


(Sean Molenaar) #3

Here’s an example of a formula using resources in the install step:


(Bryce Glover) #4

     OK, thanks; so, that gets me a resource’s name, as I would have expected had I not gotten confused by the (now obviously incorrect) documentation. Now, what about its staging path? I can manipulate this path’s host directory by having Resource.stage pass a staging object into the block I pass it like so:

⋮

  def install
    resources.each do |resource|
      resource.stage do |resource, staging|
        # Do something with `staging`
      end
    end
  end

⋮

but the best way I can come up with to get the staged file’s full path is to construct it halfway manually:

⋮
        # Do something with `staging/"#{resource.name}.jar"`…
⋮

I’m surprised there isn’t a method on Resource to obtain its path programmatically, though perhaps I’m missing something…? (Eh, I’ll submit something and ask for review comments.)