teambox/viper-module-generator

View on GitHub
lib/vipergen/generator.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Vipergen
    # Cosntants
    class Generator
        # Constants
        LANGUAGES = ["swift", "objc"]
        REPLACEMENT_KEY = "VIPER"

        # Main method that generate the VIPER files structure
        def self.generate_viper(template, language, name, path)
            puts "Generating VIPER-Module"
            puts "Template: #{template}"
            puts "Language: #{language}"
            puts "Name: #{name}"
            puts "Path: #{path}"
            path_from = Vipergen::FileManager.path_from(template, language)
            path_to = Vipergen::FileManager.destination_viper_path(path, name)
            Vipergen::FileManager.copy(path_from, path_to)
            files = Vipergen::FileManager.files_in_path(path_to)
            rename_files(files,name)
        end

        # Rename all the files in the files array
        # - It renames the name of the file 
        # - It renames the content of the file
        def self.rename_files(files, name)
            files.each do |file|
                raise SyntaxError unless file.include? (Vipergen::Generator::REPLACEMENT_KEY)
                rename_file(file, name)
            end
        end

        # Rename a given file
        # - It renames the name of the file
        # - It renames the content of the file
        def self.rename_file(file, name)
            new_path = file.gsub((Vipergen::Generator::REPLACEMENT_KEY), name)
            Vipergen::FileManager.move(file, new_path)
            rename_file_content(new_path, name)
        end

        # Rename the file content
        # @return: An String with the every VIPER replaced by 'name'
        def self.rename_file_content(filename, name)
            # Reading content
            file = File.open(filename, "rb")
            content = file.read
            file.close

            # Replacing content
            content = content.gsub((Vipergen::Generator::REPLACEMENT_KEY), name)

            # Saving content with replaced string
            File.open(filename, "w+") do |file|
                   file.write(content)
            end
        end
    end
end