Sha256: 1d185ece0ad41fbd28ee457ce0ed56ccc3d4bf4c298542aa1e2afcf1d84781c0

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

#!/usr/bin/env ruby

require 'fileutils'
require 'active_support/core_ext'

class DaemonCreator
  def self.create(*args)
    new(*args).create
  end

  def initialize(pwd, daemon_name)
    @pwd = pwd
    @template_dir = File.expand_path('../../template', __FILE__)
    @daemon_name = daemon_name
    @daemon_dir = "#{pwd}/#{daemon_name}"
  end

  def create
    error_exit("Please provide a daemon name. e.g. larva spawn my_daemon_name") if @daemon_name.blank?
    error_exit("Directory #{@daemon_dir} already exists. Please remove it before continuing.") if File.exists?(@daemon_dir)

    # Copy template directory
    FileUtils.cp_r(@template_dir, @daemon_dir)
    rename_directories
    rename_files
    rename_file_contents
  end

  private


  def rename_directories
    Dir.glob("#{@daemon_dir}/**/*/").each do |original_path|
      new_path = original_path.gsub('larva_spawn', @daemon_name)
      next if new_path == original_path

      FileUtils.mv(original_path, new_path)
      rename_directories
      return
    end
  end

  def rename_files
    Dir.glob("#{@daemon_dir}/**/*").each do |original_path|
      new_path = original_path.gsub('larva_spawn', @daemon_name)
      FileUtils.mv(original_path, new_path) if new_path != original_path
    end
  end

  def rename_file_contents
    Dir.glob("#{@daemon_dir}/**/*").each do |path|
      next unless File.file?(path)
      contents = File.read(path)
      File.open(path, 'w+') do |file|
        contents.gsub!("LarvaSpawn", @daemon_name.classify)
        contents.gsub!("larva_spawn", @daemon_name)
        contents.gsub!("LARVA_SPAWN", @daemon_name.upcase)
        file.write(contents)
      end
    end
  end
end

def error_exit(error)
  $stderr.puts "!! Error: #{error}"
  exit
end

error_exit("Please provide a valid command. Valid commands: spawn") unless ARGV[0] == "spawn"
DaemonCreator.create(ENV["PWD"], ARGV[1])

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
larva-0.6.2 bin/larva