Class: Appjam::Generators::Gist

Inherits:
Jam
  • Object
show all
Includes:
Actions, CLIColorize, Thor::Actions
Defined in:
lib/appjam/generators/gist.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



29
30
31
32
33
34
35
36
37
# File 'lib/appjam/generators/gist.rb', line 29

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


152
# File 'lib/appjam/generators/gist.rb', line 152

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

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



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

def download_gist(gist_id,git_category,gist_name,gist_type)
  puts "-- fetching gist [#{gist_name}] --"
  # 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 File.directory?("Gist/#{git_category}/#{gist_name.downcase}")
    `rm -rf Gist/#{git_category}/#{gist_name.downcase}`
  end
  if("#{gist_id}".is_numeric?)
    if system('which git') != nil  
      `git clone git://gist.github.com/#{gist_id}.git Gist/#{git_category}/#{gist_name.downcase} && rm -rf Gist/#{git_category}/#{gist_name.downcase}/.git`
    else
      say "="*70
      say "Git was not installed!! check http://git-scm.com/ for installation."
      say "="*70              
    end
  else
    if "#{gist_type}".strip == 'hg'
      if system('which hg') != nil
        `hg clone #{gist_id} Gist/#{git_category}/#{gist_name.downcase} && rm -rf Gist/#{git_category}/#{gist_name.downcase}/.hg`
      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
        `svn co #{gist_id} Gist/#{git_category}/#{gist_name.downcase} && rm -rf Gist/#{git_category}/#{gist_name.downcase}/.svn`
      else
         say "="*70
         say "Subversion was not installed!! check http://www.open.collab.net/downloads/community/ for installation."
         say "="*70              
      end 
    end 
    if "#{gist_type}".strip == 'git'   
      if system('which git') != nil                  
        `git clone #{gist_id} Gist/#{git_category}/#{gist_name.downcase} && rm -rf Gist/#{git_category}/#{gist_name.downcase}/.git`
      else
        say "="*70
        say "Git was not installed!! check http://git-scm.com/ for installation."
        say "="*70              
      end
    end
  end
  if system('which qlmanage')
    system("qlmanage -p Gist/#{git_category}/#{gist_name.downcase}/* >& /dev/null")
  end
end

.download_gists(username, page = 1) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/appjam/generators/gist.rb', line 72

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/appjam/generators/gist.rb', line 39

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 Gist root



151
# File 'lib/appjam/generators/gist.rb', line 151

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

.update_gistObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/appjam/generators/gist.rb', line 57

def update_gist
  appjam_dir = '~/.appjam'
  appjam_gist = File.expand_path("~") + '/.appjam/gist.yml'
  system "mkdir -p #{appjam_dir}" unless File.exist?(appjam_dir)
  begin
    puts "fetching new gist list from server ... "
    page_source = Net::HTTP.get(URI.parse("http://eiffelqiu.github.com/appjam/gist.yml"))
    File.open(appjam_gist, 'w') {|f| f.write(page_source) }
  rescue SocketError => e
    puts "can not access github.com, back to local version gist.yml"
    origin_gist = File.expand_path(File.dirname(__FILE__) + '/gist.yml')
    system "cp #{origin_gist} #{appjam_gist}"
  end      
end

Instance Method Details

#create_gitObject



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

def create_git  
    valid_constant?(options[:gist] || name)
    @gist_name = (options[:app] || name).gsub(/W/, "_").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
              
    unless @gist_name == 'update'
      if in_app_root? 
        require 'yaml'
        @xcode_project_name = File.basename(Dir.glob('*.xcodeproj')[0],'.xcodeproj').downcase
        # begin
        #   page_source = Net::HTTP.get(URI.parse("http://eiffelqiu.github.com/appjam/gist.yml"))
        # rescue SocketError => e
        #   puts "can not access github.com, back to local version gist.yml"
        # end   
        gistfile = File.expand_path("~") + '/.appjam/gist.yml'
        Gist::update_gist unless File.exist?(gistfile)
        begin 
          puts "fetching new gists ..." 
          g = YAML.load_file(gistfile)  
        rescue ArgumentError => e
          puts "can't fetch new gists, loading local gists ..."
          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
          unless (gcategory == 'lib' or gcategory == 'info')
            
            g[key].each { |k|
              k.each_pair { |k1,v1|
                if "#{k1}" == @gist_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}"
                  Gist::download_gist("#{gid}",gcategory,gname,gtype)
                  eval(File.read(__FILE__) =~ /^__END__/ && $' || '')
                  say "================================================================="
                  say "Your '#{gname.capitalize}' snippet code has been generated."
                  say "Check Gist/#{gcategory}/#{gname}/ for snippet"
                  say "Open #{@xcode_project_name.capitalize}.xcodeproj"
                  say "Add 'Gist/#{gcategory}/#{gname}/' folder to the 'Classes/apps' Group"
                  say "Build and Run"          
                  say "================================================================="              
                end                  
              }
            }
          end
        }
      else 
        puts
        puts '-'*70
        puts "You are not in an iphone project folder"
        puts '-'*70
        puts
      end            
    else
      Gist::update_gist
    end
end

#in_app_root?Boolean

Returns:

  • (Boolean)


165
166
167
168
# File 'lib/appjam/generators/gist.rb', line 165

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