Class: WSlaveNew

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

Instance Method Summary collapse

Constructor Details

#initialize(path, version = '', wspath = '', wppath = '') ⇒ WSlaveNew

Returns a new instance of WSlaveNew.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wslave_new.rb', line 9

def initialize(path, version = '', wspath = '', wppath = '')
  puts '⚙ Initializing Toolchain・・・'

  if (wspath != '')
    manual_path = true
    if ((Pathname.new(wspath)).absolute?)
      base_path = File.expand_path "#{wspath}/base/"
      template_path = File.expand_path "#{wspath}/templates/"
    else
      base_path = File.expand_path "#{path}/#{wspath}/base/"
      template_path = File.expand_path "#{path}/#{wspath}/templates/"
    end
  else
    manual_path = false
    wspath = "#{__dir__}/.."
    base_path = File.expand_path "#{wspath}/base/"
    template_path = File.expand_path "#{wspath}/templates/"
  end

  FileUtils.mkdir_p path

  Dir.chdir path

  puts "  > Setting up WordPress WSlave setup in: #{path}"
  FileUtils.cp_r Dir.glob("#{base_path}/*"), path
  FileUtils.cp_r Dir.glob("#{template_path}/*"), path
  add_path_to_Gemspec(wspath, path) if manual_path

  spec = Gem::Specification::load("#{wspath}/wslave.gemspec")
  File.open("#{path}/config/.wslave", 'w') {|f| f.write(spec.version)}

  `cd #{path} && git init && git add --all && git commit -am "initial commit by wslave"`

  if (wppath != '')
    wppath = File.expand_path(wppath)
    puts "  >> Checking wppath (#{wppath}) ..."
    if (File.directory? wppath)
      puts "  >> wppath is a folder. Copying..."
      Dir.chdir path
      FileUtils.cp_r wppath, "public/wordpress"
      Dir.chdir "public/wordpress"
      `git clean -fdx; git stash`
      `git checkout master`
      `git pull`
    end
  end

  `cd #{path} && git submodule add https://github.com/WordPress/WordPress.git public/wordpress`
  `cd #{path} && git submodule update --init --recursive public/wordpress`
  if (version == 'edge' || version == 'master')
    `cd #{path}/public/wordpress && git checkout master`
  elsif version != ''
    `cd #{path}/public/wordpress && git checkout #{version}`
  else
    `cd #{path}/public/wordpress && git checkout #{get_stable_branch_version("#{path}/public/wordpress")}-branch`
  end

  puts "  > Preparing detached content directory"
  FileUtils.cp_r("#{path}/public/wordpress/wp-content", "#{path}/public/wp-content")
  FileUtils.mkdir("#{path}/public/wp-content/uploads") unless Dir.exist?("#{path}/public/wp-content/uploads")
  FileUtils.touch("#{path}/public/wp-content/uploads/.gitkeep")
  FileUtils.mkdir("#{path}/public/wp-content/upgrade") unless Dir.exist?("#{path}/public/wp-content/upgrade")
  FileUtils.touch("#{path}/public/wp-content/upgrade/.gitkeep")
  Dir.chdir path

  puts "  > Preparing static data directory"
  FileUtils.mkdir("#{path}/public/data") unless Dir.exist?("#{path}/public/data")

  puts "  > Setting permissions"
  WSlaveTools.set_dev_perms

  `cd #{path} && git add --all && git commit -am "Add and initialize WordPress#{version}"`
  puts "  > Done!"
end

Instance Method Details

#add_path_to_Gemspec(wspath, path) ⇒ Object



84
85
86
87
88
# File 'lib/wslave_new.rb', line 84

def add_path_to_Gemspec(wspath, path)
  gemtext = File.read("#{path}/Gemfile")
  gemtext.gsub!("gem 'wslave'", "gem 'wslave', path: '#{wspath}'")
  File.open("#{path}/Gemfile", "w") {|gemfile| gemfile.puts gemtext}
end

#get_stable_branch_version(path) ⇒ Object



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
# File 'lib/wslave_new.rb', line 90

def get_stable_branch_version(path)
  latest_major = 5
  latest_minor = 7

  reg = /^(\d*)\.(\d)-branch$/
  puts "> Checking for WordPress versions in: #{path}"
  cdir = Dir.pwd()
  Dir.chdir(path)
  g = Git.open("./")
  g.branches.remote.each do |branch|
    ver = reg.match(branch.name)
    if (ver) # If the branch matched the x.y-branch pattern
      if ((ver[1].to_i >= latest_major) && (ver[2].to_i > latest_minor))
        latest_major = ver[1].to_i
        latest_minor = ver[2].to_i
      end
    end

  end
  Dir.chdir(cdir)

  latest = "#{latest_major}.#{latest_minor}"
  puts "> Detected latest WordPress version as: #{latest}"
  latest
end