Sha256: 21e3ff8c2fd76c487927757294b636fcd50a87e90f980b51ab807888b994c3c7

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'gorgon/settings/simple_project_files_content'
require 'gorgon/settings/rails_project_files_content'

module Settings
  class InitialFilesCreator
    GORGON_JSON_FILE = 'gorgon.json'

    # TODO: we may change this, so it knows what Creator to use according to the Gemfile, so the user doesn't need to specify 'framework'
    def self.run framework
      if framework.nil?
        create_files SimpleProjectFilesContent.new
      elsif framework == 'rails'
        create_files RailsProjectFilesContent.new
      else
        $stderr.puts "Unknown framework"
        exit(1)
      end
    end

    def self.create_files content
      self.create_gorgon_json content
    end

    private

    def self.create_gorgon_json content
      if File.exist? GORGON_JSON_FILE
        puts "#{GORGON_JSON_FILE} exists. Skipping..."
        return
      end

      config = {
        connection: {host: content.amqp_host},
        file_server: {host: content.file_server_host},
        job: {
          sync: {
            exclude: content.sync_exclude,
            rsync_transport: "ssh"
          }
        },
        files: content.files
      }

      log_file = content.originator_log_file
      config[:originator_log_file] = log_file if log_file

      if content.callbacks
        create_callback_file(content)

        config[:job][:callbacks] = {
          callbacks_class_file: content.callbacks[:file_name]
        }
      end

      puts "Creating #{GORGON_JSON_FILE}..."
      File.open(GORGON_JSON_FILE, 'w') do |f|
        Yajl::Encoder.encode(config, f, :pretty => true, :indent => "  ")
      end
    end

    def self.create_callback_file(content)
      file_path = content.callbacks[:file_name]
      if File.exist? file_path
        puts "#{file_path} already exists. Skipping..."
        return
      end

      puts "Creating #{file_path}..."
      FileUtils.mkdir_p(File.dirname(file_path))
      File.open(file_path, 'w') do |f|
        f.write content.callbacks[:file_content]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gorgon-0.8.3 lib/gorgon/settings/initial_files_creator.rb
gorgon-0.8.2 lib/gorgon/settings/initial_files_creator.rb
gorgon-0.8.1 lib/gorgon/settings/initial_files_creator.rb