Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support arguments passed to the postprocessor #19

Merged
merged 1 commit into from Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions owlbot-postprocessor/Dockerfile
Expand Up @@ -13,9 +13,9 @@
# limitations under the License.

FROM ruby:3.0-buster
RUN mkdir /ruby-postprocessor
RUN mkdir /ruby-postprocessor && gem install toys-core
WORKDIR /ruby-postprocessor
COPY lib/*.rb /ruby-postprocessor/
COPY *.md /ruby-postprocessor/
ENTRYPOINT [ "ruby" ]
CMD [ "/ruby-postprocessor/entrypoint.rb" ]
ENTRYPOINT ["ruby", "/ruby-postprocessor/entrypoint.rb"]
CMD []
19 changes: 18 additions & 1 deletion owlbot-postprocessor/lib/entrypoint.rb
Expand Up @@ -14,6 +14,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require "toys-core"
require "logger"
require_relative "owlbot"

OwlBot.entrypoint quiet_level: ::ENV["QUIET_LEVEL"].to_i
cli = ::Toys::CLI.new base_level: ::Logger::INFO

cli.add_config_block do
desc "Ruby postprocessor for OwlBot"

flag :gem_name, "--gem=NAME"

def run
OwlBot.entrypoint gem_name: gem_name, logger: logger
rescue OwlBot::Error => e
logger.error e.message
exit 1
end
end

exit cli.run ::ARGV
80 changes: 55 additions & 25 deletions owlbot-postprocessor/lib/owlbot.rb
Expand Up @@ -59,6 +59,12 @@ def call src, dest, path
end
end

##
# Exception thrown in the case of fatal preprocessor errors
#
class Error < ::StandardError
end

class << self
##
# The full path to the root of the repository clone
Expand Down Expand Up @@ -138,14 +144,11 @@ class << self
attr_reader :content_modifiers

##
# Quietness of logging.
# The logger in use
#
# * `0` (the default) indicates normal logging, displaying for each file
# the move decision made and any modifications done
# * `1` indicates quiet mode, displaying only warnings and fatal exceptions
# * `2` indicates extra-quiet mode, displaying only fatal exceptions
# @return [Logger]
#
attr_accessor :quiet_level
attr_accessor :logger

##
# The version of the Ruby postprocessor
Expand Down Expand Up @@ -216,12 +219,21 @@ def move_files
self
end

##
# You may call this method to report a fatal error
#
# @param message [String]
#
def error message
raise Error, message
end

# ---- Private implementation below this point ----

# @private
def entrypoint quiet_level: 0
@quiet_level = quiet_level
setup
def entrypoint logger: nil, gem_name: nil
setup logger, gem_name
sanity_check
apply_default_config
if script_path
load script_path
Expand All @@ -233,30 +245,48 @@ def entrypoint quiet_level: 0

private

def setup
def setup logger, gem_name
@logger = logger
@gem_name = gem_name
@repo_dir = ::Dir.getwd
@staging_root_dir = ::File.join @repo_dir, STAGING_ROOT_NAME
staging_dirs = ::Dir.children @staging_root_dir
raise "Unexpected staging dirs: #{staging_dirs.inspect}" unless staging_dirs.size == 1
@staging_dir = ::File.join @staging_root_dir, staging_dirs.first
@gem_name = ::File.basename @staging_dir
@gem_name ||= find_staged_gem_name @staging_root_dir
@staging_dir = ::File.join @staging_root_dir, @gem_name
@gem_dir = ::File.join @repo_dir, @gem_name
path = ::File.join @gem_dir, SCRIPT_NAME
@script_path = ::File.file?(path) ? path : nil
@script_path = find_custom_script gem_dir
@manifest_path = ::File.join @gem_dir, MANIFEST_NAME
@previous_generated_files = []
@previous_static_files = []
@previous_generated_files, @previous_static_files = load_existing_manifest @manifest_path
@next_generated_files = []
@next_static_files = []
if ::File.file? @manifest_path
manifest = ::JSON.load_file @manifest_path
@previous_generated_files = manifest["generated"] || []
@previous_static_files = manifest["static"] || []
end
@preserved_paths = []
@content_modifiers = []
end

def find_staged_gem_name staging_root_dir
staging_dirs = ::Dir.children staging_root_dir
error "Unexpected staging dirs: #{staging_dirs.inspect}" unless staging_dirs.size == 1
staging_dirs.first
end

def find_custom_script gem_dir
path = ::File.join gem_dir, SCRIPT_NAME
::File.file?(path) ? path : nil
end

def load_existing_manifest manifest_path
if ::File.file? manifest_path
manifest = ::JSON.load_file manifest_path
[manifest["generated"] || [], manifest["static"] || []]
else
[[], []]
end
end

def sanity_check
error "No staging directory #{@staging_dir}" unless ::File.directory? @staging_dir
error "No gem directory #{@gem_dir}" unless ::File.directory? @gem_dir
end

def apply_default_config
preserve path: ["CHANGELOG.md", "lib/#{gem_name.tr '-', '/'}/version.rb"]

Expand Down Expand Up @@ -398,11 +428,11 @@ def write_manifest
end

def path_info path, str
puts "#{path}: #{str}" if quiet_level <= 0
logger&.info "#{path}: #{str}"
end

def path_warning path, str
puts "#{path}: WARNING: #{str}" if quiet_level <= 1
logger&.warn "#{path}: #{str}"
end
end
end
104 changes: 71 additions & 33 deletions owlbot-postprocessor/test/test_owlbot.rb
Expand Up @@ -18,7 +18,6 @@
require "fileutils"

describe OwlBot do
let(:quiet_level) { 2 }
let(:base_dir) { ::File.dirname __dir__ }
let(:image_name) { "owlbot-postprocessor-test" }
let(:manifest_file_name) { ".owlbot-manifest.json" }
Expand All @@ -44,16 +43,18 @@
::FileUtils.rm_rf repo_dir
end

def create_staging_file path, content
path = ::File.join staging_dir, path
def create_staging_file path, content, gem: nil
dir = gem ? ::File.join(staging_root_dir, gem) : staging_dir
path = ::File.join dir, path
::FileUtils.mkdir_p ::File.dirname path
::File.open path, "w" do |file|
file.write content
end
end

def create_gem_file path, content
path = ::File.join gem_dir, path
def create_gem_file path, content, gem: nil
dir = gem ? ::File.join(repo_dir, gem) : gem_dir
path = ::File.join dir, path
::FileUtils.mkdir_p ::File.dirname path
::File.open path, "w" do |file|
file.write content
Expand All @@ -70,33 +71,35 @@ def create_existing_manifest generated: [], static: []
end
end

def assert_gem_file path, content
path = ::File.join gem_dir, path
def assert_gem_file path, content, gem: nil
dir = gem ? ::File.join(repo_dir, gem) : gem_dir
path = ::File.join dir, path
assert ::File.exist? path
assert_equal content, ::File.read(path)
end

def refute_gem_file path
path = ::File.join gem_dir, path
def refute_gem_file path, gem: nil
dir = gem ? ::File.join(repo_dir, gem) : gem_dir
path = ::File.join dir, path
refute ::File.exist? path
end

def invoke_owlbot
def invoke_owlbot gem: nil
::Dir.chdir repo_dir do
OwlBot.entrypoint quiet_level: quiet_level
OwlBot.entrypoint gem_name: gem
end
end

def invoke_image
def invoke_image *args
cmd = [
"docker", "run",
"--rm",
"--user", "#{::Process.uid}:#{::Process.gid}",
"-v", "#{repo_dir}:/repo",
"-w", "/repo",
"-e", "QUIET_LEVEL=#{quiet_level}",
image_name
]
image_name,
"-qq"
] + args
assert system cmd.join(" ")
end

Expand All @@ -117,24 +120,6 @@ def invoke_image
assert_equal [], manifest["static"]
end

it "copies files using the image" do
create_gem_file "static.txt", "here before\n"
create_staging_file "hello.txt", "hello world\n"
create_staging_file "lib/hello.rb", "puts 'hello'\n"

invoke_image

assert_gem_file "hello.txt", "hello world\n"
assert_gem_file "lib/hello.rb", "puts 'hello'\n"
assert_gem_file "static.txt", "here before\n"

paths = ::Dir.glob "**/*", base: gem_dir
assert_equal 4, paths.size # Three files and one directory

