module MH

Constants

DEFAULT_CONFIG
DEFAULT_GITIGNORE
DEFAULT_PROCFILE
DEFAULT_SERVER

Public Class Methods

create(name) click to toggle source
# File lib/mini-heroku.rb, line 37
def self.create(name)
    puts("MH: Creating #{name}")
    system("git init")
    system("heroku create #{name}")
    refresh()
end
deploy() click to toggle source
# File lib/mini-heroku.rb, line 195
def self.deploy
    puts("MH: Name the commit:")
    name = STDIN.gets().chomp()
    system("git add .")
    system("git commit -m \"#{name}\"")
    system("git push heroku master")
    puts("MH: Deploy complete")
end
env(key) click to toggle source
# File lib/mini-heroku.rb, line 204
def self.env(key)
    val = ENV[key]
    if val[0..4] == "JSON:"
        val = val.gsub("JSON:", "")
        val = JSON.parse(val)
    else
        val = val.gsub("\\n", "\n")
    end
    return val
end
getGems() click to toggle source
# File lib/mini-heroku.rb, line 44
def self.getGems()
    installed = `gem list`.split("\n")
    installed.map! do |g|
        name, rest = g.split(" (")
        version = rest.scan(/^(\D)*((\d|\.)*)/)[0][1]
        [name, version]
    end
    return Hash[installed]
end
loadEnvironment() click to toggle source
# File lib/mini-heroku.rb, line 164
def self.loadEnvironment()
    environment = :development
    environment = readJSON("mhconfig.json")[:devUsesProdEnviromnent] ? :production : :development
    environment = readJSON("mhenvironment.json")[environment]
    environment.each do |key, val|
        if val.is_a?(Hash)
            val = "JSON:" + val.to_json
        end
        ENV[key.to_s] = val.gsub("\n", "\\n")
    end
end
readJSON(filename) click to toggle source
# File lib/mini-heroku.rb, line 33
def self.readJSON(filename)
    return JSON.parse(File.read(filename), :symbolize_names => true)
end
refresh() click to toggle source
# File lib/mini-heroku.rb, line 54
def self.refresh()
    #config
    config = DEFAULT_CONFIG
    if File.file?("mhconfig.json") 
        config.merge!(readJSON("mhconfig.json"))
        puts("MH: Found config")
    else
        puts "MH: Creating default config"
    end
    File.open("mhconfig.json", "wb") do |f|
        f.write JSON.pretty_generate(config)
    end

    #environment
    environment = {
        production: {},
        development: {}
    }
    for key in config[:environmentKeys]
        environment[:production][key.to_sym] = ""
        environment[:development][key.to_sym] = ""
    end
    if File.file?("mhenvironment.json")
        puts "MH: Found environment"
        old = readJSON("mhenvironment.json")
        environment = environment.deep_merge(old)
    else
        puts "MH: Creating default environment"
    end
    File.open("mhenvironment.json", "wb") do |f|
        f.write(JSON.pretty_generate(environment))
    end

    #procfile
    if File.file?("Procfile")
        puts "MH: Found procfile"
    else
        puts "MH: Creating default procfile"
        File.open("Procfile", "w") do |f|
            f.puts(DEFAULT_PROCFILE)
        end
    end

    #gitignore
    if File.file?(".gitignore")
        puts "MH: Found .gitignore"
    else
        puts "MH: Creating default .gitignore"
        File.open(".gitignore", "w") do |f|
            f.puts(DEFAULT_GITIGNORE)
        end
    end

    #gems
    gems = ["sinatra", "mini-heroku"]
    files = ["server"]
    if File.file?("server.rb")
        puts "MH: Found server file, checking gems" #^(\s*?)(require(\s*?)("|'))(\.\/)(.*?)("|')
        until files.size == 0
            filename = files.pop
            for line in File.read(filename + ".rb").split("\n") do
                # (start of line)(any amount of indentation)(begin quote)(gem name, but not .)(end quote)
                gem = line.scan(/^(\s*?)(require(\s*?)("|'))([^\.]*?)("|')/).dig(0, 4)
                gems << gem if gem
                file = line.scan(/^(\s*?)(require(\s*?)("|'))(\.\/)(.*?)("|')/).dig(0, 5)
                files << file.gsub(".rb", "") if file
                file = line.scan(/^(\s*?)(require_relative(\s*?)("|'))(.*?)("|')/).dig(0, 4)
                files << file.gsub(".rb", "") if file
            end
        end
        gems.uniq!
    else
        puts "MH: Creating default server file"
        File.open("server.rb", "w") do |f|
            f.puts(DEFAULT_SERVER)
        end
    end
    config[:customGems].each do |key, val|
        if gems.include?(key.to_s)
            gems.delete(key.to_s)
            gems << val.to_s if val
        end
    end
    installedGems = getGems()
    didInstall = false
    for gem in gems
        if !(installedGems.keys.include?(gem))
            puts "MH: Installing Gem #{gem}"
            system("gem install #{gem} --no-document")
            didInstall = true
        end
    end
    if didInstall
        puts "MH: Re-checking Gems"
        installedGems = getGems()
    end
    puts "MH: Creating Gemfile"
    File.open("Gemfile", "w") do |f|
        f.puts("source 'https://rubygems.org'")
        f.puts("ruby '#{RUBY_VERSION}'")
        gems.each do |gem|         
            f.puts("gem '#{gem}', '>=#{installedGems[gem]}'")
        end
    end

    puts "MH: Bundle"
    system("bundle")
    puts "MH: Refresh complete"
end
setProductionEnvironment() click to toggle source
# File lib/mini-heroku.rb, line 183
def self.setProductionEnvironment()
    environment = readJSON("mhenvironment.json")[:production]
    out = "heroku config:set"
    environment.each do |key, val|
        if val.is_a?(Hash)
            val = "JSON:" + val.to_json
        end
        out += " #{key}=\"#{val.gsub("\n", "\\n").gsub('"', '\"')}\""
    end
    system(out)
end
start() click to toggle source
# File lib/mini-heroku.rb, line 176
def self.start()
    puts "MH: Loading environment"
    loadEnvironment()
    puts "MH: Starting server"
    system("ruby server.rb")
end