freyia 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7bf6cf315bb9c907a5e521b072379f571e10440205bd34dec31f44b91c7f5eca
4
+ data.tar.gz: 0f780348c2b60c049dba969b7a34f790739a7061003926a61a05c2624f0c77db
5
+ SHA512:
6
+ metadata.gz: 8c0731ec3f312fb869009c1f4bddcae1d72a96f406bbf6f6f04a062fa372bd6b0a306c5c520fd5581dbb3b0954bd14c729a8180d3caccf8feba918d024f1427f
7
+ data.tar.gz: 4bf4c0a3321169d742108f5ccda4af3e86b6f5cf5462d56d26a459f520bb05e8adebd9ea0b6ed94587a86aa9b2a12013428da0c5110a43ad3e038e006769d2b4
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.5.0] - 2025-11-17
4
+
5
+ - Initial creation of the Freyia gem, hard-fork of the shell/actions components of Thor
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2008 Yehuda Katz, Eric Hodel, et al., 2025 Jared White
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Freyia
2
+
3
+ Define and execute automated tasks in Ruby like the party girl you are! Let your freak _Norse goddess_ flag fly. 🥳
4
+
5
+ > [!NOTE]
6
+ > Freyia is a hard-fork of extracted shell/actions components from the [Thor](https://github.com/rails/thor) project, now modernized and usable in any Ruby application with any command line tooling.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ bundle add freyia
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```ruby
17
+ class TestAutomation < Freyia::Base
18
+ def call(external_file)
19
+ say_status :test, "This is a test!"
20
+ run "ls"
21
+ copy_file "data.txt"
22
+ apply external_file
23
+ end
24
+ end
25
+
26
+ TestAutomation.new(source: "dirs/source", dest: "dirs/dest").call("another/automation.rb")
27
+ ```
28
+
29
+ You can also include a mixin into your own class, which will require you to set the source(s) and destination manually.
30
+
31
+ ```ruby
32
+ class MyAutomationSuperclass
33
+ include Freyia::Setup
34
+
35
+ def initialize
36
+ self.source_paths = Array("my_src/folder").map { File.expand_path(_1, Dir.pwd) }
37
+ self.destination_root = File.expand_path("my_dest/folder", Dir.pwd)
38
+ end
39
+ end
40
+
41
+ class MyLatestAutomation < MyAutomationSuperclass
42
+ def call
43
+ # ...automation statements here
44
+ end
45
+ end
46
+ ```
47
+
48
+ Note that the use of `call` here is simply a convention…you can write automations in any method or architecture you prefer.
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on Codeberg at https://codeberg.org/jaredwhite/freyia. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://codeberg.org/jaredwhite/freyia/src/branch/main/CODE_OF_CONDUCT.md).
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[rubocop test]
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "empty_directory"
4
+
5
+ module Freyia
6
+ module Automations
7
+ # Create a new file relative to the destination root with the given data,
8
+ # which is the return value of a block or a data string.
9
+ #
10
+ # ==== Parameters
11
+ # destination<String>:: the relative path to the destination root.
12
+ # data<String|NilClass>:: the data to append to the file.
13
+ # config<Hash>:: give :verbose => false to not log the status.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # create_file "lib/fun_party.rb" do
18
+ # hostname = ask("What is the virtual hostname I should use?")
19
+ # "vhost.name = #{hostname}"
20
+ # end
21
+ #
22
+ # create_file "config/apache.conf", "your apache config"
23
+ #
24
+ def create_file(destination, *args, &block)
25
+ config = args.last.is_a?(Hash) ? args.pop : {}
26
+ data = args.first
27
+ action CreateFile.new(self, destination, block || data.to_s, config)
28
+ end
29
+ alias_method :add_file, :create_file
30
+
31
+ # CreateFile is a subset of Template, which instead of rendering a file with
32
+ # ERB, it gets the content from the user.
33
+ #
34
+ class CreateFile < EmptyDirectory #:nodoc:
35
+ attr_reader :data
36
+
37
+ def initialize(base, destination, data, config = {})
38
+ @data = data
39
+ super(base, destination, config)
40
+ end
41
+
42
+ # Checks if the content of the file at the destination is identical to the rendered result.
43
+ #
44
+ # ==== Returns
45
+ # Boolean:: true if it is identical, false otherwise.
46
+ #
47
+ def identical?
48
+ # binread uses ASCII-8BIT, so to avoid false negatives, the string must use the same
49
+ exists? && File.binread(destination) == String.new(render).force_encoding("ASCII-8BIT")
50
+ end
51
+
52
+ # Holds the content to be added to the file.
53
+ #
54
+ def render
55
+ @render ||= if data.is_a?(Proc)
56
+ data.call
57
+ else
58
+ data
59
+ end
60
+ end
61
+
62
+ def invoke!
63
+ invoke_with_conflict_check do
64
+ require "fileutils"
65
+ FileUtils.mkdir_p(File.dirname(destination))
66
+ File.open(destination, "wb", config[:perm]) { |f| f.write render }
67
+ end
68
+ given_destination
69
+ end
70
+
71
+ protected
72
+
73
+ # Now on conflict we check if the file is identical or not.
74
+ #
75
+ def on_conflict_behavior(&)
76
+ if identical?
77
+ say_status :identical, :blue
78
+ else
79
+ options = base.options.merge(config)
80
+ force_or_skip_or_conflict(options[:force], options[:skip], &)
81
+ end
82
+ end
83
+
84
+ # If force is true, run the action, otherwise check if it's not being
85
+ # skipped. If both are false, show the file_collision menu, if the menu
86
+ # returns true, force it, otherwise skip.
87
+ #
88
+ def force_or_skip_or_conflict(force, skip, &)
89
+ if force
90
+ say_status :force, :yellow
91
+ yield unless pretend?
92
+ elsif skip
93
+ say_status :skip, :yellow
94
+ else
95
+ say_status :conflict, :red
96
+ force_or_skip_or_conflict(force_on_collision?, true, &)
97
+ end
98
+ end
99
+
100
+ # Shows the file collision menu to the user and gets the result.
101
+ #
102
+ def force_on_collision?
103
+ base.shell.file_collision(destination) { render }
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "create_file"
4
+
5
+ module Freyia
6
+ module Automations
7
+ # Create a new file relative to the destination root from the given source.
8
+ #
9
+ # ==== Parameters
10
+ # destination<String>:: the relative path to the destination root.
11
+ # source<String|NilClass>:: the relative path to the source root.
12
+ # config<Hash>:: give :verbose => false to not log the status.
13
+ # :: give :symbolic => false for hard link.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # create_link "config/apache.conf", "/etc/apache.conf"
18
+ #
19
+ def create_link(destination, *args)
20
+ config = args.last.is_a?(Hash) ? args.pop : {}
21
+ source = args.first
22
+ action CreateLink.new(self, destination, source, config)
23
+ end
24
+ alias_method :add_link, :create_link
25
+
26
+ # CreateLink is a subset of CreateFile, which instead of taking a block of
27
+ # data, just takes a source string from the user.
28
+ #
29
+ class CreateLink < CreateFile #:nodoc:
30
+ attr_reader :data
31
+
32
+ # Checks if the content of the file at the destination is identical to the rendered result.
33
+ #
34
+ # ==== Returns
35
+ # Boolean:: true if it is identical, false otherwise.
36
+ #
37
+ def identical?
38
+ source = File.expand_path(render, File.dirname(destination))
39
+ exists? && File.identical?(source, destination)
40
+ end
41
+
42
+ def invoke!
43
+ invoke_with_conflict_check do
44
+ require "fileutils"
45
+ FileUtils.mkdir_p(File.dirname(destination))
46
+ # Create a symlink by default
47
+ config[:symbolic] = true if config[:symbolic].nil?
48
+ File.unlink(destination) if exists?
49
+ if config[:symbolic]
50
+ File.symlink(render, destination)
51
+ else
52
+ File.link(render, destination)
53
+ end
54
+ end
55
+ given_destination
56
+ end
57
+
58
+ def exists?
59
+ super || File.symlink?(destination)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "empty_directory"
4
+
5
+ module Freyia
6
+ module Automations
7
+ # Copies recursively the files from source directory to root directory.
8
+ # If any of the files finishes with .tt, it's considered to be a template
9
+ # and is placed in the destination without the extension .tt. If any
10
+ # empty directory is found, it's copied and all .empty_directory files are
11
+ # ignored. If any file name is wrapped within % signs, the text within
12
+ # the % signs will be executed as a method and replaced with the returned
13
+ # value. Let's suppose a doc directory with the following files:
14
+ #
15
+ # doc/
16
+ # components/.empty_directory
17
+ # README
18
+ # rdoc.rb.tt
19
+ # %app_name%.rb
20
+ #
21
+ # When invoked as:
22
+ #
23
+ # directory "doc"
24
+ #
25
+ # It will create a doc directory in the destination with the following
26
+ # files (assuming that the `app_name` method returns the value "blog"):
27
+ #
28
+ # doc/
29
+ # components/
30
+ # README
31
+ # rdoc.rb
32
+ # blog.rb
33
+ #
34
+ # <b>Encoded path note:</b> Since Freyia internals use Object#respond_to? to check if it can
35
+ # expand %something%, this `something` should be a public method in the class calling
36
+ # #directory. If a method is private, Freyia stack raises PrivateMethodEncodedError.
37
+ #
38
+ # ==== Parameters
39
+ # source<String>:: the relative path to the source root.
40
+ # destination<String>:: the relative path to the destination root.
41
+ # config<Hash>:: give :verbose => false to not log the status.
42
+ # If :recursive => false, does not look for paths recursively.
43
+ # If :mode => :preserve, preserve the file mode from the source.
44
+ # If :exclude_pattern => /regexp/, prevents copying files that match that regexp.
45
+ #
46
+ # ==== Examples
47
+ #
48
+ # directory "doc"
49
+ # directory "doc", "docs", :recursive => false
50
+ #
51
+ def directory(source, *args, &)
52
+ config = args.last.is_a?(Hash) ? args.pop : {}
53
+ destination = args.first || source
54
+ action Directory.new(self, source, destination || source, config, &)
55
+ end
56
+
57
+ class Directory < EmptyDirectory #:nodoc:
58
+ attr_reader :source
59
+
60
+ def initialize(base, source, destination = nil, config = {}, &block)
61
+ @source = File.expand_path(
62
+ Dir[Util.escape_globs(base.find_in_source_paths(source.to_s))].first
63
+ )
64
+ @block = block
65
+ super(base, destination, { recursive: true }.merge(config))
66
+ end
67
+
68
+ def invoke!
69
+ base.empty_directory given_destination, config
70
+ execute!
71
+ end
72
+
73
+ def revoke!
74
+ execute!
75
+ end
76
+
77
+ protected
78
+
79
+ def execute! # rubocop:todo Metrics
80
+ lookup = Util.escape_globs(source)
81
+ lookup = File.join(lookup, "**") if config[:recursive]
82
+ lookup = file_level_lookup(lookup)
83
+
84
+ files(lookup).sort.each do |file_source|
85
+ next if File.directory?(file_source)
86
+ next if config[:exclude_pattern] && file_source.match(config[:exclude_pattern])
87
+
88
+ file_destination = File.join(given_destination, file_source.gsub(source, "."))
89
+ file_destination.gsub!("/./", "/")
90
+
91
+ case file_source
92
+ when %r{\.empty_directory$}
93
+ dirname = File.dirname(file_destination).gsub(%r{/\.$}, "")
94
+ next if dirname == given_destination
95
+
96
+ base.empty_directory(dirname, config)
97
+ when %r{#{TEMPLATE_EXTNAME}$}o
98
+ base.template(file_source, file_destination[0..-4], config, &@block)
99
+ else
100
+ base.copy_file(file_source, file_destination, config, &@block)
101
+ end
102
+ end
103
+ end
104
+
105
+ def file_level_lookup(previous_lookup)
106
+ File.join(previous_lookup, "*")
107
+ end
108
+
109
+ def files(lookup)
110
+ Dir.glob(lookup, File::FNM_DOTMATCH)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Freyia
4
+ module Automations
5
+ # Creates an empty directory.
6
+ #
7
+ # ==== Parameters
8
+ # destination<String>:: the relative path to the destination root.
9
+ # config<Hash>:: give :verbose => false to not log the status.
10
+ #
11
+ # ==== Examples
12
+ #
13
+ # empty_directory "doc"
14
+ #
15
+ def empty_directory(destination, config = {})
16
+ action EmptyDirectory.new(self, destination, config)
17
+ end
18
+
19
+ # Class which holds create directory logic. This is the base class for
20
+ # other automations like create_file and directory.
21
+ #
22
+ # This implementation is based in Templater automations, created by Jonas Nicklas
23
+ # and Michael S. Klishin under MIT LICENSE.
24
+ #
25
+ class EmptyDirectory #:nodoc:
26
+ attr_reader :base, :destination, :given_destination, :relative_destination, :config
27
+
28
+ # Initializes given the source and destination.
29
+ #
30
+ # ==== Parameters
31
+ # base<Freyia::Base>:: A Freyia::Base instance
32
+ # source<String>:: Relative path to the source of this file
33
+ # destination<String>:: Relative path to the destination of this file
34
+ # config<Hash>:: give :verbose => false to not log the status.
35
+ #
36
+ def initialize(base, destination, config = {})
37
+ @base = base
38
+ @config = { verbose: true }.merge(config)
39
+ self.destination = destination
40
+ end
41
+
42
+ # Checks if the destination file already exists.
43
+ #
44
+ # ==== Returns
45
+ # Boolean:: true if the file exists, false otherwise.
46
+ #
47
+ def exists?
48
+ ::File.exist?(destination)
49
+ end
50
+
51
+ def invoke!
52
+ invoke_with_conflict_check do
53
+ require "fileutils"
54
+ ::FileUtils.mkdir_p(destination)
55
+ end
56
+ end
57
+
58
+ def revoke!
59
+ say_status :remove, :red
60
+ require "fileutils"
61
+ ::FileUtils.rm_rf(destination) if !pretend? && exists?
62
+ given_destination
63
+ end
64
+
65
+ protected
66
+
67
+ # Shortcut for pretend.
68
+ #
69
+ def pretend?
70
+ base.options[:pretend]
71
+ end
72
+
73
+ # Sets the absolute destination value from a relative destination value.
74
+ # It also stores the given and relative destination. Let's suppose our
75
+ # script is being executed on "dest", it sets the destination root to
76
+ # "dest". The destination, given_destination and relative_destination
77
+ # are related in the following way:
78
+ #
79
+ # inside "bar" do
80
+ # empty_directory "baz"
81
+ # end
82
+ #
83
+ # destination #=> dest/bar/baz
84
+ # relative_destination #=> bar/baz
85
+ # given_destination #=> baz
86
+ #
87
+ def destination=(destination)
88
+ return unless destination
89
+
90
+ @given_destination = convert_encoded_instructions(destination.to_s)
91
+ @destination = ::File.expand_path(@given_destination, base.destination_root)
92
+ @relative_destination = base.relative_to_original_destination_root(@destination)
93
+ end
94
+
95
+ # Filenames in the encoded form are converted. If you have a file:
96
+ #
97
+ # %file_name%.rb
98
+ #
99
+ # It calls #file_name from the base and replaces %-string with the
100
+ # return value (should be String) of #file_name:
101
+ #
102
+ # user.rb
103
+ #
104
+ # The method referenced can be either public or private.
105
+ #
106
+ def convert_encoded_instructions(filename)
107
+ filename.gsub(%r{%(.*?)%}) do |initial_string|
108
+ method = ::Regexp.last_match(1).strip
109
+ base.respond_to?(method, true) ? base.send(method) : initial_string
110
+ end
111
+ end
112
+
113
+ # Receives a hash of options and just execute the block if some
114
+ # conditions are met.
115
+ #
116
+ def invoke_with_conflict_check(&)
117
+ if exists?
118
+ on_conflict_behavior(&)
119
+ else
120
+ yield unless pretend?
121
+ say_status :create, :green
122
+ end
123
+
124
+ destination
125
+ rescue Errno::EISDIR, Errno::EEXIST
126
+ on_file_clash_behavior
127
+ end
128
+
129
+ def on_file_clash_behavior
130
+ say_status :file_clash, :red
131
+ end
132
+
133
+ # What to do when the destination file already exists.
134
+ #
135
+ def on_conflict_behavior
136
+ say_status :exist, :blue
137
+ end
138
+
139
+ # Shortcut to say_status shell method.
140
+ #
141
+ def say_status(status, color)
142
+ base.shell.say_status status, relative_destination, color if config[:verbose]
143
+ end
144
+ end
145
+ end
146
+ end