Skip to content

Commit

Permalink
Merge pull request #51 from lolcatbois/release
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
NINNiT authored Nov 12, 2020
2 parents fff33cb + 91234b0 commit b084bb8
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 79 deletions.
24 changes: 21 additions & 3 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@

** Description
*ignore-it* is a tool written in ruby which helps in fetching and creating .gitignore files.
Currently .gitignore files will be fetched from [https://gitignore.io].
Currently .gitignore files will be fetched from [[https://gitignore.io]] or from local user-made templates.

** Installation

=gem install ignore-it=

** Usage
=ignore-it -f [language]= fetches a .gitignore file and creates it in the directory where =ignore-it= has been envoked.

=ignore-it -l= shows all the supported languages
#+begin_src
Commands:
ignore-it add [templateName] # Select gitignore template to create a .gitignore file or add a template to an existing .gitignore file
ignore-it help [COMMAND] # Describe available commands or one specific command
ignore-it list # Show List of available .gitignore entries
ignore-it own [fileName] # Select user-created template from the folder specified in ~/.ignore-it/config.yml. Default is ~/.ignore-it/gitignores/.

Options:
[--force]
[--output="OUTPUT"] # Optional Path to directory where .gitignore should be created.
#+end_src

You can chain multiple gitignores (e.g vsocde and csharp) in a single command with =ignore-it add vscode csharp=.

Own gitignore templates can be accessed with =ignore-it own <fileName>=.
Per default, the templates need to be created in =~/.ignore-it/gitignores/= and can be named freely.
To choose a different path for your own created gitignores, you need to specify the absolute path to the directory in =~/ignore-it/config.yml=

** Configuration
Configuration can be done in =~/.ignore-it/config.yml=.

** Development
1) Clone the repo
Expand Down
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

task(default: "test")

Rake::TestTask.new do |task|
task.pattern = "test/*_test.rb"
end
6 changes: 4 additions & 2 deletions bin/ignore-it
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
require 'optparse'
require "ignore_it"

tool = IgnoreIt::Main.new
tool.start
# tool = IgnoreIt::Main.new
# tool.start
tool = IgnoreIt::CLI
tool.start(ARGV)
8 changes: 8 additions & 0 deletions default_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
########################
# CONFIG for ignore-it #
########################

# Set ABSOLUTE path to the directory where own gitignore files are stored
# Default path is ~/.ignore-it/gitignores
# OPTIONS: "default", "absolute_path"
own_gitignore_path: "default"
24 changes: 17 additions & 7 deletions ignore_it.gemspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
Gem::Specification.new do |s|
s.name = 'ignore-it'
s.version = "1.0.2"
s.date = '2020-11-07'
s.summary = 'ignore-it your command line tool for fetching .gitignore files'
s.version = "1.1.0"
s.date = '2020-11-13'
s.summary = 'ignore-it your command line tool for creating .gitignore files'
s.description = <<-EOF
Feel it's sometimes cumbersome to browse to a website, only to download a .gitignore?
We've got your back!
ignore-it is a small cli tool, which helps in fetching and creating .gitignore files from gitignore.io or local custom templates.
We try to keep runtime dependencies as small as possible and are using mostly standard ruby libraries.
EOF
s.authors = ["Felix Macho", "Simon Sölder"]
s.metadata = { "source_code_uri" => "https://github.com/lolcatbois/ignore-it" }
s.files = Dir.glob("{bin,lib}/**/*")
s.files += ["./default_config.yml"]
s.licenses = ['MIT']
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
s.test_files = s.files.grep(%r{^test/})
s.require_paths = ["lib"]

# DEPENDENCIES
s.add_development_dependency("rake", "~> 11.3.0")
s.add_development_dependency("rubocop", "~> 1.2")
s.add_runtime_dependency('colorize', "~> 0.8.1")
# DEV DEPENDENCIES
s.add_development_dependency("rake", "~>11.2.2")
s.add_development_dependency("minitest")

# RUNTIME DEPENDENCIES
s.add_runtime_dependency('colorize', "~>0.8.1")
s.add_runtime_dependency('thor', "~>1.0.1")
end
86 changes: 62 additions & 24 deletions lib/ignore_it.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,80 @@
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'colorize'
require 'readline'
require 'ignore_it/list'
require 'ignore_it/config'
require 'ignore_it/creator'
require 'thor'

module IgnoreIt
class Main
# constructor
def initialize
@url = "https://www.toptal.com/developers/gitignore/api/list?format=json"
@options = {}
@list = List.new
class CLI < Thor
def initialize(*args)
super
@config = Config.new
@creator = Creator.new
@list = List.new
$glob_settings[:output] = "./.gitignore"
end

def start
class_options force: :boolean, output: :string

ARGV << '-h' if ARGV.empty?
desc "add [templateName]", "Select gitignore template to create a .gitignore file
or add a template to an existing .gitignore file"
def add(*templateName)
if options[:output]
return false unless @creator.check_output_path(options[:output])
$glob_settings[:output] = if options[:output][-1] == '/'
options[:output] + '.gitignore'
else
options[:output] + '/.gitignore'
end
end
if options[:force]
$glob_settings[:force] = true
end
templateName.each do |name|
name = name.downcase
if @list.check_list(name)
@creator.create_api_ignore(name)
else
puts "The template #{name} you tried to fetch does not exist".colorize(:red)
puts "Please checkout the available templates with " + "ignore-it list".colorize(:green)
end
end
end

OptionParser.new do |parser|
parser.banner = "How to Use ignore-it: Pass one of the following options: e.g => ignore-it -f csharp"
parser.on(
"-f ", "--file FILE", "Select gitignore template to fetch"
) do |file|
file = file.downcase
if @list.check_list(file)
@creator.create_ignore(file)
else
puts "The template you tried to fetch does not exist".colorize(:red)
puts "Please checkout the available templates with " + "ignore-it -l".colorize(:green)
end
# @options[:file] = true
desc "own [fileName]", "Select user-created template from the folder specified in ~/.ignore-it/config.yml. Default is ~/.ignore-it/gitignores/."
def own(*fileName)
if options[:output]
return false unless @creator.check_output_path(options[:output])
$glob_settings[:output] = if options[:output][-1] == '/'
options[:output] + '.gitignore'
else
options[:output] + '/.gitignore'
end
parser.on("-l", "--list", "Show List of available .gitignore entries") do
@list.show_list
end
if options[:force] == true
$glob_settings[:force] = true
end
fileName.each do |name|
if @list.check_own_files(name)
@creator.create_own_ignore(name)
else
puts "The template #{name} you tried to create does not exist".colorize(:red)
puts "The following templates are available:".colorize(:red)
@list.show_own_files
end
end.parse!
end
end

desc "list", "Show List of available .gitignore entries"
def list
puts "---- Available templates from gitignore.io: ----"
@list.show_list
puts "---- Available user templates (see ~/.ignore-it/config.yml) ----"
@list.show_own_files
end
end
end
49 changes: 49 additions & 0 deletions lib/ignore_it/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true
require 'yaml'

module IgnoreIt
class Config
attr_accessor :config

def initialize
$glob_settings = {}
create_initial_config
load_config
end

def print_config
puts $glob_settings.to_yaml
end

# Load user config from config directory
def load_config
Dir.chdir(Dir.home) do
if File.exist?(".ignore-it/config.yml")
$glob_settings = YAML.load_file(".ignore-it/config.yml")
else
puts "Failed to load user config in ~/.ignore-it/config.yml".colorize(:red)
puts "Defaulting...".colorize(:red)
$glob_settings = YAML.load_file(find_gem_root + "/default_config.yml")
end
end
end

# Find the gems install directory
def find_gem_root
spec = Gem::Specification.find_by_name("ignore-it")
spec.gem_dir
end

# Create initial user config and folders in home directory
def create_initial_config
Dir.chdir(Dir.home) do
unless Dir.exist?(".ignore-it")
Dir.mkdir(".ignore-it")
Dir.mkdir(".ignore-it/gitignores")
defaultConfig = File.read(find_gem_root + "/default_config.yml")
File.write(".ignore-it/config.yml", defaultConfig)
end
end
end
end
end
107 changes: 69 additions & 38 deletions lib/ignore_it/creator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# lib/creation.rb
require 'ignore_it/list'
require 'readline'
Expand All @@ -6,55 +7,85 @@
module IgnoreIt
class Creator
def initialize
list = List.new
@jsonResponse = list.jsonResponse
@list = List.new
@jsonResponse = @list.jsonResponse
end

# Code here
def create_ignore(name)
template = @jsonResponse[name]
contents = template["contents"]

def create_file(contents, name)
puts "Creating .gitignore for " + name.colorize(:green)

if File.exist?(".gitignore")
# Store the state of the terminal
sttySave = %x(stty -g).chomp
overwrite = false

begin
puts "File already exists! Overwrite? [y/n]?"
while (line = Readline.readline('> ', true).downcase)
# if (line.empty? or line != "y" or line != "n")

if line == "y"
overwrite = true
break
# puts "yo"
elsif line == "n"
break
# puts "ney"
elsif (line != "y") || (line != "n")
puts "Please provide a correct format (y or n)".colorize(:red)
# puts "wut"
unless $glob_settings[:force]
if File.exist?($glob_settings[:output])
sttySave = %x(stty -g).chomp # Store the state of the terminal
overwrite = false
append = false
begin
puts "File" + " .gitignore ".colorize(:yellow) + "already exists!"
puts "Overwrite or append? [y => yes | a => append | n => no]?"
while (line = Readline.readline('> ', true).downcase)
if line == "y"
overwrite = true
break
elsif line == "n"
break
elsif line == "a"
append = true
break
elsif (line != "y") || (line != "n") || (line != "a")
puts "Please provide a correct format (y or n)".colorize(:red)
end
end
rescue Interrupt
system('stty', sttySave) # Restore
exit
end
rescue Interrupt => e
system('stty', sttySave) # Restore
exit
end

if overwrite
File.write("./.gitignore", contents)
puts ".gitignore has been created!".colorize(:green)
if overwrite
File.write($glob_settings[:output], contents)
puts ".gitignore has been created!".colorize(:green)
elsif append
gitignoreContents = File.read($glob_settings[:output])
puts "Adding .gitignore content from " + name.colorize(:green) + " to existing .gitignore File"
gitignoreContents += contents
File.write($glob_settings[:output], gitignoreContents)
else
puts ".gitignore has NOT been created! Terminating process!".colorize(:red)
end
else
puts ".gitignore has NOT been created! Terminating process!".colorize(:red)
File.write($glob_settings[:output], contents)
puts ".gitignore has been created!".colorize(:green)
end

else
File.write("./.gitignore", contents)
File.write($glob_settings[:output], contents)
puts ".gitignore has been created!".colorize(:green)
end
end

def create_own_ignore(name)
contents = ""
if $glob_settings["own_gitignore_path"] == "default"
Dir.chdir(Dir.home) do
contents = File.read(".ignore-it/gitignores/" + name)
end
else
Dir.chdir($glob_settings["own_gitignore_path"]) do
contents = File.read(name)
end
end
create_file(contents, name)
end

def create_api_ignore(name)
template = @jsonResponse[name]
contents = template["contents"]
create_file(contents, name)
end

def check_output_path(name)
if Dir.exist?(name)
true
else
puts "The Output Path you provided does currently not exist, please create it manually before using --output".colorize(:red)
end
end
end
end
Loading

0 comments on commit b084bb8

Please sign in to comment.