class Pandora::Commands::Create

Public Class Methods

new(path, name, organization, open_when_completed = false, verbose = true) click to toggle source

Initializes the command @param [String] directory where the framework will be created. @param [String] framework name. @param [String] framework organization. @param [Bool] open the project once the command finishes. @param [Bool] verbose. @return [Create] initialized command.

# File lib/pandora/commands/create.rb, line 18
def initialize(path, name, organization, open_when_completed = false, verbose = true)
  @path = path
  @name = name
  @organization = organization
  @url = "https://github.com/frameworkoriented/template/archive/master.zip"
  @open_when_completed = open_when_completed
  @verbose = verbose
end

Public Instance Methods

execute() click to toggle source

Executes the command

# File lib/pandora/commands/create.rb, line 28
def execute
  self.download_template
  self.unzip_template
  self.rename_files(self.framework_path)
  self.rename_files_content
  puts "Enjoy #{@name} FRAMEWORK! #yatusabes".colorize(:light_yellow) if @verbose
  system "open #{project_path}" if @open_when_completed
end

Protected Instance Methods

download_template() click to toggle source
# File lib/pandora/commands/create.rb, line 39
def download_template
  puts "=> Downloading framework template".colorize(:green) if @verbose
  response = RestClient.get(@url)
  File.delete(self.zip_path) if File.exist?(self.zip_path)
  zip_file = File.new(self.zip_path, "wb")
  zip_file << response.body
  zip_file.close
  puts "=> Framework template downloaded".colorize(:green) if @verbose
end
framework_path() click to toggle source
# File lib/pandora/commands/create.rb, line 90
def framework_path
  File.join(@path, "#{@name}")
end
project_path() click to toggle source
# File lib/pandora/commands/create.rb, line 86
def project_path
  File.join(self.framework_path, "#{@name}.xcworkspace")
end
rename_files(path) click to toggle source
# File lib/pandora/commands/create.rb, line 63
def rename_files(path)
  Dir[File.join(path, "*")].each do |file_path|
    file_new_path = file_path.gsub("XXXXX", @name)
    FileUtils.mv file_path, file_new_path if file_path != file_new_path
    if File.directory?(file_new_path)
      self.rename_files(file_new_path)
    end
  end
end
rename_files_content() click to toggle source
# File lib/pandora/commands/create.rb, line 73
def rename_files_content
  puts "=> Renaming files content".colorize(:green) if @verbose
  Dir[File.join(self.framework_path, "**/*")]
      .select { |fn| !File.directory?(fn) }
      .each do |file_path|
    text = File.read(file_path)
    new_contents = text.gsub("XXXXX", @name)
    new_contents = new_contents.gsub("YYYYY", @organization)
    File.open(file_path, "w") {|file| file.puts new_contents }
  end
  puts "=> Contents renamed".colorize(:green) if @verbose
end
unzip_template() click to toggle source
# File lib/pandora/commands/create.rb, line 49
def unzip_template
  puts "=> Uncompressing template".colorize(:green) if @verbose
  Zip::File.open(self.zip_path) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(@path, f.name)
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
  FileUtils.remove(self.zip_path) if File.exist?(self.zip_path)
  FileUtils.remove_dir self.framework_path if File.exist?(self.framework_path)
  FileUtils.mv File.join(@path, "template-master"), self.framework_path
  puts '=> Template uncompressed'.colorize(:green) if @verbose
end
zip_path() click to toggle source
# File lib/pandora/commands/create.rb, line 94
def zip_path
  File.join(@path, "#{@name}.zip")
end