Class: Raykit::Project

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProject

Returns a new instance of Project.



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/raykit/project.rb', line 16

def initialize()
    @verbose=false
    @timer=Raykit::Timer.new
    @remote=''
    @commit_message_filename="commit_message.tmp"
    if(Dir.exist?(RAKE_DIRECTORY))
        @directory=RAKE_DIRECTORY
        if(Dir.exist?("#{RAKE_DIRECTORY}/.git") && Dir.exist?(@directory))
            @git_directory=Raykit::Git::Directory.new(@directory)
            @remote=@git_directory.remote
        end
    else
        @directory=''
    end

    #@log=Log.new("#{RAKE_DIRECTORY}/tmp/raykit.log")


    if(defined?(NAME))
        @name=NAME 
    else
        slns = Dir.glob("*.sln")
        if(slns.length == 1)
            @name=slns[0].gsub('.sln','')
        else
            remote_parts = @remote.split('/')
            if(remote_parts.length > 0)
                @name=remote_parts[-1].gsub('.git','')
            end
        end
    end
    @repository=Raykit::Git::Repository.new(@remote)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/raykit/project.rb', line 5

def name
  @name
end

#timerObject

Returns the value of attribute timer.



6
7
8
# File 'lib/raykit/project.rb', line 6

def timer
  @timer
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/raykit/project.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#branchObject



60
61
62
# File 'lib/raykit/project.rb', line 60

def branch
    @git_directory.branch
end

#commit(commit_message) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/raykit/project.rb', line 194

def commit(commit_message)
    warn "[DEPRECATION] 'commit_message_filename' is deprectated. Use a run command for better transparency."
    Dir.chdir(@directory) do
        if(File.exist?('.gitignore'))
            status=`git status`
            if(status.include?('Changes not staged for commit:'))
                run("git add --all")
                if(GIT_DIRECTORY.outstanding_commit?)
                    if(File.exist?(@commit_message_filename))
                        run("git commit --file #{@commit_message_filename}")
                        File.delete(@commit_message_filename)
                    else
                        run("git commit -m'#{commit_message}'")
                    end
                end  
            end
        else
            puts "warning: .gitignore not found."
        end
    end
    self
end

#commit_message_filenameObject



189
190
191
192
# File 'lib/raykit/project.rb', line 189

def commit_message_filename
    warn "[DEPRECATION] 'commit_message_filename' is deprectated."
    @commit_message_filename
end

#directoryObject



57
58
59
# File 'lib/raykit/project.rb', line 57

def directory
    @directory
end

#elapsedObject



154
155
156
157
158
159
160
161
# File 'lib/raykit/project.rb', line 154

def elapsed
    elapsed=@timer.elapsed
    if(elapsed < 1.0)
        "%.1f" % (@timer.elapsed) + "s"
    else
        "%.0f" % (@timer.elapsed) + "s"
    end
end

#get_dev_dir(subdir) ⇒ Object



49
50
51
# File 'lib/raykit/project.rb', line 49

def get_dev_dir(subdir)
    Raykit::Environment::get_dev_dir(subdir)
end

#has_diff?(filename) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/raykit/project.rb', line 89

def has_diff?(filename)
    Dir.chdir(@directory) do
        text=`git diff #{filename}`.strip
        return true if(text.length > 0) 
    end    
end

#infoObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/raykit/project.rb', line 163

def info
    ljust=35
    puts ''
    puts "PROJECT.name".ljust(ljust) + Rainbow(PROJECT.name).yellow.bright
    puts "PROJECT.version".ljust(ljust) + Rainbow(PROJECT.version).yellow.bright
    puts "PROJECT.remote".ljust(ljust) + Rainbow(PROJECT.remote).yellow.bright
    puts "PROJECT.branch".ljust(ljust) + Rainbow(PROJECT.branch).yellow.bright
    puts "PROJECT.latest_tag".ljust(ljust) + Rainbow(PROJECT.latest_tag).yellow.bright
    puts "PROJECT.latest_tag_commit".ljust(ljust) + Rainbow(PROJECT.latest_tag_commit).yellow.bright
    puts "PROJECT.latest_commit".ljust(ljust) + Rainbow(PROJECT.latest_commit).yellow.bright
    puts "PROJECT.last_modified_filename".ljust(ljust) + Rainbow(PROJECT.last_modified_filename).yellow.bright
    #puts "PROJECT.elapsed".ljust(ljust) + Rainbow(PROJECT.elapsed).yellow.bright

    puts "PROJECT.size".ljust(ljust) + Rainbow(PROJECT.size).yellow.bright
    puts "PROJECT.size_pack".ljust(ljust) + Rainbow(PROJECT.size_pack).yellow.bright
    puts "PROJECT.outstanding_commit?".ljust(ljust) + Rainbow(PROJECT.outstanding_commit?).yellow.bright
    puts "PROJECT.outstanding_tag?".ljust(ljust) + Rainbow(PROJECT.outstanding_tag?).yellow.bright
    puts ''
    self
end

#last_modified_filenameObject



126
127
128
129
130
# File 'lib/raykit/project.rb', line 126

