Class: Homesteading::Apps

Inherits:
Command
  • Object
show all
Defined in:
lib/homesteading/commands/apps.rb

Constant Summary

Constants inherited from Command

Command::COMMANDS

Instance Method Summary collapse

Methods inherited from Command

create, register

Instance Method Details

#createObject



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
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
# File 'lib/homesteading/commands/apps.rb', line 49

def create
  puts
  options = parse_options

  if options[:app].nil? && options[:app].nil?
    puts "* This requires an '--app' or '--url'"
    puts "* Example:"
    puts "  homesteading apps:create --app note"
    puts "  homesteading apps:create --url [email protected]:homesteading/homesteading-note.git"
    puts
    exit
  end

  git_url = if options[:app]
              "[email protected]:homesteading/homesteading-#{options[:app]}.git"
            else
              options[:url]
            end

  app_dir = git_url.split("/").last.split(".git").first

  if File.exist?(app_dir)
    if options[:skip]
      unless options[:quiet]
        puts "* #{app_dir} already exists"
        puts "* Skipping homesteading-#{options[:app]}"
      end
    elsif options[:force]
      unless options[:quiet]
        puts "* #{app_dir} already exists"
        puts "* Removing homesteading-#{options[:app]}"
      end

      unless options[:pretend]
        FileUtils.rm_rf app_dir
      end

      unless options[:quiet]
        if options[:app]
          puts "* Cloning homesteading app from GitHub: #{options[:app]}"
        else
          puts "* Cloning from: #{options[:url]}"
        end
      end

      unless options[:pretend]
        system "git clone #{git_url}"
      end
    else
      unless options[:quiet]
        puts "* #{app_dir} already exists"
        puts "* TODO gets user input on what to do about the conflict"
      end
    end

  else

    unless options[:quiet]
      if options[:app]
        puts "* Cloning homesteading app from GitHub: #{options[:app]}"
      else
        puts "* Cloning from: #{options[:url]}"
      end
    end

    unless options[:pretend]
      system "git clone #{git_url}"
    end
  end

  puts
end

#defaultObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/homesteading/commands/apps.rb', line 7

def default
  puts

  constellation_path = Dir.pwd
  publisher_apps     = []
  nonpublisher_apps  = []

  Dir.glob("#{Dir.pwd}/homesteading-*/").each do |app_dir|
    app           = app_dir.split("/").last.sub(/homesteading-/, "").downcase
    app_file_path = app_dir + "app.json"

    app_keywords = []
    if File.exist?(app_file_path)
      app_file     = JSON.parse(File.read(app_file_path))
      app_keywords = app_file["keywords"]
    end

    if app_keywords.include?("hs:publisher")
      publisher_apps    << app
    else
      nonpublisher_apps << app
    end
  end

  unless publisher_apps.empty?
    puts "* Publisher Apps"
    publisher_apps.each do |app|
      puts "  #{app}"
    end
    puts
  end

  unless nonpublisher_apps.empty?
    puts "* Non-Publisher Apps"
    nonpublisher_apps.each do |app|
      puts "  #{app}"
    end
  end

  puts
end

#destroyObject



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
# File 'lib/homesteading/commands/apps.rb', line 122

def destroy
  puts
  options = parse_options

  if options[:app].nil?
    puts "* '--app APP' is required for this command"
    puts
    exit
  end

  app                = options[:app].downcase
  constellation_path = Dir.pwd
  app_dir            = "#{constellation_path}/homesteading-#{app}"

  unless File.exist?(app_dir)
    puts "* #{app} does not exist"
    puts "* Did you mean one of these?"
    Dir.glob("#{Dir.pwd}/homesteading-*/").each do |ff|
      puts "  - #{ff.split("/").last.sub(/homesteading-/, "")}"
    end
    puts
    exit
  end

  if options[:confirm] == app || options[:confirm] == "homesteading-#{app}"
    puts "* Destroying #{app} permananently..."
    FileUtils.rm_rf app_dir
    puts "* ...done"
  else
    puts "!    WARNING: Potentially Destructive Action"
    puts "!    This command will destroy #{app}"
    puts "!    To proceed, type '#{app}' or re-run this command with '--confirm #{app}'"
    puts
    confirmation_input = gets.chomp
    puts

    if confirmation_input == app
      puts "* Destroying #{app} permananently..."
      FileUtils.rm_rf app_dir
      puts "* ...done"
    else
      puts "!    Confirmation did not match #{app}. Aborted."
    end
  end

  puts
