Last active
August 29, 2015 13:57
-
-
Save jacklynrose/9678692 to your computer and use it in GitHub Desktop.
Simple macro like functionality for RubyMotion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rocket science stuff! Cool.