Module: Swiftly::Helpers

Included in:
CLI, Clean, Config, Create, CreateProject, CreateWordpress, Database, Destroy, Init, Project, Pull, Push, Rollback, SSH, Setup
Defined in:
lib/swiftly/app_module.rb

Instance Method Summary collapse

Instance Method Details

#find_and_replace(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/swiftly/app_module.rb', line 46

def find_and_replace(hash)

  if hash[:file] == true

    if File.exists? hash[:input]

      replace = File.read(hash[:input]).gsub(hash[:pattern], hash[:output])

      File.open(hash[:input], "w") {|file| file.puts replace }

      return hash[:input]

    else

        say_status "#{APP_NAME}:", "file #{hash[:input]} does not exist", :yellow

    end

  else

    return hash[:input].gsub(hash[:pattern], hash[:output])

  end
end

#find_and_replace_all(array) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/swiftly/app_module.rb', line 71

def find_and_replace_all(array)

  array.each do |hash|

    find_and_replace(hash)

  end
end

#fix_serialization(file) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/swiftly/app_module.rb', line 218

def fix_serialization file

  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
  string                    = File.read file
  fixed                     = fix_text string

  open file, 'w' do |io|

    io.write fixed

  end

  return file

end

#fix_text(string) ⇒ Object

php escapes: “\” #Backslash, ‘“’ Double quotes, ”'“ Single quotes, ”a“ Bell/alert, ”b“ Backspace, ”r“ Carriage Return, ”n“ New Line, ”s“ Space, ”t“ Tab



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/swiftly/app_module.rb', line 245

def fix_text string

  pattern     = /(s\s*:\s*)(\d+)((\s*:\\*["&])(.*?)(\\?\"\s*;))/
  php_escapes = /(\\"|\\'|\\\\|\\a|\\b|\\n|\\r|\\s|\\t|\\v)/

  string.gsub( pattern ) do |match|

    head  = $1
    tail  = $3
    count = $5.bytesize - $5.scan(php_escapes).length

    "#{head}#{count}#{tail}"
  end
end

#mina(cmd, environment, project_name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/swiftly/app_module.rb', line 176

def mina cmd, environment, project_name

  settings = Swiftly::Project.settings( project_name )

  inside settings[:project][:path] do

    cmd_status = cmd.include?(':') ? cmd.split(':').last : cmd

    mina = <<-EOF.unindent
      mina #{cmd} \
      -f '#{File.join(File.dirname(__FILE__), "Rakefile")}' \
      repo='#{settings[environment][:repo]}' \
      branch='#{settings[environment][:branch]}' \
      user='#{settings[environment][:ssh_user]}' \
      domain='#{settings[environment][:domain].gsub(/http:\/\//, '')}' \
      path='#{settings[environment][:ssh_path]}'
    EOF

    if cmd == 'ssh'

      exec mina

    else

      swiftly_shell mina, cmd_status

    end
  end
end

#return_cmd(cmd) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/swiftly/app_module.rb', line 80

def return_cmd cmd

  cmd_return = ""

  Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|

    exit_status = wait_thr.value

    while line  = stdout_err.gets

      cmd_return = line

    end

    say #spacer

    unless exit_status.success?

      # TURN ON FOR SIMPLE DEBUGGING
      # abort "\n\s\s\s\s#{APP_NAME}: command failed - #{cmd}\n\n"

    end
  end

  cmd_return

end

#swiftly_shell(cmd, command_status = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/swiftly/app_module.rb', line 108

def swiftly_shell(cmd, command_status = nil)

  Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|

    exit_status = wait_thr.value

    while line  = stdout_err.gets

      say_status cmd.split( ' ' ).first.gsub(/^mina/, APP_NAME), line.gsub(/^mina/, APP_NAME).gsub(/----->/, "    \033[34m\e[1m#{command_status}\e[0m\033[0m")

    end

    say #spacer

    unless exit_status.success?

      # TURN ON FOR SIMPLE DEBUGGING
      # abort "\n\s\s\s\s#{APP_NAME}: command failed - #{cmd}\n\n"

    end
  end
end

#unzip(zipfile_name, directory) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/swiftly/app_module.rb', line 32

def unzip zipfile_name, directory

  Zip::File.open( zipfile_name ) do |zipfile|

    zipfile.each do |entry|

      path = entry.name.to_s.gsub(/^.+?#{File::SEPARATOR}/, "#{directory}#{File::SEPARATOR}")

      say_status "unzip", zipfile.extract( entry, path.to_s ) unless File.exist?(path)

    end
  end
end

#update_setting(environment, setting, value) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/swiftly/app_module.rb', line 206

def update_setting environment, setting, value

  config                       = Swiftly::Config.load :swiftly
  config[environment][setting] = value

  File.open(Swiftly::Config.swiftlyfile,'w') do |h|

     h.write config.to_yaml

  end
end

#verifiy_mina_credentials(environment, settings, verbage) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/swiftly/app_module.rb', line 131

def verifiy_mina_credentials environment, settings, verbage

    if settings[environment][:repo].nil?

      say_status "#{APP_NAME}:", "Could not #{verbage} to #{environment} environment, because the repository is not set.", :red
      say #spacer

    end

    if settings[environment][:branch].nil?

      say_status "#{APP_NAME}:", "Could not #{verbage} to #{environment} environment, because the branch is not set.", :red
      say #spacer

    end

    if settings[environment][:ssh_user].nil?

      say_status "#{APP_NAME}:", "Could not #{verbage} to #{environment} environment, because the ssh user is not set.", :red
      say #spacer

    end

    if settings[environment][:ssh_path].nil?

      say_status "#{APP_NAME}:", "Could not #{verbage} to #{environment} environment, because the ssh path is not set.", :red
      say #spacer

    end

    if settings[environment][:domain].nil?

      say_status "#{APP_NAME}:", "Could not #{verbage} to #{environment} environment, because the domain is not set.", :red
      say #spacer

    end

    abort if settings[environment][:repo].nil?
    abort if settings[environment][:branch].nil?
    abort if settings[environment][:ssh_user].nil?
    abort if settings[environment][:domain].nil?
    abort if settings[environment][:ssh_path].nil?

end

#zip(zipfile_name, directory) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/swiftly/app_module.rb', line 20

def zip zipfile_name, directory

  Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|

    Dir[ File.join(directory, '**', '**') ].each do |file|

      zipfile.add(file.sub( directory, '' ), file )

    end
  end
end