Skip to content

Commit

Permalink
Merge pull request #274 from Shopify/uk-add-silent-option
Browse files Browse the repository at this point in the history
  • Loading branch information
paracycle authored Mar 30, 2021
2 parents 1068e3f + 9cab6a0 commit 7257f87
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/tapioca/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ def todo
type: :boolean,
default: false,
desc: "Verifies RBIs are up-to-date"
option :quiet,
aliases: ["-q"],
type: :boolean,
desc: "Supresses file creation output"
def dsl(*constants)
Tapioca.silence_warnings do
generator.build_dsl(constants, should_verify: options[:verify])
generator.build_dsl(constants, should_verify: options[:verify], quiet: options[:quiet])
end
end

Expand Down
24 changes: 18 additions & 6 deletions lib/tapioca/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ def build_todos
params(
requested_constants: T::Array[String],
should_verify: T::Boolean,
quiet: T::Boolean
).void
end
def build_dsl(requested_constants, should_verify: false)
def build_dsl(requested_constants, should_verify: false, quiet: false)
load_application(eager_load: requested_constants.empty?)
load_dsl_generators

Expand All @@ -145,7 +146,12 @@ def build_dsl(requested_constants, should_verify: false)
)

compiler.run do |constant, contents|
filename = compile_dsl_rbi(constant, contents, outpath: Pathname.new(outpath))
filename = compile_dsl_rbi(
constant,
contents,
outpath: Pathname.new(outpath),
quiet: should_verify || quiet
)
rbi_files_to_purge.delete(filename) if filename
end
say("")
Expand Down Expand Up @@ -506,8 +512,11 @@ def compile_gem_rbi(gem)
end
end

sig { params(constant: Module, contents: String, outpath: Pathname).returns(T.nilable(Pathname)) }
def compile_dsl_rbi(constant, contents, outpath: config.outpath)
sig do
params(constant: Module, contents: String, outpath: Pathname, quiet: T::Boolean)
.returns(T.nilable(Pathname))
end
def compile_dsl_rbi(constant, contents, outpath: config.outpath, quiet: false)
return if contents.nil?

constant_name = Module.instance_method(:name).bind(constant).call
Expand All @@ -523,8 +532,11 @@ def compile_dsl_rbi(constant, contents, outpath: config.outpath)

FileUtils.mkdir_p(File.dirname(filename))
File.write(filename, out)
say("Wrote: ", [:green])
say(filename)

unless quiet
say("Wrote: ", [:green])
say(filename)
end

filename
end
Expand Down
20 changes: 20 additions & 0 deletions spec/tapioca/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,26 @@ def body=(body); end
CONTENTS
end

it 'can generates RBI files quietly' do
output = execute("dsl", "--quiet")

assert_equal(<<~OUTPUT, output)
Loading Rails application... Done
Loading DSL generator classes... Done
Compiling DSL RBI files...
Done
All operations performed in working directory.
Please review changes and commit them.
OUTPUT

assert_path_exists("#{outdir}/baz/role.rbi")
assert_path_exists("#{outdir}/post.rbi")
assert_path_exists("#{outdir}/namespace/comment.rbi")
refute_path_exists("#{outdir}/user.rbi")
end

it 'removes stale RBI files' do
FileUtils.mkdir_p("#{outdir}/to_be_deleted")
FileUtils.touch("#{outdir}/to_be_deleted/foo.rbi")
Expand Down

0 comments on commit 7257f87

Please sign in to comment.