end

#infoObject



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/homesteading/commands/apps.rb', line 170

def info
  puts
  options = parse_options

  if options[:app].nil?
    puts "* '--app APP' is required for this command"
    puts
    exit
  end

  app                = options[:app].downcase
  constellation_path = Dir.pwd
  app_dir            = "#{constellation_path}/homesteading-#{app}/"
  app_file_path      = app_dir + "app.json"

  if File.exist?(app_dir)
    if File.exist?(app_file_path)
      app_file      = JSON.parse(File.read(app_file_path))
      puts app_file.to_yaml
    else
      puts "* No app.json found for #{app}"
      puts
      exit
    end
  else
    puts "* #{app} does not exist"
    puts "* Did you mean one of these?"
    Dir.glob("#{Dir.pwd}/homesteading-*/").each do |ff|
      puts "  - #{ff.split("/").last.sub(/homesteading-/, "")}"
    end
    puts
    exit
  end

  puts
end

#renameObject



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
# File 'lib/homesteading/commands/apps.rb', line 207

def rename
  puts
  options = parse_options

  if options[:app].nil?
    puts "* '--app APP' is required for this command"
    puts
    exit
  end

  new_name = ARGV.shift
  if new_name.nil?
    puts "* New app name is required for this command"
    puts "* Example:"
    puts "  homesteading apps:rename status --app note"
    puts
    exit
  end

  app                = options[:app].downcase
  constellation_path = Dir.pwd
  app_dir            = "#{constellation_path}/homesteading-#{app}"
  new_app_dir        = "#{constellation_path}/homesteading-#{new_name}"

  if File.exist?(app_dir)
    puts "* Renaming #{app} to #{new_name}..."
    FileUtils.mv app_dir, new_app_dir
    puts "* ...done"
  else
    puts "* #{app} does not exist"
    puts "* Did you mean one of these?"
    Dir.glob("#{Dir.pwd}/homesteading-*/").each do |ff|
      puts "  - #{ff.split("/").last.sub(/homesteading-/, "")}"
    end
    puts
    exit
  end

  puts
end

#rerouteObject



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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/homesteading/commands/apps.rb', line 248

def reroute
  puts
  options = parse_options

  if options[:app].nil?
    puts "* '--app APP' is required for this command"
    puts
    exit
  end

  new_route = ARGV.shift
  if new_route.nil?
    puts "* New app route is required for this command"
    puts "* Example:"
    puts "  homesteading apps:reroute statuses --app note"
    puts
    exit
  end

  app                = options[:app].downcase
  constellation_path = Dir.pwd
  app_dir            = "#{constellation_path}/homesteading-#{app}/"
  app_file_path      = app_dir + "app.json"

  if File.exist?(app_dir)
    if File.exist?(app_file_path)
      app_file      = JSON.parse(File.read(app_file_path))
      app_file["routes"]["path"] = new_route

      puts "* Rerouting note to /statuses..."
      new_app_file = JSON.pretty_generate(app_file)
      File.write(app_file_path, new_app_file)
      puts "* ...done"
    else
      puts "* No app.json found for #{app}"
      puts
      exit
    end
  else
    puts "* #{app} does not exist"
    puts "* Did you mean one of these?"
    Dir.glob("#{Dir.pwd}/homesteading-*/").each do |ff|
      puts "  - #{ff.split("/").last.sub(/homesteading-/, "")}"
    end
    puts
    exit
  end

  puts
end