assert_equal ["hello.txt", "lib/hello.rb"], manifest["generated"]
assert_equal ["static.txt"], manifest["static"]
end

it "copies files into an existing gem dir" do
create_gem_file "hello.txt", "hello world\n"
create_gem_file "lib/bye.rb", "puts 'bye'\n"
Expand Down Expand Up @@ -290,4 +275,57 @@ def invoke_image
assert_equal ["generated.txt"], manifest["generated"]
assert_equal [".gitignore", "static.txt"], manifest["static"]
end

it "errors if there are multiple staging directories and no explicit gem" do
create_gem_file "hello.txt", "hello world\n"
create_gem_file "hello.txt", "hello world\n", gem: "another-gem"
create_staging_file "hello.txt", "hello again\n"
create_staging_file "hello.txt", "hello again\n", gem: "another-gem"

assert_raises OwlBot::Error do
invoke_owlbot
end
end

it "supports selecting a specific gem" do
create_gem_file "hello.txt", "hello world\n"
create_gem_file "hello.txt", "hello world\n", gem: "another-gem"
create_staging_file "hello.txt", "hello again\n"
create_staging_file "hello.txt", "hello again\n", gem: "another-gem"

invoke_owlbot gem: gem_name

assert_gem_file "hello.txt", "hello again\n"
assert_gem_file "hello.txt", "hello world\n", gem: "another-gem"
end

it "copies files using the image" do
create_gem_file "static.txt", "here before\n"
create_staging_file "hello.txt", "hello world\n"
create_staging_file "lib/hello.rb", "puts 'hello'\n"

invoke_image

assert_gem_file "hello.txt", "hello world\n"
assert_gem_file "lib/hello.rb", "puts 'hello'\n"
assert_gem_file "static.txt", "here before\n"

paths = ::Dir.glob "**/*", base: gem_dir
assert_equal 4, paths.size # Three files and one directory

assert_equal ["hello.txt", "lib/hello.rb"], manifest["generated"]
assert_equal ["static.txt"], manifest["static"]
end

it "supports selecting a specific gem using the image" do
create_gem_file "hello.txt", "hello world\n"
create_gem_file "hello.txt", "hello world\n", gem: "another-gem"
create_staging_file "hello.txt", "hello again\n"
create_staging_file "hello.txt", "hello again\n", gem: "another-gem"

invoke_image "--gem=another-gem"

assert_gem_file "hello.txt", "hello world\n"
assert_gem_file "hello.txt", "hello again\n", gem: "another-gem"
end
end