Class: Application::RepoBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/makegit/repo_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name, git_user, git_token) ⇒ RepoBuilder

Returns a new instance of RepoBuilder.



5
6
7
8
9
# File 'lib/makegit/repo_builder.rb', line 5

def initialize(project_name, git_user, git_token)
  @project_name = project_name
  @git_user = git_user
  @git_token = git_token
end

Instance Attribute Details

#git_tokenObject (readonly)

Returns the value of attribute git_token.



3
4
5
# File 'lib/makegit/repo_builder.rb', line 3

def git_token
  @git_token
end

#git_userObject (readonly)

Returns the value of attribute git_user.



3
4
5
# File 'lib/makegit/repo_builder.rb', line 3

def git_user
  @git_user
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



3
4
5
# File 'lib/makegit/repo_builder.rb', line 3

def project_name
  @project_name
end

Instance Method Details

#buildObject



11
12
13
14
15
16
# File 'lib/makegit/repo_builder.rb', line 11

def build
  Dir.chdir(project_name) do
    new_local_repo
    new_remote_repo
  end
end

#new_local_repoObject



18
19
20
21
# File 'lib/makegit/repo_builder.rb', line 18

def new_local_repo
  STDOUT.puts "Creating local git repository"
  system("git init")
end

#new_remote_repoObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/makegit/repo_builder.rb', line 23

def new_remote_repo
  current_dir = File.basename(Dir.getwd)
  STDOUT.puts "Creating remote git repository"
  repo_req = `curl -u #{git_user}:#{git_token} https://api.github.com/user/repos -d '{\"name\":\"#{current_dir}\"}'`
  response = JSON.parse(repo_req)
   if response['message'] || response['errors']
     puts response['message']
     response['errors'] && response['errors'].each do |err|
       puts "#{err['message']}"
     end
     raise "Error creating GitHub repository"
   else
    system("git add --all")
    system("git commit -m \'Initial commit\'")
    system("git remote add origin [email protected]:#{git_user}/#{current_dir}.git")
    system("git push -u origin master")
  end
end