Sha256: 2b8ec7734d11da831a60d3f5a35b37b7d3763cbd56d55b901dda80a72eb09efc

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

require "Date"

module Sign
  class Generator
    def create(license, name, year)
      raise ArgumentError, "#{license} is not available." unless File.readable?(license)

      license_template = File.read(license)
      name = get_name if name.nil?
      year = get_year if year.nil?
      
      if placeholders_exist?(license_template)
        new_license = license_template.gsub("[AUTHOR]", name)
        new_license = new_license.gsub("[YEAR]", year)
      else
        new_license = license_template
      end
      
      create_new_file(new_license)
    end
    
    def placeholders_exist?(license_template)
      placeholders = ["[YEAR]", "[AUTHOR]"]
  
      placeholders.any? { |placeholder| license_template.include?(placeholder) }
    end
    
    def create_new_file(new_license)
      new_file = File.new("LICENSE", "w")
      new_file.puts(new_license)
      new_file.close
      puts "License created \033[91m<3\033[0m"
    end
    
    def get_name
      if gitconfig_exist?
        find_name
      else
        return ""
      end
    end
    
    def find_name
      File.foreach("#{gitconfig_path}/.gitconfig").detect do |line| 
        if line.match("\tname")
          return line.scan(/= (.*)/).last[0]
        end
      end
    end
    
    def get_year
      return Date.today.year.to_s
    end
    
    def gitconfig_exist?
      File.exist?("#{gitconfig_path}/.gitconfig")
    end
    
    def gitconfig_path
      File.expand_path("~/", __dir__)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sign-1.0.3 lib/sign/generator.rb
sign-1.0.2 lib/sign/generator.rb
sign-1.0.1 lib/sign/generator.rb