Class: Swiftly::CreateWordpress

Inherits:
Thor::Group
  • Object
show all
Includes:
Helpers, Thor::Actions
Defined in:
lib/swiftly/create_wordpress.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#find_and_replace, #find_and_replace_all, #fix_serialization, #fix_text, #mina, #return_cmd, #swiftly_shell, #unzip, #update_setting, #verifiy_mina_credentials, #zip

Class Method Details

.source_rootstring

Define the source root of this file

Returns:

  • (string)

    The path to this file



24
25
26
27
28
# File 'lib/swiftly/create_wordpress.rb', line 24

def self.source_root

  File.dirname(__FILE__)

end

Instance Method Details

#create_databasevoid

This method returns an undefined value.

Create the database for the project



276
277
278
279
280
281
282
283
284
# File 'lib/swiftly/create_wordpress.rb', line 276

def create_database

  # Create a new database object
  database = Swiftly::Database.new @project_name

  # Create a new database
  database.create :local

end

#dependenciesvoid

This method returns an undefined value.

Take care of any of the project dependencies



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/swiftly/create_wordpress.rb', line 291

def dependencies

  # Change directories into the project directory
  inside File.join @project_path do

    # Run all the possible installs if any of the
    # package managers exists
    run('bundle')            unless !File.exists? 'Gemfile'
    run('bundle exec guard') unless !File.exists? 'Guardfile'
    run('npm install')       unless !File.exists? 'package.json'
    run('bower update')      unless !File.exists? 'bower.json'
  end
end

#get_pluginsvoid

This method returns an undefined value.

Download or get plugins for a location on the hard drive and add it to the Wordpress plugins folder



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/swiftly/create_wordpress.rb', line 182

def get_plugins

  # Change directories into the plugins directory
  inside File.join( @project_path, "wp-content", "plugins" ) do

    # Get all the Wordpress plugins from specified in
    # the config and in the Swiftlyfolder
    plugins = Swiftly::Plugin.all :wordpress

    # Check to see if any plugins are available
    if plugins

      # If plugins are available then
      # loop through all the plugins
      plugins[:wordpress].each do |plugin|

        # If a plugin is zipped up
        if plugin.location =~ /^#{URI::regexp}\.zip$/

          # Set the plugin up to be unzipped
          zipfile = get plugin.location, File.basename( plugin.location )

          # Unzip the plugin if it exists
          unzip zipfile, plugin.name unless File.exist? plugin.name.to_s

          # Remove the zip file if it exists
          remove_file zipfile unless !File.exist? zipfile

        else

          # If the plugin is not zipped then just
          # copy it to the plugin folder
          FileUtils.cp_r( File.join( plugin.location, plugin.name.to_s ), '.' ) unless File.exist? plugin.name.to_s

        end
      end
    end
  end
end

#get_themevoid

This method returns an undefined value.

Download or get theme for a location on the hard drive and add it to the Wordpress theme folder



70
71
72
73
74
75
76
77
78
79
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
175
# File 'lib/swiftly/create_wordpress.rb', line 70