def last_modified_filename
    Dir.chdir(@directory) do
        Dir.glob("**/*.*").select{|f| File.file?(f)}.max_by {|f| File.mtime(f)}
    end
end

#latest_commitObject

latest local commit



65
66
67
68
69
70
71
# File 'lib/raykit/project.rb', line 65

def latest_commit
    Dir.chdir(@directory) do
        text=`git log -n 1`
        scan=text.scan(/commit ([\w]+)\s/)
        return scan[0][0].to_s 
    end
end

#latest_tagObject

def update_version

@version=Raykit::Version.detect(@name,@verbose)

end



114
115
116
# File 'lib/raykit/project.rb', line 114

def latest_tag
    `git describe  --abbrev=0`.strip
end

#latest_tag_commitObject



118
119
120
# File 'lib/raykit/project.rb', line 118

def latest_tag_commit
    `git show-ref -s #{latest_tag}`.strip
end

#logObject



53
54
55
# File 'lib/raykit/project.rb', line 53

def log
    @log
end

#outstanding_commit?Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
# File 'lib/raykit/project.rb', line 73

def outstanding_commit?
    Dir.chdir(@directory) do
        return true if(!`git status`.include?('nothing to commit'))
        #text=`git diff`.strip

        #return true if(text.length > 0)

    end
    false
end

#outstanding_tag?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/raykit/project.rb', line 82

def outstanding_tag?
    #puts `git log -n 1`

    #!latest_tag_commit.eql?(latest_commit)

    #commit 2e4cb6d6c0721e16cd06afee85b7cdc27354921b (HEAD -> master, tag: 0.0.180, origin/master, origin/HEAD)

    outstanding_commit? || !`git log -n 1`.include?('tag:')
end

#pullObject



229
230
231
232
233
234
# File 'lib/raykit/project.rb', line 229

def pull
    Dir.chdir(@directory) do
        run('git pull')
    end
    self
end

#pushObject



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/raykit/project.rb', line 217

def push
    Dir.chdir(@directory) do
        status=`git status`
        if(status.include?('use "git push"'))
            run('git push')
            
            #run('git push --tags')

        end
    end
    self
end

#remoteObject



122
123
124
# File 'lib/raykit/project.rb', line 122

def remote
    @remote
end

#run(command, quit_on_failure = true) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/raykit/project.rb', line 258

def run(command,quit_on_failure=true) 
    #log.update_command_time(command,DateTime.now)

    if(command.kind_of?(Array))
        command.each{|subcommand| run(subcommand,quit_on_failure)}
    else
        cmd = Command.new(command)
        cmd.summary
        elapsed_str = Timer.get_elapsed_str(cmd.elapsed,0)
        if(cmd.exitstatus == 0)
            #puts elapsed_str + " " +  Rainbow(cmd.command).yellow.bright

            #return elapsed_str + " " + cmd.command

        else
            # display error details

            puts cmd.output
            puts cmd.error
            puts
            if(quit_on_failure)
                abort Rainbow(elapsed_str).red.bright + " " +  Rainbow(cmd.command).white
            end
        end
        cmd
    end
end

#sizeObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/raykit/project.rb', line 132

def size 
    Dir.chdir(@directory) do
        text =`git count-objects -v -H`
        if matches = text.match(/size: ([. \w]+)$/)
            matches[1]
        else
            text
        end
    end
end

#size_packObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/raykit/project.rb', line 143

def size_pack
    Dir.chdir(@directory) do
        text =`git count-objects -v -H`
        if matches = text.match(/size-pack: ([. \w]+)$/)
            matches[1]
        else
            text
        end
    end
end

#summaryObject



183
184
185
186
187
# File 'lib/raykit/project.rb', line 183

def summary
    info if(@verbose)
    
    puts "[" + elapsed + "] " + Rainbow(@name).yellow.bright + " " + Rainbow(version).yellow.bright
end

#tagObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/raykit/project.rb', line 236

def tag
    warn "[DEPRECATION] 'tag' is deprecated. Use run command(s) for better transparency."
    Dir.chdir(@directory) do
        specific_tag=`git tag -l #{@version}`.strip
        puts Rainbow("git tag - #{@version}").green if(@verbose)
        puts `git tag -l #{@version}` if(@verbose)
        if(specific_tag.length == 0)
            puts "tag #{@version} not detected." if(@verbose)
            tag = `git describe --abbrev=0 --tags`.strip
            if(@version != tag)
                puts "latest tag #{@tag}" if(@verbose)
                run("git tag #{@version} -m'#{@version}'")
                run("git push origin #{@version}")
                #push

            end
        else
            puts "already has tag #{specific_tag}" if(@verbose)
        end
    end
    self
end

#versionObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/raykit/project.rb', line 96

def version
    if(@version.nil?)
        #Dir.chdir(@directory) do

        #    if(Dir.glob("*.gemspec").length > 0)

        #        @version=`bump current`.gsub('Current version:','').strip

        #    else

        #        @version=Raykit::VersionHelper.detect(@name,@verbose)

        #    end

        @version=Raykit::Version.detect(@name,@verbose)
        #end

    end
    @version
end