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

Split generic and specific code #215

Merged
merged 7 commits into from
Apr 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2021-04-21 19:04:03 +0200 using RuboCop version 0.50.0.
# on 2021-04-22 16:30:35 +0200 using RuboCop version 0.50.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -13,12 +13,12 @@ Lint/UselessAssignment:

# Offense count: 10
Metrics/AbcSize:
Max: 53
Max: 67

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 158
Max: 128

# Offense count: 3
Metrics/CyclomaticComplexity:
Expand All @@ -27,11 +27,11 @@ Metrics/CyclomaticComplexity:
# Offense count: 13
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 40
Max: 36

# Offense count: 3
Metrics/PerceivedComplexity:
Max: 15
Max: 13

# Offense count: 8
Style/Documentation:
Expand Down
21 changes: 10 additions & 11 deletions lib/modulesync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

require 'modulesync/cli'
require 'modulesync/constants'
require 'modulesync/repository'
require 'modulesync/hook'
require 'modulesync/puppet_module'
require 'modulesync/renderer'
Expand Down Expand Up @@ -32,10 +31,6 @@ def self.local_file(config_path, file)
File.join(config_path, MODULE_FILES_DIR, file)
end

def self.module_file(puppet_module, *parts)
File.join(puppet_module.working_directory, *parts)
end

# List all template files.
#
# Only select *.erb files, and strip the extension. This way all the code will only have to handle bare paths,
Expand Down Expand Up @@ -88,7 +83,7 @@ def self.manage_file(puppet_module, filename, settings, options)
namespace = settings.additional_settings[:namespace]
module_name = settings.additional_settings[:puppet_module]
configs = settings.build_file_configs(filename)
target_file = module_file(puppet_module, filename)
target_file = puppet_module.path(filename)
if configs['delete']
Renderer.remove(target_file)
else
Expand All @@ -111,11 +106,10 @@ def self.manage_file(puppet_module, filename, settings, options)
end

def self.manage_module(puppet_module, module_files, defaults)
repository = Repository.new directory: puppet_module.working_directory, remote: puppet_module.repository_remote
puts "Syncing '#{puppet_module.given_name}'"
repository.prepare_workspace(options[:branch]) unless options[:offline]
puppet_module.repository.prepare_workspace(options[:branch]) unless options[:offline]

module_configs = Util.parse_config(module_file(puppet_module, MODULE_CONF_FILE))
module_configs = Util.parse_config puppet_module.path(MODULE_CONF_FILE)
settings = Settings.new(defaults[GLOBAL_DEFAULTS_KEY] || {},
defaults,
module_configs[GLOBAL_DEFAULTS_KEY] || {},
Expand All @@ -133,11 +127,16 @@ def self.manage_module(puppet_module, module_files, defaults)

if options[:noop]
puts "Using no-op. Files in '#{puppet_module.given_name}' may be changed but will not be committed."
repository.show_changes(options)
puppet_module.repository.show_changes(options)
options[:pr] && \
pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options)
elsif !options[:offline]
pushed = repository.submit_changes(files_to_manage, options)
pushed = puppet_module.repository.submit_changes(files_to_manage, options)
# Only bump/tag if pushing didn't fail (i.e. there were changes)
if pushed && options[:bump]
new = puppet_module.bump(options[:message], options[:changelog])
puppet_module.repository.tag(new, options[:tag_pattern]) if options[:tag]
end
pushed && options[:pr] && \
pr(puppet_module).manage(puppet_module.repository_namespace, puppet_module.repository_name, options)
end
Expand Down
72 changes: 30 additions & 42 deletions lib/modulesync/puppet_module.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,37 @@
require 'modulesync'
require 'modulesync/util'
require 'puppet_blacksmith'

module ModuleSync
# Provide methods to retrieve puppet module attributes
class PuppetModule
attr_reader :given_name
attr_reader :options

def initialize(given_name, options)
options ||= {}
@options = Util.symbolize_keys(options)

@given_name = given_name

return unless given_name.include?('/')

@repository_name = given_name.split('/').last
@repository_namespace = given_name.split('/')[0...-1].join('/')
end

def repository_name
@repository_name ||= given_name
end
require 'modulesync/source_code'

def repository_namespace
@repository_namespace ||= @options[:namespace] || ModuleSync.options[:namespace]
end

def repository_path
@repository_path ||= "#{repository_namespace}/#{repository_name}"
end

