Class: Appjam::Generators::Lib

Inherits:
Jam
  • Object
show all
Includes:
Actions, CLIColorize, Thor::Actions
Defined in:
lib/appjam/generators/lib.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actions

#apply_component_for, #ask, #check_app_existence, #destination_root, #execute_component_setup, #fetch_app_name, #fetch_component_choice, included, #invalid_fields, #resolve_valid_choice, #retrieve_component_config, #store_component_choice, #store_component_config, #valid_choice?, #valid_constant?, #which

Methods inherited from Jam

#create_jam, init_generator, parseTemplate

Class Method Details

.attr_rw(*attrs) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/appjam/generators/lib.rb', line 21

def self.attr_rw(*attrs)
  attrs.each do |attr|
    class_eval %Q{
      def #{attr}(val=nil)
        val.nil? ? @#{attr} : @#{attr} = val
      end
    }
  end
end


156
# File 'lib/appjam/generators/lib.rb', line 156

def self.banner; "appjam lib [name]"; end

.download_gist(gist_id, git_category, gist_name, gist_type) ⇒ Object



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
# File 'lib/appjam/generators/lib.rb', line 69

def download_gist(gist_id,git_category,gist_name,gist_type)
  puts "-- fetching [#{gist_name}] lib --"
  # require 'uri'
  # require 'yajl/http_stream'
  # 
  # uri = URI.parse("http://gist.github.com/api/v1/json/#{gist_id}")
  # Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
  #   
  # end    
  #     if @submodule_name == 'kissxml'
  #       eval(File.read(__FILE__) =~ /^__END__\n/ && $' || '')  
  # 
  #       system "rm -rf kissxml"
  #       system "hg clone https://kissxml.googlecode.com/hg/ Frameworks/kissxml"
  #       system "git add ."
  #       system "git commit -m 'import kissxml submodule'"
  #       say (<<-TEXT).gsub(/ {10}/,'')  
  if 'ftp' == "#{gist_type.strip}" or 'http' == "#{gist_type.strip}"  
    system "ftp #{gist_id}"
    dir = "#{File.basename(gist_id.strip)}"
    case "#{File.basename(gist_id.strip)}"
      when /.tar$/
        system "tar xvf #{File.basename(gist_id.strip)}"
      when /.gz$/
        system "tar zxvf #{File.basename(gist_id.strip)}"
      when /.bz2$/
        system "tar jxvf #{File.basename(gist_id.strip)}"
      when /.bz$/
        system "tar zxvf #{File.basename(gist_id.strip)}"
      when /.Z$/
        system "tar Zxvf #{File.basename(gist_id.strip)}"
      when /.zip$/
        system "unzip #{File.basename(gist_id.strip)}"
    end
    dir = File.basename(dir, '.gz')
    dir = File.basename(dir, '.tar')
    dir = File.basename(dir, '.bz2')
    dir = File.basename(dir, '.bz')
    dir = File.basename(dir, '.Z')
    dir = File.basename(dir, '.zip')            
    system "rm #{File.basename(gist_id.strip)}"
    system "mv #{dir} Frameworks/"
  else   
    if gist_id.include?('github.com')               
      if File.directory?("Frameworks/#{gist_name.downcase}")
        `rm -rf Frameworks/#{gist_name.downcase}`
      end
      if("#{gist_id}".is_numeric?)
        `git clone git://gist.github.com/#{gist_id}.git Frameworks/#{gist_name.downcase} && rm -rf Frameworks/#{gist_name.downcase}/.git`
      else
        `git clone #{gist_id} Frameworks/#{gist_name.downcase} && rm -rf Frameworks/#{gist_name.downcase}/.git`
      end
    else
      if "#{gist_type}".strip == 'hg'
        if system('which hg') != nil
           system "rm -rf Frameworks/#{gist_name.downcase}"
           system "hg clone https://kissxml.googlecode.com/hg/ Frameworks/#{gist_name.downcase}"
           # system "git add ."
           # system "git commit -m 'import #{gist_name.downcase} submodule'"     
        else
           say "="*70
           say "Mercurial was not installed!! check http://mercurial.selenic.com/ for installation."
           say "="*70              
        end 
      end 
      if "#{gist_type}".strip == 'svn'
        if system('which svn') != nil
           system "rm -rf Frameworks/#{gist_name.downcase}"
           system "svn co #{gist_id} Frameworks/#{gist_name.downcase}"
           # system "git add ."
           # system "git commit -m 'import #{gist_name.downcase} submodule'"     
        else
           say "="*70
           say "Subversion was not installed!! check http://www.open.collab.net/downloads/community/ for installation."
           say "="*70              
        end 
      end                 
    end            
  end
