forked from Homebrew/brew
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformula_creator.rb
More file actions
264 lines (238 loc) · 9.56 KB
/
Copy pathformula_creator.rb
File metadata and controls
264 lines (238 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "digest"
require "erb"
module Homebrew
# Class for generating a formula from a template.
class FormulaCreator
attr_accessor :name
sig {
params(name: T.nilable(String), version: T.nilable(String), tap: T.nilable(String), url: String,
mode: T.nilable(Symbol), license: T.nilable(String), fetch: T::Boolean, head: T::Boolean).void
}
def initialize(name, version, tap:, url:, mode:, license:, fetch:, head:)
@name = name
@version = Version.new(version) if version
@tap = Tap.fetch(tap || "homebrew/core")
@url = url
@mode = mode
@license = license
@fetch = fetch
@head = head
end
sig { void }
def verify
raise TapUnavailableError, @tap.name unless @tap.installed?
end
sig { params(url: String).returns(T.nilable(String)) }
def self.name_from_url(url)
stem = Pathname.new(url).stem
# special cases first
if stem.start_with? "index.cgi"
# gitweb URLs e.g. http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary
stem.rpartition("=").last
elsif url =~ %r{github\.com/\S+/(\S+)/(archive|releases)/}
# e.g. https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz
Regexp.last_match(1)
else
# e.g. http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz
pathver = Version.parse(stem).to_s
stem.sub(/[-_.]?#{Regexp.escape(pathver)}$/, "")
end
end
sig { void }
def parse_url
@name = FormulaCreator.name_from_url(@url) if @name.blank?
odebug "name_from_url: #{@name}"
@version = Version.detect(@url) if @version.nil?
case @url
when %r{github\.com/(\S+)/(\S+)\.git}
@head = true
user = Regexp.last_match(1)
repo = Regexp.last_match(2)
@github = GitHub.repository(user, repo) if @fetch
when %r{github\.com/(\S+)/(\S+)/(archive|releases)/}
user = Regexp.last_match(1)
repo = Regexp.last_match(2)
@github = GitHub.repository(user, repo) if @fetch
end
end
sig { returns(Pathname) }
def write_formula!
raise ArgumentError, "name is blank!" if @name.blank?
raise ArgumentError, "tap is blank!" if @tap.blank?
path = @tap.new_formula_path(@name)
raise "#{path} already exists" if path.exist?
if @version.nil? || @version.null?
odie "Version cannot be determined from URL. Explicitly set the version with `--set-version` instead."
end
if @fetch
unless @head
r = Resource.new
r.url(@url)
r.owner = self
filepath = r.fetch
html_doctype_prefix = "<!doctype html"
# Number of bytes to read from file start to ensure it is not HTML.
# HTML may start with arbitrary number of whitespace lines.
bytes_to_read = 100
if File.read(filepath, bytes_to_read).strip.downcase.start_with?(html_doctype_prefix)
raise "Downloaded URL is not archive"
end
@sha256 = filepath.sha256
end
if @github
@desc = @github["description"]
@homepage = @github["homepage"].presence || "https://github.com/#{@github["full_name"]}"
@license = @github["license"]["spdx_id"] if @github["license"]
end
end
path.dirname.mkpath
path.write ERB.new(template, trim_mode: ">").result(binding)
path
end
sig { params(name: String).returns(String) }
def latest_versioned_formula(name)
name_prefix = "#{name}@"
CoreTap.instance.formula_names
.select { |f| f.start_with?(name_prefix) }
.max_by { |v| Gem::Version.new(v.sub(name_prefix, "")) } || "python"
end
sig { returns(String) }
def template
<<~ERB
# Documentation: https://docs.brew.sh/Formula-Cookbook
# https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class #{Formulary.class_s(name)} < Formula
<% if @mode == :python %>
include Language::Python::Virtualenv
<% end %>
desc "#{@desc}"
homepage "#{@homepage}"
<% unless @head %>
url "#{@url}"
<% unless @version.detected_from_url? %>
version "#{@version}"
<% end %>
sha256 "#{@sha256}"
<% end %>
license "#{@license}"
<% if @head %>
head "#{@url}"
<% end %>
<% if @mode == :cabal %>
depends_on "cabal-install" => :build
depends_on "ghc" => :build
<% elsif @mode == :cmake %>
depends_on "cmake" => :build
<% elsif @mode == :crystal %>
depends_on "crystal" => :build
<% elsif @mode == :go %>
depends_on "go" => :build
<% elsif @mode == :meson %>
depends_on "meson" => :build
depends_on "ninja" => :build
<% elsif @mode == :node %>
depends_on "node"
<% elsif @mode == :perl %>
uses_from_macos "perl"
<% elsif @mode == :python %>
depends_on "#{latest_versioned_formula("python")}"
<% elsif @mode == :ruby %>
uses_from_macos "ruby"
<% elsif @mode == :rust %>
depends_on "rust" => :build
<% elsif @mode == :zig %>
depends_on "zig" => :build
<% elsif @mode.nil? %>
# depends_on "cmake" => :build
<% end %>
<% if @mode == :perl || :python || :ruby %>
# Additional dependency
# resource "" do
# url ""
# sha256 ""
# end
<% end %>
def install
<% if @mode == :cabal %>
system "cabal", "v2-update"
system "cabal", "v2-install", *std_cabal_v2_args
<% elsif @mode == :cmake %>
system "cmake", "-S", ".", "-B", "build", *std_cmake_args
system "cmake", "--build", "build"
system "cmake", "--install", "build"
<% elsif @mode == :autotools %>
# Remove unrecognized options if they cause configure to fail
# https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
system "./configure", "--disable-silent-rules", *std_configure_args
system "make", "install" # if this fails, try separate make/make install steps
<% elsif @mode == :crystal %>
system "shards", "build", "--release"
bin.install "bin/#{name}"
<% elsif @mode == :go %>
system "go", "build", *std_go_args(ldflags: "-s -w")
<% elsif @mode == :meson %>
system "meson", "setup", "build", *std_meson_args
system "meson", "compile", "-C", "build", "--verbose"
system "meson", "install", "-C", "build"
<% elsif @mode == :node %>
system "npm", "install", *std_npm_args
bin.install_symlink Dir["\#{libexec}/bin/*"]
<% elsif @mode == :perl %>
ENV.prepend_create_path "PERL5LIB", libexec/"lib/perl5"
ENV.prepend_path "PERL5LIB", libexec/"lib"
# Stage additional dependency (`Makefile.PL` style).
# resource("").stage do
# system "perl", "Makefile.PL", "INSTALL_BASE=\#{libexec}"
# system "make"
# system "make", "install"
# end
# Stage additional dependency (`Build.PL` style).
# resource("").stage do
# system "perl", "Build.PL", "--install_base", libexec
# system "./Build"
# system "./Build", "install"
# end
bin.install name
bin.env_script_all_files(libexec/"bin", PERL5LIB: ENV["PERL5LIB"])
<% elsif @mode == :python %>
virtualenv_install_with_resources
<% elsif @mode == :ruby %>
ENV["BUNDLE_VERSION"] = "system" # Avoid installing Bundler into the keg
ENV["GEM_HOME"] = libexec
system "bundle", "config", "set", "without", "development", "test"
system "bundle", "install"
system "gem", "build", "\#{name}.gemspec"
system "gem", "install", "\#{name}-\#{version}.gem"
bin.install libexec/"bin/\#{name}"
bin.env_script_all_files(libexec/"bin", GEM_HOME: ENV["GEM_HOME"])
<% elsif @mode == :rust %>
system "cargo", "install", *std_cargo_args
<% elsif @mode == :zig %>
system "zig", "build", *std_zig_args
<% else %>
# Remove unrecognized options if they cause configure to fail
# https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
system "./configure", "--disable-silent-rules", *std_configure_args
# system "cmake", "-S", ".", "-B", "build", *std_cmake_args
<% end %>
end
test do
# `test do` will create, run in and delete a temporary directory.
#
# This test will fail and we won't accept that! For Homebrew/homebrew-core
# this will need to be a test that verifies the functionality of the
# software. Run the test with `brew test #{name}`. Options passed
# to `brew install` such as `--HEAD` also need to be provided to `brew test`.
#
# The installed folder is not in the path, so use the entire path to any
# executables being tested: `system bin/"program", "do", "something"`.
system "false"
end
end
ERB
end
end
end