From 3625bda5ae18224f11e979adbf303d94275a79cd Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Thu, 6 Jun 2019 01:02:11 +0300 Subject: [PATCH] Remove --gemfile option in favor of BUNDLER_GEMFILE env variable --- lib/tapioca/cli.rb | 4 +--- lib/tapioca/gemfile.rb | 15 +++++---------- lib/tapioca/generator.rb | 15 +++------------ spec/support/repo/Gemfile | 1 + spec/support/repo/Gemfile.lock | 26 +++++++++++++++++++++++--- spec/support/repo/bin/tapioca | 29 +++++++++++++++++++++++++++++ spec/tapioca/cli_spec.rb | 31 +++++++++++++------------------ 7 files changed, 75 insertions(+), 46 deletions(-) create mode 100755 spec/support/repo/bin/tapioca diff --git a/lib/tapioca/cli.rb b/lib/tapioca/cli.rb index 82496610b..476f1a5bc 100644 --- a/lib/tapioca/cli.rb +++ b/lib/tapioca/cli.rb @@ -7,7 +7,6 @@ module Tapioca class Cli < Thor class_option :prerequire class_option :postrequire - class_option :gemfile class_option :outdir, default: "sorbet/rbi/gems" desc "generate [gem...]", "generate RBIs from gems" @@ -29,8 +28,7 @@ def generator @generator ||= Generator.new( outdir: options[:outdir], prerequire: options[:prerequire], - postrequire: options[:postrequire], - gemfile: options[:gemfile] + postrequire: options[:postrequire] ) end end diff --git a/lib/tapioca/gemfile.rb b/lib/tapioca/gemfile.rb index b31a858a5..1caf4bd4e 100644 --- a/lib/tapioca/gemfile.rb +++ b/lib/tapioca/gemfile.rb @@ -9,12 +9,10 @@ class Gemfile Spec = T.type_alias(T.any(::Bundler::StubSpecification, ::Gem::Specification)) - sig { params(gemfile: T.nilable(T.any(Pathname, String))).void } - def initialize(gemfile:) - gemfile ||= Bundler.default_gemfile - lockfile = Pathname.new("#{gemfile}.lock") - @gemfile = T.let(File.new(gemfile.to_s), File) - @lockfile = T.let(File.new(lockfile.to_s), File) + sig { void } + def initialize + @gemfile = T.let(File.new(Bundler.default_gemfile), File) + @lockfile = T.let(File.new(Bundler.default_lockfile), File) @dependencies = T.let(nil, T.nilable(T::Array[Gem])) @definition = T.let(nil, T.nilable(Bundler::Definition)) end @@ -59,10 +57,7 @@ def require_bundle(initialize_file, require_file) sig { returns(Bundler::Definition) } def definition - @definition ||= begin - ENV["BUNDLE_GEMFILE"] = gemfile.path - Bundler::Dsl.evaluate(gemfile, lockfile, {}) - end + @definition ||= Bundler::Dsl.evaluate(gemfile, lockfile, {}) end sig { params(spec: Spec).returns(T::Boolean) } diff --git a/lib/tapioca/generator.rb b/lib/tapioca/generator.rb index 27a9b4f4a..cbc4cb4dc 100644 --- a/lib/tapioca/generator.rb +++ b/lib/tapioca/generator.rb @@ -14,22 +14,18 @@ class Generator < ::Thor::Shell::Color attr_reader :prerequire sig { returns(T.nilable(String)) } attr_reader :postrequire - sig { returns(T.nilable(String)) } - attr_reader :gemfile sig do params( outdir: String, prerequire: T.nilable(String), - postrequire: T.nilable(String), - gemfile: T.nilable(String) + postrequire: T.nilable(String) ).void end - def initialize(outdir:, prerequire:, postrequire:, gemfile:) + def initialize(outdir:, prerequire:, postrequire:) @outdir = T.let(Pathname.new(outdir), Pathname) @prerequire = T.let(prerequire, T.nilable(String)) @postrequire = T.let(postrequire, T.nilable(String)) - @gemfile = T.let(gemfile, T.nilable(String)) @bundle = T.let(nil, T.nilable(Gemfile)) @compiler = T.let(nil, T.nilable(Compilers::SymbolTableCompiler)) @existing_rbis = T.let(nil, T.nilable(T::Hash[String, String])) @@ -74,12 +70,7 @@ def sync_rbis_with_gemfile sig { returns(Gemfile) } def bundle - @bundle ||= Gemfile.new(gemfile: gemfile) - end - - sig { returns(String) } - def bundle_path - File.dirname(bundle.gemfile.path) + @bundle ||= Gemfile.new end sig { returns(Compilers::SymbolTableCompiler) } diff --git a/spec/support/repo/Gemfile b/spec/support/repo/Gemfile index 6279bd7b8..26b73896e 100644 --- a/spec/support/repo/Gemfile +++ b/spec/support/repo/Gemfile @@ -5,3 +5,4 @@ source("https://rubygems.org") gem("foo", path: "../gems/foo") gem("bar", path: "../gems/bar") gem("baz", path: "../gems/baz") +gem("tapioca", path: "../../../") diff --git a/spec/support/repo/Gemfile.lock b/spec/support/repo/Gemfile.lock index 03b9ad749..8c709cd82 100644 --- a/spec/support/repo/Gemfile.lock +++ b/spec/support/repo/Gemfile.lock @@ -1,21 +1,40 @@ PATH - remote: spec/support/gems/bar + remote: ../../.. + specs: + tapioca (0.1.0) + pry (>= 0.12.2) + sorbet-runtime + sorbet-static + thor (~> 0.20.3) + zeitwerk (~> 2.1) + +PATH + remote: ../gems/bar specs: bar (0.3.0) PATH - remote: spec/support/gems/baz + remote: ../gems/baz specs: baz (0.0.2) PATH - remote: spec/support/gems/foo + remote: ../gems/foo specs: foo (0.0.1) GEM remote: https://rubygems.org/ specs: + coderay (1.1.2) + method_source (0.9.2) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + sorbet-runtime (0.4.4103) + sorbet-static (0.4.4103-universal-darwin-18) + thor (0.20.3) + zeitwerk (2.1.6) PLATFORMS ruby @@ -24,6 +43,7 @@ DEPENDENCIES bar! baz! foo! + tapioca! BUNDLED WITH 1.17.3 diff --git a/spec/support/repo/bin/tapioca b/spec/support/repo/bin/tapioca new file mode 100755 index 000000000..e13303f95 --- /dev/null +++ b/spec/support/repo/bin/tapioca @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'tapioca' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load(Gem.bin_path("tapioca", "tapioca")) diff --git a/spec/tapioca/cli_spec.rb b/spec/tapioca/cli_spec.rb index 69644b701..cc8a72bd0 100644 --- a/spec/tapioca/cli_spec.rb +++ b/spec/tapioca/cli_spec.rb @@ -12,7 +12,6 @@ def run(command, args = [], flags = {}) flags = { outdir: outdir, - gemfile: (repo_path / "Gemfile").to_s, }.merge(flags).flat_map { |k, v| ["--#{k}", v] } @exec_command = [ @@ -23,8 +22,11 @@ def run(command, args = [], flags = {}) ] IO.popen( + { + "BUNDLE_GEMFILE" => (repo_path / "Gemfile").to_s, + }, @exec_command, - chdir: Pathname.new(__dir__) / ".." / ".." / "exe" + chdir: repo_path / "bin" ).read end @@ -127,7 +129,9 @@ class Foo::Secret Processing 'foo' gem: Compiling foo, this may take a few seconds... Compiled #{outdir}/foo@0.0.1.rbi + OUTPUT + expect(output).to(include(<<~OUTPUT)) Processing 'bar' gem: Compiling bar, this may take a few seconds... Compiled #{outdir}/bar@0.3.0.rbi @@ -149,11 +153,15 @@ class Foo::Secret Processing 'bar' gem: Compiling bar, this may take a few seconds... Compiled #{outdir}/bar@0.3.0.rbi + OUTPUT + expect(output).to(include(<<~OUTPUT)) Processing 'baz' gem: Compiling baz, this may take a few seconds... Compiled #{outdir}/baz@0.0.2.rbi + OUTPUT + expect(output).to(include(<<~OUTPUT)) Processing 'foo' gem: Compiling foo, this may take a few seconds... Compiled #{outdir}/foo@0.0.1.rbi @@ -171,13 +179,7 @@ class Foo::Secret describe("#bundle") do it 'must perform no operations if everything is up-to-date' do - %w{ - foo@0.0.1.rbi - bar@0.3.0.rbi - baz@0.0.2.rbi - }.each do |rbi| - FileUtils.touch("#{outdir}/#{rbi}") - end + run("generate") output = run("bundle") @@ -199,18 +201,11 @@ class Foo::Secret expect(File).to(exist("#{outdir}/foo@0.0.1.rbi")) expect(File).to(exist("#{outdir}/bar@0.3.0.rbi")) expect(File).to(exist("#{outdir}/baz@0.0.2.rbi")) - expect(File).to_not(exist("#{outdir}/outdated@5.0.0.rbi")) end it 'must remove outdated RBIs' do - %w{ - foo@0.0.1.rbi - bar@0.3.0.rbi - baz@0.0.2.rbi - outdated@5.0.0.rbi - }.each do |rbi| - FileUtils.touch("#{outdir}/#{rbi}") - end + run("generate") + FileUtils.touch("#{outdir}/outdated@5.0.0.rbi") output = run("bundle")