Class: Quickdraw::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/quickdraw/cli.rb

Constant Summary collapse

BINARY_EXTENSIONS =
%w(png gif jpg jpeg eot svg ttf woff otf swf ico)
IGNORE =
%w(config.yml)
DEFAULT_WHITELIST =
%w(layout/ assets/ config/ snippets/ templates/)
TIMEFORMAT =
"%H:%M:%S"

Instance Method Summary collapse

Instance Method Details

#configure(api_key = nil, password = nil, store = nil, theme_id = nil) ⇒ Object



24
25
26
27
# File 'lib/quickdraw/cli.rb', line 24

def configure(api_key=nil, password=nil, store=nil, theme_id=nil)
	config = {:api_key => api_key, :password => password, :store => store, :theme_id => theme_id}
	create_file('config.yml', config.to_yaml)
end

#download(filter = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quickdraw/cli.rb', line 57

def download(filter=nil)
	asset_list = Celluloid::Actor[:shopify_connector].get_asset_list

	if filter
		assets = asset_list.select{ |i| i[/^#{filter.gsub(/[^a-z0-9A-Z\/]/, '')}/] }.map{|a| FilePath.getwd / 'theme' / a }
	else
		assets = asset_list.map{|a| FilePath.getwd / 'theme' / a }
	end

	futures = []
	assets.each do |asset|
		futures << Celluloid::Actor[:shopify_connector].future.download_asset(asset)
	end
	futures.each do |future|
		asset, response = future.value
		if response.success?
			say("Downloaded: #{asset.relative_to(Quickdraw.getwd)}", :green)
		else
			say("[" + Time.now.strftime(TIMEFORMAT) + "] Error: Could not download #{asset.relative_to(Quickdraw.getwd)}. #{errors_from_response(response)}\n", :red)
		end
	end
	say("Done.", :green)
end

#remove(filter = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/quickdraw/cli.rb', line 127

def remove(filter=nil)
	asset_list = Celluloid::Actor[:shopify_connector].get_asset_list

	if filter
		assets = asset_list.select{ |i| i[/^#{filter.gsub(/[^a-z0-9A-Z\/]/, '')}/] }.map{|a| FilePath.getwd / 'theme' / a }
	else
		assets = asset_list.map{|a| FilePath.getwd / 'theme' / a }
	end

	futures = []
	assets.each do |asset|
		futures << Celluloid::Actor[:shopify_connector].future.remove_asset(asset)
	end
	futures.each do |future|
		asset, response = future.value
		if response.success?
			say("Deleted: #{asset.relative_to(Quickdraw.getwd)}", :green)
		else
			say("[" + Time.now.strftime(TIMEFORMAT) + "] Error: Could not remove #{asset.relative_to(Quickdraw.getwd)}. #{errors_from_response(response)}\n", :red)
		end
	end
	say("Done.", :green) unless options['quiet']
end

#replace(filter = nil) ⇒ Object



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
# File 'lib/quickdraw/cli.rb', line 82

def replace(filter=nil)
	say("Are you sure you want to completely replace your shop theme assets? This is not undoable.", :yellow)
	if ask("Continue? (y/n): ") == "y"
		# only delete files on remote that are not present locally
		# files present on remote and present locally get overridden anyway
		asset_list = Celluloid::Actor[:shopify_connector].get_asset_list

		if filter
			remote_assets = asset_list.select{ |i| i[/^#{filter.gsub(/[^a-z0-9A-Z\/]/, '')}/] }.map{|a| (FilePath.getwd / 'theme' / a) }
		else
			remote_assets = asset_list.map{|a| (FilePath.getwd / 'theme' / a) }
		end

		if filter
			local_assets = (FilePath.getwd / 'theme').files(true).select{ |i| i.relative_to(Quickdraw.theme_dir).to_s[/^#{filter.gsub(/[^a-z0-9A-Z\/]/, '')}/] }
		else
			local_assets = (FilePath.getwd / 'theme').files(true)
		end

		remote_only_assets = remote_assets.to_a.map{|p| p.to_s} - local_assets.to_a.map{|p| p.to_s}

		futures = []
		remote_only_assets.each do |asset|
			futures << Celluloid::Actor[:shopify_connector].future.remove_asset(asset.as_path)
		end
		local_assets.each do |asset|
			futures << Celluloid::Actor[:shopify_connector].future.upload_asset(asset)
		end
		futures.each do |future|
			asset, response = future.value
			if response.success?
				if response.request.http_method.to_s == "Net::HTTP::Put"
					say("Uploaded: #{asset.relative_to(Quickdraw.getwd)}", :green)
				else
					say("Removed: #{asset.relative_to(Quickdraw.getwd)}", :green)
				end
			else
				say("[" + Time.now.strftime(TIMEFORMAT) + "] Error: Could not download #{asset.relative_to(Quickdraw.getwd)}. #{errors_from_response(response)}\n", :red)
			end
		end
		say("Done.", :green) unless options['quiet']
	end
end

#upload(filter = nil) ⇒ Object



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
# File 'lib/quickdraw/cli.rb', line 30

def upload(filter=nil)

	if filter
		assets = (FilePath.getwd / 'theme').files(true).select{ |i|
			i.relative_to(Quickdraw.theme_dir).to_s[/^#{filter.gsub(/[^a-z0-9A-Z\/]/, '')}/]
		}
	else
		assets = (FilePath.getwd / 'theme').files(true)
	end

	futures = []
	assets.each do |asset|
		futures << Celluloid::Actor[:shopify_connector].future.upload_asset(asset)
	end
	futures.each do |future|
		asset, response = future.value
		if response.success?
			say("Uploaded: #{asset.relative_to(Quickdraw.getwd)}", :green)
		else
			say("[" + Time.now.strftime(TIMEFORMAT) + "] Error: Could not upload #{asset.relative_to(Quickdraw.getwd)}. #{errors_from_response(response)}\n", :red)
		end
	end

	say("Done.", :green)
end

#watchObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/quickdraw/cli.rb', line 152

def watch
	puts "Watching current folder: #{Dir.pwd}"

	futures = []

	listener = Listen.to((Quickdraw.getwd/'theme').to_s, (Quickdraw.getwd/'src').to_s, :force_polling => true, :latency => 0.2 ) do |modified, added, removed|
		modified.each do |asset|
			asset = asset.as_path
			say("MODIFIED: #{asset.relative_to(Quickdraw.getwd)}", :green)
			if theme_file?(asset)
				futures << Celluloid::Actor[:shopify_connector].future.upload_asset(asset)
			elsif src_file?(asset)
				futures << Celluloid::Actor[:shopify_connector].future.compile_asset(asset)
			end
		end
		added.each do |asset|
			asset = asset.as_path
			say("ADDED: #{asset}", :green)
			if theme_file?(asset)
				futures << Celluloid::Actor[:shopify_connector].future.upload_asset(asset)
			else
			end
		end
		removed.each do |asset|
			asset = asset.as_path
			say("REMOVED: #{asset}", :green)
			if theme_file?(asset)
				futures << Celluloid::Actor[:shopify_connector].future.remove_asset(asset)
			else
			end
		end
	end
	listener.start

	loop do

		futures.each do |future|
			asset, response = future.value
			if response
				unless response.success?
					say("[" + Time.now.strftime(TIMEFORMAT) + "] Error: #{asset.relative_to(Quickdraw.getwd)} Failed", :red)
				end
			else
				say("Compiled: #{asset.relative_to(Quickdraw.getwd)}")
			end
			futures.delete(future)
		end

		sleep 0.2
	end

rescue
	puts "exiting...."
end