def repository_remote
@repository_remote ||= @options[:remote] || _repository_remote
end

def working_directory
@working_directory ||= File.join(ModuleSync.options[:project_root], repository_path)
module ModuleSync
# Provide methods to manipulate puppet module code
class PuppetModule < SourceCode
def update_changelog(version, message)
changelog = path('CHANGELOG.md')
if File.exist?(changelog)
puts "Updating #{changelog} for version #{version}"
changes = File.readlines(changelog)
File.open(changelog, 'w') do |f|
date = Time.now.strftime('%Y-%m-%d')
f.puts "## #{date} - Release #{version}\n\n"
f.puts "#{message}\n\n"
# Add old lines again
f.puts changes
end
repository.git.add('CHANGELOG.md')
else
puts 'No CHANGELOG.md file found, not updating.'
end
end

private

def _repository_remote
git_base = ModuleSync.options[:git_base]
git_base.start_with?('file://') ? "#{git_base}#{repository_path}" : "#{git_base}#{repository_path}.git"
def bump(message, changelog = false)
m = Blacksmith::Modulefile.new path('metadata.json')
new = m.bump!
puts "Bumped to version #{new}"
repository.git.add('metadata.json')
update_changelog(new, message) if changelog
repository.git.commit("Release version #{new}")
repository.git.push
new
end
end
end
37 changes: 0 additions & 37 deletions lib/modulesync/repository.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'git'
require 'puppet_blacksmith'

module ModuleSync
# Wrapper for Git in ModuleSync context
Expand Down Expand Up @@ -79,37 +78,6 @@ def prepare_workspace(branch)
end
end

# PuppetModule, is it used?
def update_changelog(version, message)
changelog = "#{@directory}/CHANGELOG.md"
if File.exist?(changelog)
puts "Updating #{changelog} for version #{version}"
changes = File.readlines(changelog)
File.open(changelog, 'w') do |f|
date = Time.now.strftime('%Y-%m-%d')
f.puts "## #{date} - Release #{version}\n\n"
f.puts "#{message}\n\n"
# Add old lines again
f.puts changes
end
repo.add('CHANGELOG.md')
else
puts 'No CHANGELOG.md file found, not updating.'
end
end

# PuppetModule
def bump(message, changelog = false)
m = Blacksmith::Modulefile.new("#{@directory}/metadata.json")
new = m.bump!
puts "Bumped to version #{new}"
repo.add('metadata.json')
update_changelog(new, message) if changelog
repo.commit("Release version #{new}")
repo.push
new
end

def tag(version, tag_pattern)
tag = tag_pattern % version
puts "Tagging with #{tag}"
Expand Down Expand Up @@ -151,11 +119,6 @@ def submit_changes(files, options)
else
repo.push('origin', branch, opts_push)
end
# Only bump/tag if pushing didn't fail (i.e. there were changes)
if options[:bump]
new = bump(message, options[:changelog])
tag(new, options[:tag_pattern]) if options[:tag]
end
rescue Git::GitExecuteError => e
if e.message.match?(/working (directory|tree) clean/)
puts "There were no changes in '#{@directory}'. Not committing."
Expand Down
57 changes: 57 additions & 0 deletions lib/modulesync/source_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'modulesync'
require 'modulesync/repository'
require 'modulesync/util'

module ModuleSync
# Provide methods to retrieve source code attributes
class SourceCode
attr_reader :given_name
attr_reader :options

def initialize(given_name, options)
@options = Util.symbolize_keys(options || {})

@given_name = given_name

return unless given_name.include?('/')

@repository_name = given_name.split('/').last
@repository_namespace = given_name.split('/')[0...-1].join('/')
end

def repository
@repository ||= Repository.new directory: working_directory, remote: repository_remote
end

def repository_name
@repository_name ||= given_name
end

def repository_namespace
@repository_namespace ||= @options[:namespace] || ModuleSync.options[:namespace]
end

def repository_path
@repository_path ||= "#{repository_namespace}/#{repository_name}"
end

def repository_remote
@repository_remote ||= @options[:remote] || _repository_remote
end

def working_directory
@working_directory ||= File.join(ModuleSync.options[:project_root], repository_path)
end

def path(*parts)
File.join(working_directory, *parts)
end

private

def _repository_remote
git_base = ModuleSync.options[:git_base]
git_base.start_with?('file://') ? "#{git_base}#{repository_path}" : "#{git_base}#{repository_path}.git"
end
end
end