Class: Transender::Ji

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

Overview

Transender - Ji

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Ji

Supply a Hash of options containing:

options

your fresh apps title

options

git repository of the base project - app_title becomes a clone of it

options

output dir

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/transender.rb', line 91

def initialize(options)
  raise ArgumentError unless options.class == Hash && options[:app_title] && options[:transform] #&& options[:ji_path]
  @options = options
  @ji_path = @options[:ji_path] || Dir.getwd
  @ji_path = Dir.getwd if @ji_path.empty?
  puts "Ji path is: #{@ji_path}"
  unless File.exists? @ji_path
    begin
      FileUtils.mkdir @ji_path
    rescue 
      puts "Output path could not be created."
      raise ArgumentError
    end
  end
  @app_title = @options[:app_title]
  @app_path = File.join(@ji_path, @app_title)
  @abowl_path = File.join(@ji_path, "abowl")
  @abowl_yml = File.join(@abowl_path, "abowl.yml")
  @transform = @options[:transform]
  @transform_title = Transender.extract_name(@transform)
  @id = Time.now.strftime("%Y-%m-%d-%s")
end

Instance Attribute Details

#abowl_pathObject

Returns the value of attribute abowl_path.



85
86
87
# File 'lib/transender.rb', line 85

def abowl_path
  @abowl_path
end

#abowl_ymlObject

Returns the value of attribute abowl_yml.



85
86
87
# File 'lib/transender.rb', line 85

def abowl_yml
  @abowl_yml
end

#app_pathObject

Returns the value of attribute app_path.



85
86
87
# File 'lib/transender.rb', line 85

def app_path
  @app_path
end

#app_titleObject

Returns the value of attribute app_title.



85
86
87
# File 'lib/transender.rb', line 85

def app_title
  @app_title
end

#idObject

Returns the value of attribute id.



85
86
87
# File 'lib/transender.rb', line 85

def id
  @id
end

#ji_pathObject

Returns the value of attribute ji_path.



85
86
87
# File 'lib/transender.rb', line 85

def ji_path
  @ji_path
end

#optionsObject

Returns the value of attribute options.



85
86
87
# File 'lib/transender.rb', line 85

def options
  @options
end

#transformObject

Returns the value of attribute transform.



85
86
87
# File 'lib/transender.rb', line 85

def transform
  @transform
end

#transform_titleObject

Returns the value of attribute transform_title.



85
86
87
# File 'lib/transender.rb', line 85

def transform_title
  @transform_title
end

Class Method Details

.transend(t = {}) ⇒ Object

Transender::Ji.transend if there is abowl, it will be used for transending



345
346
347
348
349
350
351
352
# File 'lib/transender.rb', line 345

def self.transend(t={})
  ji = Ji.new(t)
  ji.backdrop
  ji.clone_and_remove_git
  ji.rename
  ji.transend
  ji.dropback
end

.transend_and_zip(t = {}) {|zip| ... } ⇒ Object

Use maybe like this: Transender::Ji.transend_and_zip(ahash) {|zip| render :text => zip}

Yields:



338
339
340
341
# File 'lib/transender.rb', line 338

def self.transend_and_zip(t={}, &block)
  zip = Ji.new(t).transendize
  yield zip if block
end

.transform_and_zip(t = {}) {|zip| ... } ⇒ Object

Use maybe like this: Transender::Ji.transform_and_zip(ahash) {|zip| render :text => zip}

Yields:



332
333
334
335
# File 'lib/transender.rb', line 332

def self.transform_and_zip(t={}, &block)
  zip = Ji.new(t).transformize
  yield zip if block
end

.xcode_rename(t = {}) ⇒ Object

Use maybe like this: Transender::Ji.xcode_rename(ahash)



327
328
329
# File 'lib/transender.rb', line 327

def self.xcode_rename(t={})
  Ji.new(t).copy_and_rename
end

Instance Method Details

#backdropObject

backdrop and dropback are used inconjunction, to form a form of mending the code back into the project, while maintaining transendability with the mothers source saves Clasess, Artwork, Transends and Views to abowl this will later be copied back to the project



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
# File 'lib/transender.rb', line 176

def backdrop
  #make sure there is abowl
  make_abowl unless read_abowl 

  #if the app is already there
  if File.exists?(app_path)

    # handle artwork if any
    files = Dir["#{app_path}/Artwork/*"]
    if files&&files.size>0
      #remove original artwork from abowl
      FileUtils.rm_rf "#{abowl_path}/Artwork"
      #copy this apps artworks to abowl
      FileUtils.cp_r "#{app_path}/Artwork", "#{abowl_path}/"
    end

    # handle transends if any
    files = Dir["#{app_path}/Transends/*"]
    if files&&files.size>0
      #remove original transends from abowl
      FileUtils.rm_rf "#{abowl_path}/Transends"
      #copy this bowls transends
      FileUtils.cp_r "#{app_path}/Transends", "#{abowl_path}/"
    end

    # handle classes&views file
    #FileUtils.rm_rf "#{abowl_path}/Classes"
    #FileUtils.rm_rf "#{abowl_path}/Views"

    if @abowl['files']
      dirz = @abowl['files'].keys
      dirz.each do |type|
        FileUtils.cp_r "#{app_path}/Classes/#{type}", "#{abowl_path}/Classes/" unless type == "Views"
      end
    end
    FileUtils.cp_r "#{app_path}/Views", "#{abowl_path}/"

  end

  #else do nothinf

end

#clone_and_remove_gitObject

clones from transform then removes git



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/transender.rb', line 261

