Class: BooticCli::Themes::Workflows

Inherits:
Object
  • Object
show all
Defined in:
lib/bootic_cli/themes/workflows.rb

Constant Summary collapse

CONCURRENCY =
10

Instance Method Summary collapse

Constructor Details

#initialize(prompt: NullPrompt) ⇒ Workflows

Returns a new instance of Workflows.



30
31
32
# File 'lib/bootic_cli/themes/workflows.rb', line 30

def initialize(prompt: NullPrompt)
  @prompt = prompt
end

Instance Method Details

#compare(local_theme, remote_theme) ⇒ Object



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
# File 'lib/bootic_cli/themes/workflows.rb', line 117

def compare(local_theme, remote_theme)
  diff = ThemeDiff.new(source: local_theme, target: remote_theme)
  notice 'Comparing local and remote copies of theme...'

  unless diff.any?
    prompt.say "No changes between versions."
    return
  end

  notice "Local <--- Remote"

  diff.updated_in_target.templates.each do |t|
    puts "Updated template in remote: #{t.file_name} (updated at #{t.updated_on})"
    puts t.diff.to_s(:color)
  end

  diff.updated_in_target.assets.each do |t|
    puts "Updated asset in remote: #{t.file_name} (updated at #{t.updated_on})"
  end

  diff.missing_in_source.templates.each do |t|
    puts "Remote template not in local dir: #{t.file_name}"
  end

  diff.missing_in_source.assets.each do |t|
    puts "Remote asset not in local dir: #{t.file_name}"
  end

  notice "Local ---> Remote"

  diff.updated_in_source.templates.each do |t|
    puts "Updated locally: #{t.file_name} (updated at #{t.updated_on})"
    puts t.diff.to_s(:color)
  end

  diff.updated_in_source.assets.each do |t|
    puts "Updated locally: #{t.file_name} (updated at #{t.updated_on})"
  end

  diff.missing_in_target.templates.each do |f|
    puts "Local template not in remote: #{f.file_name}"
  end

  diff.missing_in_target.assets.each do |f|
    puts "Local asset not in remote: #{f.file_name}"
  end
end

#publish(local_theme, remote_theme) ⇒ Object



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
# File 'lib/bootic_cli/themes/workflows.rb', line 165

def publish(local_theme, remote_theme)
  raise "This command is meant for dev themes only" unless remote_theme.dev?

  changes = ThemeDiff.new(source: local_theme, target: remote_theme)
  if changes.any?
    prompt.say "There are differences between your local and the remote version of your shop's development theme."
    puts changes.summary

    if prompt.yes_or_no? "Push your local changes now?", true
      push(local_theme, remote_theme, delete: true)
    else
      prompt.say "No problem. Please make sure both versions are synced before publishing.", :magenta
      exit(1)
    end
  end

  delete_dev = prompt.yes_or_no? "Delete the development copy of your theme after publishing?", true
  prompt.notice "Alrighty! Publishing your development theme..."
  updated_theme = remote_theme.publish(delete: delete_dev)

  prompt.notice "Yay! Your development theme has been made public. Take a look at #{remote_theme.path.sub('/preview/dev', '')}"

  if delete_dev
    prompt.say "Run `bootic themes dev` on this directory to create a development copy of your public theme later."
  end
end

#pull(local_theme, remote_theme, delete: true) ⇒ Object



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
# File 'lib/bootic_cli/themes/workflows.rb', line 34

def pull(local_theme, remote_theme, delete: true)
  diff = ThemeDiff.new(source: local_theme, target: remote_theme, force_update: true)
  check_dupes!(local_theme.assets)

  download_opts = {
    overwrite: false,
    interactive: true
  }

  notice 'Updating local templates...'
  maybe_update(diff.updated_in_target.templates, 'remote', 'local') do |t|
    local_theme.add_template t.file_name, t.body
  end

  if delete
    notice 'Removing local files that were removed on remote...'
    remove_all(diff.missing_in_target, local_theme)
  else
    notice 'Not removing local files that were removed on remote.'
  end

  notice 'Pulling missing files from remote...'
  copy_templates(diff.missing_in_source, local_theme, download_opts)
  # lets copy all of them and let user decide to overwrite existing
  copy_assets(remote_theme, local_theme, download_opts)
end

#push(local_theme, remote_theme, delete: true) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bootic_cli/themes/workflows.rb', line 61

def push(local_theme, remote_theme, delete: true)
  diff = ThemeDiff.new(source: local_theme, target: remote_theme, force_update: true)
  check_dupes!(local_theme.assets)

  notice 'Pushing local changes to remote...'

  # update existing templates
  notice 'Updating remote templates...'
  maybe_update(diff.updated_in_source.templates, 'local', 'remote') do |t|
    remote_theme.add_template t.file_name, t.body
  end

  notice 'Pushing files that are missing in remote...'
  copy_assets(diff.missing_in_target, remote_theme, overwrite: true)
  copy_templates(diff.missing_in_target, remote_theme)

  if delete
    notice 'Removing remote files that were removed locally...'
    remove_all(diff.missing_in_source, remote_theme)
  else
    notice 'Not removing remote files that were removed locally.'
  end
end

#sync(local_theme, remote_theme) ⇒ Object



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
# File 'lib/bootic_cli/themes/workflows.rb', line 85

def sync(local_theme, remote_theme)
  diff = ThemeDiff.new(source: local_theme, target: remote_theme)
  check_dupes!(local_theme.assets)
  notice 'Syncing local copy with remote...'

  download_opts = {
    overwrite: false,
    interactive: false
  }

  # first, update existing templates in each side
  notice 'Updating local templates...'
  maybe_update(diff.updated_in_target.templates, 'remote', 'local') do |t|
    local_theme.add_template(t.file_name, t.body)
  end

  notice 'Updating remote templates...'
  maybe_update(diff.updated_in_source.templates, 'local', 'remote') do |t|
    remote_theme.add_template(t.file_name, t.body)
  end

  # now, download missing files on local end
  notice 'Downloading missing local templates & assets...'
  copy_templates(diff.missing_in_source, local_theme, download_opts)
  copy_assets(diff.missing_in_source, local_theme, download_opts)

  # now, upload missing files on remote
  notice 'Uploading missing remote templates & assets...'
  copy_templates(diff.missing_in_target, remote_theme, download_opts)
  copy_assets(diff.missing_in_target, remote_theme, download_opts)
end

#watch(dir, remote_theme, watcher: Listen) ⇒ Object



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
# File 'lib/bootic_cli/themes/workflows.rb', line 192

def watch(dir, remote_theme, watcher: Listen)
  listener = watcher.to(dir) do |modified, added, removed|

    if modified.any?
      modified.each do |path|
        upsert_file(remote_theme, path, dir)
      end
    end

    if added.any?
      added.each do |path|
        upsert_file(remote_theme, path, dir)
      end
    end

    if removed.any?
      removed.each do |path|
        delete_file(remote_theme, path, dir)
      end
    end

    # update local cache
    remote_theme.reload!
  end

  notice "Watching #{File.expand_path(dir)} for changes..."
  listener.start

  # ctrl-c
  Signal.trap('INT') {
    begin
      listener.stop
    rescue ThreadError => e # cant be called from trap context
      # nil
    end
    puts "\nSee you in another lifetime, brother."
    exit
  }

  prompt.say "Preview changes at #{remote_theme.path} -- Hit Ctrl-C to stop watching for changes.", :cyan
  Kernel.sleep
end