Class: Conjoiner::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/conjoiner/cli.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#clone(url) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/conjoiner/cli.rb', line 91

def clone(url)
  home                = Pathname.new(Dir.home)
  joined_dir          = home.join('joined')

  # Create repository pathname from the URL.
  git_clone_url       = GitCloneUrl.parse(url)
  path_parts          = git_clone_url.path.sub(%r{^/}, '').sub(/\.git$/, '').split('/')
  repository_pathname =
    joined_dir
    .join(git_clone_url.host)
    .join(*path_parts)

  # Clone is repository if it is not already present.
  if repository_pathname.exist?
    puts "Already cloned at #{repository_pathname}"
  else
    puts "Clone #{url} to #{repository_pathname}"
    repository_pathname.mkpath
    Git.clone(url, repository_pathname)
  end
rescue URI::Error
  puts "Invalid URL <#{url}>"
end

#init_aspect(aspect = nil, year = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
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
55
56
57
58
59
60
61
62
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
88
# File 'lib/conjoiner/cli.rb', line 21

def init_aspect(aspect = nil, year = nil)
  aspect ||= configuration.default_aspect
  year   ||= Time.now.year

  puts "init aspect #{aspect} #{year}"

  home       = Pathname.new(Dir.home)
  dated_root = home.join(
    'joined',
    'git.andrewcant.ca',
    'andrew',
    'aspects',
    aspect,
    'dated',
    year.to_s
  )

  configuration.dated_repository_names(aspect).each do |name|
    repo = "[email protected]:andrew/aspects/#{aspect}/dated/#{year}/#{name}"
    dir  = dated_root.join(name)

    git =
      if dir.exist?
        begin
          git_open = Git.open(dir)
          puts "Open #{name}"
          git_open
        rescue Git::GitExecuteError
          puts "Init #{name}"
          Git.init(dir.to_s).tap do |git_init|
            git_init.commit('Initial commit', allow_empty: true)
          end
        end
      else
        puts "Clone #{name}"
        puts "  #{repo}"
        puts "  #{dir}"

        dir.mkpath
        Git.clone(repo, dir)
      end

    # Add any existing files.
    begin
      git.add(all: true)
      git.commit_all('Commit existing files')
    rescue Git::GitExecuteError
      # Nothing to do, continue with the rest of the sync.
    end

    # Ensure that the remote exists.
    if git.remotes.empty?
      puts 'Add remote....'
      git.add_remote('origin', repo)
    end

    # TODO: should set upstream for regular commits

    # Try synchronize the new commits with the remote origin.
    begin
      puts 'Rync with the remote'
      git.pull('origin')
      git.push('origin')
    rescue Git::GitExecuteError
      # Nothing to do, continue with the rest of the sync.
    end
  end
end

#lsObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/conjoiner/cli.rb', line 281

def ls
  home       = Pathname.new(Dir.home)
  joined_dir = home.join('joined')
  result     = []
  joined_dir.find do |pathname|
    next unless pathname.directory? && pathname.join('.git').directory?

    if options[:origin]
      origin_url = Git.open(pathname).remote('origin').url
      result.push(origin_url) if origin_url
    elsif options[:noorigin]
      origin_url = Git.open(pathname).remote('origin').url
      result.push(pathname) unless origin_url
    else
      result.push(pathname)
    end

    Find.prune
  end

  result.sort.each { |x| puts x }
end

#show(aspect = nil, year = nil) ⇒ Object



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

def show(aspect = nil, year = nil)
  aspect ||= configuration.default_aspect
  year   ||= Time.now.year

  home = Pathname.new(Dir.home)
  joined_dir = home.join('joined')
  aspect_dir = joined_dir.join(
    'git.andrewcant.ca',
    'andrew',
    'aspects',
    aspect
  )

  gits =
    [
      aspect_dir.join('dated', year.to_s),
      aspect_dir.join('dated', (year - 1).to_s),
      aspect_dir.join('gtd'),
      aspect_dir.join('wiki'),
      aspect_dir.join('contacts'),
      aspect_dir.join('password-store')
    ].map { |x| x.glob('**/.git').map(&:dirname) }.flatten.map { |x| Git.open(x) }

  output =
    gits.map do |git|
      additions = git.status.added.count + git.status.changed.count
      OpenStruct.new( # rubocop:disable Style/OpenStructUse
        path:   Pathname.new(git.dir.to_s).relative_path_from(joined_dir).to_s,
        status: "+#{additions} -#{git.status.deleted.count} *#{git.status.untracked.count}"
      )
    end

  tp.set(:max_width, 256)
  tp(output, :path, :status)