def clone_and_remove_git
  #prepare destination without any warning
  FileUtils.rm_rf @app_path #`rm -rf #{@app_path}`
  
  if Object.const_defined? 'RSPEC'
    #todo 
    #this does not create a .git inside the cloned project
    `git --work-tree=#{@app_path} --git-dir=#{@app_path}/.git clone --no-hardlinks #{@transform} #{@app_path}`
  else
    `git clone --no-hardlinks #{@transform} #{@app_path}`
  end
  
  #remove any past life remains from the fresh project
  FileUtils.rm_rf File.join(@app_path, 'build') #`rm -rf #{File.join(@app_path, 'build')}`
  FileUtils.rm_rf File.join(@app_path, '.git')  #`rm -rf #{File.join(@app_path, '.git')}`

  #cleanup Transends and Artwork, so it is empty
  FileUtils.rm_rf Dir.glob("#{@app_path}/Artwork/**/*")
  FileUtils.rm_rf Dir.glob("#{@app_path}/Transends/**/*")
  
  puts "Cloned from #{@transform} into #{@app_path}."
end

#copy_and_renameObject

copies from transform and renames



253
254
255
256
257
258
# File 'lib/transender.rb', line 253

def copy_and_rename
  #prepare destination without any warning
  FileUtils.rm_rf @app_path #`rm -rf #{@app_path}`
  `cp -r #{@transform} #{@app_path}`
  rename
end

#dropbackObject

drops the diff between saved abowl files back into the app_path this way, you can write code without having to worry about .git and stuff the mother source can be your template you just write the difference if you transend with a new release of the mother source, just add existing files in XCode after transending



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/transender.rb', line 224

def dropback
  #the abowl has Classes, and the app has classes
  #get all abowl classes and copy them over if they do not exist in the app_path
  #do the same for views...

  # handle artwork if any
  files = Dir["#{abowl_path}/Artwork/*"]
  if files&&files.size>0
    #remove original artwork
    FileUtils.rm_rf "#{app_path}/Artwork"
    #copy this bowls artworks
    FileUtils.cp_r "#{abowl_path}/Artwork", "#{app_path}/"
  end

  # handle transends if any
  files = Dir["#{abowl_path}/Transends/*"]
  if files&&files.size>0
    #remove original transends
    FileUtils.rm_rf "#{app_path}/Transends"
    #copy this bowls transends
    FileUtils.cp_r "#{abowl_path}/Transends", "#{app_path}/"
  end

  #copy from abowl.classes to app.classes if abowl.classes.file not in app.classes
  `cp -rn '#{abowl_path}/Classes' '#{app_path}/';cp -rn '#{abowl_path}/Views' '#{app_path}/'`

end

#renameObject

renames within project app_title



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/transender.rb', line 285

def rename
  todo = ""
  if rename_files
    if replace_strings_in_files
    else
      todo << 'TODO: You need to replace some strings in some files manually. Thanks.\n'
    end
  else
    todo << 'TODO: You need to rename some files manually. Thanks.\n'
  end
  todo = todo.empty? ? "Renamed/replaced occurences of #{transform_title} to #{app_title}." : todo
  puts todo
end

#transendObject

read abowl, transend that information into existing project deleting, copying files in this proces



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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/transender.rb', line 116

def transend
  if read_abowl 
    
    # handle classes&views files
    if @abowl['files']
      dirz = @abowl['files'].keys
      dirz.each do |type|
        dir_string = "#{app_path}/Classes/#{type}/*.{m,h}"
        dir_string = "#{app_path}/Views/*.{xib, nib}" if type == "Views"
        filenames = @abowl['files'][type]
        if filenames&&filenames.size>0
          unless filenames.size == 1 && filenames[0] == "all"
            Dir[dir_string].each do |file|
              name = Transender.extract_name(file)
              FileUtils.rm file unless filenames.member? name
            end
          end
        else
          FileUtils.rm_rf(Dir.glob(dir_string))
        end
      end
    end
    
    #handle app delegate defines
    defines = @abowl['app']
    appDelegate = "#{app_path}/Classes/Application/#{app_title}AppDelegate.h"
    appDelegate_temp = "#{app_path}/Classes/Application/#{app_title}AppDelegate.h.temp"
    File.open(appDelegate_temp, "w") do |outfile|
      File.open(appDelegate, "r") do |infile|
        while (line = infile.gets)
          if line =~ /#define/
            key = line.split[1]
            if defines.has_key? key
              value = defines[key]
              if value.to_s =~ /^(\d)*\.(\d)*$/ #is numerical
                outfile << (line.gsub /^#define #{key} .*$/, "#define #{key} #{value}")
                puts "#define #{key} #{value}"
              else
                outfile << (line.gsub /^#define #{key} .*$/, "#define #{key} @\"#{value}\"")
                puts "#define #{key} #{value}"
              end
            end
          else
            outfile << line
          end
        end
      end
    end
    FileUtils.mv appDelegate_temp, appDelegate
    
    puts "Transended #{@abowl['app']['APP_TITLE']}."

  else #try to make a bowl then
    make_abowl
  end
end

#transendizeObject



317
318
319
320
321
322
323
324
# File 'lib/transender.rb', line 317

def transendize
  backdrop
  clone_and_remove_git
  rename
  transend
  dropback
  zip
end

#transformizeObject



311
312
313
314
315
# File 'lib/transender.rb', line 311

def transformize
  clone_and_remove_git
  rename
  zip
end

#zipObject



299
300
301
302
303
304
305
306
307
308
309
# File 'lib/transender.rb', line 299

def zip
  z = File.join(ji_path, "#{app_title}.zip")
  FileUtils.rm_rf z #`rm -rf #{z}` #remove any previous zips without any warnings
  
  #cd into and zip
  `cd #{ji_path}; tar cvfz #{app_title}.zip #{app_title}/`

  #return path to zip
  puts "Zipped #{app_title} into #{z}" if File.exists?(z)
  z
end