forked from alohaeditor/Aloha-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli
executable file
·675 lines (548 loc) · 13.5 KB
/
cli
1
2
3
4
5
6
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
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
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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env ruby
# == Name
# cli - Aloha Editor's Command Line Interface
#
# == Synopsis
# Refer to the README file for usage
#
# == Options
# -h, --help Displays help message
# -v, --version Display the version, then exit
# -q, --quiet Output as little as possible, overrides verbose
# -V, --verbose Verbose output
#
require 'optparse'
require 'ostruct'
require 'date'
begin
require 'rdoc/usage'
RDOC_INCLUDED = true
rescue LoadError
RDOC_INCLUDED = false
end
IS_WINDOWS = ENV['windir'] ? true : false
WINDOWS_CLI = `echo $PATH`.strip().eql?('$PATH') ? true : false
class App
# Remotes
REMOTE_ALOHA_URL = 'http://github.com/alohaeditor/Aloha-Editor.git'
REMOTE_ALOHA = 'aloha'
# Versioning
BRANCH_DEV = 'dev'
BRANCH_VERSION = '0.10'
BRANCH_MASTER = 'master'
# Building
BUILD_DIR = './.build'
# Uglifiy
UGLIFY_URL = 'https://github.com/mishoo/UglifyJS/raw/master/bin/uglifyjs'
UGLIFY_DIR = './.build/uglify'
UGLIFY_FILE = './.build/uglify/uglify'
# Closure
CLOSURE_URL = 'http://closure-compiler.googlecode.com/files/compiler-latest.zip'
CLOSURE_DIR = './.build/closure'
CLOSURE_ZIP = './.build/closure/compiler.zip'
CLOSURE_FILE = './.build/closure/compiler.jar'
SOURCE_MAP = './scripts/closure.map'
# YUI
YUI_URL = 'http://yuilibrary.com/downloads/yuicompressor/yuicompressor-2.4.2.zip'
YUI_DIR = './.build/yui'
YUI_ZIP = './.build/yui/compiler.zip'
YUI_FILE = './.build/yui/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar'
# ===========================================================================
# Init Helpers
# Run a Command
def run ( command, output=true )
# Recurse
if command.kind_of?(Array)
result = ""
command.each do |c|
result = result + run(c,output)
end
return result
end
# Output
if output
puts command + "\n"
end
# Windows
if WINDOWS_CLI
command_e = command.gsub('"','\\"')
result = `bash -c "#{command_e}"`
else
result = `#{command}`
end
# Output
if output
puts result
end
# Done
return result
end
# Check if a binary exists
def bin_exists? (bin)
exists = !run("which #{bin}",false).strip.empty?
return exists
end
# Fail if the binary doesn't exist
def check_req (req)
# Recurse
if req.kind_of?(Array)
req.each do |r|
check_req(r)
end
return true
end
# Check
req_exists = bin_exists?(req)
unless req_exists
abort("CLI requires the following binary which is not installed: #{req}")
end
# Done
return true
end
# Download a File
def download ( url, file )
result = run("curl -L #{url} -o #{file}")
end
# Extract a File
def extract ( dir, file )
file = file.gsub(dir,'.')
result = run(["cd #{dir}","tar -xf #{file}","rm -Rf #{file}"])
end
# ===========================================================================
# Init Commands
# Initialise our CLI
def initialize
init_env
end
# Initialise our Environment
def init_env
# Check for Requirements
check_req(['mkdir','curl','tar','git','rm','mv'])
# Do not care for compilation yet
return
# Check for Closure Compiler
if !File.exists?(CLOSURE_FILE)
run("mkdir -p #{CLOSURE_DIR}")
puts "Downloading the Closure Compiler..."
download(CLOSURE_URL, CLOSURE_ZIP)
extract(CLOSURE_DIR, CLOSURE_ZIP)
run("chmod +x #{CLOSURE_FILE}")
end
# Check for Uglify
if !File.exists?(UGLIFY_FILE)
run("mkdir -p #{UGLIFY_DIR}")
puts "Downloading the Uglify Compiler..."
download(UGLIFY_URL, UGLIFY_FILE)
run("chmod +x #{UGLIFY_FILE}")
end
# Check for YUI Compiler
if !File.exists?(YUI_FILE)
run("mkdir -p #{YUI_DIR}")
puts "Downloading the YUI Compiler..."
download(YUI_URL, (YUI_ZIP))
extract(YUI_DIR, YUI_ZIP)
run("chmod +x #{YUI_FILE}")
end
end
# ===========================================================================
# Build Helpers
def compressCss ( in_file, out_file )
# Calculate
in_file_size = File.size(in_file)
# Handle
if in_file.equal? out_file
out_file = out_file.gsub(/\.js$/, '.min.js')
compressCssWithYui(in_file,out_file)
run("rm #{in_file}")
run("mv #{out_file} #{in_file}")
out_file = in_file
else
compressCssWithYui(in_file,out_file)
out_file_size = File.size(out_file)
end
# Calculate
out_file_size = File.size(out_file)
ratio = Float(out_file_size)/Float(in_file_size)
reduction = ((1-ratio)*100).round
# Log
puts "Compressed the file [#{in_file}] to [#{out_file}] with a #{reduction}% reduction"
end
def compressJs ( in_file, out_file )
# Calculate
in_file_size = File.size(in_file)
# Handle
if in_file.equal? out_file
out_file = out_file.gsub(/\.js$/, '.min.js')
compressJsWithUglify(in_file,out_file)
run("rm #{in_file}")
run("mv #{out_file} #{in_file}")
out_file = in_file
else
compressJsWithUglify(in_file,out_file)
out_file_size = File.size(out_file)
end
# Calculate
out_file_size = File.size(out_file)
ratio = Float(out_file_size)/Float(in_file_size)
reduction = ((1-ratio)*100).round
# Log
puts "Compressed the file [#{in_file}] to [#{out_file}] with a #{reduction}% reduction"
end
# Compress a CSS File with YUI
def compressCssWithYui ( in_file, out_file )
result = run("java -jar #{YUI_FILE} --type css -o #{out_file} --charset UTF-8 #{in_file}")
end
# Compress a Javascript File with Uglify
def compressJsWithUglify ( in_file, out_file )
result = run("#{UGLIFY_FILE} -o #{out_file} #{in_file}")
end
# Compress a Javascript file with Closure
def compressJsWithClosure ( in_file, out_file )
result = run("java -jar #{CLOSURE_FILE} --js_output_file=#{out_file} --js=#{in_file}")
end
# ===========================================================================
# Git Helpers
# Aborts if Changes are Found
def has_changes
result = run("git status")
if result.include? 'Changed but not updated'
abort("You have un-committed changes that need to be committed before we can proceed.\n#{result}")
end
end
# Check if a Branch Exists
def branch_exists(branch)
branches = run("git branch",false)
regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(branch.to_s) + '\\n')
result = ((branches =~ regex) ? true : false)
return result
end
# Check if a Branch Exists
def remote_branch_exists(branch)
fetch
branches = run("git branch -all",false)
regex = Regexp.new('\\/' + Regexp.escape(branch.to_s) + '\\n')
result = ((branches =~ regex) ? true : false)
return result
end
# Checkout a Branch
def checkout(branch)
if branch_exists(branch)
run("git checkout #{branch}")
else
if remote_branch_exists(branch)
run("git checkout -b #{branch} #{REMOTE_ALOHA}/#{branch}")
else
run("git branch #{branch}")
end
end
end
# Apply multiple commands to submodules
def submodule_foreach(command)
# Recursion
if command.kind_of?(Array)
result = ""
command.each do |c|
result = result + submodule_foreach(c)
end
return result
end
result = run("git submodule foreach --recursive '" + command + "'")
return result
end
# Check if a Remote Exists
def remote_exists(remote)
remotes = run("git remote")
regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(remote) + '\\n')
result = ((remotes =~ regex) ? true : false)
return result
end
# Add a Remote
def remote_add(url,name)
unless remote_exists(name)
run("git remote add #{name} #{url}")
fetch(name)
end
end
# Fetch a Remote
def fetch(remote="")
run("git fetch " + remote)
end
# Merge
def merge(base,*args)
if args.length === 0
return run("git merge #{base}")
end
checkout(base)
args.each do |branch|
checkout(branch)
merge(base)
end
checkout(base)
end
# Stage Known Files
def add
run("git add -u")
end
# Push to Origin
def push(remote='',branch='')
run("git push "+remote+" "+branch)
end
# Pull
def pull(remote='',branch='')
run("git pull "+remote+" "+branch)
end
# Push to Origin
def push_all
run("git push origin --all")
end
# Pull From Everywhere
def pull_all
run("git pull --all")
end
# ===========================================================================
# Git Handlers
# ---------------------------------------------------------------------------
# Checkout
# Checkout Version Branch
def version
checkout(BRANCH_VERSION)
end
# Checkout Dev Branch
def dev
checkout(BRANCH_DEV)
end
# Checkout Master Branch
def master
checkout(BRANCH_MASTER)
end
# ---------------------------------------------------------------------------
# Branch
# Update Branch
def update_branch(branch)
checkout(branch)
update
end
# ---------------------------------------------------------------------------
# Branches
# Initialise Branches
def init_branches
remote_add(REMOTE_ALOHA_URL,REMOTE_ALOHA)
master
version
dev
end
# Update Branches
def update_branches
update_branch(BRANCH_MASTER)
update_branch(BRANCH_VERSION)
update_branch(BRANCH_DEV)
end
# ---------------------------------------------------------------------------
# Submodules
# Add a Plugin
def plugin(url,name='')
url = url.gsub(/^.+?(github.com)./,'http://\\1/')
if name.empty?
name = 'com.gentics.aloha.plugins.' + url.gsub(/^.+?Aloha-Plugin-(\w+)\.git/,'\\1')
end
dir = 'WebContent/plugins/'+name
run("git submodule add #{url} #{dir}")
end
# Initialise Submodules
def init_submodules
run([
"git submodule init",
"git submodule update"
])
submodule_foreach([
"git reset --hard",
"git remote update",
"git fetch origin",
"git checkout master",
"git submodule init",
"git submodule update"
])
end
# Update Submodules
def update_submodules
run([
"git submodule init",
"git submodule update --merge"
])
submodule_foreach([
"git submodule init",
"git submodule update --merge"
])
end
# Upgrade Submodules
def upgrade_submodules
run([
"git submodule init",
"git submodule update --merge"
])
submodule_foreach([
"git checkout master",
"git pull --all"
])
end
# ---------------------------------------------------------------------------
# Utility
# Give Birth to a New Aloha Editor
def birth
puts "Welcome to the Aloha Editor CLI.\nWhat is your repository URL? E.g. git@github.com:balupton/Aloha-Editor.git"
url = gets.chomp.strip
run("git init")
remote_add(url,"origin")
install
end
# Install Aloha Editor
def install
init_branches
init_submodules
end
# Update
def update
pull
update_submodules
end
# Upgrade Aloha Editor
def upgrade
pull
upgrade_submodules
end
# Build Aloha Editor
def build
check_req(['java','ant'])
run("ant -f build/build.xml build")
end
# ---------------------------------------------------------------------------
# Deploy
# Deploy
def deploy(base,*args)
merge(base,*args)
push
end
# Deploy the Changes From Master to Version + Dev
def deploy_from_master
version
merge(BRANCH_MASTER)
dev
merge(BRANCH_MASTER)
master
push
end
# Deploy the Changes From Dev to Version + Master
def deploy_from_dev
version
merge(BRANCH_DEV)
master
merge(BRANCH_DEV)
dev
push
end
end
# ===========================================================================
# Booter
class Booter
VERSION = :'0.0.1'
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Set defaults
@options = OpenStruct.new
@options.verbose = false
@options.quiet = false
# TO DO - add additional defaults
end
# Parse options, check arguments, then process the command
def run
if parsed_options? && arguments_valid?
puts "Start at #{DateTime.now}\n\n" if @options.verbose
output_options if @options.verbose # [Optional]
process_arguments
process_command
puts "\nFinished at #{DateTime.now}" if @options.verbose
else
output_usage
end
end
protected
# Parse options
def parsed_options?
# Specify options
opts = OptionParser.new
opts.on('-v', '--version') { output_version ; exit 0 }
opts.on('-h', '--help') { output_help }
opts.on('-V', '--verbose') { @options.verbose = true }
opts.on('-q', '--quiet') { @options.quiet = true }
# TO DO - add additional options
opts.parse!(@arguments) rescue return false
process_options
true
end
# Performs post-parse processing on options
def process_options
@options.verbose = false if @options.quiet
end
def output_options
puts :"Options:\n"
@options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
end
# True if required arguments were provided
def arguments_valid?
# TO DO - implement your real logic here
true if @arguments.length >= 1
end
# Setup the arguments
def process_arguments
# TO DO - place in local vars, etc
end
def output_help
output_version
if RDOC_INCLUDED
RDoc::usage() #exits app
else
puts "No RDoc"
end
end
def output_usage
if RDOC_INCLUDED
RDoc::usage(:'usage') # gets usage from comments above
else
puts "No RDoc"
end
end
def output_version
puts "#{File.basename(__FILE__)} version #{VERSION}"
end
def process_command
# Create Application
app = App.new
# Extract
args = @arguments.clone
command = args.shift.gsub('-','_')
# Fetch + Execute
unless app.respond_to?(command)
abort("Unknown command: #{command}")
end
if args.length === 0
app.send(command)
else
app.send(command,*args)
end
end
def process_standard_input
input = @stdin.read
# TO DO - process input
# [Optional]
#@stdin.each do |line|
# # TO DO - process each line
#end
end
end
# Create Booter
booter = Booter.new(ARGV, STDIN)
booter.run