Skip to content

Commit

Permalink
Remove --gemfile option in favor of BUNDLER_GEMFILE env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
paracycle committed Jun 5, 2019
1 parent e864294 commit 3625bda
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 46 deletions.
4 changes: 1 addition & 3 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
15 changes: 5 additions & 10 deletions lib/tapioca/gemfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand Down
15 changes: 3 additions & 12 deletions lib/tapioca/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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) }
Expand Down
1 change: 1 addition & 0 deletions spec/support/repo/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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: "../../../")
26 changes: 23 additions & 3 deletions spec/support/repo/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,6 +43,7 @@ DEPENDENCIES
bar!
baz!
foo!
tapioca!

BUNDLED WITH
1.17.3
29 changes: 29 additions & 0 deletions spec/support/repo/bin/tapioca
Original file line number Diff line number Diff line change
@@ -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"))
31 changes: 13 additions & 18 deletions spec/tapioca/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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

Expand Down Expand Up @@ -127,7 +129,9 @@ class Foo::Secret
Processing 'foo' gem:
Compiling foo, this may take a few seconds...
Compiled #{outdir}/[email protected]
OUTPUT

expect(output).to(include(<<~OUTPUT))
Processing 'bar' gem:
Compiling bar, this may take a few seconds...
Compiled #{outdir}/[email protected]
Expand All @@ -149,11 +153,15 @@ class Foo::Secret
Processing 'bar' gem:
Compiling bar, this may take a few seconds...
Compiled #{outdir}/[email protected]
OUTPUT

expect(output).to(include(<<~OUTPUT))
Processing 'baz' gem:
Compiling baz, this may take a few seconds...
Compiled #{outdir}/[email protected]
OUTPUT

expect(output).to(include(<<~OUTPUT))
Processing 'foo' gem:
Compiling foo, this may take a few seconds...
Compiled #{outdir}/[email protected]
Expand All @@ -171,13 +179,7 @@ class Foo::Secret

describe("#bundle") do
it 'must perform no operations if everything is up-to-date' do
%w{
[email protected]
[email protected]
[email protected]
}.each do |rbi|
FileUtils.touch("#{outdir}/#{rbi}")
end
run("generate")

output = run("bundle")

Expand All @@ -199,18 +201,11 @@ class Foo::Secret
expect(File).to(exist("#{outdir}/[email protected]"))
expect(File).to(exist("#{outdir}/[email protected]"))
expect(File).to(exist("#{outdir}/[email protected]"))
expect(File).to_not(exist("#{outdir}/[email protected]"))
end

it 'must remove outdated RBIs' do
%w{
[email protected]
[email protected]
[email protected]
[email protected]
}.each do |rbi|
FileUtils.touch("#{outdir}/#{rbi}")
end
run("generate")
FileUtils.touch("#{outdir}/[email protected]")

output = run("bundle")

Expand Down

0 comments on commit 3625bda

Please sign in to comment.