Skip to content

Commit

Permalink
Merge pull request #35 from pajapro/#28
Browse files Browse the repository at this point in the history
Remove prompt for creating CHANGEPLAN.md in project root
  • Loading branch information
pajapro authored Nov 11, 2018
2 parents 4cf3483 + 86e783d commit 2c86983
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 73 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
61 changes: 0 additions & 61 deletions lib/fastlane/plugin/changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/fastlane/plugin/changelog/actions/read_changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions lib/fastlane/plugin/changelog/actions/stamp_changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: ["###"])
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions lib/fastlane/plugin/changelog/actions/update_changelog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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",
Expand Down
71 changes: 66 additions & 5 deletions lib/fastlane/plugin/changelog/helper/changelog_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fastlane/plugin/changelog/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fastlane
module Changelog
VERSION = "0.9.0"
VERSION = "0.10.0"
end
end

0 comments on commit 2c86983

Please sign in to comment.