Class: Raykit::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/raykit/console.rb

Overview

The implementation for the raykit console application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



9
10
11
12
13
14
15
16
17
18
# File 'lib/raykit/console.rb', line 9

def initialize
    @opts = Slop.parse do |o|
        o.string '-t','--task','rake task', default: 'default'
    end

    if(opts.verbose?)
        puts "options: " + Rainbow(@opts.to_hash).yellow.bright
        puts "arguments:" + Rainbow(@otps.arguments).yellow.bright
    end
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



8
9
10
# File 'lib/raykit/console.rb', line 8

def opts
  @opts
end

Instance Method Details

#addObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/raykit/console.rb', line 60

def add
    if(@opts.arguments.length < 2)
        puts 'add requires a url'
        return 1
    end
    url=@opts.arguments[1] 
    if(REPOSITORIES.include?(url))
        puts 'url ' + url + ' already exists.'
    else
        REPOSITORIES << url
        REPOSITORIES.save(REPOSITORIES.filename)
        puts 'url ' + url + ' added.'
    end
end

#cleanObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/raykit/console.rb', line 141

def clean
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    REPOSITORIES.matches(pattern).each{|url|
        repo=Raykit::Git::Repository.new(url)
        work=Raykit::Git::Directory.new(repo.get_dev_dir('work'))
        if(Dir.exist?(work.directory))
            puts 'removing ' + work.directory
            begin
                FileUtils.rm_rf(work.directory,secure:true)
            rescue
                puts 'error removing ' + work.directory
            end
        end
    }
end

#copyObject



170
171
172
173
174
175
176
177
178
179
# File 'lib/raykit/console.rb', line 170

def copy
    if(@pts.arguments.length < 3)
        puts 'source and destination arguments are required for copy'
        return 1
    end
    source = @opts.arguments[1]
    dest = @opts.arguments[2]
    FileUtils.cp(source,dest)
    puts "copied #{source} to #{dest}"
end

#importObject



131
132
133
134
135
136
137
138
139
# File 'lib/raykit/console.rb', line 131

def import
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    puts 'scanning...'
    count=REPOSITORIES.length
    REPOSITORIES.import(pattern)
    new_count=REPOSITORIES.length-count
    puts "imported #{new_count} git repositories"
end

#pullObject



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/raykit/console.rb', line 118

def pull
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    REPOSITORIES.matches(pattern).each{|url|
        repo=Raykit::Git::Repository.new(url)
        work=Raykit::Git::Directory.new(repo.get_dev_dir('work'))
        if(Dir.exist?(work.directory))
            puts url
            work.pull
        end
    }
end

#rake(hash) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/raykit/console.rb', line 181

def rake(hash)
    REPOSITORIES.each{|remote|
        if(remote.include?(hash[:pattern]))
            begin
                puts "remote: #{remote}"
                cmd = Raykit::Rake::run(remote,'master')
                elapsed_str = Timer.get_elapsed_str(cmd.elapsed)
                if(cmd.exitstatus == 0)
                    puts elapsed_str + " " +  Rainbow(SECRETS.hide(cmd.command)).yellow.bright + " (#{cmd.directory})"
                else
                    puts "\r\n" + cmd.command + "\r\n"
                    puts "\r\n" + cmd.output + "\r\n"
                    puts "\r\n" + cmd.error + "\r\n"
                    puts ''
                    puts Rainbow(elapsed_str).red.bright + " " +  Rainbow(cmd.command).white
                end
            rescue
                puts 'rescued...'
            end
        end
    }
end

#removeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/raykit/console.rb', line 75

def remove
    if(@opts.arguments.length < 2)
        puts 'remove requires a url or pattern'
        return 1
    end
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    remove_urls = REPOSITORIES.matches(pattern)
    if(remove_urls.length == 0)
        puts 'no matching urls found.'
    
    else
        remove_urls.each{|url|
        REPOSITORIES.delete(url)
        puts 'url ' + url + ' removed.'
        }
        REPOSITORIES.save(REPOSITORIES.filename)
    end
end

#runObject



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
# File 'lib/raykit/console.rb', line 20

def run
    verb = "usage"
    verb = @opts.arguments[0] if(@opts.arguments.length > 0)
    case verb
    when "usage"
        usage
    when "add"
        add
    when "remove"
        remove
    when "show"
        show
    when "work"
        work
    when "import"
        import
    when "clean"
        clean
    when "sync_version"
        sync_version
    when "copy"
        copy
    when "pull"
        pull
    else
        puts 'unrecognized command ' + verb
        return 1
    end
end

#showObject



95
96
97
98
99
100
101
# File 'lib/raykit/console.rb', line 95

def show
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    REPOSITORIES.matches(pattern).each{|url|
        puts url
    }
end

#sync_versionObject



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/raykit/console.rb', line 158

def sync_version
    if(@opts.arguments.length < 3)
        puts 'source and destination arguments are required for sync_version'
        return 1
    end
    source = @opts.arguments[1]
    dest = @opts.arguments[2]
    Raykit::Version::sync_file_versions(source,dest)
    version = Raykit::Version::detect(source,false)
    puts "version #{version} detected in #{source}, updating #{dest} to match."
end

#usageObject



54
55
56
57
58
# File 'lib/raykit/console.rb', line 54

def usage
    puts @opts
    puts "verbs: " + verbs.to_s
    0
end

#verbsObject



50
51
52
# File 'lib/raykit/console.rb', line 50

def verbs
    ["show"]
end

#workObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/raykit/console.rb', line 103

def work
    pattern=''
    pattern=@opts.arguments[1] if(@opts.arguments.length > 1)
    REPOSITORIES.matches(pattern).each{|url|
        repo=Raykit::Git::Repository.new(url)
        work=Raykit::Git::Directory.new(repo.get_dev_dir('work'))
        if(!Dir.exist?(work.directory))
            PROJECT.run("git clone #{url} #{work.directory}")
        end
        if(Dir.exist?(work.directory))
            work.rake(@opts[:task])
        end
    }
end