Skip to content

Instantly share code, notes, and snippets.

@jacklynrose
Last active August 29, 2015 13:57
Show Gist options
  • Save jacklynrose/9678692 to your computer and use it in GitHub Desktop.
Save jacklynrose/9678692 to your computer and use it in GitHub Desktop.
Simple macro like functionality for RubyMotion
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
#MBEGIN
#if ENV['test']
# @contents << "self.test"
#end
#MEND
true
end
#MBEGIN
#if ENV['test']
#@contents << "def test"
#@contents << " puts 'test'"
#@contents << "end"
#end
#MEND
end
# put in the 'lib' folder
class ProcessedFile
def initialize(path)
@path = path
@contents = []
@current_macro_lines = []
process
end
def changed?
@changed
end
def process
File.open(@path) do |io|
io.each do |line|
if line =~ /#MBEGIN/ .. line =~ /#MEND/
@changed = true
@current_macro_lines << line.gsub(/\s*#/, '') unless line =~ /#MBEGIN/ || line =~ /#MEND/
else
eval(@current_macro_lines.join("\n"))
@current_macro_lines = []
@contents << line
end
end
end
end
def contents
@contents.join("\n")
end
end
class Preprocessor
def self.process(app)
app.files.each do |file|
pf = ProcessedFile.new(file)
if pf.changed?
app.files -= [file]
system 'mkdir', '-p', File.join(File.dirname(app.project_dir), "tmp")
tmpfile = File.join(File.dirname(app.project_dir), "tmp/#{File.basename(file)}")
if File.exists? tmpfile
File.delete(tmpfile)
end
io = File.new(tmpfile, 'w')
io.puts pf.contents
io.close
app.files += [tmpfile]
end
end
end
end
# -*- coding: utf-8 -*-
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project/template/ios'
require './lib/preprocessor'
begin
require 'bundler'
Bundler.require
rescue LoadError
end
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Macros'
Preprocessor.process(app)
end
@flagman
Copy link

flagman commented May 14, 2015

Rocket science stuff! Cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment