diff --git a/CHANGELOG.md b/CHANGELOG.md index b66bfa8..251db1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed: +- Remove prompt to create `CHANGELOG.md` in project root, while using custom changelog_path param ([Issue #28](https://github.com/pajapro/fastlane-plugin-changelog/issues/28) and [Issue #23](https://github.com/pajapro/fastlane-plugin-changelog/issues/23)) ## [0.9.0] - 2017-11-25 - Add `placeholder_line` param to `stamp_changelog` action ([feature request #22](https://github.com/pajapro/fastlane-plugin-changelog/issues/22)) diff --git a/lib/fastlane/plugin/changelog.rb b/lib/fastlane/plugin/changelog.rb index 53f5194..54b761e 100644 --- a/lib/fastlane/plugin/changelog.rb +++ b/lib/fastlane/plugin/changelog.rb @@ -2,69 +2,11 @@ module Fastlane module Changelog - CHANGELOG_PATH = './CHANGELOG.md' - DEFAULT_CHANGELOG = '# Change Log -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](http://keepachangelog.com/) -and this project adheres to [Semantic Versioning](http://semver.org/). - -## [Unreleased] -### Added -- Your awesome new feature' - # Return all .rb files inside the "actions" and "helper" directory def self.all_classes Dir[File.expand_path('**/{actions,helper}/*.rb', File.dirname(__FILE__))] end - # Offer user to setup CHANGELOG.md file in project folder (unless it already exists) - def self.setup_changelog - unless has_changelog? - if FastlaneCore::UI.confirm('Your project folder does not have CHANGELOG.md - do you want to create one now?') - create_changelog - - FastlaneCore::UI.message('Changelog plugin can automaticaly create a link for comparison between two tags (see https://github.com/pajapro/fastlane-plugin-changelog#--stamp_changelog)') - if FastlaneCore::UI.confirm('Do you want to create links for comparing tags?') - repo_url = FastlaneCore::UI.input('Enter your GitHub or Bitbucket repository URL (e.g.: https://github.com/owner/project or https://bitbucket.org/owner/project):') - create_comparison_link(repo_url) - else - write_to_changelog(DEFAULT_CHANGELOG) - end - end - end - end - - # Does CHANGELOG.md exists in project folder? - def self.has_changelog? - File.exist?(CHANGELOG_PATH) - end - - # Create CHANGELOG.md file - def self.create_changelog - FileUtils.touch 'CHANGELOG.md' - end - - # Create a link for tag comparison - def self.create_comparison_link(repo_url) - if repo_url.start_with?('https://github.com') - output = DEFAULT_CHANGELOG + "\n\n[Unreleased]: #{repo_url}/compare/master...HEAD" - write_to_changelog(output) - elsif repo_url.start_with?('https://bitbucket.org') - output = DEFAULT_CHANGELOG + "\n\n[Unreleased]: #{repo_url}/compare/master..HEAD" - write_to_changelog(output) - else - FastlaneCore::UI.error('Unknown repository host') - FastlaneCore::UI.message('Creating CHANGELOG.md without links for comparing tags') - write_to_changelog(DEFAULT_CHANGELOG) - end - end - - # Write given content to CHANGELOG.md - def self.write_to_changelog(changelog) - File.open(CHANGELOG_PATH, 'w') { |f| f.write(changelog) } - FastlaneCore::UI.success('Successfuly created CHANGELOG.md') - end end end @@ -73,6 +15,3 @@ def self.write_to_changelog(changelog) Fastlane::Changelog.all_classes.each do |current| require current end - -# By default we want to ensure CHANGELOG.md is present -Fastlane::Changelog.setup_changelog diff --git a/lib/fastlane/plugin/changelog/actions/read_changelog.rb b/lib/fastlane/plugin/changelog/actions/read_changelog.rb index 4042be0..1e3df37 100644 --- a/lib/fastlane/plugin/changelog/actions/read_changelog.rb +++ b/lib/fastlane/plugin/changelog/actions/read_changelog.rb @@ -8,7 +8,7 @@ module SharedValues class ReadChangelogAction < Action def self.run(params) changelog_path = params[:changelog_path] unless params[:changelog_path].to_s.empty? - UI.error("CHANGELOG.md at path '#{changelog_path}' does not exist") unless File.exist?(changelog_path) + changelog_path = Helper::ChangelogHelper.ensure_changelog_exists(changelog_path) section_identifier = params[:section_identifier] unless params[:section_identifier].to_s.empty? escaped_section_identifier = Regexp.escape(section_identifier[/\[(.*?)\]/, 1]) @@ -77,7 +77,7 @@ def self.details def self.available_options [ FastlaneCore::ConfigItem.new(key: :changelog_path, - env_name: "FL_READ_CHANGELOG_PATH_TO_CHANGELOG", + env_name: "FL_CHANGELOG_PATH", description: "The path to your project CHANGELOG.md", is_string: true, default_value: "./CHANGELOG.md", diff --git a/lib/fastlane/plugin/changelog/actions/stamp_changelog.rb b/lib/fastlane/plugin/changelog/actions/stamp_changelog.rb index b043f78..7779b65 100644 --- a/lib/fastlane/plugin/changelog/actions/stamp_changelog.rb +++ b/lib/fastlane/plugin/changelog/actions/stamp_changelog.rb @@ -6,7 +6,7 @@ class StampChangelogAction < Action def self.run(params) # 1. Ensure CHANGELOG.md exists changelog_path = params[:changelog_path] unless params[:changelog_path].to_s.empty? - UI.error("CHANGELOG.md at path '#{changelog_path}' does not exist") unless File.exist?(changelog_path) + changelog_path = Helper::ChangelogHelper.ensure_changelog_exists(changelog_path) # 2. Ensure there are changes in [Unreleased] section unreleased_section_content = Actions::ReadChangelogAction.run(changelog_path: changelog_path, section_identifier: UNRELEASED_IDENTIFIER, excluded_markdown_elements: ["###"]) @@ -108,7 +108,7 @@ def self.details def self.available_options [ FastlaneCore::ConfigItem.new(key: :changelog_path, - env_name: "FL_STAMP_CHANGELOG_PATH_TO_CHANGELOG", + env_name: "FL_CHANGELOG_PATH", description: "The path to your project CHANGELOG.md", is_string: true, default_value: "./CHANGELOG.md", diff --git a/lib/fastlane/plugin/changelog/actions/update_changelog.rb b/lib/fastlane/plugin/changelog/actions/update_changelog.rb index 64b68e2..f19e0ed 100644 --- a/lib/fastlane/plugin/changelog/actions/update_changelog.rb +++ b/lib/fastlane/plugin/changelog/actions/update_changelog.rb @@ -3,7 +3,7 @@ module Actions class UpdateChangelogAction < Action def self.run(params) changelog_path = params[:changelog_path] unless params[:changelog_path].to_s.empty? - UI.error("CHANGELOG.md at path '#{changelog_path}' does not exist") unless File.exist?(changelog_path) + changelog_path = Helper::ChangelogHelper.ensure_changelog_exists(changelog_path) section_identifier = params[:section_identifier] unless params[:section_identifier].to_s.empty? escaped_section_identifier = section_identifier[/\[(.*?)\]/, 1] @@ -95,7 +95,7 @@ def self.details def self.available_options [ FastlaneCore::ConfigItem.new(key: :changelog_path, - env_name: "FL_UPDATE_CHANGELOG_PATH_TO_CHANGELOG", + env_name: "FL_CHANGELOG_PATH", description: "The path to your project CHANGELOG.md", is_string: true, default_value: "./CHANGELOG.md", diff --git a/lib/fastlane/plugin/changelog/helper/changelog_helper.rb b/lib/fastlane/plugin/changelog/helper/changelog_helper.rb index 1f5e53a..74554e7 100644 --- a/lib/fastlane/plugin/changelog/helper/changelog_helper.rb +++ b/lib/fastlane/plugin/changelog/helper/changelog_helper.rb @@ -1,13 +1,74 @@ module Fastlane module Helper + CHANGELOG_PATH = './CHANGELOG.md' + DEFAULT_CHANGELOG = '# Change Log +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] +### Added +- Your awesome new feature' + + # TODO: ❗️ Add unit tests for methods in this class class ChangelogHelper - # class methods that you define here become available in your action - # as `Helper::ChangelogHelper.your_method` - # - def self.show_message - UI.message("Hello from the changelog plugin helper!") + + # Ensures CHANGELOG.md exists at given path. If not, offers to create a default one. + # Returns path to CHANGELOG.md to be used + def self.ensure_changelog_exists(path) + if File.exist?(path) + FastlaneCore::UI.success "Found CHANGELOG.md at #{path}" + path + else + FastlaneCore::UI.message("Cannot find CHANGELOG.md at #{path}") + generate_changelog + CHANGELOG_PATH + end + end + + # Generates CHANGELOG.md in project root + def self.generate_changelog + if FastlaneCore::UI.confirm('Do you want to generate default CHANGELOG.md in the project root?') + FileUtils.touch 'CHANGELOG.md' + generate_comparison_link + else + FastlaneCore::UI.error("Cannot continue without CHANGELOG.md file") + end end + # Generates link for tag comparison + def self.generate_comparison_link + FastlaneCore::UI.message('Changelog plugin can automaticaly create a link for comparison between two tags (see https://github.com/pajapro/fastlane-plugin-changelog#--stamp_changelog)') + if FastlaneCore::UI.confirm('Do you want to create links for comparing tags?') + repo_url = FastlaneCore::UI.input('Enter your GitHub or Bitbucket repository URL (e.g.: https://github.com/owner/project or https://bitbucket.org/owner/project):') + compose_comparison_link(repo_url) + else + write_to_changelog(DEFAULT_CHANGELOG) + end + end + + # Composes link for tag comparison for GitHub or Bitbucket + def self.compose_comparison_link(repo_url) + if repo_url.start_with?('https://github.com') + output = DEFAULT_CHANGELOG + "\n\n[Unreleased]: #{repo_url}/compare/master...HEAD" + write_to_changelog(output) + elsif repo_url.start_with?('https://bitbucket.org') + output = DEFAULT_CHANGELOG + "\n\n[Unreleased]: #{repo_url}/compare/master..HEAD" + write_to_changelog(output) + else + FastlaneCore::UI.error('Unknown repository host') + FastlaneCore::UI.message('Creating CHANGELOG.md without links for comparing tags') + write_to_changelog(DEFAULT_CHANGELOG) + end + end + + # Writes given content to CHANGELOG.md in project root + def self.write_to_changelog(changelog) + File.open(CHANGELOG_PATH, 'w') { |f| f.write(changelog) } + FastlaneCore::UI.success('Successfuly created CHANGELOG.md') + end + def self.get_line_separator(file_path) f = File.open(file_path) enum = f.each_char diff --git a/lib/fastlane/plugin/changelog/version.rb b/lib/fastlane/plugin/changelog/version.rb index b5bb0d2..469f1f2 100644 --- a/lib/fastlane/plugin/changelog/version.rb +++ b/lib/fastlane/plugin/changelog/version.rb @@ -1,5 +1,5 @@ module Fastlane module Changelog - VERSION = "0.9.0" + VERSION = "0.10.0" end end