Class: Nimbu::Command::Themes

Inherits:
Base
  • Object
show all
Defined in:
lib/nimbu/command/themes.rb

Overview

working with themes (upload / download)

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, namespace, #nimbu

Methods included from Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, disable_error_capture, #display, #display_header, #display_object, #display_row, #display_table, enable_error_capture, #error, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output, #output_with_arrow, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Methods included from Helpers::System

#browser_launcher, #command?, #osx?, #tmp_dir, #which, #windows?

Constructor Details

This class inherits a constructor from Nimbu::Command::Base

Instance Method Details

#diffObject

themes

list available commands or display help for a specific command



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nimbu/command/themes.rb', line 76

def diff
  require 'diffy'
  @diff = {}
  input = args.shift.downcase rescue nil
  theme = if !input.to_s.strip.empty?
    input.to_s.strip
  else
    Nimbu::Auth.theme
  end
  display "\nShowing differences between local and server\nlayouts, templates, snippets and assets for '#{theme.green.bold}':"
  json = nimbu.themes(:subdomain => Nimbu::Auth.site).get(theme)
  check_differences(json, theme, "layouts", "templates", "snippets")
rescue ::Nimbu::Error::Forbidden
  Nimbu::Auth.invalid_access!
end

#indexObject

themes

list available commands or display help for a specific command



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nimbu/command/themes.rb', line 12

def index
  themes = nimbu.themes(:subdomain => Nimbu::Auth.site).list
  if themes.any?
    display "\nYou have following themes for this website:"
    themes.each do |theme|
      puts " - #{theme.name.bold} (#{theme.short})"
    end
  else
    puts "Hm. You seem to have no themes. Is that normal?"
  end
  puts ""
  puts "Currently this directory is configured for '#{Nimbu::Auth.theme.red.bold}'"
rescue ::Nimbu::Error::Forbidden
  Nimbu::Auth.invalid_access!
end

#listObject

themes:list

list all layouts, templates and assets



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
# File 'lib/nimbu/command/themes.rb', line 32

def list
  input = args.shift.downcase rescue nil
  if !input.to_s.strip.empty?
    theme = input.to_s.strip
  else
    theme = Nimbu::Auth.theme
  end
  display "\nShowing layouts, templates, snippets and assets for '#{theme.red.bold}':"
  contents = nimbu.themes(:subdomain => Nimbu::Auth.site).get(theme)
  if contents["layouts"].any?
    display "\nLayouts:".bold
    contents["layouts"].each do |l|
      display " - layouts/#{l["name"]}"
    end
  end

  if contents["templates"].any?
    display "\nTemplates:".bold
    contents["templates"].each do |t|
      display " - templates/#{t["name"]}"
    end
  end

  if contents["snippets"].any?
    display "\nSnippets:".bold
    contents["snippets"].each do |s|
      display " - snippets/#{s["name"]}"
    end
  end

  if contents["assets"].any?
    display "\nAssets:".bold
    contents["assets"].each do |a|
      display " - #{a["folder"]}/#{a["name"]}"
    end
  end
rescue ::Nimbu::Error::Forbidden
  Nimbu::Auth.invalid_access!
end

#pushObject

themes:push

push all layouts, templates and assets –liquid, –liquid-only # only push template code –css, –css-only # only push template code –js, –js-only # only push template code –images-only # only push new images –fonts-only # only push fonts –only # only push the files given on the command line –force # skip the usage check and upload anyway



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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/nimbu/command/themes.rb', line 103

