class Enoki::CLI

Constants

EXT_TEMPLATE
NAME_PLACEHOLDER
SOURCE_CODE_EXT_LIST

Public Class Methods

source_root() click to toggle source
# File lib/enoki/cli/init.rb, line 5
def self.source_root
  File.expand_path("../", File.dirname(__FILE__))
end

Public Instance Methods

add_file_reference(path, target_name) click to toggle source
# File lib/enoki/cli/generate.rb, line 42
def add_file_reference(path, target_name)
  path_list = path.to_path.split(File::SEPARATOR)
  group = project.root_object.main_group

  while path_list.size > 1
    dir = path_list.shift
    next_group = group.children.find { |g| g.path == dir } || group.new_group(dir, dir)
    group = next_group
  end

  file = path_list.shift
  file_ref = group.files.find { |f| f.path == file } || group.new_reference(file)

  if SOURCE_CODE_EXT_LIST.include?(path.extname)
    target = project.targets.find { |t| t.name == target_name } || project.targets.first
    if target
      target.source_build_phase.add_file_reference(file_ref, true)
    end
  end
end
context_for_template(name) click to toggle source
# File lib/enoki/cli/generate.rb, line 38
def context_for_template(name)
  binding
end
generate(template_name, name) click to toggle source
# File lib/enoki/cli/generate.rb, line 13
def generate(template_name, name)
  unless templates.include? template_name
    say_error "Template not found"
    exit 1
  end

  selected_template_root = Pathname.new(File.expand_path(template_name, template_dir))
  project_root = Pathname.new(project_root_dir)
  file_list = Dir.glob(selected_template_root.join("**/*#{EXT_TEMPLATE}")).map do |file|
    Pathname.new(file).relative_path_from(selected_template_root)
  end

  file_list.each do |path|
    resolved_path = Pathname.new(path.to_path.gsub(NAME_PLACEHOLDER, name).chomp(EXT_TEMPLATE))
    source_path = path.expand_path(selected_template_root).to_path
    dest_path = resolved_path.expand_path(project_root).to_path

    template(source_path, dest_path, context: context_for_template(name))
    add_file_reference(resolved_path, options[:target])
  end

  project.save
end
init() click to toggle source
# File lib/enoki/cli/init.rb, line 10
def init
  if File.exist?(settings_file)
    exit 0 unless yes?("Settings file already exist. Re-create settings file? (y/N)", :yellow)
  end

  default_template_dir = "./templates"
  default_project_root_dir = "./"
  default_project_file_path = "./YourProject.xcodeproj"

  template_dir = ask("Your template directory?", default: default_template_dir, path: true)
  project_root_dir = ask("Your project root directory?", default: default_project_root_dir, path: true)
  project_file_path = ask("Your project file path?", default: default_project_file_path, path: true)

  template("templates/.enoki.yml.tt", settings_file, context: binding)
end
list() click to toggle source
# File lib/enoki/cli/list.rb, line 4
def list
  puts templates
end
project() click to toggle source
# File lib/enoki/cli/common.rb, line 43
def project
  @project ||= Xcodeproj::Project.open(project_file_path)
end
project_file_path() click to toggle source
# File lib/enoki/cli/common.rb, line 30
def project_file_path
  @project_file_path ||= begin
    if settings["project_file_path"].nil?
      raise "Error: Required settings not found in .enoki.yml! (project_file_path)"
    end
    File.absolute_path(settings["project_file_path"])
  end
end
project_root_dir() click to toggle source
# File lib/enoki/cli/common.rb, line 26
def project_root_dir
  @project_root_dir ||= File.absolute_path(settings["project_root_dir"] || "./")
end
say_error(message) click to toggle source
# File lib/enoki/cli/shell.rb, line 4
def say_error(message)
  say(message, :red)
end
say_warning(message) click to toggle source
# File lib/enoki/cli/shell.rb, line 8
def say_warning(message)
  say(message, :yellow)
end
settings() click to toggle source
# File lib/enoki/cli/common.rb, line 11
def settings
  @settings ||= begin
    unless File.exist?(settings_file)
      say_error("Settings file not found. Please create '.enoki.yml' or run 'enoki init'")
      exit 1
    end

    YAML.load_file(settings_file)
  end
end
settings_file() click to toggle source
# File lib/enoki/cli/common.rb, line 7
def settings_file
  "./.enoki.yml"
end
show() click to toggle source
# File lib/enoki/cli/show.rb, line 4
def show
  [:template_dir, :project_root_dir, :project_file_path].each do |e|
    puts("#{e}: #{send(e)}")
  end
end
template_dir() click to toggle source
# File lib/enoki/cli/common.rb, line 22
def template_dir
  @template_dir ||= File.absolute_path(settings["template_dir"] || "./templates")
end
templates() click to toggle source
# File lib/enoki/cli/common.rb, line 39
def templates
  Dir.glob(File.join(template_dir, "*/")).map { |f| File.basename(f) }
end
version() click to toggle source
# File lib/enoki/cli.rb, line 13
def version
  puts Enoki::VERSION
end