Class: Rubypack::RubypackFile
- Inherits:
-
Object
- Object
- Rubypack::RubypackFile
- Defined in:
- lib/rubypack/rubypack_file.rb
Constant Summary collapse
- DEFAULT_FILENAME =
'.rubypack'
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #exclude(*args) ⇒ Object
- #format_rule(rule, files) ⇒ Object
- #include(*args) ⇒ Object
-
#initialize(filename:, output:) ⇒ RubypackFile
constructor
A new instance of RubypackFile.
- #list_files ⇒ Object
- #name(name) ⇒ Object
- #output_filename ⇒ Object
- #read! ⇒ Object
- #run(command) ⇒ Object
- #version(version) ⇒ Object
Constructor Details
#initialize(filename:, output:) ⇒ RubypackFile
Returns a new instance of RubypackFile.
8 9 10 11 12 13 14 |
# File 'lib/rubypack/rubypack_file.rb', line 8 def initialize(filename:, output:) fail("File not found: #{filename}") unless File.exists?(filename) @filename = File.basename(filename) @path = File.dirname(filename) @output = output read! end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/rubypack/rubypack_file.rb', line 6 def filename @filename end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/rubypack/rubypack_file.rb', line 6 def path @path end |
Instance Method Details
#exclude(*args) ⇒ Object
43 44 45 46 |
# File 'lib/rubypack/rubypack_file.rb', line 43 def exclude(*args) @rules = [] unless defined?(@rules) args.each { |entry| @rules << { action: :exclude, filter: entry } } end |
#format_rule(rule, files) ⇒ Object
48 49 50 51 52 |
# File 'lib/rubypack/rubypack_file.rb', line 48 def format_rule(rule, files) @output.status(" Action: #{rule[:action]}, Filter: #{rule[:filter]}") icon = (rule[:action] == :include) ? ' +' : ' -' files.each { |file| @output.verbose(icon, file) } end |
#include(*args) ⇒ Object
38 39 40 41 |
# File 'lib/rubypack/rubypack_file.rb', line 38 def include(*args) @rules = [] unless defined?(@rules) args.each { |entry| @rules << { action: :include, filter: entry } } end |
#list_files ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rubypack/rubypack_file.rb', line 54 def list_files old_pwd = Dir.pwd Dir.chdir(@path) # If not rules were defined, include all by default unless defined?(@rules) files = Dir['**/**'] format_rule({ action: :include, filter: '**/**' }, files) return ffiles end # Determinate if the package must start with all files or with none action = @rules.first[:action] if action == :include files = [] elsif action == :exclude files = Dir['**/**'] format_rule({ action: :include, filter: '**/**' }, files) else fail("Action not implemented: #{action}") end # Include/exclude based on the rules @rules.each do |rule| if rule[:action] == :include filtered_files = Dir[rule[:filter]] files |= filtered_files format_rule(rule, filtered_files) elsif rule[:action] == :exclude filtered_files = Dir[rule[:filter]] files -= filtered_files format_rule(rule, filtered_files) else fail("Action not implemented: #{rule[:action]}") end end # Remove directories files.reject! { |file| File.directory?(file) } # Sort the file names files.sort ensure Dir.chdir(old_pwd) end |
#name(name) ⇒ Object
20 21 22 23 |
# File 'lib/rubypack/rubypack_file.rb', line 20 def name(name) @output.status(' Name:', name) @name = name end |
#output_filename ⇒ Object
30 31 32 |
# File 'lib/rubypack/rubypack_file.rb', line 30 def output_filename "#{@name}-#{@version}" end |
#read! ⇒ Object
16 17 18 |
# File 'lib/rubypack/rubypack_file.rb', line 16 def read! instance_exec { eval(File.read(File.join(@path, @filename))) } end |
#run(command) ⇒ Object
34 35 36 |
# File 'lib/rubypack/rubypack_file.rb', line 34 def run(command) @command = command end |
#version(version) ⇒ Object
25 26 27 28 |
# File 'lib/rubypack/rubypack_file.rb', line 25 def version(version) @output.status(' Version:', version) @version = version end |