diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 20ac188a..b69b2939 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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: @@ -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: diff --git a/lib/modulesync.rb b/lib/modulesync.rb index 2837ccfa..8a9abfdd 100644 --- a/lib/modulesync.rb +++ b/lib/modulesync.rb @@ -3,7 +3,6 @@ require 'modulesync/cli' require 'modulesync/constants' -require 'modulesync/repository' require 'modulesync/hook' require 'modulesync/puppet_module' require 'modulesync/renderer' @@ -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, @@ -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 @@ -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] || {}, @@ -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 diff --git a/lib/modulesync/puppet_module.rb b/lib/modulesync/puppet_module.rb index d485c5e3..7cb4710c 100644 --- a/lib/modulesync/puppet_module.rb +++ b/lib/modulesync/puppet_module.rb @@ -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 diff --git a/lib/modulesync/repository.rb b/lib/modulesync/repository.rb index 071b9d78..f5c1f8f9 100644 --- a/lib/modulesync/repository.rb +++ b/lib/modulesync/repository.rb @@ -1,5 +1,4 @@ require 'git' -require 'puppet_blacksmith' module ModuleSync # Wrapper for Git in ModuleSync context @@ -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}" @@ -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." diff --git a/lib/modulesync/source_code.rb b/lib/modulesync/source_code.rb new file mode 100644 index 00000000..7415d86b --- /dev/null +++ b/lib/modulesync/source_code.rb @@ -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