partial upload support for scss

This commit is contained in:
Sam 2018-03-13 18:27:05 +11:00
parent 6f40037ba5
commit cb7f3c6537

@ -27,6 +27,7 @@ WATCHER_SETTINGS_FILE = File.expand_path("~/.discourse-theme-watcher")
$api_key = ENV['DISCOURSE_API_KEY'] $api_key = ENV['DISCOURSE_API_KEY']
$dir = ARGV[0] $dir = ARGV[0]
$site = ARGV[1] $site = ARGV[1]
$theme_id = nil
if $site !~ /https?:\/\//i if $site !~ /https?:\/\//i
$site = "http://#{$site}" $site = "http://#{$site}"
@ -72,9 +73,8 @@ ensure
sgz.close sgz.close
end end
def diagnose_errors(text) def diagnose_errors(json)
count = 0 count = 0
json = JSON.parse(text)
json["theme"]["theme_fields"].each do |row| json["theme"]["theme_fields"].each do |row|
if (error = row["error"]) && error.length > 0 if (error = row["error"]) && error.length > 0
if count == 0 if count == 0
@ -82,13 +82,44 @@ def diagnose_errors(text)
end end
count += 1 count += 1
puts puts
puts "Error in #{row["target"]} #{row["name"]}} : #{row["error"]}" puts "Error in #{row["target"]} #{row["name"]}: #{row["error"]}"
puts puts
end end
end end
count count
end end
def upload_theme_field(target: , name: , type_id: , value:)
args = {
theme: {
theme_fields: [{
name: name,
target: target,
type_id: type_id,
value: value
}]
}
}
uri = URI.parse($site + "/admin/themes/#{$theme_id}?api_key=#{$api_key}")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = args.to_json
http.start do |h|
response = h.request(request)
if response.code.to_i == 200
json = JSON.parse(response.body)
if diagnose_errors(json) == 0
puts "(done)"
end
else
puts "Error importing field status: #{response.code}"
end
end
end
def upload_full_theme(dir, site) def upload_full_theme(dir, site)
filename = "#{Pathname.new(Dir.tmpdir).realpath}/bundle_#{SecureRandom.hex}.tar.gz" filename = "#{Pathname.new(Dir.tmpdir).realpath}/bundle_#{SecureRandom.hex}.tar.gz"
compress_dir(filename, dir) compress_dir(filename, dir)
@ -104,9 +135,13 @@ def upload_full_theme(dir, site)
) )
response = http.request(request) response = http.request(request)
if response.code.to_i == 201 if response.code.to_i == 201
if diagnose_errors(response.body) == 0 json = JSON.parse(response.body)
$theme_id = json["theme"]["id"]
if diagnose_errors(json) == 0
puts "(done)" puts "(done)"
end end
else
puts "Error importing theme status: #{response.code}"
end end
end end
@ -114,19 +149,34 @@ ensure
FileUtils.rm_f filename FileUtils.rm_f filename
end end
print "Uploading theme: "
upload_full_theme($dir, $site) upload_full_theme($dir, $site)
def resolve_file(path) def resolve_file(path)
dir_len = File.expand_path($dir).length
name = File.expand_path(path)[dir_len + 1..-1]
target, file = name.split("/")
if ["common", "desktop", "mobile"].include?(target)
if file = "#{target}.scss"
# a CSS file
return [target, "scss", 1]
end
end
nil
end end
listener = Listen.to($dir) do |modified, added, removed| listener = Listen.to($dir) do |modified, added, removed|
if modified.length == 1 && if modified.length == 1 &&
added.length == 0 && added.length == 0 &&
removed.length == 0 && removed.length == 0 &&
(target, name = resolve_file(modified[0])) (target, name, type_id = resolve_file(modified[0]))
print "Updating #{target} #{name}" print "Updating #{target} #{name}: "
upload_theme_field(target: target, name: name, value: File.read(modified[0]), type_id: type_id)
else else
print "Full re-sync is required, re-uploading theme" print "Full re-sync is required, re-uploading theme: "
upload_full_theme($dir, $site) upload_full_theme($dir, $site)
end end
end end