From f60751d91c1c2cd8d0f20cc634be303d6665dfa4 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sun, 3 Jun 2018 01:15:28 +0530 Subject: [PATCH 1/2] create: Use Parser to parse args --- Library/Homebrew/dev-cmd/create.rb | 294 +++++++++++++++-------------- 1 file changed, 154 insertions(+), 140 deletions(-) diff --git a/Library/Homebrew/dev-cmd/create.rb b/Library/Homebrew/dev-cmd/create.rb index d9905a39da..a46601b019 100644 --- a/Library/Homebrew/dev-cmd/create.rb +++ b/Library/Homebrew/dev-cmd/create.rb @@ -21,6 +21,7 @@ require "formula" require "missing_formula" +require "cli_parser" require "digest" require "erb" @@ -29,6 +30,19 @@ module Homebrew # Create a formula from a tarball URL def create + Homebrew::CLI::Parser.parse do + switch "--autotools" + switch "--cmake" + switch "--meson" + switch "--no-fetch" + switch "--HEAD" + switch :force + switch :verbose + switch :debug + flag "--set-name=" + flag "--set-version=" + flag "--tag=" + end raise UsageError if ARGV.named.empty? # Ensure that the cache exists so we can fetch the tarball @@ -36,9 +50,9 @@ module Homebrew url = ARGV.named.first # Pull the first (and only) url from ARGV - version = ARGV.next if ARGV.include? "--set-version" - name = ARGV.next if ARGV.include? "--set-name" - tap = ARGV.next if ARGV.include? "--tap" + version = args.set_version + name = args.set_name + tap = args.tap fc = FormulaCreator.new fc.name = name @@ -47,11 +61,11 @@ module Homebrew raise TapUnavailableError, tap unless fc.tap.installed? fc.url = url - fc.mode = if ARGV.include? "--cmake" + fc.mode = if args.cmake? :cmake - elsif ARGV.include? "--autotools" + elsif args.autotools? :autotools - elsif ARGV.include? "--meson" + elsif args.meson? :meson end @@ -64,7 +78,7 @@ module Homebrew # Don't allow blacklisted formula, or names that shadow aliases, # unless --force is specified. - unless ARGV.force? + unless args.force? if reason = MissingFormula.blacklisted_reason(fc.name) raise "#{fc.name} is blacklisted for creation.\n#{reason}\nIf you really want to create this formula use --force." end @@ -89,153 +103,153 @@ module Homebrew gots = $stdin.gets.chomp gots.empty? ? nil : gots end -end -class FormulaCreator - attr_reader :url, :sha256, :desc, :homepage - attr_accessor :name, :version, :tap, :path, :mode + class FormulaCreator + attr_reader :url, :sha256, :desc, :homepage + attr_accessor :name, :version, :tap, :path, :mode - def url=(url) - @url = url - path = Pathname.new(url) - if @name.nil? - case url - when %r{github\.com/(\S+)/(\S+)\.git} - @user = Regexp.last_match(1) - @name = Regexp.last_match(2) - @head = true - @github = true - when %r{github\.com/(\S+)/(\S+)/(archive|releases)/} - @user = Regexp.last_match(1) - @name = Regexp.last_match(2) - @github = true + def url=(url) + @url = url + path = Pathname.new(url) + if @name.nil? + case url + when %r{github\.com/(\S+)/(\S+)\.git} + @user = Regexp.last_match(1) + @name = Regexp.last_match(2) + @head = true + @github = true + when %r{github\.com/(\S+)/(\S+)/(archive|releases)/} + @user = Regexp.last_match(1) + @name = Regexp.last_match(2) + @github = true + else + @name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1] + end + end + update_path + if @version + @version = Version.create(@version) else - @name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1] + @version = Version.detect(url, {}) end end - update_path - if @version - @version = Version.create(@version) - else - @version = Version.detect(url, {}) + + def update_path + return if @name.nil? || @tap.nil? + @path = Formulary.path "#{@tap}/#{@name}" end - end - def update_path - return if @name.nil? || @tap.nil? - @path = Formulary.path "#{@tap}/#{@name}" - end + def fetch? + !Homebrew.args.no_fetch? + end - def fetch? - !ARGV.include?("--no-fetch") - end + def head? + @head || Homebrew.args.head? + end - def head? - @head || ARGV.build_head? - end + def generate! + raise "#{path} already exists" if path.exist? - def generate! - raise "#{path} already exists" if path.exist? - - if version.nil? || version.null? - opoo "Version cannot be determined from URL." - puts "You'll need to add an explicit 'version' to the formula." - elsif fetch? - unless head? - r = Resource.new - r.url(url) - r.version(version) - r.owner = self - @sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy - end - - if @user && @name - begin - metadata = GitHub.repository(@user, @name) - @desc = metadata["description"] - @homepage = metadata["homepage"] - rescue GitHub::HTTPNotFoundError - # If there was no repository found assume the network connection is at - # fault rather than the input URL. - nil + if version.nil? || version.null? + opoo "Version cannot be determined from URL." + puts "You'll need to add an explicit 'version' to the formula." + elsif fetch? + unless head? + r = Resource.new + r.url(url) + r.version(version) + r.owner = self + @sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy end - end - end - path.write ERB.new(template, nil, ">").result(binding) - end - - def template - <<~EOS - # Documentation: https://docs.brew.sh/Formula-Cookbook - # http://www.rubydoc.info/github/Homebrew/brew/master/Formula - # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! - class #{Formulary.class_s(name)} < Formula - desc "#{desc}" - homepage "#{homepage}" - <% if head? %> - head "#{url}" - <% else %> - url "#{url}" - <% unless version.nil? or version.detected_from_url? %> - version "#{version}" - <% end %> - sha256 "#{sha256}" - <% end %> - <% if mode == :cmake %> - depends_on "cmake" => :build - <% elsif mode == :meson %> - depends_on "meson-internal" => :build - depends_on "ninja" => :build - depends_on "python" => :build - <% elsif mode.nil? %> - # depends_on "cmake" => :build - <% end %> - - def install - # ENV.deparallelize # if your formula fails when building in parallel - <% if mode == :cmake %> - system "cmake", ".", *std_cmake_args - <% elsif mode == :autotools %> - # Remove unrecognized options if warned by configure - system "./configure", "--disable-debug", - "--disable-dependency-tracking", - "--disable-silent-rules", - "--prefix=\#{prefix}" - <% elsif mode == :meson %> - ENV.refurbish_args - - mkdir "build" do - system "meson", "--prefix=\#{prefix}", ".." - system "ninja" - system "ninja", "install" + if @user && @name + begin + metadata = GitHub.repository(@user, @name) + @desc = metadata["description"] + @homepage = metadata["homepage"] + rescue GitHub::HTTPNotFoundError + # If there was no repository found assume the network connection is at + # fault rather than the input URL. + nil end - <% else %> - # Remove unrecognized options if warned by configure - system "./configure", "--disable-debug", - "--disable-dependency-tracking", - "--disable-silent-rules", - "--prefix=\#{prefix}" - # system "cmake", ".", *std_cmake_args - <% end %> - <% if mode != :meson %> - system "make", "install" # if this fails, try separate make/make install steps - <% 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 - EOS + + path.write ERB.new(template, nil, ">").result(binding) + end + + def template + <<~EOS + # Documentation: https://docs.brew.sh/Formula-Cookbook + # http://www.rubydoc.info/github/Homebrew/brew/master/Formula + # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! + class #{Formulary.class_s(name)} < Formula + desc "#{desc}" + homepage "#{homepage}" + <% if head? %> + head "#{url}" + <% else %> + url "#{url}" + <% unless version.nil? or version.detected_from_url? %> + version "#{version}" + <% end %> + sha256 "#{sha256}" + <% end %> + <% if mode == :cmake %> + depends_on "cmake" => :build + <% elsif mode == :meson %> + depends_on "meson-internal" => :build + depends_on "ninja" => :build + depends_on "python" => :build + <% elsif mode.nil? %> + # depends_on "cmake" => :build + <% end %> + + def install + # ENV.deparallelize # if your formula fails when building in parallel + <% if mode == :cmake %> + system "cmake", ".", *std_cmake_args + <% elsif mode == :autotools %> + # Remove unrecognized options if warned by configure + system "./configure", "--disable-debug", + "--disable-dependency-tracking", + "--disable-silent-rules", + "--prefix=\#{prefix}" + <% elsif mode == :meson %> + ENV.refurbish_args + + mkdir "build" do + system "meson", "--prefix=\#{prefix}", ".." + system "ninja" + system "ninja", "install" + end + <% else %> + # Remove unrecognized options if warned by configure + system "./configure", "--disable-debug", + "--disable-dependency-tracking", + "--disable-silent-rules", + "--prefix=\#{prefix}" + # system "cmake", ".", *std_cmake_args + <% end %> + <% if mode != :meson %> + system "make", "install" # if this fails, try separate make/make install steps + <% 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 + EOS + end end end From e05f72bc5232a7fd880da28050e59e45123ef0d7 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sun, 3 Jun 2018 15:28:48 +0530 Subject: [PATCH 2/2] create: Move FormulaCreator to formula_creator.rb --- Library/Homebrew/dev-cmd/create.rb | 154 +--------------------------- Library/Homebrew/formula_creator.rb | 153 +++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 152 deletions(-) create mode 100644 Library/Homebrew/formula_creator.rb diff --git a/Library/Homebrew/dev-cmd/create.rb b/Library/Homebrew/dev-cmd/create.rb index a46601b019..33ba1821e9 100644 --- a/Library/Homebrew/dev-cmd/create.rb +++ b/Library/Homebrew/dev-cmd/create.rb @@ -20,10 +20,9 @@ #: the specified tap. require "formula" +require "formula_creator" require "missing_formula" require "cli_parser" -require "digest" -require "erb" module Homebrew module_function @@ -41,7 +40,7 @@ module Homebrew switch :debug flag "--set-name=" flag "--set-version=" - flag "--tag=" + flag "--tap=" end raise UsageError if ARGV.named.empty? @@ -103,153 +102,4 @@ module Homebrew gots = $stdin.gets.chomp gots.empty? ? nil : gots end - - class FormulaCreator - attr_reader :url, :sha256, :desc, :homepage - attr_accessor :name, :version, :tap, :path, :mode - - def url=(url) - @url = url - path = Pathname.new(url) - if @name.nil? - case url - when %r{github\.com/(\S+)/(\S+)\.git} - @user = Regexp.last_match(1) - @name = Regexp.last_match(2) - @head = true - @github = true - when %r{github\.com/(\S+)/(\S+)/(archive|releases)/} - @user = Regexp.last_match(1) - @name = Regexp.last_match(2) - @github = true - else - @name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1] - end - end - update_path - if @version - @version = Version.create(@version) - else - @version = Version.detect(url, {}) - end - end - - def update_path - return if @name.nil? || @tap.nil? - @path = Formulary.path "#{@tap}/#{@name}" - end - - def fetch? - !Homebrew.args.no_fetch? - end - - def head? - @head || Homebrew.args.head? - end - - def generate! - raise "#{path} already exists" if path.exist? - - if version.nil? || version.null? - opoo "Version cannot be determined from URL." - puts "You'll need to add an explicit 'version' to the formula." - elsif fetch? - unless head? - r = Resource.new - r.url(url) - r.version(version) - r.owner = self - @sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy - end - - if @user && @name - begin - metadata = GitHub.repository(@user, @name) - @desc = metadata["description"] - @homepage = metadata["homepage"] - rescue GitHub::HTTPNotFoundError - # If there was no repository found assume the network connection is at - # fault rather than the input URL. - nil - end - end - end - - path.write ERB.new(template, nil, ">").result(binding) - end - - def template - <<~EOS - # Documentation: https://docs.brew.sh/Formula-Cookbook - # http://www.rubydoc.info/github/Homebrew/brew/master/Formula - # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! - class #{Formulary.class_s(name)} < Formula - desc "#{desc}" - homepage "#{homepage}" - <% if head? %> - head "#{url}" - <% else %> - url "#{url}" - <% unless version.nil? or version.detected_from_url? %> - version "#{version}" - <% end %> - sha256 "#{sha256}" - <% end %> - <% if mode == :cmake %> - depends_on "cmake" => :build - <% elsif mode == :meson %> - depends_on "meson-internal" => :build - depends_on "ninja" => :build - depends_on "python" => :build - <% elsif mode.nil? %> - # depends_on "cmake" => :build - <% end %> - - def install - # ENV.deparallelize # if your formula fails when building in parallel - <% if mode == :cmake %> - system "cmake", ".", *std_cmake_args - <% elsif mode == :autotools %> - # Remove unrecognized options if warned by configure - system "./configure", "--disable-debug", - "--disable-dependency-tracking", - "--disable-silent-rules", - "--prefix=\#{prefix}" - <% elsif mode == :meson %> - ENV.refurbish_args - - mkdir "build" do - system "meson", "--prefix=\#{prefix}", ".." - system "ninja" - system "ninja", "install" - end - <% else %> - # Remove unrecognized options if warned by configure - system "./configure", "--disable-debug", - "--disable-dependency-tracking", - "--disable-silent-rules", - "--prefix=\#{prefix}" - # system "cmake", ".", *std_cmake_args - <% end %> - <% if mode != :meson %> - system "make", "install" # if this fails, try separate make/make install steps - <% 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 - EOS - end - end end diff --git a/Library/Homebrew/formula_creator.rb b/Library/Homebrew/formula_creator.rb new file mode 100644 index 0000000000..d3d7494949 --- /dev/null +++ b/Library/Homebrew/formula_creator.rb @@ -0,0 +1,153 @@ +require "digest" +require "erb" + +module Homebrew + class FormulaCreator + attr_reader :url, :sha256, :desc, :homepage + attr_accessor :name, :version, :tap, :path, :mode + + def url=(url) + @url = url + path = Pathname.new(url) + if @name.nil? + case url + when %r{github\.com/(\S+)/(\S+)\.git} + @user = Regexp.last_match(1) + @name = Regexp.last_match(2) + @head = true + @github = true + when %r{github\.com/(\S+)/(\S+)/(archive|releases)/} + @user = Regexp.last_match(1) + @name = Regexp.last_match(2) + @github = true + else + @name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1] + end + end + update_path + if @version + @version = Version.create(@version) + else + @version = Version.detect(url, {}) + end + end + + def update_path + return if @name.nil? || @tap.nil? + @path = Formulary.path "#{@tap}/#{@name}" + end + + def fetch? + !Homebrew.args.no_fetch? + end + + def head? + @head || Homebrew.args.HEAD? + end + + def generate! + raise "#{path} already exists" if path.exist? + + if version.nil? || version.null? + opoo "Version cannot be determined from URL." + puts "You'll need to add an explicit 'version' to the formula." + elsif fetch? + unless head? + r = Resource.new + r.url(url) + r.version(version) + r.owner = self + @sha256 = r.fetch.sha256 if r.download_strategy == CurlDownloadStrategy + end + + if @user && @name + begin + metadata = GitHub.repository(@user, @name) + @desc = metadata["description"] + @homepage = metadata["homepage"] + rescue GitHub::HTTPNotFoundError + # If there was no repository found assume the network connection is at + # fault rather than the input URL. + nil + end + end + end + + path.write ERB.new(template, nil, ">").result(binding) + end + + def template + <<~EOS + # Documentation: https://docs.brew.sh/Formula-Cookbook + # http://www.rubydoc.info/github/Homebrew/brew/master/Formula + # PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! + class #{Formulary.class_s(name)} < Formula + desc "#{desc}" + homepage "#{homepage}" + <% if head? %> + head "#{url}" + <% else %> + url "#{url}" + <% unless version.nil? or version.detected_from_url? %> + version "#{version}" + <% end %> + sha256 "#{sha256}" + <% end %> + <% if mode == :cmake %> + depends_on "cmake" => :build + <% elsif mode == :meson %> + depends_on "meson-internal" => :build + depends_on "ninja" => :build + depends_on "python" => :build + <% elsif mode.nil? %> + # depends_on "cmake" => :build + <% end %> + + def install + # ENV.deparallelize # if your formula fails when building in parallel + <% if mode == :cmake %> + system "cmake", ".", *std_cmake_args + <% elsif mode == :autotools %> + # Remove unrecognized options if warned by configure + system "./configure", "--disable-debug", + "--disable-dependency-tracking", + "--disable-silent-rules", + "--prefix=\#{prefix}" + <% elsif mode == :meson %> + ENV.refurbish_args + + mkdir "build" do + system "meson", "--prefix=\#{prefix}", ".." + system "ninja" + system "ninja", "install" + end + <% else %> + # Remove unrecognized options if warned by configure + system "./configure", "--disable-debug", + "--disable-dependency-tracking", + "--disable-silent-rules", + "--prefix=\#{prefix}" + # system "cmake", ".", *std_cmake_args + <% end %> + <% if mode != :meson %> + system "make", "install" # if this fails, try separate make/make install steps + <% 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 + EOS + end + end +end