Class: Marv::Project::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/marv/project/actions.rb

Instance Method Summary collapse

Constructor Details

#initialize(project, builder) ⇒ Actions

Initialize project actions



8
9
10
11
12
13
# File 'lib/marv/project/actions.rb', line 8

def initialize(project, builder)
  @project = project
  @task = project.task
  @builder = builder
  @global = Marv::Global.new(project.task)
end

Instance Method Details

#build_to_temp_dirObject

Built to a temporary directory



134
135
136
137
138
139
140
141
# File 'lib/marv/project/actions.rb', line 134

def build_to_temp_dir
  @builder.build_project

  # Copy build files to temporary directory
  @task.shell.mute do
    @task.directory @project.build_path, ::File.join(@project.package_path, @package_name), :force => true
  end
end

Create project link



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/marv/project/actions.rb', line 87

def create_link
  unless ::File.directory?(@project.build_path)
    @task.shell.mute do
      @task.empty_directory @project.build_path
    end
  end

  begin
    @task.create_link @link_target, @project.build_path
  rescue Exception => e
    @task.say_error "An error occured while creating project link", e.message, false. true
    abort
  end
end

#create_packageObject

Create package



103
104
105
106
107
108
109
110
# File 'lib/marv/project/actions.rb', line 103

def create_package
  create_package_dir
  set_package_filename
  build_to_temp_dir
  create_temp_zip
  create_zip_file
  remove_temp_files
end

#create_package_dirObject

Create package directory



113
114
115
116
117
118
119
# File 'lib/marv/project/actions.rb', line 113

def create_package_dir
  @task.shell.mute do
    unless ::File.directory?(@project.package_path)
      @task.empty_directory @project.package_path
    end
  end
end

#create_temp_zipObject

Create temporary package



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/marv/project/actions.rb', line 144

def create_temp_zip
  tmp_filename = ::File.join(@project.package_path, "#{@package_name}.tmp")

  # Create an temporary file
  ::Zip.continue_on_exists_proc = true
  ::Zip::File.open(tmp_filename, Zip::File::CREATE) do |zip|
    # Get all filenames
    filenames = ::Dir.glob(::File.join(@project.package_path, @package_name, '**', '*'))

    # Add each file in the zip file
    filenames.each do |filename|
      zip.add filename.gsub("#{@project.package_path}/", ''), filename
    end
  end
end

#create_zip_fileObject

Create the package



161
162
163
164
# File 'lib/marv/project/actions.rb', line 161

def create_zip_file
  zip_filename = ::File.join(@project.package_path, "#{@package_name}.zip")
  @task.copy_file ::File.join(@project.package_path, "#{@package_name}.tmp"), zip_filename
end

#get_package_nameObject

Get package name



129
130
131
# File 'lib/marv/project/actions.rb', line 129

def get_package_name
  ::File.basename(@project.root)
end

Link project



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/marv/project/actions.rb', line 16

def link(dir)
  @link_dir = dir

  @task.say_warning "This will link project #{@project.project_id} to server #{dir}."

  link_options
  link_target

  @task.say_empty
  create_link
end

Link global



66
67
68
69
70
# File 'lib/marv/project/actions.rb', line 66

def link_global
  if @link_dir == 'global'
    ::File.join(@global.global_path, @link_options[:folder], ::File.basename(@project.root))
  end
end

Ask for link details



38
39
40
41
42
43
# File 'lib/marv/project/actions.rb', line 38

def link_options
  options = {}
  options[:folder] = @task.ask_input "Where do you want to link your project?", :limited_to => ["themes", "plugins"], :default => "themes"

  @link_options = options
end

Link target



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/marv/project/actions.rb', line 73

def link_target
  target = link_to_server unless link_to_server.nil?
  target = link_to_folder unless link_to_folder.nil?
  target = link_global unless link_global.nil?

  if target.nil?
    @task.say_error "Destination server does not exist!", nil, false, true
    abort
  end

  @link_target = target
end

Link to wordpress



57
58
59
60
61
62
63
# File 'lib/marv/project/actions.rb', line 57

def link_to_folder
  unless @link_dir == 'global'
    if ::File.directory?(@link_dir)
      ::File.join(@link_dir, 'wp-content', @link_options[:folder], ::File.basename(@project.root))
    end
  end
end

Link to server



46
47
48
49
50
51
52
53
54
# File 'lib/marv/project/actions.rb', line 46

def link_to_server
  unless @link_dir == 'global'
    path = ::File.join(@global.servers_path, @link_dir, 'wp-content', @link_options[:folder])

    if ::File.directory?(path)
      ::File.join(path, ::File.basename(@project.root))
    end
  end
end

#package(filename) ⇒ Object

Packgage project



29
30
31
32
33
34
35
# File 'lib/marv/project/actions.rb', line 29

def package(filename)
  @package_name = filename
  pkg_name = filename || get_package_name

  @task.say_warning "This will package project #{@project.project_id} as #{pkg_name}.zip."
  create_package
end

#remove_temp_filesObject

Remove temporary build directory



167
168
169
170
171
172
# File 'lib/marv/project/actions.rb', line 167

def remove_temp_files
  @task.shell.mute do
    @task.remove_dir ::File.join(@project.package_path, @package_name)
    @task.remove_file ::File.join(@project.package_path, "#{@package_name}.tmp")
  end
end

#set_package_filenameObject

Set the package file name



122
123
124
125
126
# File 'lib/marv/project/actions.rb', line 122

def set_package_filename
  if @package_name.nil?
    @package_name = get_package_name
  end
end