Class: Lolcommits::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/lolcommits/init.rb

Class Method Summary collapse

Class Method Details

.enable_for_all_projectsObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lolcommits/init.rb', line 70

def self.enable_for_all_projects
  projs = git_projects
  puts projs
  puts 'Do you want to enable snapgit for all those repos? (y/n)'
  abort unless STDIN.getch.strip == 'y'

  projs.each do |current|
    enable_for_project(current)
  end
  puts "Successfully enabled snapgit for #{projs.count} projects 🎉"
end

.enable_for_local_folderObject



65
66
67
68
# File 'lib/lolcommits/init.rb', line 65

def self.enable_for_local_folder
  enable_for_project('.')
  puts "Successfully enabled snapgit 🎉"
end

.enable_for_project(path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lolcommits/init.rb', line 82

def self.enable_for_project(path)
  puts "Enabling snapgit for '#{File.expand_path(path)}'..."
  Dir.chdir(path) do
    # Add the `lolcommits --capture` to the post-hook
    Lolcommits::Installation.do_enable

    # Copy the config.yml to the ~/.lolcommits/[project] folder
    to_path = Lolcommits::Configuration.new.loldir # this will use the current dir by default
    # rubocop:disable Lint/HandleExceptions
    begin
      FileUtils.cp(@config_path, to_path)
    rescue ArgumentError # if the file is the same
    end
    # rubocop:enable Lint/HandleExceptions
  end
end

.git_projectsObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/lolcommits/init.rb', line 99

def self.git_projects
  puts 'Searching for git repos'
  # We're using README.md assuming that every repo has one
  # This is due to Spotlight not finding hidden files (.git)
  potential = `mdfind -name "README.md" -onlyin ~`.split("\n")

  # After we have all README.md we look for a .git folder in
  # each of those
  potential.collect do |current|
    path = File.expand_path('..', current)
    path if File.directory?(File.join(path, '.git'))
  end.delete_if(&:nil?)
end

.request_auth_tokenssuccess or not

Returns:

  • (success or not)


60
61
62
63
# File 'lib/lolcommits/init.rb', line 60

def self.request_auth_tokens
  $stdout.sync = true
  Configuration.new.do_configure!('snapgit')
end

.run_setup(lolcommits_binary) ⇒ Object

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lolcommits/init.rb', line 9

def self.run_setup(lolcommits_binary)
  require 'io/console'
  @lolcommits_binary = lolcommits_binary
  set_lolcommits_env_config

  puts 'This setup will run through the necessary steps to get you up and running'
  puts 'Please follow the wizard to authenticate Twitter and Gravatar'
  puts "If you don't want to use Gravatar, just don't provide any values"
  puts 'Confirm with Enter'
  STDIN.getch

  Dir.mktmpdir do |_tmp_dir|
    `git init` # just to make lolcommits believe we're in a git folder
    result = request_auth_tokens

    if result == false
      puts 'Setup failed - please try again'
      abort
    end
    @config_path = result
  end

  puts '-'
  puts 'Successfully generated keys... now setting up your git projects:'
  puts '-'

  puts 'Do you want snapgit to automatically enable itself for all local git repositories? (y/n)'
  if STDIN.getch.strip == 'y'
    enable_for_all_projects
  elsif File.directory?('.git')
    puts 'Do you want to enable snapgit just for the local directory? (y/n)'
    if STDIN.getch.strip == 'y'
      enable_for_local_folder
      return
    end
  else
    puts '-'
    puts 'Please navigate to the project you want to enable snapgit for'
    puts 'and run `snapgit init`'
    abort
  end
end

.set_lolcommits_env_configObject

rubocop:enable Metrics/MethodLength



53
54
55
56
57
# File 'lib/lolcommits/init.rb', line 53

def self.set_lolcommits_env_config
  ENV['LOLCOMMITS_INIT_PARAMS'] = ' --delay 1' # this is required to actually work on a Mac
  ENV['LOLCOMMITS_INIT_PARAMS'] += ' --stealth' # we don't want any output
  ENV['LOLCOMMITS_INIT_PARAMS'] += ' &' # this way the delay is not noticable
end