def get_theme

  # Check to see if the template name is default and if it
  # is then change it to the default template name
  @template.name = Swiftly::Template.default_name if @template.name == :default

  # Change directories to inside of the theme directory
  inside File.join( @project_path, 'wp-content', 'themes') do

    # Check to see if the theme is a zip file
    if @template.location =~ /^#{URI::regexp}\.zip$/

      # If the theme is a zip file then download the theme
      zipfile = get @template.location, File.basename( @template.location )

      # Unzip the theme if it has not been unzipped already
      unzip zipfile, @template.name unless File.exist? @template.name.to_s

      # Rmove the zipfile if it still exists
      remove_file zipfile unless !File.exist? zipfile

    else

      # If the theme location does not point to a zip file
      # then the theme to the theme folder
      FileUtils.cp_r( File.join( @template.location, @template.name.to_s ), '.' )

    end

    # Change the theme name from whatever it was named
    # into the the name of the project
    FileUtils.mv( @template.name.to_s, @project_name ) unless !File.exists? @template.name.to_s

    # Change directories
    inside @project_name do

      # Loop through each of these files
      # and delete them if the exist
      [
        '.git',
        '_resources',
        '.gitignore',
        '.htaccess',
        ".#{APP_NAME}",
        ".#{APP_NAME}ignore"
      ].each do |file|

        remove_file file unless !File.exists? file

      end

      # Loop through each of these files
      # and perform a find and replace to change
      # the theme name into the name of the project
      [
        '.htaccess',
        'wp-config.php',
        'bower.json',
        'config.rb',
        'Guardfile',
        'Gemfile',
        'Gemfile.lock'
      ].each do |file|

        gsub_file file, /(#{@template.name})/, @project_name.gsub(/\-|\./, '_') unless !File.exists? file

        FileUtils.mv( file, @project_path ) unless !File.exists? file

      end

      # Move the project specific plugin into the
      # plugins directory if it exists
      FileUtils.mv(
        "#{@template.name}-specific-plugin",
        File.join( @project_path, "wp-content", "plugins", "#{@project_name}-specific-plugin")
      ) unless !File.exists? "#{@template.name}-specific-plugin"

      # Change the name of the theme logo
      # if it exists
      FileUtils.mv(
        File.join( "wp-login-logo-#{@template.name}.png" ) ,
        File.join( "wp-login-logo-#{@project_name}.png" )
      ) unless !File.exists? File.join( "wp-login-logo-#{@template.name}.png" )

      # Perform a find and replace on the function file to
      # change any mentions of the theme name into the name of the project
      gsub_file 'functions.php', /(#{@template.name})/, @project_name

    end

  end

  # Change directories into the project specific plugin
  inside File.join( @project_path, "wp-content", "plugins", "#{@project_name}-specific-plugin") do

    # Change the file name to match the project name
    FileUtils.mv(
      "#{@template.name}-plugin.php",
      "#{@project_name}-plugin.php"
    ) unless !File.exists? "#{@template.name}-plugin.php"

    # Perform a find and replace files to match the project name
    gsub_file "#{@project_name}-plugin.php", /(#{@template.name})/, @project_name.capitalize unless !File.exists? "#{@template.name}-plugin.php"

  end
end

#get_wordpressvoid

This method returns an undefined value.

This method grabs the latest version of wordpress, unzips it and places it into the project directory



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/swiftly/create_wordpress.rb', line 36

def get_wordpress

  # change directory into the project path
  inside @project_path do

    # download Wordpress unless it already exists
    get 'https://wordpress.org/latest.zip', 'latest.zip' unless File.exist? 'wordpress'

    # unzip the zip unless it is already unzipped
    unzip 'latest.zip', 'wordpress' unless File.exist? 'wordpress'

    # remove the zip file if the zip file exists
    remove_file 'latest.zip' unless !File.exist? 'latest.zip'

    # Grab all of the folders out of the wordpress directory
    Dir[File.join('wordpress', '*')].each do |e|

      # Move all of the folders our of the wordpress directory
      # and into the root of the project if it exists
      FileUtils.mv( e, @project_path ) unless File.exist? e.gsub(/^wordpress/, @project_path )

    end

    # Remove the empty wordpress directory if it still exits
    remove_file 'wordpress' unless !(Dir.entries('wordpress') - %w{ . .. }).empty?
  end
end

#wp_configvoid

This method returns an undefined value.

Handle the wp-config file in peperations for database set up



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/swiftly/create_wordpress.rb', line 226

def wp_config

  # Change directories into the project directory
  inside File.join @project_path do

    # Get rid of the sample wp-config file
    remove_file "wp-config-sample.php"

    # Add salts to the wp-config file
    gsub_file 'wp-config.php', /\/\/\s*Insert_Salts_Below/, Net::HTTP.get('api.wordpress.org', '/secret-key/1.1/salt')

    # Change the table prefix for the database
    gsub_file 'wp-config.php', /(table_prefix\s*=\s*')(wp_')/, '\1' + @project_name[0,3] + "_'"

    # Retrieve the server settings for the
    # local database
    settings = Swiftly::Config.load :swiftly

    # Check to see if all of the database settings
    # are present
    if !settings.nil? &&
       !settings[:local][:db_host].nil? &&
       !settings[:local][:db_user].nil? &&
       !settings[:local][:db_pass].nil?

      # If the database settings are present the
      # perform a find and replace on the local wp-config
      # database settings
      gsub_file 'wp-config.php', /(\$local\s*?=[\s|\S]*?)({[\s|\S]*?})/  do |match|

        '$local = \'{
      "db_name": "' + @project_name.gsub(/\-|\./, '_') + '_local_wp",
      "db_host": "' + settings[:local][:db_host] + '",
      "db_user": "' + settings[:local][:db_user] + '",
      "db_pass": "' + settings[:local][:db_pass] + '",
      "domain":  "http://' + @project_name + '.dev",
      "wp_home": "http://' + @project_name + '.dev"
    }'

      end
    end

  end
end