Module: MiniHeroku

Defined in:
lib/mini-heroku.rb

Constant Summary collapse

DEFAULT_PROCFILE =
"web: bundle exec ruby server.rb -p $PORT"
DEFAULT_GITIGNORE =
["mhenvironment.json"].join("\n")
DEFAULT_SERVER =
[
    "require 'sinatra'",
    "require 'mini-heroku'",
    "",
    "get '/' do",
    "    \"Hello world! Your variable defaultKey is (probably empty): \#{MH.env('defaultKey')} \#{MH.env('defaultKey').inspect}\"",
    "end"
]
DEFAULT_CONFIG =
{
    devUsesProdEnviromnent: true,
    mhVersion: "0.2.2",
    environmentKeys: [
        :defaultKey
    ],
    customGems: {
        "rack/ssl": "rack-ssl"
    }
}

Class Method Summary collapse

Class Method Details

.create(name) ⇒ Object



37
38
39
40
41
42
# File 'lib/mini-heroku.rb', line 37

def self.create(name)
    puts("MH: Creating #{name}")
    system("git init")
    system("heroku create #{name}")
    refresh()
end

.deployObject



195
196
197
198
199
200
201
202
# 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) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# 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

.getGemsObject



44
45
46
47
48
49
50
51
52
# 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

.loadEnvironmentObject



164
165
166
167
168
169
170
171
172
173
174
# 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) ⇒ Object



33
34
35
# File 'lib/mini-heroku.rb', line 33

def self.readJSON(filename)
    return JSON.parse(File.read(filename), :symbolize_names => true)
end

.refreshObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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

.setProductionEnvironmentObject



183
184
185
186
187
188
189
190
191
192
193
# 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

.startObject



176
177
178
179
180
181
# File 'lib/mini-heroku.rb', line 176

def self.start()
    puts "MH: Loading environment"
    loadEnvironment()
    puts "MH: Starting server"
    system("ruby server.rb")
end