end

#sync(aspect = nil, year = nil) ⇒ Object



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

def sync(aspect = nil, year = nil)
  aspect ||= configuration.default_aspect
  year   ||= Time.now.year

  home = Pathname.new(Dir.home)
  joined_dir = home.join('joined')
  root = joined_dir.join(
    'git.andrewcant.ca',
    'andrew',
    'aspects',
    aspect
  )

  gits =
    [
      root.join('dated', year.to_s),
      root.join('dated', (year - 1).to_s),
      root.join('gtd'),
      root.join('wiki')
    ].map { |x| x.glob('**/.git').map(&:dirname) }.flatten.map { |x| Git.open(x) }

  gits.each do |git|
    print '<'
    # Add any existing files.
    begin
      git.add(all: true)
      git.commit_all('auto commit')
    rescue Git::GitExecuteError
      # Nothing to do, continue with the rest of the sync.
    end

    print '.'

    git.pull('origin')
    git.push('origin')

    print '>'
  end
  puts ''
end

#syncerObject



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

def syncer
  year   ||= Time.now.year

  home = Pathname.new(Dir.home)
  joined_dir   = home.join('joined')
  aspects_root = joined_dir.join(
    'git.andrewcant.ca',
    'andrew',
    'aspects'
  )

  listen_pathnames =
    configuration.aspects.map do |aspect|
      aspect_dir = aspects_root.join(aspect)
      [
        aspect_dir.join('dated', year.to_s),
        aspect_dir.join('dated', (year - 1).to_s),
        aspect_dir.join('gtd'),
        aspect_dir.join('wiki')
      ]
    end
  # Flatten and include only existing directories.
  listen_pathnames =
    listen_pathnames.flatten.compact.select(&:directory?)

  # Find the repositories within those directories.
  listen_repository_pathnames =
    listen_pathnames.map { |x| x.glob('**/.git').map(&:dirname) }.flatten

  puts 'For these aspects:'
  configuration.aspects.each { |x| puts "  * #{x}" }
  puts 'Watch the following repositories:'
  listen_repository_pathnames.each { |x| puts "  * #{x}" }

  listener =
    Listen.to(
      *listen_repository_pathnames.map(&:to_s),
      ignore:         /#{File::SEPARATOR}\.git#{File::SEPARATOR}/o,
      latency:        5,
      wait_for_delay: 5
    ) do |modified, added, removed|
      changed_files = [modified, added, removed].flatten.compact.uniq
      changed_files
        .map { |file| listen_repository_pathnames.map(&:to_s).find { |repo| file.start_with?(repo) } }
        .compact
        .uniq
        .map { |x| Git.open(x) }
        .select { |x| x.status.added.any? || x.status.changed.any? || x.status.deleted.any? || x.status.untracked.any? } # rubocop:disable Layout/LineLength
        .each do |git|
          puts "Sync #{git.dir}"

          # Add any existing files.
          begin
            git.add(all: true)
            git.commit_all('auto commit')
          rescue Git::GitExecuteError
            # Nothing to do, continue with the rest of the sync.
          end

          # Sync the commits
          # TODO: needs conflict handling. Review what is done in gitdocs.
          begin
            git.pull('origin')
            git.push('origin')
          rescue => e # rubocop:disable Style/RescueStandardError
            puts "Error sycing #{git.dir}: #{e}"
          end
        end
    end

  listener.start
  puts 'Start listening...'
  begin
    sleep
  rescue Interrupt
    puts 'Exit'
    exit
  rescue SignalException => e
    puts "Exit on #{e}"
    exit
  end
end