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



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

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."
    exit 1
  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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/bootic_cli/themes/workflows.rb', line 209

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 some differences between your local and the remote version of your development theme."
    if prompt.yes_or_no? "Do you want to push your local changes now?", false
      push(local_theme, remote_theme, delete: true)
    else
      prompt.say "Oh well. Please make sure both versions are synced before publishing.", :magenta
      exit(1)
    end
  end

  prompt.notice "Alrighty! Publishing your development theme..."
  remote_theme.publish # syncs dev to public, without flipping them
  prompt.notice "Yay! Your development theme has been made public. Take a look at #{remote_theme.path}"
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
60
61
# 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)

  prompt.say "Done! Preview this theme at #{remote_theme.path}", :cyan
end

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



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

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

  prompt.say "Done! View updated version at #{remote_theme.path}", :cyan
end

#sync(local_theme, remote_theme) ⇒ Object



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

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)

  prompt.say "Synced! Preview this theme at #{remote_theme.path}", :cyan
end

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



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

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
      end
    end

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

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

    # update local cache
    remote_theme.reload!
  end

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

  # ctrl-c
  Signal.trap('INT') {
    listener.stop
    puts 'See 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