Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

y2j 1.1.1 (new formula) #10927

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Formula/y2j.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Y2j < Formula
include Language::Python::Virtualenv
desc "command-line tool for converting between YAML and JSON and vice versa."
homepage "https://github.com/wildducktheories/y2j"
url "https://github.com/wildducktheories/y2j/archive/v1.1.1.tar.gz"
sha256 "e11ac6886937b3c9784f61d3c77d00d9b4dbf3fa10ec6ddc6f847b68196d9f5d"
head "https://github.com/wildducktheories/y2j.git"

depends_on "jq" => :run
depends_on "base64" => :run
depends_on :python => :run
depends_on "docker" => :optional

resource "PyYAML" do
url "https://files.pythonhosted.org/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz"
sha256 "592766c6303207a20efc445587778322d7f73b161bd994f227adaa341ba212ab"
end

def install
py_version = Language::Python.major_minor_version "python"
venv_site_packages_path = libexec/"lib/python#{py_version}/site-packages"
ENV["PYTHONPATH"] = venv_site_packages_path
venv = virtualenv_create(libexec, "python")

python_resources = resources.map(&:name).to_set
python_resources.each do |r|
venv.pip_install resource(r)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can venv.pip_install resources instead.


# Force our venv python for use with y2j
inreplace buildpath/"y2j.sh", /^#!.*bash/,
"#!/usr/bin/env bash\nexport PYTHONPATH=#{venv_site_packages_path}:$PYTHONPATH"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do so in the formula: ENV["PYTHONPATH"] = ….

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this only affect the runtime of the brew install y2j ruby script that is executed? What do you mean by "in the formula"? The need here was to ensure that the y2j.sh script environment uses the homebrew managed python virtualenv. So my thought was: just force a shebang line & PYTHONPATH variable set as a shim inside the y2j.sh script to set it for this tool.


install_script = `./y2j.sh installer #{prefix.to_s}`

File.open("install_y2j.sh", "w") do |f|
f.write(install_script)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use (buildpath/"install_y2j.sh").write install_script.

chmod 0755, "./install_y2j.sh"
system "./install_y2j.sh"

bin.install "y2j.sh", "y2j", "j2y", "yq"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use #{bin} for the install_script so you can remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand what you mean on this one. It looked like #{bin} just was an expansion of #{prefix}/bin which would be the formula's bin directory.

The problem here was that "install_y2j.sh" placed these as symlinks to the main y2j.sh script which lives in the upstream project's source repo as checked out by the install_y2j.sh script. As such, the real bash script executable in this case is y2j.sh which is going to be located under something like: /usr/local/Cellar/y2j/<version e.g.: 1.2.3>/y2j.sh

It was my understanding that the usage of bin.install was meant for this use case. That is: placing & setting the execute bit for executables such that they are placed into the formula's bin dir (e.g.: /usr/local/Cellar/y2j/<version e.g.: 1.2.3>/bin/).

end

test do
yaml_test_input = <<-EOS.undent
---
foo: bar
EOS

expected_output = "{\n \"foo\": \"bar\"\n}"

require "open3"
Open3.popen3("#{bin}/y2j") do |stdin, stdout, _|
stdin.write(yaml_test_input)
stdin.close
assert_equal expected_output, stdout.read
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pipe_output instead:

assert_equal "{\n    \"foo\": \"bar\"\n}", pipe_output("#{bin}/y2j", yaml_test_input)

end
end