This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
120 lines (100 loc) · 2.36 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# Configuration
# -------------
# Set local sqlite DB.
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/local.db")
# Enable Sessions for Rack Flash Messages.
enable :sessions
# Global settings.
GIT_URL = ENV['GIT_URL']
PACKAGES_DIR = 'packages'
PACKAGES_PATH = "#{Dir.pwd}/public/#{PACKAGES_DIR}"
TRANSIFEX_DIR = "#{Dir.pwd}/tmp/transifex"
# Helpers
# -------
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == [ENV['USERNAME'], ENV['PASSWORD']]
end
end
# Models
# ------
require "#{Dir.pwd}/models/models.rb"
# Datamapper Commands
# -------------------
DataMapper.finalize
# DataMapper.auto_migrate!
DataMapper.auto_upgrade!
# Routes
# ------
get "/" do
protected!
@devs = Package.all(:branch => :dev).reverse
@masters = Package.all(:branch => :master).reverse
@last_dev = @devs.first
@last_master = @masters.first
erb :home
end
post "/package" do
protected!
package = Package.new(params[:package])
# Find old, not public packages.
not_public = Package.all(
:published => false,
:branch => package.branch
)
# Destroy them.
not_public.destroy
# Create new package.
package.pack
if package.save
redirect "/"
else
flash[:error] = package.errors.full_messages.join("<br />")
redirect "/"
end
end
post "/publish" do
protected!
package = Package.get(params[:id].to_i)
previous_public = Package.all(
:branch => package.branch,
:published => true
)
previous_public.update(:published => false)
if package.update(:published => true)
redirect "/"
else
flash[:error] = package.errors.full_messages.join("<br />")
redirect "/"
end
end
# API
# params[:branch] (dev | master)
get "/release/check" do
# Get published package
published_package = Package.first(
:branch => params[:branch],
:published => true
)
if published_package
"#{published_package.version}"
end
end
# params[:branch] (dev | master)
get "/release/zip" do
# Get published package
published_package = Package.first(
:branch => params[:branch],
:published => true
)
if published_package
file = "#{Dir.pwd}/public/#{published_package.url}"
send_file(file, :filename => published_package.zip_file)
end
end