Class: Dev::Project

Inherits:
Hash
  • Object
show all
Defined in:
lib/dev/Project.rb

Instance Method Summary collapse

Methods inherited from Hash

get_hash_value, #get_value, print_hash, set_hash_value, #set_value

Constructor Details

#initialize(hash, init_defaults = true) ⇒ Project

Returns a new instance of Project.



9
10
11
12
13
# File 'lib/dev/Project.rb', line 9

def initialize(hash,init_defaults=true)
  puts_debug "initialize, copying hash values"
  hash.each { |name,value| self[name]=value }
  update_default_values if init_defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



43
# File 'lib/dev/Project.rb', line 43

def method_missing( name, *args ); array_method(name); end

Instance Method Details

#array_method(name) ⇒ Object



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
# File 'lib/dev/Project.rb', line 15

def array_method(name)
  string_name=name.to_s
  puts_debug "method_missing name=" + string_name
  if(self.get_value(string_name).nil? || self.get_value(string_name).length < 1)
    puts_debug "no directives defined for method #{string_name}"
    return
  end
  puts_debug "found directives for #{string_name}: #{self.get_value(string_name).to_s}"
  self.get_value(string_name).each{ |c|
    # expand the command here....
    command=expand_command(c)
    if c.include?('<%') && c.include?('%>')
      #puts "Command: " + c
      eval(c.gsub("<%","").gsub("%>",""))
    else
     # can the command be converted to a hash?
     hash=Dev::Environment.s_to_hash(command)
     call=nil
     if(hash.nil?)
       call=Dev::SystemCall.new(command)
     else
       call=Dev::SystemCall.new(hash)
     end
     call.puts_summary
    end
  }
end

#commitObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/dev/Project.rb', line 117

def commit
  call=nil
  call=Dev::SystemCall.new('git diff') if File.exists?(".git")
  call=Dev::SystemCall.new('svn diff') if File.exists?(".svn")
  unless call.nil? || call.output.length==0
    array_method("commit")
  else
    puts "  no differences detected"
  end
end

#expand_command(command) ⇒ Object



44
# File 'lib/dev/Project.rb', line 44

def expand_command(command); expand_project_variables(command); end

#expand_project_variables(str) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/dev/Project.rb', line 46

def expand_project_variables(str)
  result=Dev::Environment.expand_string_variables(str) # expands ruby variables, e.g. '#{name}' to 'widget'
  if result.include?('<') && result.include?('>')
    result.scan(/(<[\w,]+>)/).each { |pvar|            # expand project variables, e.g. '<name>' to 'widget
      key = pvar[0].gsub("<","").gsub(">","")          #                                '<paths,nunit>' to 'C:/wherever/NUnit.exe'
      result = result.gsub(pvar[0],get_value(key))
    }
  end
  result
end

#featuresObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/dev/Project.rb', line 106

def features
  if self[:type]=="C#" && File.exists?("bin/x86/Release/#{self[:name]}.Features.dll")
    call=Dev::SystemCall.new(expand_project_variables("<paths,nunit> /nologo bin/x86/Release/#{self[:name]}.Features.dll"))
    puts call.command
    puts call.output
    raise "exit_code=#{call.status.to_s}" + call.error unless call.status==0
  end
  
  array_method("features")
end

#get_default_value(key) ⇒ Object



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
# File 'lib/dev/Project.rb', line 142

