Sha256: 51520b37b7645dd1744f250fb883c586d9b846dffaa2020db15f9c1bb59a8354

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

class TokensWriter

    def write_for(environment, master_file, tokens, globals, check_usage)		
		@master_file = master_file
		@environment = environment

		output_file = @master_file.gsub('.master', environment.empty? ? '' : ".#{environment}")
		text = File.read(@master_file)
		
		check_if_tokens_are_used(text, tokens) if check_usage
		text = replace_tokens(text, tokens)
		text = replace_tokens(text, globals)

        write_to_file(output_file, text)        

		matches = text.scan(/(\$[\w\d\-_\.]+\$)/)
        
        if (matches.any?)
            raise UnReplacedTokenException, "There are untokenised value(s): #{matches.join(', ').strip} still in #{@output_file}!".red
        end
    end

	private
	
    def replace_tokens(text, tokens)		
		tokens.each_pair do |key, value|        
			text.gsub!(/\$#{key}\$/, value.to_s)
        end
		
		text
    end

	def check_if_tokens_are_used(text, tokens)
		tokens.each { |key, value| print_unused_token_warning(key) if !text.include?("$#{key}$") }
	end

	def print_unused_token_warning(key)
		puts "$#{key}$ token is not used for '#{@environment}'".yellow
	end

	def write_to_file(file, text)
		File.open(file, 'w') { |f| f.write(text) } 
	end

end

class UnReplacedTokenException < StandardError  
end 

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
tokenr-0.0.1 lib/tokenr/tokens_writer.rb
rakeoff-0.0.9 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.8 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.7 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.6 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.5 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.4 lib/rakeoff/tokens_writer.rb
rakeoff-0.0.3 lib/rakeoff/tokens_writer.rb