end

.download_gists(username, page = 1) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/appjam/generators/lib.rb', line 49

def download_gists(username, page=1)
  puts "-- Downloading page #{page} of gists --"
  url = URI.parse("http://gist.github.com")
  res = Net::HTTP.start(url.host, url.port) do |http|
      response = http.get("/#{username}?page=#{page}")
      if response.code == '200'
        links = get_links(response.body)
        links.each do |link, gist_id|
          puts "git://gist.github.com/#{gist_id}.git"
          if File.directory?("Gist/#{gist_id}")
            `cd Gist/#{gist_id} && git pull ; cd ..`
          else
            `git clone git://gist.github.com/#{gist_id}.git Gist/#{gist_id}`
          end
        end
        download_gists(username, page+1) unless links.empty?
      end
  end
end

.preview_gist(gid) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appjam/generators/lib.rb', line 31

def preview_gist(gid)
  uri  = URI("https://gist.github.com/#{gid}.txt")
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == 'https'
    http.use_ssl     = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  result   = http.start {|h| h.request(Net::HTTP::Get.new(uri.path))}
  tempfile = Tempfile.new('gist')
  tempfile.puts(result)
  tempfile.close

  if system('which qlmanage')
    system("qlmanage -p #{tempfile.path}/* >& /dev/null")
  end          
end

.source_rootObject

Define the source lib root



155
# File 'lib/appjam/generators/lib.rb', line 155

def self.source_root; File.expand_path(File.dirname(__FILE__)); end

Instance Method Details

#create_libObject



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
# File 'lib/appjam/generators/lib.rb', line 174

def create_lib
  if in_app_root? 
    valid_constant?(options[:lib] || name)
    @lib_name = (options[:app] || name).gsub(/W/, "_").downcase
    @xcode_project_name = File.basename(Dir.glob('*.xcodeproj')[0],'.xcodeproj').downcase          
    @class_name = (options[:app] || name).gsub(/W/, "_").capitalize
    @developer = "eiffel"
    @created_on = Date.today.to_s
    self.destination_root = options[:root]
    
    puts colorize( "Appjam Version: #{Appjam::Version::STRING}", { :foreground => :red, :background => :white, :config => :underline } )
    puts
    require 'yaml'
    # begin
    #   page_source = Net::HTTP.get(URI.parse("http://eiffelqiu.github.com/appjam/gist.yml"))
    # rescue SocketError => e
    # end   
    gistfile = File.expand_path("~") + '/.appjam/gist.yml'
    Gist::update_gist unless File.exist?(gistfile)          
    begin 
      g = YAML.load_file(gistfile)  
    rescue ArgumentError => e
      g = YAML.load_file(File.expand_path(File.dirname(__FILE__) + '/gist.yml'))
    end
    puts "notice: a new version '#{g['info']}' released" if g['info'] and g['info'].strip != "#{Appjam::Version::STRING}"
    puts
    g.each_pair {|key,value|
      gcategory = key.downcase
      if gcategory == 'lib'
        g[key].each { |k|
          k.each_pair { |k1,v1|
            if "#{k1}" == @lib_name
              gid = k[k1][0]['id']
              gname = k[k1][1]['name']
              gtype = 'git'
              if k[k1].length == 4
                gtype = k[k1][3]['type']
              end                    
              puts "repository type: #{gtype}"
              eval(File.read(__FILE__) =~ /^__END__\n/ && $' || '')                       
              Lib::download_gist("#{gid}",gcategory,gname,gtype)
              eval(File.read(__FILE__) =~ /^__END__/ && $' || '')
              say "================================================================="
              say "Check Frameworks/#{gname.downcase}/ for new framework [#{gname}]"       
              say "================================================================="              
            end                  
          }
        }
      end
    }
  else 
    puts
    puts '-'*70
    puts "You are not in an iphone project folder"
    puts '-'*70
    puts
  end        
end

#in_app_root?Boolean

Returns:

  • (Boolean)


169
170
171
172
# File 'lib/appjam/generators/lib.rb', line 169

def in_app_root?
  # File.exist?('Classes')
  Dir.glob("*.xcodeproj").count >= 1
end