def get_default_value(key)
  system_call = Dev::SystemCall.new("semver init") if(!File.exist?(".semver"))  
  version = SemVer.find
 
  value="#{Dev::Environment.dev_root}" if key=="dev_root"
  value="C#" if key=="type"

  
  value="**/*.{rb,feature,semver}" if key=="src_glob"
  value="**/*.{cs,xaml,rb,resx,settings,feature,semver}" if key=="src_glob" && self[:type]=="C#" 
  value="**/*.{c,cpp,h,hpp,semver}" if key=="src_glob" && (self[:type]=="C++" || self[:type]=="c++" || self[:type]=="cpp")
  value="**/*.{rb,feature,semver}" if key=="src_glob" && self[:type]=="ruby" 
  value="**/*.{rb,feature,gemspec,semver}" if key=="src_glob" && self[:type]=="gem"
  value=loc_total if key=="loc"
  value="svn" if key=="scm_type" && File.exists?(".svn")
  value="git" if key=="scm_type" && File.exists?(".git")
  value="#{version.major}.#{version.minor}.#{version.patch}" if key=="version"

  if key=="file_count" 
    unless Dir.glob(self[:src_glob]).nil?
      value=Dir.glob(self[:src_glob]).length.to_s
    end
  end
  
  hash=Hash.new
  
  if(key=="paths")
    if(self[:type]=="C#")
      hash.set_value("msbuild","C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe")
      nunit_version="2.5.10.11092"
      hash.set_value("nunit","#{Dev::Environment.dev_root}/dep/ThirdParty/NUnit/#{nunit_version}/bin/net-2.0/nunit-console-x86.exe")
    end
  end
  value=hash if hash.length > 0
  
  array=Array.new
  if(key=="setup")
    array << "bundle install" if File.exists?("Gemfile")
    
    # dep,svn directives
    dep_hash=self.get_value("dep")
    unless dep_hash.nil?
      dep_hash.each do |key,value|
        if value.kind_of?(Hash)
          # potential hash keys: uri, dir, rake
          # uri is required
          unless value.get_value("uri").nil?
            uri=expand_project_variables(value.get_value("uri"))
            uri_words=uri.split('/')
            while(uri_words.length > 3) do
              uri_words.shift 
            end
            dir=uri_words.join('/')
            dir=expand_project_variables(value.get_value("dir")) unless value.get_value("dir").nil?
            dir=self[:dev_root] + "/dep/" + dir unless dir.include?(self[:dev_root]) 
           
	  cmd = "svn export " + expand_project_variables(value.get_value("uri")) + " " + dir 
	  eval_cmd = "<%SystemCall.new('#{cmd}') unless File.exists?('#{dir}')%>"
	  array << eval_cmd
            array << "<%puts ' #{dir} exists.' if File.exist?('#{dir}')%>"
            
            unless value.get_value("rake").nil?
              array << "{cmd: 'rake #{value.get_value("rake").to_s}', dir: '#{dir}', cache:'true'}" unless value.get_value("cache").nil?
              array << "{cmd: 'rake #{value.get_value("rake").to_s}', dir: '#{dir}'}" if value.get_value("cache").nil?
            end
          end
        end
      end
    end
  end
  if(key=="compile")
    Dir.glob("*.gemspec").each { |gs| array << "gem build #{gs}" } if self[:type]=="gem"
    
    if self[:type]=="C#"
      if(!has_key?(:x86_release_compile_flags))
        self[:x86_release_compile_flags]="/property:Configuration=Release /property:Platform=\"x86\"  /p:OutputPath=./bin/x86/Release"
      end
    end
    Dir.glob("*.csproj").each{|cs|array<<"<paths,msbuild> #{cs} <x86_release_compile_flags>"}
  end
  if(key=="test")
    #Dir.glob("*.gemspec").each { |gs| array << "gem uninstall #{gs.gsub('.gemspec','')} -v #{self[:version]}" } if self[:type]=="gem"
    Dir.glob("*.gemspec").each { |gs| array << "gem install ./#{gs.gsub('.gemspec','')}-#{self[:version]}.gem" } if self[:type]=="gem"
    if self[:type]=="C#"
      Dir.glob("*.{Test.csproj,Features.csproj}").each { |cs|
        dll_name="bin/x86/Release/#{File.basename(cs,'.csproj')}.dll"
        array << "<paths,nunit> /nologo #{dll_name} /xml:#{dll_name}.nunit-results.xml"
      }
    end
  end
  if(key=="features")
    array << "cucumber features" if File.exists?("features") && Dir.glob("features/**/*.rb").length > 0
  end
  if(key=="has_diff")
    array << 'git diff' if File.exists?(".git")
    array << 'svn diff' if File.exists?(".svn")
  end
  if(key=="commit")
    array << 'git commit -a -m "rake commit all"' if File.exists?(".git")
    array << 'svn commit -m "rake commit all"' if File.exists?(".svn")
  end
  
  value=array if array.length > 0
  
  return value
end

#infoObject



128
129
130
131
132
133
134
135
136
# File 'lib/dev/Project.rb', line 128

def info
 [ "file_count","loc" ].each do |k| 
    puts_debug "set_default_value(#{k})"
    set_default_value(k) 
  end
  puts " "
	Hash.print_hash("",self)
  puts " "
end

#locObject



65
66
67
# File 'lib/dev/Project.rb', line 65

def loc
  system(loc_cmd)
end

#loc_cmdObject



57
58
59
60
61
62
63
# File 'lib/dev/Project.rb', line 57

def loc_cmd
  cmd="countloc --recurse ."
  cmd="countloc --recurse --mode ruby ." if self[:type]=="ruby" || self[:type]=="gem"
  cmd="countloc --recurse --mode csharp ." if self[:type]=="C#"
  cmd="countloc --recurse --mode cpp ." if self[:type]=="c++"
  return cmd
end

#loc_totalObject



69
70
71
72
73
74
75
76
77
# File 'lib/dev/Project.rb', line 69

def loc_total
  # parse the output for TOTAL LOC
  call=Dev::SystemCall.new(loc_cmd)
  words=call.output.split
  if(words.length>6)
    return words[words.length-6]
  end
  "?"
end

#replaceObject



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
# File 'lib/dev/Project.rb', line 79

def replace
  #puts " "
  #puts "replace".foreground(:yellow).bright
  hash=self[:replace] 
  if hash.nil?
    puts "  no replace directives"
  end
  unless hash.nil?
    hash.each do |k,v| 
      unless v.nil?
        file=v[:file]
        search=v[:search]
        replace=v[:replace]
        glob=v[:glob]
        unless search.nil? || replace.nil?
          unless file.nil?
            Dev::Environment.replace_text_in_file(file,search,replace)
          end
          unless glob.nil?
            Dev::Environment.replace_text_in_glob(glob,search,replace)
          end
        end
      end
    end
  end
end

#set_default_value(key) ⇒ Object



138
139
140
# File 'lib/dev/Project.rb', line 138

def set_default_value(key)
  set_value(key,get_default_value(key)) if get_value(key).nil? && !get_default_value(key).nil?
end

#update_default_valuesObject



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/dev/Project.rb', line 249

def update_default_values
  puts_debug "update_default_values"
  
  self[:type]="C#" if self[:type].nil?
  
  [ "version","dev_root","src_glob","scm_type",
    "paths","setup","compile","test","features","commit" ].each do |k| 
    puts_debug "set_default_value(#{k})"
    set_default_value(k) 
  end
  
  generate_tasks(self)
end