def push
  liquid_only = options[:liquid]
  css_only = options[:css]
  js_only = options[:js]
  images_only = options[:images_only]
  fonts_only = options[:fonts_only]
  files_only = options[:only]
  force = options[:force]

  # if !input.to_s.strip.empty?
  #   theme = input.to_s.strip
  # else
  # end
  theme = Nimbu::Auth.theme
  display "Pushing layouts, templates and assets for '#{theme}' to the server:"

  # What should we push?
  push_liquid = !(css_only || js_only || images_only || fonts_only)
  push_css = !(liquid_only || js_only || images_only || fonts_only)
  push_js = !(liquid_only || css_only || images_only || fonts_only)
  push_images = !(liquid_only || css_only || js_only || fonts_only)
  push_fonts = !(liquid_only || css_only || js_only || images_only)

  if push_fonts
    if files_only
      font_files = args.select{|file| file.start_with?("fonts")}.map{|file| file.gsub("fonts/", "")}
    else
      font_files = Dir.glob("#{Dir.pwd}/fonts/**/*").map {|dir| dir.gsub("#{Dir.pwd}/fonts/","")} rescue []
    end

    print "\nFonts:\n"
    font_files.each do |font|
      begin
        file = "#{Dir.pwd}/fonts/#{font}"
        next if !force && (File.directory?(file) || (!anyFileWithWord?(css_glob,font) && !anyFileWithWord?(js_glob,font) && !anyFileWithWord?(layouts_glob,font) && !anyFileWithWord?(templates_glob,font) && !anyFileWithWord?(snippets_glob,font)))
        io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
        nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "fonts/#{font}", :file => io})
        print " - fonts/#{font}"
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, font) do
          nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "fonts/#{font}", :file => io, force: "true"})
        end
      end
    end
  end

  if push_images
    if files_only
      image_files = args.select{|file| file.start_with?("images")}.map{|file| file.gsub("images/", "")}
    else
      image_files = Dir.glob("#{Dir.pwd}/images/**/*").map {|dir| dir.gsub("#{Dir.pwd}/images/","")}
    end

    print "\nImages:\n"
    image_files.each do |image|
      begin
        file = "#{Dir.pwd}/images/#{image}"
        next if !force && (File.directory?(file) || (!anyFileWithWord?(css_glob,image) && !anyFileWithWord?(js_glob,image) && !anyFileWithWord?(layouts_glob,image) && !anyFileWithWord?(templates_glob,image) && !anyFileWithWord?(snippets_glob,image)))
        io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
        nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "images/#{image}", :file => io})
        print " - images/#{image}"
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, image) do
          nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "images/#{image}", :file => io, force: "true"})
        end
      end
    end
  end

  if push_css
    if files_only
      css_files = args.select{|file| file.start_with?("stylesheets")}.map{|file| file.gsub("stylesheets/", "")}
    else
      css_files = css_glob.map {|dir| dir.gsub("#{Dir.pwd}/stylesheets/","")}
    end

    print "\nStylesheets:\n"
    css_files.each do |css|
      begin
        file = "#{Dir.pwd}/stylesheets/#{css}"
        next if !force && (File.directory?(file) || (!anyFileWithWord?(layouts_glob,css) && !anyFileWithWord?(templates_glob,css) && !anyFileWithWord?(snippets_glob,css)))
        io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
        nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "stylesheets/#{css}", :file => io})
        print " - stylesheets/#{css}"
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, css) do
          io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
          nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "stylesheets/#{css}", :file => io, force: "true"})
        end
      end
    end
  end

  if push_js
    if files_only
      js_files = args.select{|file| file.start_with?("javascripts")}.map{|file| file.gsub("javascripts/", "")}
    else
      js_files = js_glob.map {|dir| dir.gsub("#{Dir.pwd}/javascripts/","")}
    end

    print "\nJavascripts:\n"
    js_files.each do |js|
      begin
        file = "#{Dir.pwd}/javascripts/#{js}"
        next if !force && (File.directory?(file) || (!anyFileWithWord?(layouts_glob,js) && !anyFileWithWord?(templates_glob,js) && !anyFileWithWord?(snippets_glob,js)))
        io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
        nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "javascripts/#{js}", :file => io})
        print " - javascripts/#{js}"
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, js) do
          io = Faraday::UploadIO.new(File.open(file), 'application/octet-stream', File.basename(file))
          nimbu.themes(:subdomain => Nimbu::Auth.site).assets(:theme_id => theme).create({:name => "javascripts/#{js}", :file => io, force: "true"})
        end
      end
    end
  end

  if push_liquid
    if files_only
      layouts_files = args.select{|file| file.start_with?("layouts")}.map{|file| file.gsub("layouts/", "")}
      templates_files = args.select{|file| file.start_with?("templates")}.map{|file| file.gsub("templates/", "")}
      snippets_files = args.select{|file| file.start_with?("snippets")}.map{|file| file.gsub("snippets/", "")}
    else
      layouts_files = layouts_glob.map {|dir| dir.gsub("#{Dir.pwd}/layouts/","")}
      templates_files = templates_glob.map {|dir| dir.gsub("#{Dir.pwd}/templates/","")}
      snippets_files = snippets_glob.map {|dir| dir.gsub("#{Dir.pwd}/snippets/","")}
    end

    print "\nSnippets:\n"
    snippets_files.each do |snippet|
      begin
        file = "#{Dir.pwd}/snippets/#{snippet}"
        next if !force && (File.directory?(file))
        print " - snippets/#{snippet}"
        nimbu.themes(:subdomain => Nimbu::Auth.site).snippets(:theme_id => theme).create({:name => snippet, :content => IO.read(file).force_encoding('UTF-8')})
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, snippet) do
          nimbu.themes(:subdomain => Nimbu::Auth.site).snippets(:theme_id => theme).create({:name => snippet, :content => IO.read(file).force_encoding('UTF-8'), force: "true"})
        end
      end
    end

    print "\nLayouts:\n"
    layouts_files.each do |layout|
      begin
        file = "#{Dir.pwd}/layouts/#{layout}"
        next if !force && (File.directory?(file))
        print " - layouts/#{layout}"
        nimbu.themes(:subdomain => Nimbu::Auth.site).layouts(:theme_id => theme).create({:name => layout, :content => IO.read(file).force_encoding('UTF-8')})
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, layout) do
          nimbu.themes(:subdomain => Nimbu::Auth.site).layouts(:theme_id => theme).create({:name => layout, :content => IO.read(file).force_encoding('UTF-8'), force: "true"})
        end
      end
    end

    print "\nTemplates:\n"
    templates_files.each do |template|
      begin
        file = "#{Dir.pwd}/templates/#{template}"
        next if !force && (File.directory?(file))
        print " - templates/#{template}"
        nimbu.themes(:subdomain => Nimbu::Auth.site).templates(:theme_id => theme).create({:name => template, :content => IO.read(file).force_encoding('UTF-8')})
        print " (ok)\n"
      rescue Nimbu::Error::Conflict => error
        handle_conflict(error, template) do
          nimbu.themes(:subdomain => Nimbu::Auth.site).templates(:theme_id => theme).create({:name => template, :content => IO.read(file).force_encoding('UTF-8'), force: "true"})
        end
      end
    end
  end
rescue ::Nimbu::Error::Forbidden
  Nimbu::Auth.invalid_access!
end