Skip to content

Commit b52a622

Browse files
authored
Merge pull request #828 from sile-typesetter/make-homebrew-formula
chore(build): Add script to generate homebrew formula
2 parents fba931a + ba11544 commit b52a622

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

build-aux/create-homebrew-formula.pl

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/perl
2+
use strict;
3+
use warnings;
4+
use LWP::Simple;
5+
use Crypt::Digest::SHA256 qw(sha256_hex);
6+
use Data::Dumper;
7+
8+
my @build_depends = qw(luarocks pkg-config);
9+
my @homebrew_depends = qw(expat fontconfig harfbuzz icu4c libpng lua openssl@1.1 zlib);
10+
11+
my %not_rocks = map {$_ => 1} qw(lua bit32 compat53);
12+
my %rocks = load_rockspec();
13+
14+
my %rock_url_templates = (
15+
cassowary => "https://github.com/sile-typesetter/cassowary.lua/archive/vVERSION.tar.gz",
16+
cosmo => "https://github.com/mascarenhas/cosmo/archive/vVERSION.tar.gz",
17+
linenoise => "https://github.com/hoelzro/lua-linenoise/archive/VERSION.tar.gz",
18+
lpeg => "http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-VERSION.tar.gz",
19+
lua_cliargs => "https://github.com/amireh/lua_cliargs/archive/vUNSTRIPPEDVERSION.tar.gz",
20+
"lua-zlib" => "https://github.com/brimworks/lua-zlib/archive/vVERSION.tar.gz",
21+
luaexpat => "https://github.com/tomasguisasola/luaexpat/archive/vVERSION.tar.gz",
22+
luaepnf => "https://github.com/siffiejoe/lua-luaepnf/archive/vVERSION.tar.gz",
23+
luafilesystem => "https://github.com/keplerproject/luafilesystem/archive/vVERSIONUNDERSCORE.tar.gz",
24+
luarepl => "https://github.com/hoelzro/lua-repl/archive/VERSION.tar.gz",
25+
luasocket => "https://github.com/diegonehab/luasocket/archive/vVERSION.tar.gz",
26+
luasec => "https://github.com/brunoos/luasec/archive/luasec-VERSION.tar.gz",
27+
penlight => "https://github.com/Tieske/Penlight/archive/VERSION.tar.gz",
28+
vstruct => "https://github.com/ToxicFrog/vstruct/archive/vVERSION.tar.gz",
29+
stdlib => "https://github.com/lua-stdlib/lua-stdlib/archive/release-vVERSION.tar.gz"
30+
);
31+
32+
my $version = `git describe --tags --abbrev=0`;
33+
chomp $version;
34+
$version =~ s/^v//;
35+
print "Writing formula for SILE $version\n";
36+
37+
print "Fetching tarball...\n";
38+
my $github_url = "https://github.com/sile-typesetter/sile/releases/download/v$version/sile-$version.tar.bz2";
39+
my $tarball = get($github_url);
40+
die "Couldn't download tarball - check $github_url !" unless defined $tarball;
41+
my $shasum_tarball = sha256_hex($tarball);
42+
43+
open OUT, ">sile.rb" or die $!;
44+
45+
print OUT <<EOF;
46+
class Sile < Formula
47+
desc "Modern typesetting system inspired by TeX"
48+
homepage "https://www.sile-typesetter.org"
49+
url "$github_url"
50+
sha256 "$shasum_tarball"
51+
52+
head "https://github.com/sile-typesetter/sile.git", :shallow => false
53+
54+
bottle do
55+
sha256 "f2fdd492e9272036fe2d35636d245a1ea05a0beebbb3a0a6b9b4019f36def3f3" => :catalina
56+
sha256 "ca6e60229ac5f6a6a9e0554c5fb1d2aecc3807556ae7b57943602cfc05061b20" => :mojave
57+
sha256 "d3b337dfa79cb2179426687064fb4eb5cc80cbc345ba6acdf0e885abd1360aa9" => :high_sierra
58+
end
59+
60+
if build.head?
61+
depends_on "autoconf" => :build
62+
depends_on "automake" => :build
63+
depends_on "libtool" => :build
64+
end
65+
66+
EOF
67+
68+
print OUT " depends_on \"$_\" => :build\n" for @build_depends;
69+
print OUT " depends_on \"$_\"\n" for @homebrew_depends;
70+
71+
while (my ($rock, $version) = each %rocks) {
72+
if (!exists $rock_url_templates{$rock}) {
73+
print "WARNING: No URL template for rock $rock found in rockspec - should it be in the formula? If not, list it in \%not_rocks";
74+
print "Skipping $rock...\n";
75+
next;
76+
}
77+
my $url = $rock_url_templates{$rock};
78+
$url =~ s/UNSTRIPPEDVERSION/$version/g;
79+
my $underscore = $version; $underscore =~ s/[\.-]/_/g;
80+
$url =~ s/VERSIONUNDERSCORE/$underscore/g;
81+
$version =~ s/-\d+$//;
82+
$url =~ s/VERSION/$version/g;
83+
print("Fetching $rock $version from $url...\n");
84+
my $tarball = get($url);
85+
die "Can't load $url" unless $tarball;
86+
my $shasum = sha256_hex($tarball);
87+
print OUT <<EOF;
88+
resource "$rock" do
89+
url "$url"
90+
sha256 "$shasum"
91+
end
92+
93+
EOF
94+
delete $rock_url_templates{$rock};
95+
}
96+
97+
if (keys %rock_url_templates) {
98+
print "WARNING: the following rocks were in the formula template, but not in the rockspec\n";
99+
print "(Maybe they should be added?)\n";
100+
print join " ", keys %rock_url_templates;
101+
print "\n";
102+
}
103+
104+
while (<DATA>) { print OUT $_ };
105+
close OUT;
106+
print "Written successfully\n";
107+
108+
sub load_rockspec {
109+
open my $rockspec, "sile-dev-1.rockspec" or die "Can't load rockspec: $!";
110+
my %rocks;
111+
while (<$rockspec>) {
112+
if (/"([\w-]+) == ([\d\.-]+)"/) {
113+
$rocks{$1} = $2 unless $not_rocks{$1};
114+
}
115+
}
116+
return %rocks;
117+
}
118+
119+
__DATA__
120+
121+
def install
122+
luapath = libexec/"vendor"
123+
ENV["LUA_PATH"] = "#{luapath}/share/lua/5.3/?.lua;#{luapath}/share/lua/5.3/?/init.lua;#{luapath}/share/lua/5.3/lxp/?.lua"
124+
ENV["LUA_CPATH"] = "#{luapath}/lib/lua/5.3/?.so"
125+
126+
resources.each do |r|
127+
r.stage do
128+
if r.name == "lua-zlib"
129+
# https://github.com/brimworks/lua-zlib/commit/08d6251700965
130+
mv "lua-zlib-1.1-0.rockspec", "lua-zlib-1.2-0.rockspec"
131+
system "luarocks", "make", "#{r.name}-#{r.version}-0.rockspec", "--tree=#{luapath}", "ZLIB_DIR=#{Formula["zlib"].opt_prefix}"
132+
elsif r.name == "luaexpat"
133+
system "luarocks", "build", r.name, "--tree=#{luapath}", "EXPAT_DIR=#{Formula["expat"].opt_prefix}"
134+
elsif r.name == "luasec"
135+
system "luarocks", "build", r.name, "--tree=#{luapath}", "OPENSSL_DIR=#{Formula["openssl@1.1"].opt_prefix}"
136+
else
137+
system "luarocks", "build", r.name, "--tree=#{luapath}"
138+
end
139+
end
140+
end
141+
142+
system "./bootstrap.sh" if build.head?
143+
system "./configure", "--disable-debug",
144+
"--disable-dependency-tracking",
145+
"--disable-silent-rules",
146+
"--with-system-luarocks",
147+
"--with-lua=#{prefix}",
148+
"--prefix=#{prefix}"
149+
system "make"
150+
system "make", "install"
151+
152+
(libexec/"bin").install bin/"sile"
153+
(bin/"sile").write <<~EOS
154+
#!/bin/bash
155+
export LUA_PATH="#{ENV["LUA_PATH"]};;"
156+
export LUA_CPATH="#{ENV["LUA_CPATH"]};;"
157+
"#{libexec}/bin/sile" "$@"
158+
EOS
159+
end
160+
161+
test do
162+
assert_match "SILE #{version.to_s.match(/\d\.\d\.\d/)}", shell_output("#{bin}/sile --version")
163+
end
164+
end

0 commit comments

Comments
 (0)