updates to theme watcher
This commit is contained in:
parent
f11f3f4c5b
commit
8ad7f8b44f
|
@ -11,6 +11,7 @@ require 'net/http'
|
||||||
require 'net/http/post/multipart'
|
require 'net/http/post/multipart'
|
||||||
require 'uri'
|
require 'uri'
|
||||||
require 'listen'
|
require 'listen'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
# Work in progress theme watcher for Discourse
|
# Work in progress theme watcher for Discourse
|
||||||
#
|
#
|
||||||
|
@ -21,13 +22,31 @@ def usage
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
if $site !~ /https?:\/\//i
|
||||||
|
$site = "http://#{$site}"
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Watching #{$dir} and uploading changes to #{$site}"
|
||||||
|
|
||||||
|
if !$api_key && File.exist?(WATCHER_SETTINGS_FILE)
|
||||||
|
$api_key = File.read(WATCHER_SETTINGS_FILE).strip
|
||||||
|
puts "Using previously stored api key in #{WATCHER_SETTINGS_FILE}"
|
||||||
|
end
|
||||||
|
|
||||||
if !$api_key
|
if !$api_key
|
||||||
puts "No API key found in DISCOURSE_API_KEY env var enter your API key: "
|
puts "No API key found in DISCOURSE_API_KEY env var enter your API key: "
|
||||||
$api_key = gets
|
$api_key = STDIN.gets.strip
|
||||||
|
puts "Would you like me to store this API key in #{WATCHER_SETTINGS_FILE}? (Yes|No)"
|
||||||
|
answer = STDIN.gets.strip
|
||||||
|
if answer =~ /y(es)?/i
|
||||||
|
File.write WATCHER_SETTINGS_FILE, $api_key
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if !File.exist?("#{$dir}/about.json")
|
if !File.exist?("#{$dir}/about.json")
|
||||||
|
@ -53,6 +72,23 @@ ensure
|
||||||
sgz.close
|
sgz.close
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def diagnose_errors(text)
|
||||||
|
count = 0
|
||||||
|
json = JSON.parse(text)
|
||||||
|
json["theme"]["theme_fields"].each do |row|
|
||||||
|
if (error = row["error"]) && error.length > 0
|
||||||
|
if count == 0
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
count += 1
|
||||||
|
puts
|
||||||
|
puts "Error in #{row["target"]} #{row["name"]}} : #{row["error"]}"
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
count
|
||||||
|
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)
|
||||||
|
@ -67,7 +103,11 @@ def upload_full_theme(dir, site)
|
||||||
"bundle" => UploadIO.new(tgz, "application/tar+gzip", "bundle.tar.gz"),
|
"bundle" => UploadIO.new(tgz, "application/tar+gzip", "bundle.tar.gz"),
|
||||||
)
|
)
|
||||||
response = http.request(request)
|
response = http.request(request)
|
||||||
p response.code
|
if response.code.to_i == 201
|
||||||
|
if diagnose_errors(response.body) == 0
|
||||||
|
puts "(done)"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ensure
|
ensure
|
||||||
|
@ -76,9 +116,19 @@ end
|
||||||
|
|
||||||
upload_full_theme($dir, $site)
|
upload_full_theme($dir, $site)
|
||||||
|
|
||||||
|
def resolve_file(path)
|
||||||
|
end
|
||||||
|
|
||||||
listener = Listen.to($dir) do |modified, added, removed|
|
listener = Listen.to($dir) do |modified, added, removed|
|
||||||
puts "Change detected"
|
if modified.length == 1 &&
|
||||||
upload_full_theme($dir, $site)
|
added.length == 0 &&
|
||||||
|
removed.length == 0 &&
|
||||||
|
(target, name = resolve_file(modified[0]))
|
||||||
|
print "Updating #{target} #{name}"
|
||||||
|
else
|
||||||
|
print "Full re-sync is required, re-uploading theme"
|
||||||
|
upload_full_theme($dir, $site)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
listener.start
|
listener.start
|
||||||
|
|
Loading…
Reference in New Issue