73 lines
2.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "formula"
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2018-10-27 22:59:08 +05:30
def diy_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`diy` [<options>]
Automatically determine the installation prefix for non-Homebrew software.
Using the output from this command, you can install your own software into
the Cellar and then link it into Homebrew's prefix with `brew link`.
EOS
flag "--name=",
2019-04-30 08:44:35 +01:00
description: "Explicitly set the provided <name> of the package being installed."
2018-10-27 22:59:08 +05:30
flag "--version=",
2019-04-30 08:44:35 +01:00
description: "Explicitly set the provided <version> of the package being installed."
2018-10-27 22:59:08 +05:30
switch :verbose
switch :debug
end
end
def diy
2018-10-27 22:59:08 +05:30
diy_args.parse
path = Pathname.getwd
2018-10-27 22:59:08 +05:30
version = args.version || detect_version(path)
name = args.name || detect_name(path, version)
prefix = HOMEBREW_CELLAR/name/version
2014-03-15 12:55:14 -05:00
if File.file? "CMakeLists.txt"
puts "-DCMAKE_INSTALL_PREFIX=#{prefix}"
elsif File.file? "configure"
puts "--prefix=#{prefix}"
elsif File.file? "meson.build"
puts "-Dprefix=#{prefix}"
else
raise "Couldn't determine build system. You can manually put files into #{prefix}"
end
end
def detect_version(path)
version = path.version.to_s
2016-09-22 20:12:28 +02:00
raise "Couldn't determine version, set it with --version=<version>" if version.empty?
version
end
def detect_name(path, version)
basename = path.basename.to_s
detected_name = basename[/(.*?)-?#{Regexp.escape(version)}/, 1] || basename
canonical_name = Formulary.canonical_name(detected_name)
2017-10-15 02:28:32 +02:00
odie <<~EOS if detected_name != canonical_name
The detected name #{detected_name.inspect} exists in Homebrew as an alias
of #{canonical_name.inspect}. Consider using the canonical name instead:
brew diy --name=#{canonical_name}
To continue using the detected name, pass it explicitly:
brew diy --name=#{detected_name}
2018-06-06 23:34:19 -04:00
EOS
detected_name
end
end