Created
November 26, 2016 06:50
-
-
Save monkstone/6f61ecf6c0f222d9b80250bd60a8c84f to your computer and use it in GitHub Desktop.
Batch convert JRubyArt sketches to propane
This file contains 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
# frozen_string_literal: true | |
# the scanner class | |
class Scanner | |
attr_reader :sketches, :rest, :sketch | |
def initialize(path) | |
@sketches = FindSketches.new(path).sketches | |
end | |
def read_file(file_name) | |
requ = %w(#!/usr/bin/env\ jruby require\ 'propane') | |
@rest = [] | |
File.open(file_name) do |file| | |
file.each_line do |line| | |
line.strip! | |
if line =~ /^require/ | |
requ << line | |
else | |
rest << line | |
end | |
end | |
end | |
requ | |
end | |
def build_sketch | |
class_format = 'class %s < Propane::App' | |
new_format = '%s.new' | |
sketches.each do |dfil| | |
class_name = ClassFromFilename.name(dfil) | |
@sketch = read_file(dfil) | |
sketch << '' | |
sketch << format(class_format, class_name) | |
@sketch += rest | |
sketch << "end\n" | |
sketch << format(new_format, class_name) | |
File.open(dfil, 'w') { |file| file.write(sketch.join("\n")) } | |
end | |
end | |
end | |
# build class name from filename | |
class ClassFromFilename | |
def self.name(filename) | |
base = File.basename(filename, '.rb') | |
humanize = base.split('_').map(&:capitalize) | |
humanize.join('') | |
end | |
end | |
# find sketch files | |
class FindSketches | |
attr_reader :sketches | |
def initialize(path) | |
@sketches = [] | |
where = File.join(path, '/**/*.rb') | |
Dir[where].each do |file_name| | |
unless File.readlines(file_name).grep(/sketch_title/).empty? | |
sketches << file_name | |
end | |
end | |
end | |
end | |
puts Scanner.new('/home/tux/k9_samples').build_sketch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment