diff --git a/README.md b/README.md index e4f05e7..de5803f 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,16 @@ Type `github_changelog_generator --help` for details. More detailed info about params you can find in Wiki page: [**Advanced change log generation examples**](https://github.com/skywinder/github-changelog-generator/wiki/Advanced-change-log-generation-examples) +### Params File +You can put Params in a .github_changelog_generator file in Project Root to override default params: + +Example: +``` +unreleased=false +future-release=5.0.0 +since-tag=1.0.0 +``` + ### GitHub token Since GitHub allows you to make only 50 requests without authentication it's recommended to run this script with a token (`-t, --token` option) diff --git a/lib/github_changelog_generator.rb b/lib/github_changelog_generator.rb index 3b1e60f..31d86ae 100755 --- a/lib/github_changelog_generator.rb +++ b/lib/github_changelog_generator.rb @@ -7,6 +7,7 @@ require "benchmark" require_relative "github_changelog_generator/helper" require_relative "github_changelog_generator/parser" +require_relative "github_changelog_generator/parser_file" require_relative "github_changelog_generator/generator/generator" require_relative "github_changelog_generator/version" require_relative "github_changelog_generator/reader" diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index d1b36b7..5001ad6 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -9,8 +9,10 @@ module GitHubChangelogGenerator def self.parse_options options = get_default_options - parser = setup_parser(options) + parser_file = ParserFile.new options + parser_file.parse! + parser = setup_parser(options) parser.parse! if options[:user].nil? || options[:project].nil? diff --git a/lib/github_changelog_generator/parser_file.rb b/lib/github_changelog_generator/parser_file.rb new file mode 100644 index 0000000..74f3cd3 --- /dev/null +++ b/lib/github_changelog_generator/parser_file.rb @@ -0,0 +1,36 @@ +module GitHubChangelogGenerator + class ParserFile + def initialize(options) + @options = options + end + + def file + File.expand_path(@options[:params_file] || ".github_changelog_generator") + end + + def file? + File.exist?(file) + end + + def file_open + File.open(file) + end + + def parse! + return unless file? + file_open.each do |line| + begin + key, value = line.split("=") + key_sym = key.sub("-", "_").to_sym + value = value.gsub(/[\n\r]+/, "") + value = true if value =~ (/^(true|t|yes|y|1)$/i) + value = false if value =~ (/^(false|f|no|n|0)$/i) + @options[key_sym] = value + rescue + raise "File #{file} is incorrect in line \"#{line.gsub(/[\n\r]+/, '')}\"" + end + end + @options + end + end +end diff --git a/spec/files/github_changelog_params_empty b/spec/files/github_changelog_params_empty new file mode 100644 index 0000000..e69de29 diff --git a/spec/files/github_changelog_params_incorrect b/spec/files/github_changelog_params_incorrect new file mode 100644 index 0000000..35fa3d8 --- /dev/null +++ b/spec/files/github_changelog_params_incorrect @@ -0,0 +1,2 @@ +unreleased_label: staging +unreleased: false diff --git a/spec/files/github_changelog_params_override b/spec/files/github_changelog_params_override new file mode 100644 index 0000000..ff0a972 --- /dev/null +++ b/spec/files/github_changelog_params_override @@ -0,0 +1,2 @@ +unreleased_label=staging +unreleased=false diff --git a/spec/unit/parse_file_spec.rb b/spec/unit/parse_file_spec.rb new file mode 100644 index 0000000..55c743d --- /dev/null +++ b/spec/unit/parse_file_spec.rb @@ -0,0 +1,32 @@ +describe GitHubChangelogGenerator::ParserFile do + describe ".github_changelog_generator" do + context "when no has file" do + let(:options) { {} } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_nil } + end + + context "when file is empty" do + let(:options) { { params_file: "spec/files/github_changelog_params_empty" } } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_a(Hash) } + it { is_expected.to eq(options) } + end + + context "when file is incorrect" do + let(:options) { { params_file: "spec/files/github_changelog_params_incorrect" } } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + it { expect { fail.raise! }.to raise_error RuntimeError } + end + + context "when override default values" do + let(:options) { { params_file: "spec/files/github_changelog_params_override" }.merge(GitHubChangelogGenerator::Parser.get_default_options) } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + subject { parse.parse! } + it { is_expected.to be_a(Hash) } + it { is_expected.to eq(options.merge(unreleased_label: "staging", unreleased: false)) } + end + end +end