Class: Strap::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/strap/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli = nil) ⇒ Core

Returns a new instance of Core.



9
10
11
# File 'lib/strap/core.rb', line 9

def initialize(cli = nil)
  self.cli = cli
end

Instance Attribute Details

#cliObject

Returns the value of attribute cli.



7
8
9
# File 'lib/strap/core.rb', line 7

def cli
  @cli
end

Instance Method Details

#clone_repo(repo, directory = ".") ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/strap/core.rb', line 46

def clone_repo(repo, directory=".")
  if !repo.nil? && !File.exists?("#{directory}/.git")
    output "- Cloning source repo"
    clone = system("cd #{directory} && git clone #{repo} strap_temp")
    unless clone
      output "- Source repo clone failed", :error
      exit
    end
    remove_tmp = system("cd #{directory} && shopt -s dotglob && mv strap_temp/* . && rm -rf strap_temp/* && rm -rf .git")
    unless remove_tmp
      output "- Removal of temporary clone failed", :error
      exit
    end
  else
    output "- No source repo provided", :error
    exit
  end
end

#commit_to_repo(repo, directory = ".", force = false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/strap/core.rb', line 65

def commit_to_repo(repo, directory=".", force=false)
  if !repo.nil?
    commit = system("cd #{directory} && git init . && git add -A && git commit -m 'Initial commit from Strap'")
    if commit
      output "- Git repo initialized"
      push = system("cd #{directory} && git remote add origin #{repo} && git push #{"-f" if force} origin master")
      if push 
        output "- Project pushed to destination repo"
      else
        output "- Git push to destination repo failed", :error
        exit
      end
    else
      output "- Git init failed", :error
      exit
    end
  end
end

#create_config(path, template = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/strap/core.rb', line 18

def create_config(path, template = nil)
  strapfile = "#{path}/#{CONFIG_FILENAME}"
  output "- Writing new config to #{strapfile}"
  if template
    config = File.expand_path(File.expand_path("~/.strap/#{template}"))
  elsif File.exists?(File.expand_path('~/.strap/default'))
    config = File.expand_path(File.expand_path('~/.strap/default'))
  else
    config = CONFIG_TEMPLATE
  end
  FileUtils.cp(config, strapfile)
end

#create_database(db_user, db_password, db_socket = nil, db_name = nil, db_port = nil, db_host = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/strap/core.rb', line 84

def create_database(db_user, db_password, db_socket=nil, db_name=nil, db_port=nil, db_host=nil)
  if db_user.nil? and db_password.nil? and db_name.nil?
    return false
  end
  mysql = Mysql::new(db_host, db_user, db_password, nil, db_port, db_socket)
  output("- Error connecting to MySQL", :error) unless mysql
  db_exists = mysql.list_dbs.include?(db_name)
  if db_exists
    output "- Database already exists", :error
  else
    mysql.query("CREATE DATABASE #{db_name}")
    mysql.list_dbs.include?(db_name) ? output("- Database created") : output("- Error creating database", :error)
  end
end

#create_project_directory(path) ⇒ Object



13
14
15
16
# File 'lib/strap/core.rb', line 13

def create_project_directory(path)
  output "- Creating project directory: #{path}"
  Dir::mkdir(path)
end

#extract_project_name_from_path(path) ⇒ Object



31
32
33
# File 'lib/strap/core.rb', line 31

def extract_project_name_from_path(path)
  path.match(/\w*$/).to_s
end

#import_to_database(sql, db_user, db_password, db_socket = nil, db_name = nil, db_port = nil, db_host = nil) ⇒ Object



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

def import_to_database(sql, db_user, db_password, db_socket=nil, db_name=nil, db_port=nil, db_host=nil)
  if db_user.nil? and db_password.nil? and db_name.nil? and sql.nil?
    return false
  end
  import = system("mysql -u#{db_user} -p#{db_password} -S#{db_socket} #{db_name} < #{sql}")
  if import
    output "- Database dump imported"
  else
    output "- Database import failed", :error
    exit
  end
end

#update_database_name(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/strap/core.rb', line 35

def update_database_name(path)
  project_name = extract_project_name_from_path(path) 
  bootfile = "#{path}/#{CONFIG_FILENAME}"
  bootfile_text = File.read(bootfile)
  temporary_database_name = /set :db_name,\s*("\w*")/.match(bootfile_text)[0]
  updated_boofile_text = bootfile_text.gsub(/#{temporary_database_name}/, "set :db_name, \"#{project_name}\"")
  File.open(bootfile, "w") do |file| 
    file.puts updated_boofile_text
  end
end