Class: JekyllAuth::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_auth/commands.rb

Constant Summary collapse

FILES =
%w{Rakefile config.ru .gitignore .env}
VARS =
%w{client_id client_secret team_id org_id}

Class Method Summary collapse

Class Method Details

.changed?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/jekyll_auth/commands.rb', line 15

def self.changed?
  execute_command("git", "status", destination, "--porcelain").length != 0
rescue
  false
end

.configure_heroku(options) ⇒ Object



67
68
69
70
71
# File 'lib/jekyll_auth/commands.rb', line 67

def self.configure_heroku(options)
  VARS.each do |var|
    execute_command "heroku", "config:set", "GITHUB_#{var.upcase}=#{options[var]}" if options[var]
  end
end

.copy_templatesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/jekyll_auth/commands.rb', line 27

def self.copy_templates
  FILES.each do |file|
    if File.exist? "#{destination}/#{file}"
      puts "* #{destination}/#{file} already exists... skipping."
    else
      puts "* creating #{destination}/#{file}"
      FileUtils.cp "#{source}/#{file}", "#{destination}/#{file}"
    end
  end
end

.destinationObject



11
12
13
# File 'lib/jekyll_auth/commands.rb', line 11

def self.destination
  @destination ||= Dir.pwd
end

.env_var_set?(var) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/jekyll_auth/commands.rb', line 46

def self.env_var_set?(var)
  !(ENV[var].to_s.blank?)
end

.execute_command(*args) ⇒ Object



21
22
23
24
25
# File 'lib/jekyll_auth/commands.rb', line 21

def self.execute_command(*args)
  output, status = Open3.capture2e(*args)
  raise "Command `#{args.join(" ")}` failed: #{output}" if status != 0
  output
end

.heroku_remote_set?Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/jekyll_auth/commands.rb', line 62

def self.heroku_remote_set?
  remotes = execute_command "git", "remote", "-v"
  !!(remotes =~ /^heroku\s/)
end

.init_repoObject



50
51
52
53
54
55
56
# File 'lib/jekyll_auth/commands.rb', line 50

def self.init_repo
  execute_command "git", "init", destination
  FILES.each do |file|
    next if file == ".env"
    execute_command("git", "add", "--", "#{destination}/#{file}")
  end
end

.initial_commitObject



58
59
60
# File 'lib/jekyll_auth/commands.rb', line 58

def self.initial_commit
  execute_command "git", "commit", "-m", "'[Jekyll Auth] Initial setup'"
end

.sourceObject



7
8
9
# File 'lib/jekyll_auth/commands.rb', line 7

def self.source
  @source ||= File.expand_path( "../../templates", File.dirname(__FILE__) )
end

.team_id(org, team) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jekyll_auth/commands.rb', line 38

def self.team_id(org, team)
  client = Octokit::Client.new :access_token => ENV["GITHUB_TOKEN"]
  client.auto_paginate = true
  teams = client.organization_teams org
  found = teams.find { |t| t[:slug] == team }
  found[:id] if found
end