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

Add "package_name_format" field to recipe #215

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/fpm/cookery/lifecycle_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ module LifecycleHooks
def run_lifecycle_hook(hook_name, *args)
Log.debug("Run lifecycle hook: #{hook_name} (args: #{args.inspect})")
self.__send__(hook_name, *args)

# Backward compatibility for users of deprecated lifecycle hooks
case hook_name
when :before_package_file_create
unless self.__send__(:before_package_create, args[1]) == :UNUSED
Log.deprecated("Switch from \"before_package_create\" lifecycle hook to \"before_package_file_create\"")
end
when :after_package_file_create
unless self.__send__(:after_package_create, args[1]) == :UNUSED
Log.deprecated("Switch from \"after_package_create\" lifecycle hook to \"after_package_file_create\"")
end
end
end

def before_dependency_installation
Expand Down Expand Up @@ -39,12 +51,24 @@ def before_install
def after_install
end

# Gets a the output filename and the FPM::Package object as argument.
def before_package_file_create(filename, package)
end

# Gets a the output filename and the FPM::Package object as argument.
def after_package_file_create(filename, package)
end

# Gets a FPM::Package object as argument.
# @deprecated Use #after_package_file_create.
def before_package_create(package)
:UNUSED
end

# Gets a FPM::Package object as argument.
# @deprecated Use #after_package_file_create.
def after_package_create(package)
:UNUSED
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/fpm/cookery/package/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'fpm/cookery/exceptions'
require 'fpm/cookery/log'

module FPM
module Cookery
module Package
Expand Down Expand Up @@ -28,6 +31,21 @@ def initialize(recipe, target, config)
@config = config
@revision = recipe.revision
@version, @epoch = split_version(@recipe.version)

if [email protected]? and !recipe.epoch.nil?
# If the epoch is defined in the version string and set in the
# epoch field, we don't know what to choose.
message = "The \"epoch\" is defined in the recipe's version (#{@recipe.version}) AND epoch (#{@recipe.epoch}) fields"
Log.error message
raise Error::Misconfiguration, message
end

# The epoch in the version string has precedence over the #epoch
# attribute in the recipe. (backward compatibility)
@epoch = recipe.epoch if @epoch.nil?

# Ensure that epoch is always a string
@epoch = @epoch.to_s unless @epoch.nil?
end

def vendor
Expand Down
13 changes: 7 additions & 6 deletions lib/fpm/cookery/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,19 @@ def build_package(recipe, config)

output = input.convert(output_class)

recipe.run_lifecycle_hook(:before_package_create, output)
output_filename = output.to_s(recipe.package_name_format)
recipe.run_lifecycle_hook(:before_package_file_create, output_filename, output)
begin
output.output(output.to_s)
recipe.run_lifecycle_hook(:after_package_create, output)
output.output(output_filename)
recipe.run_lifecycle_hook(:after_package_file_create, output_filename, output)
rescue FPM::Package::FileAlreadyExists
Log.info "Removing existing package file: #{output.to_s}"
FileUtils.rm_f(output.to_s)
Log.info "Removing existing package file: #{output_filename}"
FileUtils.rm_f(output_filename)
retry
ensure
input.cleanup if input
output.cleanup if output
Log.info "Created package: #{File.join(Dir.pwd, output.to_s)}"
Log.info "Created package: #{File.join(Dir.pwd, output_filename)}"
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/fpm/cookery/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class BaseRecipe
:revision, :section, :sha1, :sha256, :sha512, :spec, :vendor, :version,
:pre_install, :post_install, :pre_uninstall, :post_uninstall,
:license, :omnibus_package, :omnibus_dir, :chain_package,
:default_prefix, :docker, :docker_image, :dockerfile
:default_prefix, :docker, :docker_image, :dockerfile,
:package_name_format, :epoch

attr_rw_list :build_depends, :config_files, :conflicts, :depends,
:exclude, :patches, :provides, :replaces, :omnibus_recipes,
Expand Down
3 changes: 3 additions & 0 deletions recipes/facter/recipe.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class FacterRubyGem < FPM::Cookery::RubyGemRecipe
name 'facter'
version '1.6.16'
epoch 2

package_name_format 'NAME_EPOCH:FULLVERSION_ARCH.EXTENSION'
end
40 changes: 40 additions & 0 deletions spec/lifecycle_hooks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'
require 'fpm/cookery/lifecycle_hooks'

describe 'LifecycleHooks' do
let(:object) do
Class.new do
attr_accessor :canary

include FPM::Cookery::LifecycleHooks

def before_package_create(package)
self.canary = package
end

def after_package_create(package)
self.canary = package
end
end.new
end

let(:canary) { Object.new }

describe 'backward compatibility' do
describe 'Running :before_package_file_create hook' do
it 'calls the deprecated :before_package_create hook' do
object.run_lifecycle_hook(:before_package_file_create, 'filename', canary)

expect(object.canary).to eq(canary)
end
end

describe 'Running :after_package_file_create hook' do
it 'calls the deprecated :after_package_create hook' do
object.run_lifecycle_hook(:after_package_file_create, 'filename', canary)

expect(object.canary).to eq(canary)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/package_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@
expect(version.epoch).to eq(nil)
end
end

context 'with epoch set in recipe' do
it 'returns the recipe epoch' do
recipe.version = '1.2.3'
recipe.epoch = 2

expect(version.epoch).to eq('2') # Must be a string
end
end

context 'with epoch set in recipe version and epoch' do
it 'raises an Misconfiguration error' do
recipe.version = '3:1.2.3'
recipe.epoch = 2

expect { version.epoch }.to raise_error(FPM::Cookery::Error::Misconfiguration)
end
end
end

describe '#to_s' do
Expand Down